Update app.py
Browse files
app.py
CHANGED
|
@@ -7,56 +7,63 @@ from transformers import pipeline
|
|
| 7 |
# --- Constants ---
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
-
# --- Basic Agent Definition ---
|
| 11 |
class BasicAgent:
|
| 12 |
def __init__(self):
|
| 13 |
-
print("Loading
|
| 14 |
self.pipeline = pipeline(
|
| 15 |
"text2text-generation",
|
| 16 |
-
model="
|
| 17 |
max_new_tokens=128,
|
| 18 |
temperature=0.3
|
| 19 |
)
|
| 20 |
-
print("Model loaded.")
|
| 21 |
|
| 22 |
def __call__(self, question: str, task_id: str = None) -> str:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def model_response(self, question: str) -> str:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def search_wikipedia(self, question: str) -> str:
|
| 39 |
import wikipedia
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
return summary
|
| 43 |
except Exception as e:
|
| 44 |
-
return f"
|
| 45 |
|
| 46 |
def parse_excel(self, task_id: str) -> str:
|
| 47 |
try:
|
| 48 |
file_url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
|
| 49 |
df = pd.read_excel(file_url)
|
| 50 |
-
|
| 51 |
-
food_sales = df[df['category'] == 'food']['sales'].sum()
|
| 52 |
return f"${food_sales:.2f}"
|
| 53 |
except Exception as e:
|
| 54 |
-
return f"Excel
|
| 55 |
|
| 56 |
def reverse_sentence(self, question: str) -> str:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
| 7 |
# --- Constants ---
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
|
|
|
| 10 |
class BasicAgent:
|
| 11 |
def __init__(self):
|
| 12 |
+
print("🔄 Loading model: flan-alpaca-base...")
|
| 13 |
self.pipeline = pipeline(
|
| 14 |
"text2text-generation",
|
| 15 |
+
model="declare-lab/flan-alpaca-base",
|
| 16 |
max_new_tokens=128,
|
| 17 |
temperature=0.3
|
| 18 |
)
|
| 19 |
+
print("✅ Model loaded.")
|
| 20 |
|
| 21 |
def __call__(self, question: str, task_id: str = None) -> str:
|
| 22 |
+
question_lower = question.lower()
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
if "wikipedia" in question_lower or "how many" in question_lower:
|
| 26 |
+
return self.search_wikipedia(question)
|
| 27 |
+
elif "excel" in question_lower or "attached" in question_lower:
|
| 28 |
+
return self.parse_excel(task_id)
|
| 29 |
+
elif "reverse" in question_lower or "write the opposite" in question_lower:
|
| 30 |
+
return self.reverse_sentence(question)
|
| 31 |
+
else:
|
| 32 |
+
return self.model_response(question)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"[Tool error: {e}]"
|
| 35 |
|
| 36 |
def model_response(self, question: str) -> str:
|
| 37 |
+
few_shot = (
|
| 38 |
+
"Question: List just the vegetables from [milk, eggs, carrots, onions, cookies].\n"
|
| 39 |
+
"Answer: carrots, onions\n\n"
|
| 40 |
+
)
|
| 41 |
+
prompt = few_shot + f"Please answer this question step by step:\n{question}"
|
| 42 |
+
result = self.pipeline(prompt)[0]["generated_text"]
|
| 43 |
+
return result.strip().split("Answer:")[-1].strip()
|
| 44 |
|
| 45 |
def search_wikipedia(self, question: str) -> str:
|
| 46 |
import wikipedia
|
| 47 |
try:
|
| 48 |
+
return wikipedia.summary(question, sentences=2)
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
+
return f"Couldn't find info: {e}"
|
| 51 |
|
| 52 |
def parse_excel(self, task_id: str) -> str:
|
| 53 |
try:
|
| 54 |
file_url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
|
| 55 |
df = pd.read_excel(file_url)
|
| 56 |
+
food_sales = df[df["category"].str.lower() == "food"]["sales"].sum()
|
|
|
|
| 57 |
return f"${food_sales:.2f}"
|
| 58 |
except Exception as e:
|
| 59 |
+
return f"Excel error: {e}"
|
| 60 |
|
| 61 |
def reverse_sentence(self, question: str) -> str:
|
| 62 |
+
try:
|
| 63 |
+
sentence = question.split(",")[0].strip()
|
| 64 |
+
return sentence[::-1]
|
| 65 |
+
except:
|
| 66 |
+
return "Could not reverse the sentence."
|
| 67 |
|
| 68 |
|
| 69 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|