import gradio as gr import requests import uuid import os import json HF_TOKEN = os.environ.get("HF_TOKEN") # api server url API_URL = "https://baxin-simulator.hf.space/simulate" def generate_unique_name(): return f"protocol_{uuid.uuid4().hex[:8]}.py" def simulate_protocol(protocol_content: str): if not HF_TOKEN: return "Error: HF_TOKEN is not set. Please set it in your environment variables." protocol_name = generate_unique_name() headers = { "Content-Type": "application/json", "Authorization": f"Bearer {HF_TOKEN}" } data = { "name": protocol_name, "content": protocol_content } try: response = requests.post(API_URL, json=data, headers=headers, timeout=60) if response.status_code == 200: response_data = response.json() return f"
{response_data.get('run_log', 'Success, but no run log was returned.')}"
else:
try:
error_data = response.json()
detail = error_data.get("detail", {})
if isinstance(detail, dict):
error_message = detail.get('error_details', response.text)
else:
error_message = detail
return f"API Error (Status {response.status_code}):\n\n{error_message}"
except json.JSONDecodeError:
return f"API Error (Status {response.status_code}):\n\n{response.text}"
except requests.exceptions.RequestException as e:
return f"Network Error: Failed to connect to the API.\n\n{e}"
def add_to_chatbot(protocol_text: str, chat_history: list):
chat_history.append((f"{protocol_text}", None))
response = simulate_protocol(protocol_text)
chat_history.append((None, response))
return chat_history
# --- Gradio UI ---
title = """
Enter your Opentrons protocol code below and click 'Run Simulation' to see the result on the right.