Spaces:
Runtime error
Runtime error
File size: 1,338 Bytes
169aba7 3daa598 169aba7 3daa598 169aba7 2ea4eb5 3daa598 a2c2e91 3daa598 a2c2e91 3daa598 a2c2e91 3daa598 169aba7 2d950c6 0122af1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 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()
|