import os import sys import subprocess # Ensure Backend modules are importable sys.path.insert(0, os.path.join(os.path.dirname(__file__), "Backend")) import spaces # REQUIRED for ZeroGPU import gradio as gr import uvicorn import nltk # Download required NLTK data before importing backend nltk.download('punkt') nltk.download('punkt_tab') nltk.download('wordnet') nltk.download('stopwords') nltk.download('omw-1.4') from Backend.api import app as fastapi_app # Create a minimal Gradio UI to satisfy ZeroGPU's requirements @spaces.GPU def dummy_gpu_task(): # ZeroGPU requires at least one GPU function to exist return "GPU is working!" with gr.Blocks() as demo: gr.Markdown("# Math Search Engine API") gr.Markdown("The React frontend is served via FastAPI at the root URL.") btn = gr.Button("Ping GPU") out = gr.Textbox() btn.click(dummy_gpu_task, outputs=out) # Mount FastAPI onto the Gradio app (or vice versa) # We serve Gradio at /gradio, and the rest is our FastAPI app (which serves React) app = gr.mount_gradio_app(fastapi_app, demo, path="/gradio") if __name__ == "__main__": port = os.getenv("PORT", "7860") # Use subprocess to run Uvicorn to hide it from ZeroGPU's patcher subprocess.Popen([sys.executable, "-m", "uvicorn", "app:app", "--host", "0.0.0.0", "--port", port]).wait()