Spaces:
Sleeping
Sleeping
Qscar KIM commited on
Commit Β·
ad4f10e
1
Parent(s): 913b19a
update codes
Browse files
app.py
CHANGED
|
@@ -4,54 +4,67 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
import time
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool
|
| 10 |
|
| 11 |
-
# ---
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# --- Basic Agent Definition ---
|
| 15 |
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
| 16 |
class BasicAgent:
|
| 17 |
def __init__(self):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# model_idλ₯Ό λΉμλλ©΄ smolagents νλ μμν¬κ° μ½μ€ μ€μ΅μ©μΌλ‘ 보μ₯νλ κ°μ₯ μμ μ μΈ κΈ°λ³Έ λͺ¨λΈμ μλμΌλ‘ λ§€νν©λλ€.
|
| 24 |
-
self.model = InferenceClientModel(token=hf_token)
|
| 25 |
self.search_tool = DuckDuckGoSearchTool()
|
| 26 |
-
|
| 27 |
-
# κ°μ λ³Έμ°μ λͺ©μ μΈ ReAct(Thought -> Action) 루νλ₯Ό μννλ μμ¨ν μ½λ μμ΄μ νΈ μ‘°λ¦½
|
| 28 |
self.agent = CodeAgent(
|
| 29 |
tools=[self.search_tool],
|
| 30 |
model=self.model,
|
| 31 |
max_steps=5,
|
| 32 |
-
|
| 33 |
)
|
| 34 |
-
print("BasicAgent: smolagents κ·κ²©μ μ€μνμ¬ μμ¨ν μμ΄μ νΈ μ΄κΈ°νλ₯Ό μλ£νμ΅λλ€.")
|
| 35 |
|
| 36 |
def __call__(self, question: str) -> str:
|
| 37 |
-
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 38 |
-
|
| 39 |
try:
|
| 40 |
refined_prompt = (
|
| 41 |
f"{question}\n\n"
|
| 42 |
f"Solve this task step by step using your tools. "
|
| 43 |
f"Provide the final short answer clearly at the very end."
|
| 44 |
)
|
| 45 |
-
# μμ¨ μΆλ‘ λ° μ€ν 루ν κ°λ
|
| 46 |
result = self.agent.run(refined_prompt)
|
| 47 |
if result is None:
|
| 48 |
return "unknown"
|
| 49 |
-
|
| 50 |
return str(result).strip()
|
| 51 |
-
|
| 52 |
except Exception as e:
|
| 53 |
-
# π΄ λ¨Ήν΅ νμ λΆμμ μν΄ unknown λμ μ€μ ν°μ§ μλ¬ μμΈμ ν
μ΄λΈμ λ
ΈμΆμν΅λλ€.
|
| 54 |
-
print(f"β μμ΄μ νΈ μ€ν μ€ν¨ λ‘κ·Έ: {e}")
|
| 55 |
return f"Error: {type(e).__name__}"
|
| 56 |
|
| 57 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
@@ -119,8 +132,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 119 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 120 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 121 |
|
| 122 |
-
|
| 123 |
-
time.sleep(3)
|
| 124 |
except Exception as e:
|
| 125 |
print(f"Error running agent on task {task_id}: {e}")
|
| 126 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
import time
|
| 7 |
+
import re
|
| 8 |
|
| 9 |
+
from smolagents import CodeAgent, InferenceClientModel, Tool
|
|
|
|
| 10 |
|
| 11 |
+
# --- Custom Tool Definition ---
|
| 12 |
+
class DuckDuckGoSearchTool(Tool):
|
| 13 |
+
name = "web_search"
|
| 14 |
+
description = "Searches the web for a given query and returns snippet results."
|
| 15 |
+
inputs = {"query": {"type": "string", "description": "The search query"}}
|
| 16 |
+
output_type = "string"
|
| 17 |
+
|
| 18 |
+
def forward(self, query: str) -> str:
|
| 19 |
+
try:
|
| 20 |
+
url = f"https://html.duckduckgo.com/html/?q={requests.utils.quote(query)}"
|
| 21 |
+
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
|
| 22 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 23 |
+
if response.status_code != 200:
|
| 24 |
+
return f"Search failed with status code {response.status_code}"
|
| 25 |
+
|
| 26 |
+
html = response.text
|
| 27 |
+
snippets = re.findall(r'<a class="result__snippet"[^>]*>(.*?)</a>', html, re.DOTALL)
|
| 28 |
+
cleaned_snippets = []
|
| 29 |
+
for snip in snippets[:5]:
|
| 30 |
+
clean = re.sub(r'<[^>]+>', '', snip)
|
| 31 |
+
clean = clean.replace('\n', ' ').strip()
|
| 32 |
+
cleaned_snippets.append(clean)
|
| 33 |
+
|
| 34 |
+
if cleaned_snippets:
|
| 35 |
+
return "\n".join(f"- {s}" for s in cleaned_snippets)
|
| 36 |
+
return "No results found."
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Search error: {str(e)}"
|
| 39 |
|
| 40 |
# --- Basic Agent Definition ---
|
| 41 |
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
| 42 |
class BasicAgent:
|
| 43 |
def __init__(self):
|
| 44 |
+
self.model = InferenceClientModel(
|
| 45 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 46 |
+
token=os.getenv("HF_TOKEN")
|
| 47 |
+
)
|
|
|
|
|
|
|
|
|
|
| 48 |
self.search_tool = DuckDuckGoSearchTool()
|
|
|
|
|
|
|
| 49 |
self.agent = CodeAgent(
|
| 50 |
tools=[self.search_tool],
|
| 51 |
model=self.model,
|
| 52 |
max_steps=5,
|
| 53 |
+
additional_authorized_imports=["pandas", "numpy", "json", "math", "re", "datetime"]
|
| 54 |
)
|
|
|
|
| 55 |
|
| 56 |
def __call__(self, question: str) -> str:
|
|
|
|
|
|
|
| 57 |
try:
|
| 58 |
refined_prompt = (
|
| 59 |
f"{question}\n\n"
|
| 60 |
f"Solve this task step by step using your tools. "
|
| 61 |
f"Provide the final short answer clearly at the very end."
|
| 62 |
)
|
|
|
|
| 63 |
result = self.agent.run(refined_prompt)
|
| 64 |
if result is None:
|
| 65 |
return "unknown"
|
|
|
|
| 66 |
return str(result).strip()
|
|
|
|
| 67 |
except Exception as e:
|
|
|
|
|
|
|
| 68 |
return f"Error: {type(e).__name__}"
|
| 69 |
|
| 70 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
|
| 132 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 133 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 134 |
|
| 135 |
+
time.sleep(2)
|
|
|
|
| 136 |
except Exception as e:
|
| 137 |
print(f"Error running agent on task {task_id}: {e}")
|
| 138 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|