Lui3ui3ui commited on
Commit
d2118dc
ยท
verified ยท
1 Parent(s): 4d99d19

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -7
app.py CHANGED
@@ -1,22 +1,20 @@
1
  import gradio as gr
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
 
1
  import gradio as gr
 
2
  from agents import build_graph
3
 
 
4
  graph = build_graph()
5
 
6
  async def run_book_recommender(user_input):
 
7
  initial_state = {"user_input": user_input}
8
 
9
+ final_state = None
10
+ # .astream yields after each node runs
11
+ async for state in graph.astream(initial_state):
12
+ final_state = state
13
 
14
+ # now state after the last node is in final_state
15
  recs = final_state.get("final_recommendations", [])
16
  reasoning = final_state.get("final_reasoning", "")
17
 
 
18
  recs_text = "\n\n".join(
19
  f"๐Ÿ“˜ {r['title']}\n๐Ÿ”— {r.get('link','')}\n๐Ÿ’ก {r.get('reason','')}"
20
  for r in recs