Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,21 @@ import gradio as gr
|
|
| 2 |
import asyncio
|
| 3 |
from agents import build_graph
|
| 4 |
|
|
|
|
| 5 |
graph = build_graph()
|
| 6 |
|
| 7 |
async def run_book_recommender(user_input):
|
| 8 |
-
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
recs = final_state.get("final_recommendations", [])
|
| 11 |
reasoning = final_state.get("final_reasoning", "")
|
| 12 |
|
|
|
|
| 13 |
recs_text = "\n\n".join(
|
| 14 |
f"๐ {r['title']}\n๐ {r.get('link','')}\n๐ก {r.get('reason','')}"
|
| 15 |
for r in recs
|
|
@@ -29,4 +36,4 @@ with gr.Blocks() as demo:
|
|
| 29 |
outputs=[out_recs, out_reason])
|
| 30 |
|
| 31 |
if __name__ == "__main__":
|
| 32 |
-
demo.launch()
|
|
|
|
| 2 |
import asyncio
|
| 3 |
from agents import build_graph
|
| 4 |
|
| 5 |
+
# Build + compile once at import time
|
| 6 |
graph = build_graph()
|
| 7 |
|
| 8 |
async def run_book_recommender(user_input):
|
| 9 |
+
# 1) build initial state
|
| 10 |
+
initial_state = {"user_input": user_input}
|
| 11 |
|
| 12 |
+
# 2) run the graph in one shot
|
| 13 |
+
final_state = await graph.run(initial_state)
|
| 14 |
+
|
| 15 |
+
# 3) extract node-3 outputs from the top-level state
|
| 16 |
recs = final_state.get("final_recommendations", [])
|
| 17 |
reasoning = final_state.get("final_reasoning", "")
|
| 18 |
|
| 19 |
+
# 4) format for display
|
| 20 |
recs_text = "\n\n".join(
|
| 21 |
f"๐ {r['title']}\n๐ {r.get('link','')}\n๐ก {r.get('reason','')}"
|
| 22 |
for r in recs
|
|
|
|
| 36 |
outputs=[out_recs, out_reason])
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
| 39 |
+
demo.launch()
|