Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,28 @@ import requests
|
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
-
from smolagents import CodeAgent,
|
|
|
|
| 7 |
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
class CustomHFModel:
|
| 12 |
def __init__(self, model_id: str, token: str):
|
| 13 |
self.model_id = model_id
|
|
@@ -26,7 +43,7 @@ class CustomHFModel:
|
|
| 26 |
|
| 27 |
response = self.client.chat_completion(
|
| 28 |
messages=formatted_messages,
|
| 29 |
-
max_tokens=
|
| 30 |
temperature=0.0
|
| 31 |
)
|
| 32 |
return response.choices[0].message
|
|
@@ -38,10 +55,9 @@ class CustomHFModel:
|
|
| 38 |
|
| 39 |
def create_agent(token: str):
|
| 40 |
model = CustomHFModel(model_id=MODEL_ID, token=token)
|
| 41 |
-
search_tool = DuckDuckGoSearchTool()
|
| 42 |
|
| 43 |
agent = CodeAgent(
|
| 44 |
-
tools=[
|
| 45 |
model=model,
|
| 46 |
max_steps=3
|
| 47 |
)
|
|
@@ -54,7 +70,7 @@ def solve_question(agent, question: str) -> str:
|
|
| 54 |
prompt = (
|
| 55 |
f"Question: {question}\n\n"
|
| 56 |
"Instructions:\n"
|
| 57 |
-
"1. Use the
|
| 58 |
"2. Return ONLY the concise final answer (e.g., a single number, name, or short string).\n"
|
| 59 |
"3. Do not include extra sentences, explanation, or quotes."
|
| 60 |
)
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
+
from smolagents import CodeAgent, tool
|
| 7 |
+
from duckduckgo_search import DDGS
|
| 8 |
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 11 |
|
| 12 |
+
@tool
|
| 13 |
+
def web_search(query: str) -> str:
|
| 14 |
+
"""Performs a web search to find current factual information.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
query: The search query string.
|
| 18 |
+
"""
|
| 19 |
+
try:
|
| 20 |
+
with DDGS() as ddgs:
|
| 21 |
+
results = list(ddgs.text(query, max_results=3))
|
| 22 |
+
if not results:
|
| 23 |
+
return "No results found."
|
| 24 |
+
return "\n".join([f"- {r.get('title', '')}: {r.get('body', '')}" for r in results])
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Search error: {e}"
|
| 27 |
+
|
| 28 |
class CustomHFModel:
|
| 29 |
def __init__(self, model_id: str, token: str):
|
| 30 |
self.model_id = model_id
|
|
|
|
| 43 |
|
| 44 |
response = self.client.chat_completion(
|
| 45 |
messages=formatted_messages,
|
| 46 |
+
max_tokens=300,
|
| 47 |
temperature=0.0
|
| 48 |
)
|
| 49 |
return response.choices[0].message
|
|
|
|
| 55 |
|
| 56 |
def create_agent(token: str):
|
| 57 |
model = CustomHFModel(model_id=MODEL_ID, token=token)
|
|
|
|
| 58 |
|
| 59 |
agent = CodeAgent(
|
| 60 |
+
tools=[web_search],
|
| 61 |
model=model,
|
| 62 |
max_steps=3
|
| 63 |
)
|
|
|
|
| 70 |
prompt = (
|
| 71 |
f"Question: {question}\n\n"
|
| 72 |
"Instructions:\n"
|
| 73 |
+
"1. Use the `web_search` tool if you need external knowledge or facts.\n"
|
| 74 |
"2. Return ONLY the concise final answer (e.g., a single number, name, or short string).\n"
|
| 75 |
"3. Do not include extra sentences, explanation, or quotes."
|
| 76 |
)
|