Lui3ui3ui commited on
Commit
9fa9533
·
verified ·
1 Parent(s): d2118dc

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -4,34 +4,52 @@ from agents import build_graph
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
21
  ) or "No recommendations found."
22
 
 
23
  return recs_text, reasoning
24
 
25
  with gr.Blocks() as demo:
26
  gr.Markdown("# 📚 AI Book Recommender")
27
- user_input = gr.Textbox(label="Tell me some books you like")
28
  btn = gr.Button("Get Recommendations")
29
  out_recs = gr.Textbox(label="Recommended Books", lines=10)
30
  out_reason = gr.Textbox(label="Reasoning / Debug Log", lines=15)
31
 
32
- btn.click(run_book_recommender,
33
- inputs=user_input,
34
- outputs=[out_recs, out_reason])
 
 
35
 
36
- if __name__ == "__main__":
37
  demo.launch()
 
4
  graph = build_graph()
5
 
6
  async def run_book_recommender(user_input):
7
+ # 1) Kick off the state‐graph via .astream
8
  initial_state = {"user_input": user_input}
 
9
  final_state = None
 
 
 
10
 
11
+ try:
12
+ async for state in graph.astream(initial_state):
13
+ final_state = state
14
+ except Exception as e:
15
+ # Log it somewhere if you want
16
+ print("🔥 Exception while streaming graph:", e)
17
+ # And then re-raise so Gradio can show it
18
+ raise
19
+
20
+ # 2) If for some bizarre reason the graph yielded zero times,
21
+ # fall back to a safe default
22
+ if final_state is None:
23
+ final_state = {
24
+ "final_recommendations": [],
25
+ "final_reasoning": "⚠️ Graph never yielded a final state."
26
+ }
27
+
28
+ # 3) Extract the real outputs
29
  recs = final_state.get("final_recommendations", [])
30
  reasoning = final_state.get("final_reasoning", "")
31
 
32
+ # 4) Format them
33
  recs_text = "\n\n".join(
34
  f"📘 {r['title']}\n🔗 {r.get('link','')}\n💡 {r.get('reason','')}"
35
  for r in recs
36
  ) or "No recommendations found."
37
 
38
+ # 5) **Explicitly return** in all cases
39
  return recs_text, reasoning
40
 
41
  with gr.Blocks() as demo:
42
  gr.Markdown("# 📚 AI Book Recommender")
43
+ user_in = gr.Textbox(label="Tell me some books you like")
44
  btn = gr.Button("Get Recommendations")
45
  out_recs = gr.Textbox(label="Recommended Books", lines=10)
46
  out_reason = gr.Textbox(label="Reasoning / Debug Log", lines=15)
47
 
48
+ btn.click(
49
+ fn=run_book_recommender,
50
+ inputs=user_in,
51
+ outputs=[out_recs, out_reason],
52
+ )
53
 
54
+ if __name__=="__main__":
55
  demo.launch()