File size: 3,251 Bytes
fe443c6
8212c64
 
 
fe443c6
8212c64
6bd4790
fe443c6
8212c64
 
 
 
 
 
 
 
 
 
 
 
fe443c6
8212c64
 
 
 
 
 
6bd4790
8212c64
6bd4790
8212c64
 
 
 
 
 
 
 
 
 
 
fe443c6
8212c64
 
 
 
fe443c6
8212c64
 
 
fe443c6
8212c64
6bd4790
 
8212c64
 
 
6bd4790
fe443c6
 
8212c64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe443c6
6bd4790
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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")))