Spaces:
Sleeping
Sleeping
Fix API: use Textbox.submit with api_name for reliable endpoint
Browse files
app.py
CHANGED
|
@@ -10,14 +10,13 @@ DATASET_REPO = "bobber/routangseng-telemetry"
|
|
| 10 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 11 |
BUFFER = []
|
| 12 |
BUFFER_LOCK = Lock()
|
| 13 |
-
FLUSH_INTERVAL = 60
|
| 14 |
-
FLUSH_SIZE = 10
|
| 15 |
|
| 16 |
api = HfApi(token=HF_TOKEN)
|
| 17 |
|
| 18 |
|
| 19 |
def flush_buffer():
|
| 20 |
-
"""Append buffered entries to the dataset."""
|
| 21 |
global BUFFER
|
| 22 |
with BUFFER_LOCK:
|
| 23 |
if not BUFFER:
|
|
@@ -46,26 +45,23 @@ def flush_buffer():
|
|
| 46 |
|
| 47 |
|
| 48 |
def periodic_flush():
|
| 49 |
-
"""Flush buffer periodically."""
|
| 50 |
try:
|
| 51 |
n = flush_buffer()
|
| 52 |
if n > 0:
|
| 53 |
-
print(f"[telemetry] Flushed {n} entries
|
| 54 |
except Exception as e:
|
| 55 |
print(f"[telemetry] Flush error: {e}")
|
| 56 |
Timer(FLUSH_INTERVAL, periodic_flush).start()
|
| 57 |
|
| 58 |
|
| 59 |
-
# Start periodic flush
|
| 60 |
Timer(FLUSH_INTERVAL, periodic_flush).start()
|
| 61 |
|
| 62 |
|
| 63 |
-
def collect(data: str) ->
|
| 64 |
-
"""Receive telemetry JSON from the WebGPU Space."""
|
| 65 |
try:
|
| 66 |
entry = json.loads(data)
|
| 67 |
if "question" not in entry or "answer" not in entry:
|
| 68 |
-
return {"status": "error", "message": "missing
|
| 69 |
|
| 70 |
entry["received_at"] = datetime.utcnow().isoformat() + "Z"
|
| 71 |
|
|
@@ -76,47 +72,39 @@ def collect(data: str) -> dict:
|
|
| 76 |
if buf_size >= FLUSH_SIZE:
|
| 77 |
try:
|
| 78 |
n = flush_buffer()
|
| 79 |
-
return {"status": "ok", "flushed": n}
|
| 80 |
except Exception as e:
|
| 81 |
-
return {"status": "ok", "buffered": buf_size, "
|
| 82 |
|
| 83 |
-
return {"status": "ok", "buffered": buf_size}
|
| 84 |
-
except
|
| 85 |
-
return {"status": "error", "message":
|
| 86 |
|
| 87 |
|
| 88 |
-
def
|
| 89 |
with BUFFER_LOCK:
|
| 90 |
buf = len(BUFFER)
|
| 91 |
-
return f"Buffer: {buf}
|
| 92 |
|
| 93 |
|
| 94 |
-
def
|
| 95 |
try:
|
| 96 |
n = flush_buffer()
|
| 97 |
-
return f"Flushed {n} entries
|
| 98 |
except Exception as e:
|
| 99 |
-
return f"
|
| 100 |
|
| 101 |
|
| 102 |
with gr.Blocks(title="θη³η Telemetry") as demo:
|
| 103 |
-
gr.Markdown("# π θη³η Telemetry
|
| 104 |
-
|
| 105 |
with gr.Row():
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
output = gr.Textbox(label="Result")
|
| 109 |
-
|
| 110 |
-
status_btn.click(fn=status, outputs=output)
|
| 111 |
-
flush_btn.click(fn=manual_flush, outputs=output)
|
| 112 |
-
|
| 113 |
-
# API endpoint for the WebGPU Space
|
| 114 |
-
collect_input = gr.Textbox(visible=False)
|
| 115 |
-
collect_output = gr.JSON(visible=False)
|
| 116 |
-
collect_btn = gr.Button(visible=False)
|
| 117 |
-
collect_btn.click(fn=collect, inputs=collect_input, outputs=collect_output, api_name="collect")
|
| 118 |
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
|
| 122 |
-
demo.launch()
|
|
|
|
| 10 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 11 |
BUFFER = []
|
| 12 |
BUFFER_LOCK = Lock()
|
| 13 |
+
FLUSH_INTERVAL = 60
|
| 14 |
+
FLUSH_SIZE = 10
|
| 15 |
|
| 16 |
api = HfApi(token=HF_TOKEN)
|
| 17 |
|
| 18 |
|
| 19 |
def flush_buffer():
|
|
|
|
| 20 |
global BUFFER
|
| 21 |
with BUFFER_LOCK:
|
| 22 |
if not BUFFER:
|
|
|
|
| 45 |
|
| 46 |
|
| 47 |
def periodic_flush():
|
|
|
|
| 48 |
try:
|
| 49 |
n = flush_buffer()
|
| 50 |
if n > 0:
|
| 51 |
+
print(f"[telemetry] Flushed {n} entries")
|
| 52 |
except Exception as e:
|
| 53 |
print(f"[telemetry] Flush error: {e}")
|
| 54 |
Timer(FLUSH_INTERVAL, periodic_flush).start()
|
| 55 |
|
| 56 |
|
|
|
|
| 57 |
Timer(FLUSH_INTERVAL, periodic_flush).start()
|
| 58 |
|
| 59 |
|
| 60 |
+
def collect(data: str) -> str:
|
|
|
|
| 61 |
try:
|
| 62 |
entry = json.loads(data)
|
| 63 |
if "question" not in entry or "answer" not in entry:
|
| 64 |
+
return json.dumps({"status": "error", "message": "missing fields"})
|
| 65 |
|
| 66 |
entry["received_at"] = datetime.utcnow().isoformat() + "Z"
|
| 67 |
|
|
|
|
| 72 |
if buf_size >= FLUSH_SIZE:
|
| 73 |
try:
|
| 74 |
n = flush_buffer()
|
| 75 |
+
return json.dumps({"status": "ok", "flushed": n})
|
| 76 |
except Exception as e:
|
| 77 |
+
return json.dumps({"status": "ok", "buffered": buf_size, "error": str(e)})
|
| 78 |
|
| 79 |
+
return json.dumps({"status": "ok", "buffered": buf_size})
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return json.dumps({"status": "error", "message": str(e)})
|
| 82 |
|
| 83 |
|
| 84 |
+
def get_status() -> str:
|
| 85 |
with BUFFER_LOCK:
|
| 86 |
buf = len(BUFFER)
|
| 87 |
+
return f"Buffer: {buf} | Token: {'β
' if HF_TOKEN else 'β'} | Dataset: {DATASET_REPO}"
|
| 88 |
|
| 89 |
|
| 90 |
+
def do_flush() -> str:
|
| 91 |
try:
|
| 92 |
n = flush_buffer()
|
| 93 |
+
return f"Flushed {n} entries"
|
| 94 |
except Exception as e:
|
| 95 |
+
return f"Error: {e}"
|
| 96 |
|
| 97 |
|
| 98 |
with gr.Blocks(title="θη³η Telemetry") as demo:
|
| 99 |
+
gr.Markdown("# π θη³η Telemetry")
|
| 100 |
+
|
| 101 |
with gr.Row():
|
| 102 |
+
gr.Button("Status").click(fn=get_status, outputs=gr.Textbox(label="Status"))
|
| 103 |
+
gr.Button("Flush").click(fn=do_flush, outputs=gr.Textbox(label="Result"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
# Public API endpoint
|
| 106 |
+
inp = gr.Textbox(label="JSON data")
|
| 107 |
+
out = gr.Textbox(label="Response")
|
| 108 |
+
inp.submit(fn=collect, inputs=inp, outputs=out, api_name="collect")
|
| 109 |
|
| 110 |
+
demo.launch()
|
|
|