Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,105 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import uuid
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
return binary_string
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
"files": (protocol_name, protocol_file),
|
| 18 |
-
}
|
| 19 |
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# if the protocol file is existing, remove it
|
| 28 |
-
os.remove(protocol_name)
|
| 29 |
-
|
| 30 |
-
if 'data' in response_data:
|
| 31 |
-
response_data = response.json()
|
| 32 |
-
protocol_id = response_data["data"]["id"]
|
| 33 |
-
analysis_result=response_data["data"]["analyses"]
|
| 34 |
-
analysis_id = response_data["data"]["analysisSummaries"][0]["id"]
|
| 35 |
-
analysis_status = response_data["data"]["analysisSummaries"][0]["status"]
|
| 36 |
-
print(protocol_id
|
| 37 |
-
)
|
| 38 |
-
print(analysis_result)
|
| 39 |
-
print(analysis_id)
|
| 40 |
-
print(analysis_status)
|
| 41 |
-
return f"success\n protocol_id:{protocol_id}\n analysis_id:{analysis_id}"
|
| 42 |
-
else:
|
| 43 |
-
print("analysis error")
|
| 44 |
-
error_id = response_data["errors"][0]['id']
|
| 45 |
-
error_code = response_data["errors"][0]["errorCode"]
|
| 46 |
-
error_detail = response_data["errors"][0]['detail']
|
| 47 |
-
print(error_id)
|
| 48 |
-
print(error_code)
|
| 49 |
-
print(error_detail)
|
| 50 |
-
return f"{error_id}\n{error_code}\n{error_detail}"
|
| 51 |
-
|
| 52 |
-
def generate_unique_name():
|
| 53 |
-
unique_name = str(uuid.uuid4()) + ".py"
|
| 54 |
-
return unique_name
|
| 55 |
-
|
| 56 |
-
def send_post_request(payload):
|
| 57 |
-
url = "https://baxin-simulator.hf.space/protocol"
|
| 58 |
-
protocol_name = generate_unique_name()
|
| 59 |
-
data = {"name"
|
| 60 |
-
: protocol_name, "content": payload}
|
| 61 |
-
headers = {"Content-Type": "application/json"}
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
response = requests.post(url, json=data, headers=headers)
|
| 65 |
-
|
| 66 |
-
if response.status_code != 200:
|
| 67 |
-
print("Error: " + response.text)
|
| 68 |
-
return "Error: " + response.text
|
| 69 |
-
|
| 70 |
-
# Check the response before returning it
|
| 71 |
-
response_data = response.json()
|
| 72 |
-
if "error_message" in response_data:
|
| 73 |
-
print("Error in response:", response_data["error_message"])
|
| 74 |
-
return response_data["error_message"]
|
| 75 |
-
elif "protocol_name" in response_data:
|
| 76 |
-
print("Protocol executed successfully. Run log:", response_data["run_log"])
|
| 77 |
-
return response_data["run_log"]
|
| 78 |
-
else:
|
| 79 |
-
print("Unexpected response:", response_data)
|
| 80 |
-
return "Unexpected response"
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
def send_message(text, chatbot):
|
| 86 |
-
# Send POST request and get response
|
| 87 |
-
# response = send_post_request(text)
|
| 88 |
-
# binary_protocol = string_to_binary(text)
|
| 89 |
-
protocol_name = generate_unique_name()
|
| 90 |
-
with open(protocol_name, "w") as file:
|
| 91 |
-
file.write(text)
|
| 92 |
-
|
| 93 |
-
response = upload_protocol(protocol_name)
|
| 94 |
-
# Update chatbot with response
|
| 95 |
-
chatbot.append(("opentrons analysis result", response))
|
| 96 |
-
return chatbot
|
| 97 |
|
| 98 |
with gr.Blocks() as app:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
| 102 |
clear_button = gr.ClearButton([textbox, chatbot])
|
|
|
|
| 103 |
send_button.click(send_message, [textbox, chatbot], [chatbot])
|
|
|
|
| 104 |
|
| 105 |
-
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
API_BASE = os.getenv("ANALYSIS_API_URL", "https://baxin-ot-analysis-api-server.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")))
|