WINTER4000 commited on
Commit
642bf8b
·
verified ·
1 Parent(s): 7ed7128

Bootstrap: app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -4
app.py CHANGED
@@ -1,7 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
1
+ """Gradio entrypoint for the TuringDNA assistant Space.
2
+
3
+ Two surfaces:
4
+
5
+ 1. Gradio ChatInterface at "/" — for humans visiting
6
+ huggingface.co/spaces/winter4000/turingdna-assistant directly to
7
+ sanity-check the model.
8
+
9
+ 2. Auto-generated Gradio API at "/api/predict" (or /run/predict on
10
+ older Gradio) — what the Flask app's /api/chat proxy calls via
11
+ gradio_client.Client(...).predict(...).
12
+
13
+ The Flask side uses `api_name="/chat"`, so the ChatInterface event is
14
+ registered with that api_name.
15
+
16
+ ZeroGPU note: the actual inference is in llm.generate() which carries
17
+ the @spaces.GPU(duration=60) decorator. From here we just hand control
18
+ to ChatInterface — Gradio handles the request queueing and ZeroGPU
19
+ acquires the A100 when generate() is called.
20
+ """
21
+
22
+ import os
23
+
24
  import gradio as gr
25
 
26
+ from llm import chat, MODEL_ID_IN_USE, SYSTEM_PROMPT
27
+
28
+
29
+ def chat_fn(message: str, history: list[dict]) -> str:
30
+ """Gradio ChatInterface callback. `history` is the OpenAI-format
31
+ list of {role, content} dicts when type="messages"."""
32
+ if not (message or "").strip():
33
+ return "Ask me a question about your sequence, mutations, or library."
34
+ try:
35
+ return chat(message, history=history)
36
+ except Exception as exc: # noqa: BLE001
37
+ # Surface model errors to the user instead of leaving the
38
+ # Gradio chat hanging. Useful for debugging too.
39
+ return f"Generation error: {type(exc).__name__}: {exc}"
40
+
41
+
42
+ # Modest demo prompts shown above the composer — give the visiting
43
+ # researcher (or me, when sanity-checking) a one-click way to see what
44
+ # the assistant is good at without having to compose a question from
45
+ # scratch.
46
+ DEMO_PROMPTS = [
47
+ "Explain the ESM-2 ΔLL score in one paragraph for someone with a biochem background.",
48
+ "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?",
49
+ "What's the difference between site-saturation mutagenesis and the combinatorial library approach this engine uses?",
50
+ "Why does codon optimization for E. coli sometimes hurt expression in yeast?",
51
+ ]
52
+
53
+
54
+ demo = gr.ChatInterface(
55
+ fn=chat_fn,
56
+ type="messages",
57
+ title="TuringDNA Assistant",
58
+ description=(
59
+ f"In-app biology assistant powered by **{MODEL_ID_IN_USE}** on ZeroGPU. "
60
+ "Ask questions about your protein, mutations, ΔLL scores, codon optimization, or cloning strategy. "
61
+ "First call after the Space wakes up takes 10–30 s while the GPU is acquired."
62
+ ),
63
+ examples=DEMO_PROMPTS,
64
+ cache_examples=False,
65
+ theme=gr.themes.Soft(
66
+ primary_hue="blue",
67
+ secondary_hue="gray",
68
+ neutral_hue="stone",
69
+ font=[gr.themes.GoogleFont("Source Serif 4"), "Georgia", "serif"],
70
+ font_mono=[gr.themes.GoogleFont("JetBrains Mono"), "monospace"],
71
+ ),
72
+ api_name="chat", # gradio_client will call .predict(api_name="/chat")
73
+ )
74
+
75
+
76
+ # Tiny meta tab so the README isn't the only place the system prompt is
77
+ # discoverable. Lets reviewers see exactly what context the model is
78
+ # given without having to clone the Space and grep llm.py.
79
+ with gr.Blocks(theme=demo.theme) as full_app:
80
+ with gr.Tab("Chat"):
81
+ demo.render()
82
+ with gr.Tab("System prompt"):
83
+ gr.Markdown(
84
+ f"**Model in use:** `{MODEL_ID_IN_USE}`\n\n"
85
+ "**System prompt baked into every conversation:**\n\n"
86
+ f"```\n{SYSTEM_PROMPT}\n```"
87
+ )
88
+
89
 
90
+ if __name__ == "__main__":
91
+ full_app.queue(default_concurrency_limit=1).launch(
92
+ server_name="0.0.0.0",
93
+ # When running on HF Spaces, the platform sets the port via
94
+ # the GRADIO_SERVER_PORT env var; locally we default to 7860.
95
+ server_port=int(os.environ.get("GRADIO_SERVER_PORT", "7860")),
96
+ show_api=True, # exposes /docs for the auto-generated API
97
+ )