Bootstrap: app.py
Browse files1234567import gradio as grdef greet(name):return "Hello " + name + "!!"demo = gr.Interface(fn=greet, inputs="text", outputs="text")demo.launch()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697"""Gradio entrypoint for the TuringDNA assistant Space.Two surfaces:1. Gradio ChatInterface at "/" — for humans visitinghuggingface.co/spaces/winter4000/turingdna-assistant directly tosanity-check the model.2. Auto-generated Gradio API at "/api/predict" (or /run/predict onolder Gradio) — what the Flask app's /api/chat proxy calls viagradio_client.Client(...).predict(...).The Flask side uses `api_name="/chat"`, so the ChatInterface event isregistered with that api_name.ZeroGPU note: the actual inference is in llm.generate() which carriesthe @spaces.GPU(duration=60) decorator. From here we just hand controlto ChatInterface — Gradio handles the request queueing and ZeroGPUacquires the A100 when generate() is called."""import osimport gradio as grfrom llm import chat, MODEL_ID_IN_USE, SYSTEM_PROMPTdef chat_fn(message: str, history: list[dict]) -> str:"""Gradio ChatInterface callback. `history` is the OpenAI-formatlist of {role, content} dicts when type="messages"."""if not (message or "").strip():return "Ask me a question about your sequence, mutations, or library."try:return chat(message, history=history)except Exception as exc: # noqa: BLE001# Surface model errors to the user instead of leaving the# Gradio chat hanging. Useful for debugging too.return f"Generation error: {type(exc).__name__}: {exc}"# Modest demo prompts shown above the composer — give the visiting# researcher (or me, when sanity-checking) a one-click way to see what# the assistant is good at without having to compose a question from# scratch.DEMO_PROMPTS = ["Explain the ESM-2 ΔLL score in one paragraph for someone with a biochem background.","I have a 280-aa enzyme and the engine is suggesting M1L, V8I, and K42R. Which of these is most likely to affect activity, and why?","What's the difference between site-saturation mutagenesis and the combinatorial library approach this engine uses?","Why does codon optimization for E. coli sometimes hurt expression in yeast?",]demo = gr.ChatInterface(fn=chat_fn,type="messages",title="TuringDNA Assistant",description=(f"In-app biology assistant powered by **{MODEL_ID_IN_USE}** on ZeroGPU. ""Ask questions about your protein, mutations, ΔLL scores, codon optimization, or cloning strategy. ""First call after the Space wakes up takes 10–30 s while the GPU is acquired."),examples=DEMO_PROMPTS,cache_examples=False,theme=gr.themes.Soft(primary_hue="blue",secondary_hue="gray",neutral_hue="stone",font=[gr.themes.GoogleFont("Source Serif 4"), "Georgia", "serif"],font_mono=[gr.themes.GoogleFont("JetBrains Mono"), "monospace"],),api_name="chat", # gradio_client will call .predict(api_name="/chat"))# Tiny meta tab so the README isn't the only place the system prompt is# discoverable. Lets reviewers see exactly what context the model is# given without having to clone the Space and grep llm.py.with gr.Blocks(theme=demo.theme) as full_app:with gr.Tab("Chat"):demo.render()with gr.Tab("System prompt"):gr.Markdown(f"**Model in use:** `{MODEL_ID_IN_USE}`\n\n""**System prompt baked into every conversation:**\n\n"f"```\n{SYSTEM_PROMPT}\n```")if __name__ == "__main__":full_app.queue(default_concurrency_limit=1).launch(server_name="0.0.0.0",# When running on HF Spaces, the platform sets the port via# the GRADIO_SERVER_PORT env var; locally we default to 7860.server_port=int(os.environ.get("GRADIO_SERVER_PORT", "7860")),show_api=True, # exposes /docs for the auto-generated API)
app.py+94-4