# Modified app.py to serve both the FastAPI backend and Gradio frontend import os import gradio as gr import subprocess import threading import sys # Import the FastAPI app from app_main import app as fastapi_app # Import the gradio interface from gradio_app import app as gradio_interface # Function to start the FastAPI server in a separate thread def run_fastapi(): import uvicorn uvicorn.run(fastapi_app, host="0.0.0.0", port=8000) # Main entry point if __name__ == "__main__": # Start FastAPI in a background thread threading.Thread(target=run_fastapi, daemon=True).start() # Start Gradio on the main thread - using port 7860 which is the default for Gradio # When running in a Space, use 0.0.0.0 as the server if os.getenv("SPACE_ID"): gradio_interface.launch(server_name="0.0.0.0", server_port=7860) else: gradio_interface.launch()