|
import gradio as gr |
|
import requests |
|
import uuid |
|
import os |
|
|
|
def generate_unique_name(): |
|
unique_name = str(uuid.uuid4()) + ".py" |
|
return unique_name |
|
|
|
def send_post_request(payload): |
|
url = "https://baxin-simulator.hf.space/protocol" |
|
token = os.environ['HF_TOKEN'] |
|
protocol_name = generate_unique_name() |
|
data = {"name": protocol_name, "content": payload} |
|
headers = {"Content-Type": "application/json", 'Authorization': 'Bearer {}'.format(token)} |
|
|
|
response = requests.post(url, json=data, headers=headers) |
|
|
|
if response.status_code != 200: |
|
print("Error: " + response.text) |
|
return "Error: " + response.text |
|
|
|
|
|
|
|
response_data = response.json() |
|
if "error_message" in response_data: |
|
print("Error in response:", response_data["error_message"]) |
|
return response_data["error_message"] |
|
elif "protocol_name" in response_data: |
|
|
|
|
|
return response_data["run_status"] |
|
|
|
|
|
|
|
else: |
|
print("Unexpected response:", response_data) |
|
return "Unexpected response" |
|
|
|
def send_message(text, chatbot): |
|
|
|
response = send_post_request(text) |
|
|
|
chatbot.append(("opentrons_simulator result", response)) |
|
return chatbot |
|
|
|
title = """ |
|
<div align="center"> |
|
# OT-2 Simulator: Emulating Opentrons Protocols with Python API |
|
|
|
</div> |
|
""" |
|
with gr.Blocks() as app: |
|
gr.Markdown(title) |
|
textbox = gr.Textbox(label="Enter protocol") |
|
send_button = gr.Button(value="Send") |
|
chatbot = gr.Chatbot() |
|
clear_button = gr.ClearButton([textbox, chatbot]) |
|
send_button.click(send_message, [textbox, chatbot], [chatbot]) |
|
|
|
app.launch() |
|
|