import modal

image = (
    modal.Image.from_registry("ghcr.io/open-webui/open-webui:main")
    .run_commands("rm -rf /app/backend/data/*")
    .env({
        "ENV": "prod", "PORT": "8080", "USE_OLLAMA_DOCKER": "false", "USE_CUDA_DOCKER": "false",
        "USE_EMBEDDING_MODEL_DOCKER": "false", "USE_RERANKING_MODEL_DOCKER": "false",
        "WEBUI_SECRET_KEY": "059d6dda-58bf-477c-988c-73d163a19dcb", "WEBUI_AUTH": "false",
        "CORS_ALLOW_ORIGIN": "*", "FORWARDED_ALLOW_IPS": "*",
        "SCARF_NO_ANALYTICS": "true", "DO_NOT_TRACK": "true", "ANONYMIZED_TELEMETRY": "false",
    })
    .workdir("/app/backend")
)

vol = modal.Volume.from_name("open-webui-data", create_if_missing=True)

app = modal.App(name="open-webui", image=image,
                volumes={"/app/backend/data": vol})

with image.imports():
    @app.function(image=image, max_containers=1)
    @modal.concurrent(max_inputs=1000)
    @modal.asgi_app()
    def open_webui():
        from open_webui.main import app

        return app
