import os import gradio as gr from main import app as fastapi_app import uvicorn from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import RedirectResponse # Set environment variables for Hugging Face Spaces os.environ["ALLOWED_ORIGINS"] = "*" # Configure CORS for Hugging Face Spaces fastapi_app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Root endpoint redirects to docs @fastapi_app.get("/", include_in_schema=False) async def root(): return RedirectResponse(url="/docs") # Create a Gradio interface to display API information with gr.Blocks(title="MOSDAC AI Assistant API") as demo: gr.Markdown("# MOSDAC AI Assistant API") gr.Markdown(""" This is the backend API for the MOSDAC AI Assistant. ## API Endpoints - `/docs`: Swagger UI documentation - `/redoc`: ReDoc documentation - `/health`: Health check endpoint - `/api/query`: Main query endpoint for the AI assistant ## Frontend The frontend is hosted on Netlify at [https://astroiq-mosdac.netlify.app](https://astroiq-mosdac.netlify.app) """) with gr.Row(): with gr.Column(): gr.Markdown("### API Status") status_btn = gr.Button("Check API Status") status_output = gr.Textbox(label="Status") def check_status(): try: return "API is running" except Exception as e: return f"Error: {str(e)}" status_btn.click(check_status, inputs=[], outputs=status_output) with gr.Column(): gr.Markdown("### API Documentation") gr.Markdown("[Swagger UI](/docs) | [ReDoc](/redoc)") # Mount FastAPI app in Gradio demo.queue() app = gr.mount_gradio_app(fastapi_app, demo, path="/") # For local development if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860)