Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI | |
| from backend.api import items, pdfreader, textreader | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import gradio as gr | |
| app = FastAPI(title="Multi-layered FastAPI Example") | |
| # === CORS Middleware === | |
| origins = ["*"] | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=origins, | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # === Include API routers === | |
| app.include_router(items.router) | |
| app.include_router(pdfreader.router) | |
| app.include_router(textreader.router) | |
| # === Gradio Function === | |
| def gradio_func(text): | |
| return f"Gradio received: {text}" | |
| gradio_app = gr.Interface(fn=gradio_func, inputs="text", outputs="text") | |
| # === Mount Gradio at /gradio === | |
| from gradio.routes import App as GradioApp | |
| gradio_app.launch(inline=True, prevent_thread_lock=True) | |
| app.mount("/gradio", GradioApp.create_app(gradio_app)) | |
| # === Uvicorn Entry === | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True) | |