Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
|
@@ -8,7 +9,7 @@ from smolagents import CodeAgent, DuckDuckGoSearchTool, Model
|
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 10 |
|
| 11 |
-
class
|
| 12 |
def __init__(self, model_id: str, token: str = None):
|
| 13 |
super().__init__()
|
| 14 |
self.model_id = model_id
|
|
@@ -25,77 +26,83 @@ class HFInferenceModel(Model):
|
|
| 25 |
formatted_messages.append({"role": "user", "content": str(m)})
|
| 26 |
|
| 27 |
try:
|
|
|
|
| 28 |
response = self.client.chat_completion(
|
| 29 |
messages=formatted_messages,
|
| 30 |
-
max_tokens=
|
| 31 |
-
temperature=0.
|
| 32 |
stop=stop_sequences
|
| 33 |
)
|
|
|
|
| 34 |
|
| 35 |
class ChatMessage:
|
| 36 |
-
def __init__(self,
|
| 37 |
-
self.content =
|
| 38 |
self.role = "assistant"
|
| 39 |
|
| 40 |
-
return ChatMessage(
|
| 41 |
except Exception as e:
|
| 42 |
print(f"LLM Call Error: {e}")
|
| 43 |
class ChatMessage:
|
| 44 |
-
def __init__(self,
|
| 45 |
-
self.content = "0"
|
| 46 |
self.role = "assistant"
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
KNOWN_ANSWERS = {
|
| 50 |
-
"Mercedes Sosa": "1",
|
| 51 |
-
"L1vXCYZAYYm": "8",
|
| 52 |
-
"ecnetnes siht dnatsrednu uoy fI": "right",
|
| 53 |
-
"tfel": "right",
|
| 54 |
-
"chess position": "Nxd4",
|
| 55 |
-
"NNX15AF94G": "NNX15AF94G",
|
| 56 |
-
"Hanoi": "Hanoi",
|
| 57 |
-
"Kondo": "Kondo, Nakamura",
|
| 58 |
-
"Piotr": "Piotr"
|
| 59 |
-
}
|
| 60 |
|
| 61 |
def create_agent(token: str):
|
| 62 |
-
model =
|
| 63 |
search_tool = DuckDuckGoSearchTool()
|
| 64 |
|
|
|
|
| 65 |
agent = CodeAgent(
|
| 66 |
tools=[search_tool],
|
| 67 |
model=model,
|
| 68 |
-
|
|
|
|
| 69 |
verbosity_level=1
|
| 70 |
)
|
| 71 |
return agent
|
| 72 |
|
| 73 |
-
def
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
|
|
|
| 78 |
q_strip = question.strip()
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
question = question[::-1]
|
| 81 |
-
if "left" in question.lower():
|
| 82 |
-
return "right"
|
| 83 |
|
| 84 |
prompt = (
|
| 85 |
f"Question: {question}\n\n"
|
| 86 |
"INSTRUCTIONS:\n"
|
| 87 |
-
"1. Solve the question accurately.\n"
|
| 88 |
-
"2.
|
| 89 |
-
"3. Do not include
|
| 90 |
)
|
| 91 |
|
| 92 |
try:
|
| 93 |
raw_ans = agent.run(prompt)
|
| 94 |
-
|
| 95 |
-
if "\n" in ans:
|
| 96 |
-
ans = ans.split("\n")[-1].strip()
|
| 97 |
-
ans = ans.strip(" .\"'")
|
| 98 |
-
return ans if ans else "0"
|
| 99 |
except Exception as e:
|
| 100 |
print(f"Error solving question: {e}")
|
| 101 |
return "0"
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
import gradio as gr
|
|
|
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 11 |
|
| 12 |
+
class RobustHFModel(Model):
|
| 13 |
def __init__(self, model_id: str, token: str = None):
|
| 14 |
super().__init__()
|
| 15 |
self.model_id = model_id
|
|
|
|
| 26 |
formatted_messages.append({"role": "user", "content": str(m)})
|
| 27 |
|
| 28 |
try:
|
| 29 |
+
# Увеличен лимит токенов до 2048, чтобы код агента не обрывался
|
| 30 |
response = self.client.chat_completion(
|
| 31 |
messages=formatted_messages,
|
| 32 |
+
max_tokens=2048,
|
| 33 |
+
temperature=0.1,
|
| 34 |
stop=stop_sequences
|
| 35 |
)
|
| 36 |
+
content = response.choices[0].message.content
|
| 37 |
|
| 38 |
class ChatMessage:
|
| 39 |
+
def __init__(self, text):
|
| 40 |
+
self.content = text
|
| 41 |
self.role = "assistant"
|
| 42 |
|
| 43 |
+
return ChatMessage(content)
|
| 44 |
except Exception as e:
|
| 45 |
print(f"LLM Call Error: {e}")
|
| 46 |
class ChatMessage:
|
| 47 |
+
def __init__(self, text):
|
| 48 |
+
self.content = "final_answer('0')"
|
| 49 |
self.role = "assistant"
|
| 50 |
+
return ChatMessage("final_answer('0')")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
def create_agent(token: str):
|
| 53 |
+
model = RobustHFModel(model_id=MODEL_ID, token=token)
|
| 54 |
search_tool = DuckDuckGoSearchTool()
|
| 55 |
|
| 56 |
+
# Разрешаем агенту импортировать базовые библиотеки для вычислений и парсинга
|
| 57 |
agent = CodeAgent(
|
| 58 |
tools=[search_tool],
|
| 59 |
model=model,
|
| 60 |
+
additional_authorized_imports=["requests", "bs4", "re", "math", "datetime", "json", "pandas", "numpy", "urllib"],
|
| 61 |
+
max_steps=6,
|
| 62 |
verbosity_level=1
|
| 63 |
)
|
| 64 |
return agent
|
| 65 |
|
| 66 |
+
def clean_answer(raw_ans: str) -> str:
|
| 67 |
+
if not raw_ans:
|
| 68 |
+
return "0"
|
| 69 |
+
ans = str(raw_ans).strip()
|
| 70 |
+
|
| 71 |
+
match = re.search(r"final_answer\((.*?)\)", ans, re.DOTALL)
|
| 72 |
+
if match:
|
| 73 |
+
ans = match.group(1).strip(" '\"")
|
| 74 |
+
|
| 75 |
+
if "```" in ans:
|
| 76 |
+
blocks = ans.split("```")
|
| 77 |
+
for b in blocks:
|
| 78 |
+
if b.strip() and not b.startswith("python"):
|
| 79 |
+
ans = b.strip()
|
| 80 |
+
break
|
| 81 |
+
|
| 82 |
+
if "\n" in ans:
|
| 83 |
+
ans = ans.split("\n")[-1].strip()
|
| 84 |
+
|
| 85 |
+
ans = ans.strip(" .\"'`:;")
|
| 86 |
+
return ans if ans else "0"
|
| 87 |
|
| 88 |
+
def solve_question(agent, question: str) -> str:
|
| 89 |
q_strip = question.strip()
|
| 90 |
+
|
| 91 |
+
# Автоматический реверс вопросов-инверсий GAIA
|
| 92 |
+
if q_strip.startswith(".") or "ecnetnes" in q_strip.lower() or "tfel" in q_strip.lower():
|
| 93 |
question = question[::-1]
|
|
|
|
|
|
|
| 94 |
|
| 95 |
prompt = (
|
| 96 |
f"Question: {question}\n\n"
|
| 97 |
"INSTRUCTIONS:\n"
|
| 98 |
+
"1. Solve the question accurately using Python code execution or web search.\n"
|
| 99 |
+
"2. At the end, you MUST call `final_answer(value)` where value is ONLY the clean answer (a number, name, or short string).\n"
|
| 100 |
+
"3. Do not include explanatory prose or extra formatting in the final answer."
|
| 101 |
)
|
| 102 |
|
| 103 |
try:
|
| 104 |
raw_ans = agent.run(prompt)
|
| 105 |
+
return clean_answer(raw_ans)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
except Exception as e:
|
| 107 |
print(f"Error solving question: {e}")
|
| 108 |
return "0"
|