satquery-api / app.py
SM737's picture
Clean Gradio ZeroGPU probe with traceback logging
918f71e verified
Raw History Blame Contribute Delete
2.5 kB
"""Hugging Face Gradio SDK entry point for the existing FastAPI service.
The Gradio Space runner executes this file. SatQuery keeps its API routes so the
separate Vercel frontend can call the same endpoints without a UI rewrite.
"""
import os
import shutil
import uuid
from pathlib import Path
# 1. ZeroGPU compatibility: import spaces BEFORE torch or other GPU libraries
try:
import spaces
except ImportError:
class _MockSpaces:
@staticmethod
def GPU(func=None, **kwargs):
if func is not None:
return func
def decorator(f):
return f
return decorator
spaces = _MockSpaces()
os.environ.setdefault("SATQUERY_ENV", "production")
os.environ.setdefault("SATQUERY_OFFLINE_MODE", "true")
os.environ.setdefault("SATQUERY_MODEL_DIR", os.path.join(os.path.dirname(__file__), "models"))
os.environ.setdefault("SATQUERY_ARTIFACT_DIR", "artifacts")
os.environ.setdefault("SATQUERY_DATABASE_PATH", "artifacts/satquery.sqlite3")
import uvicorn
from app.main import app # noqa: E402
try:
import gradio as gr
@spaces.GPU
def gpu_probe_check(probe_text: str = "health"):
import torch
cuda_avail = torch.cuda.is_available()
device_name = torch.cuda.get_device_name(0) if cuda_avail else "CPU"
return {
"status": "online",
"cuda_available": cuda_avail,
"device": device_name,
"probe": probe_text,
}
with gr.Blocks(title="SatQuery GeoProof API") as demo:
gr.Markdown(
"# 🛰️ SatQuery GeoProof - Space Runner\n"
"This Space hosts the **SatQuery API** service for the Vercel frontend, accelerated with ZeroGPU.\n\n"
"- **API Endpoints**: `/api/v1/analyze`, `/health`, `/api/v1/models`\n"
"- **Interactive Swagger Docs**: [`/docs`](/docs)"
)
with gr.Row():
probe_in = gr.Textbox(label="Probe Ping", value="ZeroGPU Ready", placeholder="Status check...")
probe_out = gr.JSON(label="Hardware Info")
probe_btn = gr.Button("Test GPU Allocation", variant="primary")
probe_btn.click(fn=gpu_probe_check, inputs=[probe_in], outputs=[probe_out])
app = gr.mount_gradio_app(app, demo, path="/")
except Exception as exc:
import traceback
print("Gradio setup failed with exception:")
traceback.print_exc()
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860, workers=1)