Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,14 @@
|
|
1 |
-
|
2 |
-
import os, json, asyncio,
|
3 |
import gradio as gr
|
4 |
from dotenv import load_dotenv
|
5 |
|
6 |
load_dotenv()
|
7 |
|
8 |
from genesis.pipeline import research_once
|
9 |
-
from genesis.providers import postprocess_summary
|
10 |
from genesis.graph import build_preview_graph_html
|
|
|
11 |
|
12 |
APP_TITLE = "GENESIS-AI β Synthetic Biology Deep Research (Safety-First)"
|
13 |
APP_DESC = (
|
@@ -18,7 +19,7 @@ APP_DESC = (
|
|
18 |
DEFAULT_POST = os.getenv("POSTPROCESSOR_DEFAULT", "none").lower()
|
19 |
DEFAULT_RERANK_MODEL = os.getenv("RERANK_MODEL", "mixedbread-ai/mxbai-rerank-large-v1")
|
20 |
|
21 |
-
async def run_pipeline(query: str, fast: bool, postprocessor: str, want_graph: bool) -> tuple:
|
22 |
out = await research_once(query, fast=fast, rerank_model=DEFAULT_RERANK_MODEL)
|
23 |
|
24 |
# Optional post-processing (Gemini/DeepSeek) for polish ONLY (no lab steps)
|
@@ -29,21 +30,54 @@ async def run_pipeline(query: str, fast: bool, postprocessor: str, want_graph: b
|
|
29 |
engine=postprocessor,
|
30 |
)
|
31 |
|
|
|
|
|
|
|
|
|
|
|
32 |
# Optional graph preview
|
33 |
-
graph_html = None
|
34 |
-
if want_graph:
|
35 |
-
graph_html = build_preview_graph_html(out.get("citations", []))
|
36 |
|
37 |
-
final_md =
|
|
|
38 |
cites_md = "
|
39 |
-
".join(
|
40 |
json_blob = json.dumps(out, indent=2)
|
41 |
-
return final_md, cites_md, json_blob, graph_html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
with gr.Blocks(theme=gr.themes.Soft(), fill_height=True) as demo:
|
44 |
gr.Markdown(f"# {APP_TITLE}")
|
45 |
gr.Markdown(APP_DESC)
|
46 |
|
|
|
|
|
47 |
with gr.Row():
|
48 |
query = gr.Textbox(
|
49 |
label="Your high-level research request",
|
@@ -73,12 +107,22 @@ with gr.Blocks(theme=gr.themes.Soft(), fill_height=True) as demo:
|
|
73 |
json_out = gr.Code(language="json")
|
74 |
with gr.Tab("Graph Preview"):
|
75 |
graph_html = gr.HTML()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
go.click(
|
78 |
fn=run_pipeline,
|
79 |
-
inputs=[query, fast, post, want_graph],
|
80 |
-
outputs=[report, citations, json_out, graph_html],
|
81 |
)
|
82 |
|
|
|
|
|
|
|
83 |
if __name__ == "__main__":
|
84 |
demo.launch()
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
import os, json, asyncio, tempfile
|
3 |
import gradio as gr
|
4 |
from dotenv import load_dotenv
|
5 |
|
6 |
load_dotenv()
|
7 |
|
8 |
from genesis.pipeline import research_once
|
9 |
+
from genesis.providers import postprocess_summary, synthesize_tts
|
10 |
from genesis.graph import build_preview_graph_html
|
11 |
+
from genesis.graphdb import write_topic_and_papers
|
12 |
|
13 |
APP_TITLE = "GENESIS-AI β Synthetic Biology Deep Research (Safety-First)"
|
14 |
APP_DESC = (
|
|
|
19 |
DEFAULT_POST = os.getenv("POSTPROCESSOR_DEFAULT", "none").lower()
|
20 |
DEFAULT_RERANK_MODEL = os.getenv("RERANK_MODEL", "mixedbread-ai/mxbai-rerank-large-v1")
|
21 |
|
22 |
+
async def run_pipeline(query: str, fast: bool, postprocessor: str, want_graph: bool, state: dict) -> tuple:
|
23 |
out = await research_once(query, fast=fast, rerank_model=DEFAULT_RERANK_MODEL)
|
24 |
|
25 |
# Optional post-processing (Gemini/DeepSeek) for polish ONLY (no lab steps)
|
|
|
30 |
engine=postprocessor,
|
31 |
)
|
32 |
|
33 |
+
# Save into state for follow-ups (TTS, Graph Writer)
|
34 |
+
state["final_text"] = out.get("final_output") or ""
|
35 |
+
state["citations"] = out.get("citations", [])
|
36 |
+
state["query"] = query
|
37 |
+
|
38 |
# Optional graph preview
|
39 |
+
graph_html = build_preview_graph_html(state["citations"]) if want_graph else None
|
|
|
|
|
40 |
|
41 |
+
final_md = state["final_text"] or "_No output_"
|
42 |
+
cites_list = [f"- [{c.get('title','link')}]({c.get('url','')})" for c in state["citations"]]
|
43 |
cites_md = "
|
44 |
+
".join(cites_list) if cites_list else "_None detected_"
|
45 |
json_blob = json.dumps(out, indent=2)
|
46 |
+
return final_md, cites_md, json_blob, graph_html, state
|
47 |
+
|
48 |
+
async def do_tts(state: dict) -> tuple:
|
49 |
+
text = (state or {}).get("final_text") or ""
|
50 |
+
if not text.strip():
|
51 |
+
return None, "Nothing to narrate yet β run research first."
|
52 |
+
try:
|
53 |
+
audio_bytes, mime = await synthesize_tts(text)
|
54 |
+
if not audio_bytes:
|
55 |
+
return None, "TTS not configured or failed. Ensure ELEVEN_LABS_API_KEY/VOICE_ID are set."
|
56 |
+
suffix = ".mp3" if "mpeg" in (mime or "") else ".wav"
|
57 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as f:
|
58 |
+
f.write(audio_bytes)
|
59 |
+
path = f.name
|
60 |
+
return path, "Narration ready."
|
61 |
+
except Exception as e:
|
62 |
+
return None, f"TTS error: {e}"
|
63 |
+
|
64 |
+
async def do_graph_write(state: dict) -> str:
|
65 |
+
topic = (state or {}).get("query") or "Untitled Topic"
|
66 |
+
citations = (state or {}).get("citations") or []
|
67 |
+
if not citations:
|
68 |
+
return "No citations present β run research first."
|
69 |
+
try:
|
70 |
+
counts = await write_topic_and_papers(topic, citations)
|
71 |
+
return f"Wrote to Neo4j: nodes={counts.get('nodes',0)}, rels={counts.get('rels',0)}"
|
72 |
+
except Exception as e:
|
73 |
+
return f"Neo4j write error: {e}. Ensure NEO4J_* env vars are set."
|
74 |
|
75 |
with gr.Blocks(theme=gr.themes.Soft(), fill_height=True) as demo:
|
76 |
gr.Markdown(f"# {APP_TITLE}")
|
77 |
gr.Markdown(APP_DESC)
|
78 |
|
79 |
+
state = gr.State({"final_text": "", "citations": [], "query": ""})
|
80 |
+
|
81 |
with gr.Row():
|
82 |
query = gr.Textbox(
|
83 |
label="Your high-level research request",
|
|
|
107 |
json_out = gr.Code(language="json")
|
108 |
with gr.Tab("Graph Preview"):
|
109 |
graph_html = gr.HTML()
|
110 |
+
with gr.Tab("Graph Writer (Neo4j)"):
|
111 |
+
write_btn = gr.Button("Write Topic & Papers to Neo4j", variant="secondary")
|
112 |
+
write_status = gr.Markdown()
|
113 |
+
with gr.Tab("Narration (ElevenLabs)"):
|
114 |
+
tts_btn = gr.Button("Narrate Summary", variant="secondary")
|
115 |
+
tts_audio = gr.Audio(label="Narration", autoplay=False)
|
116 |
+
tts_status = gr.Markdown()
|
117 |
|
118 |
go.click(
|
119 |
fn=run_pipeline,
|
120 |
+
inputs=[query, fast, post, want_graph, state],
|
121 |
+
outputs=[report, citations, json_out, graph_html, state],
|
122 |
)
|
123 |
|
124 |
+
tts_btn.click(fn=do_tts, inputs=[state], outputs=[tts_audio, tts_status])
|
125 |
+
write_btn.click(fn=do_graph_write, inputs=[state], outputs=[write_status])
|
126 |
+
|
127 |
if __name__ == "__main__":
|
128 |
demo.launch()
|