Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,13 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
|
| 7 |
from langgraph.graph import StateGraph, END
|
| 8 |
from typing import TypedDict
|
| 9 |
import string
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
|
| 14 |
# (Keep Constants as is)
|
| 15 |
# --- Constants ---
|
|
@@ -124,7 +124,26 @@ class SuperSmartAgent:
|
|
| 124 |
return state
|
| 125 |
|
| 126 |
###################
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
|
| 130 |
|
|
@@ -140,11 +159,13 @@ class SuperSmartAgent:
|
|
| 140 |
|
| 141 |
builder = StateGraph(AgentState)
|
| 142 |
|
| 143 |
-
|
| 144 |
builder.add_node("check_reversed", check_reversed)
|
| 145 |
builder.add_node("fix_question", fix_question)
|
| 146 |
builder.add_node("check_riddle_or_trick", check_riddle_or_trick)
|
| 147 |
builder.add_node("solve_riddle", solve_riddle)
|
|
|
|
|
|
|
| 148 |
builder.add_node("check_python_suitability", check_python_suitability)
|
| 149 |
builder.add_node("generate_code", generate_code)
|
| 150 |
builder.add_node("fallback", fallback)
|
|
@@ -158,17 +179,27 @@ class SuperSmartAgent:
|
|
| 158 |
|
| 159 |
builder.add_conditional_edges(
|
| 160 |
"check_riddle_or_trick",
|
| 161 |
-
lambda s: "solve_riddle" if s.get("is_riddle") else "
|
| 162 |
)
|
|
|
|
| 163 |
builder.add_edge("solve_riddle", END)
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
builder.add_conditional_edges(
|
| 166 |
"check_python_suitability",
|
| 167 |
lambda s: "generate_code" if s.get("is_python") else "fallback"
|
| 168 |
)
|
|
|
|
| 169 |
builder.add_edge("generate_code", END)
|
| 170 |
builder.add_edge("fallback", END)
|
| 171 |
|
|
|
|
| 172 |
|
| 173 |
|
| 174 |
graph = builder.compile()
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
from langgraph.graph import StateGraph, END
|
| 7 |
from typing import TypedDict
|
| 8 |
import string
|
| 9 |
|
| 10 |
+
###
|
| 11 |
|
| 12 |
+
import wikipedia
|
| 13 |
|
| 14 |
# (Keep Constants as is)
|
| 15 |
# --- Constants ---
|
|
|
|
| 124 |
return state
|
| 125 |
|
| 126 |
###################
|
| 127 |
+
def check_wikipedia_suitability(state):
|
| 128 |
+
q = state["question"].lower()
|
| 129 |
+
keywords = ["who is", "what is", "where is", "tell me about", "wikipedia", "when was"]
|
| 130 |
+
state["is_wiki"] = any(kw in q for kw in keywords)
|
| 131 |
+
return state
|
| 132 |
+
|
| 133 |
+
def search_wikipedia(state):
|
| 134 |
+
query = state["question"]
|
| 135 |
+
try:
|
| 136 |
+
# Strip prompt like "Who is" to get better titles
|
| 137 |
+
cleaned_query = query.replace("who is", "").replace("what is", "").replace("tell me about", "").strip()
|
| 138 |
+
result = wikipedia.summary(cleaned_query, sentences=2)
|
| 139 |
+
state["response"] = result
|
| 140 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
| 141 |
+
state["response"] = f"Too many results for '{query}', be more specific. Example options: {e.options[:3]}"
|
| 142 |
+
except wikipedia.exceptions.PageError:
|
| 143 |
+
state["response"] = f"Sorry, I couldn't find any info on '{query}'."
|
| 144 |
+
except Exception as e:
|
| 145 |
+
state["response"] = f"Wikipedia lookup failed: {str(e)}"
|
| 146 |
+
return state
|
| 147 |
|
| 148 |
|
| 149 |
|
|
|
|
| 159 |
|
| 160 |
builder = StateGraph(AgentState)
|
| 161 |
|
| 162 |
+
# --- Nodes ---
|
| 163 |
builder.add_node("check_reversed", check_reversed)
|
| 164 |
builder.add_node("fix_question", fix_question)
|
| 165 |
builder.add_node("check_riddle_or_trick", check_riddle_or_trick)
|
| 166 |
builder.add_node("solve_riddle", solve_riddle)
|
| 167 |
+
builder.add_node("check_wikipedia_suitability", check_wikipedia_suitability) # NEW
|
| 168 |
+
builder.add_node("search_wikipedia", search_wikipedia) # NEW
|
| 169 |
builder.add_node("check_python_suitability", check_python_suitability)
|
| 170 |
builder.add_node("generate_code", generate_code)
|
| 171 |
builder.add_node("fallback", fallback)
|
|
|
|
| 179 |
|
| 180 |
builder.add_conditional_edges(
|
| 181 |
"check_riddle_or_trick",
|
| 182 |
+
lambda s: "solve_riddle" if s.get("is_riddle") else "check_wikipedia_suitability"
|
| 183 |
)
|
| 184 |
+
|
| 185 |
builder.add_edge("solve_riddle", END)
|
| 186 |
|
| 187 |
+
builder.add_conditional_edges(
|
| 188 |
+
"check_wikipedia_suitability",
|
| 189 |
+
lambda s: "search_wikipedia" if s.get("is_wiki") else "check_python_suitability"
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
builder.add_edge("search_wikipedia", END)
|
| 193 |
+
|
| 194 |
builder.add_conditional_edges(
|
| 195 |
"check_python_suitability",
|
| 196 |
lambda s: "generate_code" if s.get("is_python") else "fallback"
|
| 197 |
)
|
| 198 |
+
|
| 199 |
builder.add_edge("generate_code", END)
|
| 200 |
builder.add_edge("fallback", END)
|
| 201 |
|
| 202 |
+
|
| 203 |
|
| 204 |
|
| 205 |
graph = builder.compile()
|