File size: 2,498 Bytes
c8c487a
 
 
 
 
 
 
5c15234
 
 
c8c487a
5c15234
 
 
 
 
 
 
 
 
 
 
 
 
c8c487a
 
 
 
 
 
 
5c15234
c8c487a
 
5c15234
 
 
918f71e
5c15234
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
918f71e
 
 
 
 
5c15234
 
 
918f71e
 
 
5c15234
c8c487a
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""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)