Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,153 +1,65 @@
|
|
1 |
-
|
2 |
-
|
3 |
import os
|
4 |
-
import json
|
5 |
-
import tempfile
|
6 |
import gradio as gr
|
7 |
-
from dotenv import load_dotenv
|
8 |
-
|
9 |
-
load_dotenv()
|
10 |
|
11 |
from genesis.pipeline import research_once
|
12 |
-
from genesis.
|
13 |
-
from genesis.graph import build_preview_graph_html
|
14 |
-
from genesis.graphdb import write_topic_and_papers
|
15 |
-
|
16 |
-
APP_TITLE = "GENESIS-AI β Synthetic Biology Deep Research (Safety-First)"
|
17 |
-
APP_DESC = (
|
18 |
-
"High-level synthetic biology literature synthesis with citations. "
|
19 |
-
"This app NEVER produces operational protocols."
|
20 |
-
)
|
21 |
-
|
22 |
-
DEFAULT_POST = os.getenv("POSTPROCESSOR_DEFAULT", "none").lower()
|
23 |
-
DEFAULT_RERANK_MODEL = os.getenv("RERANK_MODEL", "mixedbread-ai/mxbai-rerank-large-v1")
|
24 |
-
|
25 |
-
|
26 |
-
async def run_pipeline(
|
27 |
-
query: str,
|
28 |
-
fast: bool,
|
29 |
-
postprocessor: str,
|
30 |
-
want_graph: bool,
|
31 |
-
state: dict,
|
32 |
-
) -> tuple[str, str, str, str | None, dict]:
|
33 |
-
"""
|
34 |
-
Orchestrates deep research + optional post-processing + optional graph preview.
|
35 |
-
Returns: (final_markdown, citations_markdown, json_blob, graph_html, state)
|
36 |
-
"""
|
37 |
-
out = await research_once(query, fast=fast, rerank_model=DEFAULT_RERANK_MODEL)
|
38 |
-
|
39 |
-
# Optional polish (Gemini/DeepSeek) β never add lab steps
|
40 |
-
if postprocessor and postprocessor != "none":
|
41 |
-
out["final_output"] = await postprocess_summary(
|
42 |
-
base_text=out.get("final_output") or "",
|
43 |
-
citations=out.get("citations", []),
|
44 |
-
engine=postprocessor,
|
45 |
-
)
|
46 |
-
|
47 |
-
# Keep state for TTS / graph writer
|
48 |
-
state = state or {}
|
49 |
-
state["final_text"] = out.get("final_output") or ""
|
50 |
-
state["citations"] = out.get("citations", [])
|
51 |
-
state["query"] = query
|
52 |
-
|
53 |
-
# Graph preview HTML
|
54 |
-
graph_html = build_preview_graph_html(state["citations"]) if want_graph else None
|
55 |
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
title = c.get("title") or "link"
|
63 |
-
url = c.get("url") or ""
|
64 |
-
cite_lines.append(f"- [{title}]({url})")
|
65 |
-
cites_md = "\n".join(cite_lines) if cite_lines else "_None detected_"
|
66 |
|
67 |
-
|
68 |
-
json_blob = json.dumps(out, indent=2)
|
69 |
|
70 |
-
|
|
|
|
|
71 |
|
|
|
|
|
|
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
if not text.strip():
|
77 |
-
return None, "Nothing to narrate yet β run research first."
|
78 |
|
79 |
-
|
80 |
-
if not audio_bytes:
|
81 |
-
return None, "TTS not configured or failed. Ensure ELEVEN_LABS_API_KEY/VOICE_ID are set."
|
82 |
|
83 |
-
suffix = ".mp3" if (mime or "").find("mpeg") >= 0 else ".wav"
|
84 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as f:
|
85 |
-
f.write(audio_bytes)
|
86 |
-
path = f.name
|
87 |
-
return path, "Narration ready."
|
88 |
-
|
89 |
-
|
90 |
-
async def do_graph_write(state: dict) -> str:
|
91 |
-
"""Write Topic -> Paper knowledge graph into Neo4j."""
|
92 |
-
topic = (state or {}).get("query") or "Untitled Topic"
|
93 |
-
citations = (state or {}).get("citations") or []
|
94 |
-
if not citations:
|
95 |
-
return "No citations present β run research first."
|
96 |
-
counts = await write_topic_and_papers(topic, citations)
|
97 |
-
return f"Wrote to Neo4j: nodes={counts.get('nodes',0)}, rels={counts.get('rels',0)}"
|
98 |
-
|
99 |
-
|
100 |
-
with gr.Blocks(theme=gr.themes.Soft(), fill_height=True) as demo:
|
101 |
-
gr.Markdown(f"# {APP_TITLE}")
|
102 |
-
gr.Markdown(APP_DESC)
|
103 |
-
|
104 |
-
state = gr.State({"final_text": "", "citations": [], "query": ""})
|
105 |
|
|
|
|
|
|
|
106 |
with gr.Row():
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
with gr.Tab("Research Report"):
|
128 |
-
report = gr.Markdown()
|
129 |
-
with gr.Tab("Citations"):
|
130 |
-
citations = gr.Markdown()
|
131 |
-
with gr.Tab("JSON Export"):
|
132 |
-
json_out = gr.Code(language="json")
|
133 |
-
with gr.Tab("Graph Preview"):
|
134 |
-
graph_html = gr.HTML()
|
135 |
-
with gr.Tab("Graph Writer (Neo4j)"):
|
136 |
-
write_btn = gr.Button("Write Topic & Papers to Neo4j", variant="secondary")
|
137 |
-
write_status = gr.Markdown()
|
138 |
-
with gr.Tab("Narration (ElevenLabs)"):
|
139 |
-
tts_btn = gr.Button("Narrate Summary", variant="secondary")
|
140 |
-
tts_audio = gr.Audio(label="Narration", autoplay=False)
|
141 |
-
tts_status = gr.Markdown()
|
142 |
-
|
143 |
-
go.click(
|
144 |
-
fn=run_pipeline,
|
145 |
-
inputs=[query, fast, post, want_graph, state],
|
146 |
-
outputs=[report, citations, json_out, graph_html, state],
|
147 |
)
|
148 |
|
149 |
-
tts_btn.click(fn=do_tts, inputs=[state], outputs=[tts_audio, tts_status])
|
150 |
-
write_btn.click(fn=do_graph_write, inputs=[state], outputs=[write_status])
|
151 |
-
|
152 |
if __name__ == "__main__":
|
153 |
demo.launch()
|
|
|
1 |
+
# app.py
|
|
|
2 |
import os
|
|
|
|
|
3 |
import gradio as gr
|
|
|
|
|
|
|
4 |
|
5 |
from genesis.pipeline import research_once
|
6 |
+
from genesis.narration import narrate_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
# Preloaded demo queries for tomorrow
|
9 |
+
demo_queries = [
|
10 |
+
"Designing synthetic gene circuits for targeted cancer therapy",
|
11 |
+
"CRISPR-based microbial engineering for biofuel production",
|
12 |
+
"Synthetic biology approaches to carbon capture and sequestration",
|
13 |
+
"Engineering probiotics for mental health treatments",
|
14 |
+
"DNA-based nanorobots for targeted drug delivery"
|
15 |
+
]
|
16 |
|
17 |
+
def run_pipeline_ui(query):
|
18 |
+
"""Run the full synthetic biology research pipeline."""
|
19 |
+
report, citations, structures, graph_status, image_url = research_once(query)
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
audio_path = narrate_text(report) or "Narration not available"
|
|
|
22 |
|
23 |
+
cites_md = "### Citations\n"
|
24 |
+
for cite in citations:
|
25 |
+
cites_md += f"- [{cite['type']} {cite['id']}]({cite['url']})\n"
|
26 |
|
27 |
+
struct_md = "### Molecular Structures\n"
|
28 |
+
for s in structures:
|
29 |
+
struct_md += f"- {s['term']}: [{s['pdb_id']}]({s['pdbe_url']}) ([RCSB]({s['rcsb_url']}))\n"
|
30 |
|
31 |
+
img_md = "### Generated Visual\n"
|
32 |
+
if image_url:
|
33 |
+
img_md += f"\n"
|
|
|
|
|
34 |
|
35 |
+
return report, cites_md, struct_md, graph_status, audio_path, img_md
|
|
|
|
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
with gr.Blocks(title="GENESIS-AI: Synthetic Biology Deep Research") as demo:
|
39 |
+
gr.Markdown("# 𧬠GENESIS-AI\nThe most advanced synthetic biology research assistant ever built.")
|
40 |
+
|
41 |
with gr.Row():
|
42 |
+
query_box = gr.Dropdown(choices=demo_queries, label="Select a Research Topic", value=demo_queries[0])
|
43 |
+
run_btn = gr.Button("π Run Research")
|
44 |
+
|
45 |
+
with gr.Tab("π Report"):
|
46 |
+
report_out = gr.Textbox(label="Full Research Report", lines=20)
|
47 |
+
with gr.Tab("π Citations"):
|
48 |
+
cites_out = gr.Markdown()
|
49 |
+
with gr.Tab("π¬ Structures"):
|
50 |
+
struct_out = gr.Markdown()
|
51 |
+
with gr.Tab("πΈ Graph"):
|
52 |
+
graph_status_out = gr.Textbox(label="Graph DB Status")
|
53 |
+
with gr.Tab("π Narration"):
|
54 |
+
audio_out = gr.Audio(label="Narrated Summary")
|
55 |
+
with gr.Tab("πΌ Visual Diagram"):
|
56 |
+
img_out = gr.Markdown()
|
57 |
+
|
58 |
+
run_btn.click(
|
59 |
+
fn=run_pipeline_ui,
|
60 |
+
inputs=[query_box],
|
61 |
+
outputs=[report_out, cites_out, struct_out, graph_status_out, audio_out, img_out]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
)
|
63 |
|
|
|
|
|
|
|
64 |
if __name__ == "__main__":
|
65 |
demo.launch()
|