Bootstrap: app.py
Browse files123456789101112131415161718192021222324253426272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889679091929394959697"""Gradio entrypoint for the TuringDNA assistant Space.
Two surfaces:
1. Gradio ChatInterface at "/" — for humans visiting huggingface.co/spaces/winter4000/turingdna-assistant directly to sanity-check the model.
2. Auto-generated Gradio API at "/api/predict" (or /run/predict on older Gradio) — what the Flask app's /api/chat proxy calls via gradio_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 os
import gradio as gr
def greet(name): return "Hello " + name + "!!"from llm import chat, MODEL_ID_IN_USE, SYSTEM_PROMPT
def chat_fn(message: str, history: list[dict]) -> str: """Gradio ChatInterface callback. `history` is the OpenAI-format list 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```" )
demo = gr.Interface(fn=greet, inputs="text", outputs="text")demo.launch()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