Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +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 ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
|
@@ -14,37 +15,33 @@ class AwesomeAgent:
|
|
| 14 |
|
| 15 |
|
| 16 |
def __init__(self):
|
| 17 |
-
|
| 18 |
-
self.
|
| 19 |
-
self.headers = {
|
| 20 |
-
"Authorization": f"Bearer {self.api_token}",
|
| 21 |
-
"Content-Type": "application/json"
|
| 22 |
-
}
|
| 23 |
-
print("AwesomeAgent initialized.")
|
| 24 |
|
|
|
|
|
|
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def __call__(self, question: str) -> str:
|
| 27 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
"parameters": {
|
| 32 |
-
"max_new_tokens": 256,
|
| 33 |
-
"temperature": 0.3,
|
| 34 |
-
"top_p": 0.9,
|
| 35 |
-
"do_sample": True
|
| 36 |
-
}
|
| 37 |
-
}
|
| 38 |
-
try:
|
| 39 |
-
response = requests.post(self.api_url, headers=self.headers, json=payload, timeout=30)
|
| 40 |
-
response.raise_for_status()
|
| 41 |
-
result = response.json()
|
| 42 |
-
answer = result[0]["generated_text"].strip()
|
| 43 |
-
print(f"Agent returning answer: {answer}")
|
| 44 |
-
return answer
|
| 45 |
-
except Exception as e:
|
| 46 |
-
print(f"API error: {e}")
|
| 47 |
-
return "Sorry, the agent failed to generate a response."
|
| 48 |
|
| 49 |
|
| 50 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
| 7 |
|
| 8 |
+
# (Keep Constants and BasicAgent class as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
def __init__(self):
|
| 18 |
+
print("BasicAgent initialized.")
|
| 19 |
+
self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
SYSTEM_PROMPT = """
|
| 22 |
+
You are a precise, domain-agnostic assistant. For every query, perform internal reasoning silently and respond only with a conclusive output using this format:
|
| 23 |
|
| 24 |
+
FINAL: [response]
|
| 25 |
+
|
| 26 |
+
Guidelines:
|
| 27 |
+
- If numerical: no commas, no units (unless explicitly requested)
|
| 28 |
+
- If textual: no articles or abbreviations; use full entity names
|
| 29 |
+
- If list: apply above rules per item
|
| 30 |
+
|
| 31 |
+
Examples:
|
| 32 |
+
Q: What is the capital of France?
|
| 33 |
+
A: FINAL: Paris
|
| 34 |
+
|
| 35 |
+
Q: What is 2+2 equal to?
|
| 36 |
+
A: 4
|
| 37 |
+
"""
|
| 38 |
+
self.agent.prompt_templates["system_prompt"] = self.agent.prompt_templates["system_prompt"] + SYSTEM_PROMPT
|
| 39 |
+
|
| 40 |
def __call__(self, question: str) -> str:
|
| 41 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 42 |
+
final_answer = self.agent.run(question)
|
| 43 |
+
print(f"Agent returning final answer: {final_answer}")
|
| 44 |
+
return final_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
|
| 47 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|