Spaces:
Sleeping
Sleeping
File size: 3,891 Bytes
3232f5e 5d94231 ae346db 3232f5e 4d99d19 d2118dc 4d99d19 9fa9533 5dfa96c 9fa9533 5dfa96c 917a35c 9fa9533 5dfa96c 9fa9533 5dfa96c 9fa9533 5dfa96c 61c6352 5dfa96c 61c6352 5dfa96c 61c6352 5dfa96c 9fa9533 61c6352 5dfa96c 61c6352 3232f5e ae346db 5dfa96c ae346db 5dfa96c ae346db 5dfa96c ae346db 5dfa96c ae346db 3232f5e 5d94231 3232f5e 9fa9533 5d94231 3232f5e 9fa9533 3232f5e 9fa9533 4d99d19 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | import gradio as gr
from agents import build_graph
import json
from pprint import pformat
graph = build_graph()
async def run_book_recommender(user_input):
initial_state = {"user_input": user_input}
final_state = None
try:
step_count = 0
async for state in graph.astream(initial_state):
step_count += 1
print(f"π Step {step_count}: State keys = {list(state.keys())}")
if "final_recommendations" in state:
print(f"π Step {step_count}: Found final_recommendations: {state['final_recommendations']}")
if "final_reasoning" in state:
print(f"π Step {step_count}: Found final_reasoning (first 200 chars): {state['final_reasoning'][:200]}...")
final_state = state
print(f"β
Graph completed in {step_count} steps")
except Exception as e:
print("π₯ Exception while streaming graph:", e)
import traceback
print("π₯ Traceback:", traceback.format_exc())
raise
if final_state is None:
final_state = {
"final_recommendations": [],
"final_reasoning": "β οΈ Graph never yielded a final state."
}
# Ensure we have the expected keys in final_state
print(f"π Final state keys: {list(final_state.keys())}")
print(f"π Final state content: {final_state}")
if "final_recommendations" not in final_state:
print("β οΈ final_recommendations not found in final state")
final_state["final_recommendations"] = []
if "final_reasoning" not in final_state:
print("β οΈ final_reasoning not found in final state")
final_state["final_reasoning"] = "β οΈ Missing reasoning data from graph execution."
# Access the final state - check both possible structures
recs = final_state.get("final_recommendations", [])
reasoning = final_state.get("final_reasoning", "")
# If not found in direct keys, check if they're nested under 'reasoning'
if not recs and "reasoning" in final_state:
reasoning_data = final_state.get("reasoning", {})
if isinstance(reasoning_data, dict):
recs = reasoning_data.get("final_recommendations", [])
reasoning = reasoning_data.get("final_reasoning", reasoning)
print(f"π Extracted recs: {recs}")
print(f"π Extracted reasoning (first 200 chars): {reasoning[:200] if reasoning else 'None'}...")
# Defensive formatting of recommendations
try:
# Ensure recs is a list
if not isinstance(recs, list):
recs = []
# Filter out invalid entries
valid_recs = []
for r in recs:
if isinstance(r, dict) and r.get('title'):
valid_recs.append(r)
if valid_recs:
# Format nicely as before
recs_text = "\n\n".join(
f"π {r.get('title', 'Unknown Title')}\nπ {r.get('link','')}\nπ‘ {r.get('reason','')}"
for r in valid_recs
)
else:
recs_text = "No recommendations found."
except Exception as e:
print(f"Error formatting recommendations: {e}")
recs_text = f"Error formatting recommendations: {e}"
return recs_text, reasoning
with gr.Blocks() as demo:
gr.Markdown("# π AI Book Recommender")
user_in = gr.Textbox(label="Tell me some books you like")
btn = gr.Button("Get Recommendations")
out_recs = gr.Textbox(label="Recommended Books", lines=10)
out_reason = gr.Textbox(label="Reasoning / Debug Log", lines=15)
btn.click(
fn=run_book_recommender,
inputs=user_in,
outputs=[out_recs, out_reason],
)
if __name__=="__main__":
demo.launch()
|