# 🚀 Modal Deployment Script (Minimum Configuration)
# Generated by modal-for-noobs - https://github.com/arthrod/modal-for-noobs
# Deployment Mode: minimum
# Features: Basic Gradio deployment with minimal dependencies
# Timeout: {{ timeout_seconds }}s

import modal
from fastapi import FastAPI
import gradio as gr
from gradio.routes import mount_gradio_app

# Configuration constants
APP_NAME = "{{ app_name }}"
APP_TITLE = "{{ app_name }} - Modal Deployment"
APP_DESCRIPTION = "Minimal deployment with essential features"
DEPLOYMENT_MODE = "minimum"

# Create Modal App
app = modal.App(APP_NAME)

# Container Image Configuration (Minimal)
{{ image_config }}

# Original Application Code
{{ original_code }}

# Modal Function Configuration
@app.function(
    image=image,
    min_containers={{ min_containers }},
    max_containers={{ max_containers }},
    timeout={{ timeout_seconds }},
    memory={{ memory_mb }},
    cpu={{ cpu }},
)
@modal.concurrent(max_inputs={{ concurrent_inputs }})
@modal.asgi_app()
def deploy_gradio():
    """Deploy Gradio app with minimal Modal configuration."""
    
    # Detect Gradio Interface
    demo = None
    interface_names = ['demo', 'app', 'interface', 'iface']
    
    for name in interface_names:
        if name in globals() and hasattr(globals()[name], 'launch'):
            demo = globals()[name]
            break
    
    if demo is None:
        for var_name, var_value in globals().items():
            if hasattr(var_value, 'queue') and hasattr(var_value, 'launch'):
                demo = var_value
                break
    
    if demo is None:
        raise ValueError("Could not find Gradio interface")
    
    # Configure demo queue
    demo.queue(max_size=10)
    
    # FastAPI Setup
    fastapi_app = FastAPI(
        title=APP_TITLE,
        description=APP_DESCRIPTION,
        version="1.0.0"
    )
    
    # Mount Gradio app
    return mount_gradio_app(fastapi_app, demo, path="/")

if __name__ == "__main__":
    app.run()