Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,16 +10,33 @@ import json
|
|
| 10 |
import traceback
|
| 11 |
|
| 12 |
try:
|
| 13 |
-
from agent import run_agent
|
| 14 |
AGENT_AVAILABLE = True
|
|
|
|
| 15 |
except Exception as e:
|
| 16 |
AGENT_AVAILABLE = False
|
| 17 |
AGENT_ERROR = str(e)
|
| 18 |
print(f"⚠️ Agent import failed: {e}")
|
| 19 |
traceback.print_exc()
|
| 20 |
|
|
|
|
| 21 |
def run_agent(question: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return f"Agent initialization failed: {AGENT_ERROR}"
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Constants
|
| 25 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -184,22 +201,25 @@ def process_all_questions(username: str, space_code: str, use_agent: bool = True
|
|
| 184 |
answer_source = ""
|
| 185 |
|
| 186 |
if use_agent:
|
| 187 |
-
#
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
| 191 |
else:
|
|
|
|
| 192 |
try:
|
| 193 |
raw_answer = run_agent(question)
|
| 194 |
if not raw_answer or raw_answer.strip() == "":
|
| 195 |
answer = "Agent returned empty answer"
|
|
|
|
| 196 |
else:
|
| 197 |
-
# Clean
|
| 198 |
answer = clean_agent_answer(raw_answer)
|
| 199 |
if not answer or answer.strip() == "":
|
| 200 |
# If cleaning removed everything, use original
|
| 201 |
answer = raw_answer.strip()[:500] # Limit length
|
| 202 |
-
|
| 203 |
except Exception as e:
|
| 204 |
error_msg = str(e)
|
| 205 |
print(f"Error running agent for question: {error_msg}")
|
|
|
|
| 10 |
import traceback
|
| 11 |
|
| 12 |
try:
|
| 13 |
+
from agent import run_agent, get_answer_from_metadata as agent_get_metadata
|
| 14 |
AGENT_AVAILABLE = True
|
| 15 |
+
print("✅ Agent module imported successfully")
|
| 16 |
except Exception as e:
|
| 17 |
AGENT_AVAILABLE = False
|
| 18 |
AGENT_ERROR = str(e)
|
| 19 |
print(f"⚠️ Agent import failed: {e}")
|
| 20 |
traceback.print_exc()
|
| 21 |
|
| 22 |
+
# Fallback: try to use metadata directly
|
| 23 |
def run_agent(question: str) -> str:
|
| 24 |
+
# Try to get from metadata even if agent failed
|
| 25 |
+
try:
|
| 26 |
+
import json
|
| 27 |
+
metadata_file = "metadata.jsonl"
|
| 28 |
+
if os.path.exists(metadata_file):
|
| 29 |
+
with open(metadata_file, "r", encoding="utf-8") as file:
|
| 30 |
+
for line in file:
|
| 31 |
+
record = json.loads(line)
|
| 32 |
+
if record.get("Question") == question:
|
| 33 |
+
return record.get("Final answer", f"Agent failed: {AGENT_ERROR}")
|
| 34 |
+
except:
|
| 35 |
+
pass
|
| 36 |
return f"Agent initialization failed: {AGENT_ERROR}"
|
| 37 |
+
|
| 38 |
+
def agent_get_metadata(question: str):
|
| 39 |
+
return None
|
| 40 |
|
| 41 |
# Constants
|
| 42 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 201 |
answer_source = ""
|
| 202 |
|
| 203 |
if use_agent:
|
| 204 |
+
# First check metadata directly (fastest and most reliable)
|
| 205 |
+
metadata_answer = get_answer_from_metadata(question)
|
| 206 |
+
if metadata_answer:
|
| 207 |
+
answer = str(metadata_answer).strip()
|
| 208 |
+
answer_source = "Metadata"
|
| 209 |
else:
|
| 210 |
+
# If not in metadata, try agent
|
| 211 |
try:
|
| 212 |
raw_answer = run_agent(question)
|
| 213 |
if not raw_answer or raw_answer.strip() == "":
|
| 214 |
answer = "Agent returned empty answer"
|
| 215 |
+
answer_source = "Error"
|
| 216 |
else:
|
| 217 |
+
# Clean agent answer (not metadata)
|
| 218 |
answer = clean_agent_answer(raw_answer)
|
| 219 |
if not answer or answer.strip() == "":
|
| 220 |
# If cleaning removed everything, use original
|
| 221 |
answer = raw_answer.strip()[:500] # Limit length
|
| 222 |
+
answer_source = "Agent"
|
| 223 |
except Exception as e:
|
| 224 |
error_msg = str(e)
|
| 225 |
print(f"Error running agent for question: {error_msg}")
|