Spaces:
Running
Running
| import os | |
| import json | |
| import requests | |
| import gradio as gr | |
| API_BASE = os.getenv("ANALYSIS_API_URL", "https://baxin-opentrons-robot-analysis.hf.space").rstrip("/") | |
| TIMEOUT_SEC = float(os.getenv("HTTP_TIMEOUT", "30")) | |
| def _pretty(obj) -> str: | |
| try: | |
| return json.dumps(obj, indent=2, ensure_ascii=False) | |
| except Exception: | |
| return str(obj) | |
| def _request(method: str, path: str, **kwargs) -> dict: | |
| url = f"{API_BASE}{path}" | |
| try: | |
| resp = requests.request(method, url, timeout=TIMEOUT_SEC, **kwargs) | |
| except requests.RequestException as e: | |
| return {"ok": False, "url": url, "error": str(e)} | |
| out = { | |
| "ok": resp.ok, | |
| "url": url, | |
| "status_code": resp.status_code, | |
| "headers": dict(resp.headers), | |
| } | |
| try: | |
| out["json"] = resp.json() | |
| except ValueError: | |
| out["text"] = resp.text[:4000] | |
| return out | |
| def ping_root(): | |
| return _pretty(_request("GET", "/")) | |
| def ping_health(): | |
| return _pretty(_request("GET", "/health")) | |
| def ping_robot_health(): | |
| return _pretty(_request("GET", "/robot/health")) | |
| def analyze(protocol_text: str, filename: str): | |
| protocol_text = (protocol_text or "").strip() | |
| if not protocol_text: | |
| return _pretty({"ok": False, "error": "Empty protocol_text"}) | |
| payload = {"protocol_text": protocol_text, "filename": filename or "protocol.py"} | |
| res = _request("POST", "/analyze", json=payload) | |
| return _pretty(res) | |
| def chat_send(text, history, filename): | |
| history = history or [] | |
| history.append({"role": "user", "content": text}) | |
| res = analyze(text, filename) | |
| history.append({"role": "assistant", "content": res}) | |
| return history | |
| with gr.Blocks() as app: | |
| gr.Markdown("## Opentrons API Server Tester (FastAPI)") | |
| with gr.Row(): | |
| api_url = gr.Textbox(value=API_BASE, label="API Base URL", interactive=False) | |
| timeout = gr.Number(value=TIMEOUT_SEC, label="Timeout (sec)", interactive=False) | |
| with gr.Row(): | |
| btn_root = gr.Button("GET / (version/info)") | |
| btn_health = gr.Button("GET /health") | |
| btn_robot_health = gr.Button("GET /robot/health") | |
| status_out = gr.Code(label="Response", language="json") | |
| btn_root.click(fn=ping_root, inputs=[], outputs=[status_out]) | |
| btn_health.click(fn=ping_health, inputs=[], outputs=[status_out]) | |
| btn_robot_health.click(fn=ping_robot_health, inputs=[], outputs=[status_out]) | |
| gr.Markdown("### Analyze (/analyze)") | |
| filename = gr.Textbox(value="protocol.py", label="filename") | |
| protocol = gr.Textbox(lines=14, label="Paste protocol Python code") | |
| with gr.Row(): | |
| btn_analyze = gr.Button("POST /analyze") | |
| btn_clear = gr.Button("Clear") | |
| btn_analyze.click(fn=analyze, inputs=[protocol, filename], outputs=[status_out]) | |
| btn_clear.click(fn=lambda: ("", ""), inputs=[], outputs=[protocol, status_out]) | |
| gr.Markdown("### Chat-style test") | |
| chatbot = gr.Chatbot(label="History") | |
| send_button = gr.Button(value="Analyze (chat)") | |
| send_button.click(chat_send, [protocol, chatbot, filename], [chatbot]) | |
| protocol.submit(chat_send, [protocol, chatbot, filename], [chatbot]) | |
| app.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860"))) | |