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