Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from smolagents import CodeAgent, LiteLLMModel, VisitWebpageTool, DuckDuckGoSear
|
|
| 10 |
# --- CONSTANTS ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
| 13 |
-
# --- AGENT CLASS (
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
| 16 |
print("🚀 Initializing Mistral-Powered Agent...")
|
|
@@ -18,69 +18,55 @@ class BasicAgent:
|
|
| 18 |
# --- 1. API KEY CHECK ---
|
| 19 |
mistral_key = os.getenv("MISTRAL_API_KEY")
|
| 20 |
if not mistral_key:
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
# --- 2. MODEL SETUP
|
| 24 |
-
# Mistral Large logic aur code ke liye best hai
|
| 25 |
model = LiteLLMModel(
|
| 26 |
model_id="mistral/mistral-large-latest",
|
| 27 |
api_key=mistral_key
|
| 28 |
)
|
| 29 |
|
| 30 |
# --- 3. TOOLS ---
|
| 31 |
-
# Web Search aur Page Visit tools
|
| 32 |
search_tool = DuckDuckGoSearchTool()
|
| 33 |
visit_tool = VisitWebpageTool()
|
| 34 |
|
| 35 |
# --- 4. CREATE AGENT ---
|
| 36 |
-
# CodeAgent use kar rahe hain jo seedha Python likhta hai
|
| 37 |
self.agent = CodeAgent(
|
| 38 |
tools=[search_tool, visit_tool],
|
| 39 |
model=model,
|
| 40 |
-
# GAIA ke liye ye libraries zaroori hain
|
| 41 |
additional_authorized_imports=[
|
| 42 |
"numpy", "pandas", "math", "datetime", "re", "csv", "json", "random", "itertools"
|
| 43 |
],
|
| 44 |
-
max_steps=25,
|
| 45 |
verbosity_level=2,
|
| 46 |
-
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
-
def __call__(self, question: str, file_path:
|
| 50 |
-
|
| 51 |
-
Ye function agent ko run karta hai aur answer clean karta hai.
|
| 52 |
-
"""
|
| 53 |
-
# --- PROMPT ENGINEERING FOR EXACT MATCH ---
|
| 54 |
prompt = f"""
|
| 55 |
-
|
| 56 |
|
| 57 |
INSTRUCTIONS:
|
| 58 |
-
1.
|
| 59 |
-
2. If a file is attached, YOU MUST READ IT using Python
|
| 60 |
-
3.
|
| 61 |
-
|
| 62 |
-
OUTPUT FORMAT RULES:
|
| 63 |
-
- Once you find the answer, output ONLY the final value.
|
| 64 |
-
- Do not write "The answer is...".
|
| 65 |
-
- Do not add units (like $, kg, years) unless specifically asked.
|
| 66 |
-
- Example: If the answer is 42, just print 42.
|
| 67 |
"""
|
| 68 |
|
| 69 |
if file_path:
|
| 70 |
-
prompt += f"\n\n⚠️ ATTACHED FILE:
|
| 71 |
|
| 72 |
try:
|
| 73 |
-
|
| 74 |
-
print(f"🤖 Agent thinking on: {question[:50]}...")
|
| 75 |
response = self.agent.run(prompt)
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
final_answer = str(response)
|
| 79 |
-
|
| 80 |
-
# Common text removal
|
| 81 |
-
final_answer = final_answer.replace("Final Answer:", "").strip()
|
| 82 |
|
| 83 |
-
# Remove trailing dot (e.g., "100." -> "100")
|
| 84 |
if final_answer.endswith(".") and len(final_answer) < 20:
|
| 85 |
final_answer = final_answer[:-1]
|
| 86 |
|
|
|
|
| 10 |
# --- CONSTANTS ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
| 13 |
+
# --- AGENT CLASS (Fixed Name) ---
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
| 16 |
print("🚀 Initializing Mistral-Powered Agent...")
|
|
|
|
| 18 |
# --- 1. API KEY CHECK ---
|
| 19 |
mistral_key = os.getenv("MISTRAL_API_KEY")
|
| 20 |
if not mistral_key:
|
| 21 |
+
# Agar Mistral nahi hai to error mat do, Qwen try karo (Fallback)
|
| 22 |
+
print("⚠️ Mistral Key not found. Please set MISTRAL_API_KEY for best results.")
|
| 23 |
+
# Fallback logic if needed, but for now we raise error to alert user
|
| 24 |
+
raise ValueError("⚠️ MISTRAL_API_KEY missing! Settings -> Secrets me add karo.")
|
| 25 |
|
| 26 |
+
# --- 2. MODEL SETUP ---
|
|
|
|
| 27 |
model = LiteLLMModel(
|
| 28 |
model_id="mistral/mistral-large-latest",
|
| 29 |
api_key=mistral_key
|
| 30 |
)
|
| 31 |
|
| 32 |
# --- 3. TOOLS ---
|
|
|
|
| 33 |
search_tool = DuckDuckGoSearchTool()
|
| 34 |
visit_tool = VisitWebpageTool()
|
| 35 |
|
| 36 |
# --- 4. CREATE AGENT ---
|
|
|
|
| 37 |
self.agent = CodeAgent(
|
| 38 |
tools=[search_tool, visit_tool],
|
| 39 |
model=model,
|
|
|
|
| 40 |
additional_authorized_imports=[
|
| 41 |
"numpy", "pandas", "math", "datetime", "re", "csv", "json", "random", "itertools"
|
| 42 |
],
|
| 43 |
+
max_steps=25,
|
| 44 |
verbosity_level=2,
|
| 45 |
+
# 👇 YAHAN CHANGE KIYA HAI (Hyphen hata ke Underscore lagaya)
|
| 46 |
+
name="Mistral_Gaia_Solver"
|
| 47 |
)
|
| 48 |
|
| 49 |
+
def __call__(self, question: str, file_path: str = None) -> str:
|
| 50 |
+
# Prompt Logic
|
|
|
|
|
|
|
|
|
|
| 51 |
prompt = f"""
|
| 52 |
+
Task: {question}
|
| 53 |
|
| 54 |
INSTRUCTIONS:
|
| 55 |
+
1. Use Python code to solve this step-by-step.
|
| 56 |
+
2. If a file is attached, YOU MUST READ IT using Python immediately.
|
| 57 |
+
3. Output ONLY the final answer value.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
"""
|
| 59 |
|
| 60 |
if file_path:
|
| 61 |
+
prompt += f"\n\n⚠️ ATTACHED FILE: '{file_path}'"
|
| 62 |
|
| 63 |
try:
|
| 64 |
+
print(f"🤖 Agent working on: {question[:30]}...")
|
|
|
|
| 65 |
response = self.agent.run(prompt)
|
| 66 |
|
| 67 |
+
# Output Cleaning
|
| 68 |
+
final_answer = str(response).replace("Final Answer:", "").strip()
|
|
|
|
|
|
|
|
|
|
| 69 |
|
|
|
|
| 70 |
if final_answer.endswith(".") and len(final_answer) < 20:
|
| 71 |
final_answer = final_answer[:-1]
|
| 72 |
|