Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,96 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
API_BASE = os.getenv("ANALYSIS_API_URL", "https://baxin-
|
| 6 |
TIMEOUT_SEC = float(os.getenv("HTTP_TIMEOUT", "30"))
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
except ValueError:
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
def
|
| 29 |
history = history or []
|
| 30 |
history.append({"role": "user", "content": text})
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
return history
|
| 34 |
|
| 35 |
with gr.Blocks() as app:
|
| 36 |
-
gr.Markdown("## Opentrons
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
app.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
+
API_BASE = os.getenv("ANALYSIS_API_URL", "https://baxin-opentrons-robot-analysis.hf.space").rstrip("/")
|
| 7 |
TIMEOUT_SEC = float(os.getenv("HTTP_TIMEOUT", "30"))
|
| 8 |
|
| 9 |
+
def _pretty(obj) -> str:
|
| 10 |
+
try:
|
| 11 |
+
return json.dumps(obj, indent=2, ensure_ascii=False)
|
| 12 |
+
except Exception:
|
| 13 |
+
return str(obj)
|
| 14 |
+
|
| 15 |
+
def _request(method: str, path: str, **kwargs) -> dict:
|
| 16 |
+
url = f"{API_BASE}{path}"
|
| 17 |
+
try:
|
| 18 |
+
resp = requests.request(method, url, timeout=TIMEOUT_SEC, **kwargs)
|
| 19 |
+
except requests.RequestException as e:
|
| 20 |
+
return {"ok": False, "url": url, "error": str(e)}
|
| 21 |
|
| 22 |
+
out = {
|
| 23 |
+
"ok": resp.ok,
|
| 24 |
+
"url": url,
|
| 25 |
+
"status_code": resp.status_code,
|
| 26 |
+
"headers": dict(resp.headers),
|
| 27 |
+
}
|
| 28 |
try:
|
| 29 |
+
out["json"] = resp.json()
|
| 30 |
except ValueError:
|
| 31 |
+
out["text"] = resp.text[:4000]
|
| 32 |
+
return out
|
| 33 |
+
|
| 34 |
+
def ping_root():
|
| 35 |
+
return _pretty(_request("GET", "/"))
|
| 36 |
+
|
| 37 |
+
def ping_health():
|
| 38 |
+
return _pretty(_request("GET", "/health"))
|
| 39 |
+
|
| 40 |
+
def ping_robot_health():
|
| 41 |
+
return _pretty(_request("GET", "/robot/health"))
|
| 42 |
|
| 43 |
+
def analyze(protocol_text: str, filename: str):
|
| 44 |
+
protocol_text = (protocol_text or "").strip()
|
| 45 |
+
if not protocol_text:
|
| 46 |
+
return _pretty({"ok": False, "error": "Empty protocol_text"})
|
| 47 |
|
| 48 |
+
payload = {"protocol_text": protocol_text, "filename": filename or "protocol.py"}
|
| 49 |
+
res = _request("POST", "/analyze", json=payload)
|
| 50 |
+
return _pretty(res)
|
| 51 |
|
| 52 |
+
def chat_send(text, history, filename):
|
| 53 |
history = history or []
|
| 54 |
history.append({"role": "user", "content": text})
|
| 55 |
+
|
| 56 |
+
res = analyze(text, filename)
|
| 57 |
+
history.append({"role": "assistant", "content": res})
|
| 58 |
return history
|
| 59 |
|
| 60 |
with gr.Blocks() as app:
|
| 61 |
+
gr.Markdown("## Opentrons API Server Tester (FastAPI)")
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
api_url = gr.Textbox(value=API_BASE, label="API Base URL", interactive=False)
|
| 65 |
+
timeout = gr.Number(value=TIMEOUT_SEC, label="Timeout (sec)", interactive=False)
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
btn_root = gr.Button("GET / (version/info)")
|
| 69 |
+
btn_health = gr.Button("GET /health")
|
| 70 |
+
btn_robot_health = gr.Button("GET /robot/health")
|
| 71 |
+
|
| 72 |
+
status_out = gr.Code(label="Response", language="json")
|
| 73 |
+
|
| 74 |
+
btn_root.click(fn=ping_root, inputs=[], outputs=[status_out])
|
| 75 |
+
btn_health.click(fn=ping_health, inputs=[], outputs=[status_out])
|
| 76 |
+
btn_robot_health.click(fn=ping_robot_health, inputs=[], outputs=[status_out])
|
| 77 |
+
|
| 78 |
+
gr.Markdown("### Analyze (/analyze)")
|
| 79 |
+
filename = gr.Textbox(value="protocol.py", label="filename")
|
| 80 |
+
protocol = gr.Textbox(lines=14, label="Paste protocol Python code")
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
btn_analyze = gr.Button("POST /analyze")
|
| 84 |
+
btn_clear = gr.Button("Clear")
|
| 85 |
+
|
| 86 |
+
btn_analyze.click(fn=analyze, inputs=[protocol, filename], outputs=[status_out])
|
| 87 |
+
btn_clear.click(fn=lambda: ("", ""), inputs=[], outputs=[protocol, status_out])
|
| 88 |
+
|
| 89 |
+
gr.Markdown("### Chat-style test")
|
| 90 |
+
chatbot = gr.Chatbot(label="History")
|
| 91 |
+
send_button = gr.Button(value="Analyze (chat)")
|
| 92 |
+
|
| 93 |
+
send_button.click(chat_send, [protocol, chatbot, filename], [chatbot])
|
| 94 |
+
protocol.submit(chat_send, [protocol, chatbot, filename], [chatbot])
|
| 95 |
|
| 96 |
app.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))
|