Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,8 +10,7 @@ import string
|
|
| 10 |
|
| 11 |
###
|
| 12 |
|
| 13 |
-
import
|
| 14 |
-
from bs4 import BeautifulSoup
|
| 15 |
|
| 16 |
# (Keep Constants as is)
|
| 17 |
# --- Constants ---
|
|
@@ -126,37 +125,46 @@ class SuperSmartAgent:
|
|
| 126 |
return state
|
| 127 |
|
| 128 |
###################
|
| 129 |
-
def
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
| 140 |
|
| 141 |
-
if
|
| 142 |
-
|
|
|
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
for p in paragraphs:
|
| 155 |
-
text = p.get_text().strip()
|
| 156 |
-
if len(text) > 50: # Skip very short paragraphs
|
| 157 |
-
return f"From Wikipedia page '{page_title}':\n\n{text}"
|
| 158 |
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
def check_wikipedia_suitability(state):
|
| 162 |
q = state["question"].lower()
|
|
@@ -168,7 +176,17 @@ class SuperSmartAgent:
|
|
| 168 |
|
| 169 |
def search_wikipedia(state):
|
| 170 |
question = state["question"]
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
return state
|
| 173 |
|
| 174 |
##################
|
|
@@ -183,21 +201,24 @@ class SuperSmartAgent:
|
|
| 183 |
|
| 184 |
builder = StateGraph(AgentState)
|
| 185 |
|
|
|
|
| 186 |
# --- Nodes ---
|
| 187 |
builder.add_node("check_reversed", check_reversed)
|
| 188 |
builder.add_node("fix_question", fix_question)
|
| 189 |
builder.add_node("check_riddle_or_trick", check_riddle_or_trick)
|
| 190 |
builder.add_node("solve_riddle", solve_riddle)
|
| 191 |
builder.add_node("check_wikipedia_suitability", check_wikipedia_suitability)
|
|
|
|
|
|
|
| 192 |
builder.add_node("search_wikipedia", search_wikipedia)
|
| 193 |
builder.add_node("check_python_suitability", check_python_suitability)
|
| 194 |
builder.add_node("generate_code", generate_code)
|
| 195 |
builder.add_node("fallback", fallback)
|
| 196 |
-
|
| 197 |
-
# --- Entry point ---
|
| 198 |
-
builder.set_entry_point("check_reversed")
|
| 199 |
|
| 200 |
-
#
|
|
|
|
|
|
|
|
|
|
| 201 |
builder.add_edge("check_reversed", "fix_question")
|
| 202 |
builder.add_edge("fix_question", "check_riddle_or_trick")
|
| 203 |
|
|
@@ -206,24 +227,30 @@ class SuperSmartAgent:
|
|
| 206 |
lambda s: "solve_riddle" if s.get("is_riddle") else "check_wikipedia_suitability"
|
| 207 |
)
|
| 208 |
|
| 209 |
-
builder.add_edge("solve_riddle", END)
|
| 210 |
-
|
| 211 |
builder.add_conditional_edges(
|
| 212 |
"check_wikipedia_suitability",
|
| 213 |
-
lambda s: "search_wikipedia" if s.get("is_wiki") else "
|
| 214 |
)
|
| 215 |
|
| 216 |
-
builder.
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
builder.add_conditional_edges(
|
| 219 |
"check_python_suitability",
|
| 220 |
lambda s: "generate_code" if s.get("is_python") else "fallback"
|
| 221 |
)
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
builder.add_edge("generate_code", END)
|
| 224 |
builder.add_edge("fallback", END)
|
| 225 |
|
| 226 |
|
|
|
|
| 227 |
|
| 228 |
|
| 229 |
graph = builder.compile()
|
|
|
|
| 10 |
|
| 11 |
###
|
| 12 |
|
| 13 |
+
import wikipedia
|
|
|
|
| 14 |
|
| 15 |
# (Keep Constants as is)
|
| 16 |
# --- Constants ---
|
|
|
|
| 125 |
return state
|
| 126 |
|
| 127 |
###################
|
| 128 |
+
def general_reasoning_qa(state):
|
| 129 |
+
question = state["question"]
|
| 130 |
+
|
| 131 |
+
# Step 1: Search Wikipedia broadly
|
| 132 |
+
search_results = wikipedia.search(question)
|
| 133 |
+
relevant_pages = search_results[:3] # get top 3 pages
|
| 134 |
+
|
| 135 |
+
context = ""
|
| 136 |
+
for title in relevant_pages:
|
| 137 |
+
try:
|
| 138 |
+
context += wikipedia.page(title).content + "\n"
|
| 139 |
+
except:
|
| 140 |
+
continue
|
| 141 |
|
| 142 |
+
if not context:
|
| 143 |
+
state["response"] = "Sorry, I couldn’t find enough information."
|
| 144 |
+
return state
|
| 145 |
|
| 146 |
+
# Step 2: Ask the LLM to reason over the context
|
| 147 |
+
prompt = f"""
|
| 148 |
+
Based on the following Wikipedia content, answer the question:
|
| 149 |
|
| 150 |
+
Question: {question}
|
| 151 |
+
|
| 152 |
+
Context:
|
| 153 |
+
{context}
|
| 154 |
+
|
| 155 |
+
Answer in a single sentence.
|
| 156 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
+
state["response"] = call_llm(prompt) # call your preferred LLM method
|
| 159 |
+
return state
|
| 160 |
+
|
| 161 |
+
def check_reasoning_needed(state):
|
| 162 |
+
q = state["question"].lower()
|
| 163 |
+
# very rough heuristic — refine as needed
|
| 164 |
+
needs_reasoning = any(word in q for word in ["whose", "only", "first", "after", "before", "no longer", "not", "but", "except"])
|
| 165 |
+
state["needs_reasoning"] = needs_reasoning
|
| 166 |
+
return state
|
| 167 |
+
|
| 168 |
|
| 169 |
def check_wikipedia_suitability(state):
|
| 170 |
q = state["question"].lower()
|
|
|
|
| 176 |
|
| 177 |
def search_wikipedia(state):
|
| 178 |
question = state["question"]
|
| 179 |
+
try:
|
| 180 |
+
page_titles = wikipedia.search(question)
|
| 181 |
+
if not page_titles:
|
| 182 |
+
state["response"] = "No relevant Wikipedia article found."
|
| 183 |
+
return state
|
| 184 |
+
|
| 185 |
+
page = wikipedia.page(page_titles[0])
|
| 186 |
+
summary = page.summary
|
| 187 |
+
state["response"] = summary
|
| 188 |
+
except Exception as e:
|
| 189 |
+
state["response"] = f"Error fetching Wikipedia content: {e}"
|
| 190 |
return state
|
| 191 |
|
| 192 |
##################
|
|
|
|
| 201 |
|
| 202 |
builder = StateGraph(AgentState)
|
| 203 |
|
| 204 |
+
|
| 205 |
# --- Nodes ---
|
| 206 |
builder.add_node("check_reversed", check_reversed)
|
| 207 |
builder.add_node("fix_question", fix_question)
|
| 208 |
builder.add_node("check_riddle_or_trick", check_riddle_or_trick)
|
| 209 |
builder.add_node("solve_riddle", solve_riddle)
|
| 210 |
builder.add_node("check_wikipedia_suitability", check_wikipedia_suitability)
|
| 211 |
+
builder.add_node("check_reasoning_needed", check_reasoning_needed)
|
| 212 |
+
builder.add_node("general_reasoning_qa", general_reasoning_qa)
|
| 213 |
builder.add_node("search_wikipedia", search_wikipedia)
|
| 214 |
builder.add_node("check_python_suitability", check_python_suitability)
|
| 215 |
builder.add_node("generate_code", generate_code)
|
| 216 |
builder.add_node("fallback", fallback)
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
+
# Entry
|
| 219 |
+
builder.set_entry_point("check_reversed")
|
| 220 |
+
|
| 221 |
+
# Edges
|
| 222 |
builder.add_edge("check_reversed", "fix_question")
|
| 223 |
builder.add_edge("fix_question", "check_riddle_or_trick")
|
| 224 |
|
|
|
|
| 227 |
lambda s: "solve_riddle" if s.get("is_riddle") else "check_wikipedia_suitability"
|
| 228 |
)
|
| 229 |
|
|
|
|
|
|
|
| 230 |
builder.add_conditional_edges(
|
| 231 |
"check_wikipedia_suitability",
|
| 232 |
+
lambda s: "search_wikipedia" if s.get("is_wiki") else "check_reasoning_needed"
|
| 233 |
)
|
| 234 |
|
| 235 |
+
builder.add_conditional_edges(
|
| 236 |
+
"check_reasoning_needed",
|
| 237 |
+
lambda s: "general_reasoning_qa" if s.get("needs_reasoning") else "check_python_suitability"
|
| 238 |
+
)
|
| 239 |
|
| 240 |
builder.add_conditional_edges(
|
| 241 |
"check_python_suitability",
|
| 242 |
lambda s: "generate_code" if s.get("is_python") else "fallback"
|
| 243 |
)
|
| 244 |
|
| 245 |
+
# Ends
|
| 246 |
+
builder.add_edge("solve_riddle", END)
|
| 247 |
+
builder.add_edge("search_wikipedia", END)
|
| 248 |
+
builder.add_edge("general_reasoning_qa", END)
|
| 249 |
builder.add_edge("generate_code", END)
|
| 250 |
builder.add_edge("fallback", END)
|
| 251 |
|
| 252 |
|
| 253 |
+
|
| 254 |
|
| 255 |
|
| 256 |
graph = builder.compile()
|