Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,9 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -12,12 +15,45 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
print("
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from dateutil import parser as date_parser
|
| 7 |
+
import calendar
|
| 8 |
+
import re
|
| 9 |
|
| 10 |
# (Keep Constants as is)
|
| 11 |
# --- Constants ---
|
|
|
|
| 15 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 16 |
class BasicAgent:
|
| 17 |
def __init__(self):
|
| 18 |
+
print("Tool-based Agent initialized (Math + Date)")
|
| 19 |
+
|
| 20 |
def __call__(self, question: str) -> str:
|
| 21 |
+
question_lower = question.lower()
|
| 22 |
+
|
| 23 |
+
# 1. Mathefragen erkennen
|
| 24 |
+
if any(word in question_lower for word in ["calculate", "sum", "add", "subtract", "multiply", "divide", "+", "-", "*", "/"]):
|
| 25 |
+
try:
|
| 26 |
+
return self.solve_math(question)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"[Math Error] {e}")
|
| 29 |
+
|
| 30 |
+
# 2. Datumsfragen erkennen
|
| 31 |
+
if "what day" in question_lower or "which day" in question_lower or "day of the week" in question_lower:
|
| 32 |
+
try:
|
| 33 |
+
return self.solve_date(question)
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"[Date Error] {e}")
|
| 36 |
+
|
| 37 |
+
# 3. Fallback
|
| 38 |
+
return "I don't know."
|
| 39 |
+
|
| 40 |
+
def solve_math(self, question: str) -> str:
|
| 41 |
+
print(f"Solving math: {question}")
|
| 42 |
+
expression = self.extract_math_expression(question)
|
| 43 |
+
if not expression:
|
| 44 |
+
raise ValueError("No valid expression found.")
|
| 45 |
+
result = eval(expression)
|
| 46 |
+
return str(result)
|
| 47 |
+
|
| 48 |
+
def extract_math_expression(self, question: str) -> str:
|
| 49 |
+
expression = re.sub(r"[^0-9\.\+\-\*\/\(\)]", "", question)
|
| 50 |
+
return expression
|
| 51 |
+
|
| 52 |
+
def solve_date(self, question: str) -> str:
|
| 53 |
+
print(f"Solving date: {question}")
|
| 54 |
+
parsed_date = date_parser.parse(question, fuzzy=True)
|
| 55 |
+
weekday = calendar.day_name[parsed_date.weekday()]
|
| 56 |
+
return weekday
|
| 57 |
|
| 58 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 59 |
"""
|