Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -128,14 +128,15 @@ class SuperSmartAgent:
|
|
| 128 |
def general_reasoning_qa(state):
|
| 129 |
question = state["question"]
|
| 130 |
|
| 131 |
-
# Step 1: Search Wikipedia
|
| 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 |
-
|
|
|
|
| 139 |
except:
|
| 140 |
continue
|
| 141 |
|
|
@@ -143,22 +144,42 @@ class SuperSmartAgent:
|
|
| 143 |
state["response"] = "Sorry, I couldn’t find enough information."
|
| 144 |
return state
|
| 145 |
|
| 146 |
-
# Step 2:
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
Based on the following Wikipedia content, answer the question:
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
Answer in a single sentence.
|
| 157 |
-
"""
|
| 158 |
-
state["response"] = agent(prompt) # Use the SuperSmartAgent to process the prompt
|
| 159 |
return state
|
| 160 |
|
| 161 |
|
|
|
|
| 162 |
def check_reasoning_needed(state):
|
| 163 |
q = state["question"].lower()
|
| 164 |
# very rough heuristic — refine as needed
|
|
|
|
| 128 |
def general_reasoning_qa(state):
|
| 129 |
question = state["question"]
|
| 130 |
|
| 131 |
+
# Step 1: Search Wikipedia for relevant pages
|
| 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 |
+
page = wikipedia.page(title)
|
| 139 |
+
context += page.content + "\n"
|
| 140 |
except:
|
| 141 |
continue
|
| 142 |
|
|
|
|
| 144 |
state["response"] = "Sorry, I couldn’t find enough information."
|
| 145 |
return state
|
| 146 |
|
| 147 |
+
# Step 2: Process the context to extract relevant information
|
| 148 |
+
# This is a simplified approach; in practice, you'd use more sophisticated NLP techniques
|
| 149 |
+
# For example, you can look for numerical data, dates, names, etc., in the context
|
|
|
|
| 150 |
|
| 151 |
+
# Example: Extract numbers and names from the context
|
| 152 |
+
import re
|
| 153 |
+
numbers = re.findall(r'\d+', context)
|
| 154 |
+
names = re.findall(r'\b[A-Z][a-z]+ [A-Z][a-z]+\b', context) # Simplified pattern for names
|
| 155 |
+
|
| 156 |
+
# Step 3: Generate an answer based on the processed context
|
| 157 |
+
# This is a placeholder; in practice, you'd need a more sophisticated method
|
| 158 |
+
if "How many" in question:
|
| 159 |
+
if numbers:
|
| 160 |
+
# Assume the first number is relevant (this is a very simplistic approach)
|
| 161 |
+
state["response"] = f"The answer is {numbers[0]}."
|
| 162 |
+
else:
|
| 163 |
+
state["response"] = "I couldn't find a numerical answer in the context."
|
| 164 |
+
elif "who" in question.lower() or "what" in question.lower():
|
| 165 |
+
if names:
|
| 166 |
+
# Assume the first name is relevant (this is a very simplistic approach)
|
| 167 |
+
state["response"] = f"The answer is {names[0]}."
|
| 168 |
+
else:
|
| 169 |
+
state["response"] = "I couldn't find a relevant name in the context."
|
| 170 |
+
else:
|
| 171 |
+
# Fallback to returning a summary if no specific pattern matches
|
| 172 |
+
try:
|
| 173 |
+
page = wikipedia.page(relevant_pages[0])
|
| 174 |
+
summary = page.summary
|
| 175 |
+
state["response"] = summary
|
| 176 |
+
except Exception as e:
|
| 177 |
+
state["response"] = f"Error fetching Wikipedia content: {e}"
|
| 178 |
|
|
|
|
|
|
|
|
|
|
| 179 |
return state
|
| 180 |
|
| 181 |
|
| 182 |
+
|
| 183 |
def check_reasoning_needed(state):
|
| 184 |
q = state["question"].lower()
|
| 185 |
# very rough heuristic — refine as needed
|