Spaces:
Sleeping
Sleeping
Update gradio_app.py
Browse files- gradio_app.py +21 -13
gradio_app.py
CHANGED
|
@@ -1,14 +1,16 @@
|
|
| 1 |
"""Entry point for a Hugging Face Space with the free Gradio SDK (the Docker SDK is paid).
|
| 2 |
|
| 3 |
-
The Space runs `python gradio_app.py`
|
| 4 |
-
/index, /refresh
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
from __future__ import annotations
|
| 9 |
|
| 10 |
# On ZeroGPU hardware the `spaces` package must be imported before anything touches torch, and the
|
| 11 |
-
#
|
| 12 |
try:
|
| 13 |
import spaces
|
| 14 |
|
|
@@ -23,13 +25,15 @@ import hmac
|
|
| 23 |
import os
|
| 24 |
|
| 25 |
import gradio as gr
|
| 26 |
-
import uvicorn
|
| 27 |
|
| 28 |
from app.main import create_app
|
| 29 |
|
| 30 |
-
api = create_app()
|
| 31 |
index = api.state.index
|
| 32 |
sync = api.state.sync
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
def status() -> str:
|
|
@@ -39,6 +43,8 @@ def status() -> str:
|
|
| 39 |
f"**Stare:** {state} · **documente:** {stats['documents']} · **fragmente:** {stats['chunks']}",
|
| 40 |
f"**Ultima sincronizare:** {sync.last_sync or '—'}",
|
| 41 |
]
|
|
|
|
|
|
|
| 42 |
if sync.last_error:
|
| 43 |
lines.append(f"**Ultima eroare:** `{sync.last_error}`")
|
| 44 |
return "\n\n".join(lines)
|
|
@@ -71,13 +77,15 @@ with gr.Blocks(title="Căutare documente") as ui:
|
|
| 71 |
gr.Button("Caută", variant="primary").click(try_search, inputs=[query, secret], outputs=output)
|
| 72 |
ui.load(status, outputs=state) # current state on every page load, not the one at start-up
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
@api.get("/", include_in_schema=False)
|
| 78 |
-
def root():
|
| 79 |
-
return {"service": "documents search", "ui": "/ui", "health": "/health"}
|
| 80 |
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""Entry point for a Hugging Face Space with the free Gradio SDK (the Docker SDK is paid).
|
| 2 |
|
| 3 |
+
The Space runs `python gradio_app.py`. Gradio's own server (`launch()`) serves:
|
| 4 |
+
* the service's API — /health, /search, /index, /refresh (see app/main.py), registered first;
|
| 5 |
+
* a small page at / to check the index and try a search.
|
| 6 |
+
Launching through Gradio matters on ZeroGPU hardware: the runtime only reports a healthy start-up
|
| 7 |
+
from `launch()`. Nothing uses the GPU — embeddings run on CPU.
|
| 8 |
"""
|
| 9 |
|
| 10 |
from __future__ import annotations
|
| 11 |
|
| 12 |
# On ZeroGPU hardware the `spaces` package must be imported before anything touches torch, and the
|
| 13 |
+
# runtime refuses to start without at least one @spaces.GPU function. This one is never called.
|
| 14 |
try:
|
| 15 |
import spaces
|
| 16 |
|
|
|
|
| 25 |
import os
|
| 26 |
|
| 27 |
import gradio as gr
|
|
|
|
| 28 |
|
| 29 |
from app.main import create_app
|
| 30 |
|
| 31 |
+
api = create_app(sync_on_start=False) # Gradio runs the server: the sync is started below
|
| 32 |
index = api.state.index
|
| 33 |
sync = api.state.sync
|
| 34 |
+
API_PATHS = {"/health", "/search", "/index", "/refresh"}
|
| 35 |
+
# The API's routes go first in Gradio's app, ahead of Gradio's own.
|
| 36 |
+
API_ROUTES = [route for route in api.routes if getattr(route, "path", None) in API_PATHS]
|
| 37 |
|
| 38 |
|
| 39 |
def status() -> str:
|
|
|
|
| 43 |
f"**Stare:** {state} · **documente:** {stats['documents']} · **fragmente:** {stats['chunks']}",
|
| 44 |
f"**Ultima sincronizare:** {sync.last_sync or '—'}",
|
| 45 |
]
|
| 46 |
+
if not (sync.app_url and sync.secret):
|
| 47 |
+
lines.append("**Configurare:** lipsesc `APP_URL` sau `RAG_SECRET` (Settings → Variables and secrets).")
|
| 48 |
if sync.last_error:
|
| 49 |
lines.append(f"**Ultima eroare:** `{sync.last_error}`")
|
| 50 |
return "\n\n".join(lines)
|
|
|
|
| 77 |
gr.Button("Caută", variant="primary").click(try_search, inputs=[query, secret], outputs=output)
|
| 78 |
ui.load(status, outputs=state) # current state on every page load, not the one at start-up
|
| 79 |
|
| 80 |
+
APP_KWARGS = {"routes": API_ROUTES}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
+
sync.start()
|
| 85 |
+
ui.launch(
|
| 86 |
+
server_name="0.0.0.0",
|
| 87 |
+
server_port=int(os.environ.get("PORT", "7860")),
|
| 88 |
+
# No server-side rendering: it would start a separate Node server on port 7860.
|
| 89 |
+
ssr_mode=False,
|
| 90 |
+
app_kwargs=APP_KWARGS,
|
| 91 |
+
)
|