baxin commited on
Commit
8212c64
·
verified ·
1 Parent(s): 658ce21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -28
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-flex-analysis.hf.space")
6
  TIMEOUT_SEC = float(os.getenv("HTTP_TIMEOUT", "30"))
7
 
8
- def analyze_protocol(text: str) -> str:
9
- text = (text or "").strip()
10
- if not text:
11
- return "Empty protocol"
 
 
 
 
 
 
 
 
12
 
13
- resp = requests.post(
14
- f"{API_BASE}/analyze",
15
- json={"protocol_text": text, "filename": "protocol.py"},
16
- timeout=TIMEOUT_SEC,
17
- )
 
18
  try:
19
- payload = resp.json()
20
  except ValueError:
21
- return f"Non-JSON response (HTTP {resp.status_code}):\n{resp.text[:2000]}"
 
 
 
 
 
 
 
 
 
 
22
 
23
- if resp.status_code >= 400 or not payload.get("ok", True):
24
- return f"Upload failed (HTTP {resp.status_code})\n{payload}"
 
 
25
 
26
- return str(payload)
 
 
27
 
28
- def send_message(text, history):
29
  history = history or []
30
  history.append({"role": "user", "content": text})
31
- result = analyze_protocol(text)
32
- history.append({"role": "assistant", "content": result})
 
33
  return history
34
 
35
  with gr.Blocks() as app:
36
- gr.Markdown("## Opentrons Protocol Analyzer (via FastAPI)")
37
- textbox = gr.Textbox(lines=12, label="Paste protocol Python code")
38
- send_button = gr.Button(value="Analyze")
39
- chatbot = gr.Chatbot(label="Results")
40
- clear_button = gr.ClearButton([textbox, chatbot])
41
-
42
- send_button.click(send_message, [textbox, chatbot], [chatbot])
43
- textbox.submit(send_message, [textbox, chatbot], [chatbot])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")))