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
|
|
@@ -6,7 +7,7 @@ from huggingface_hub import InferenceClient
|
|
| 6 |
from duckduckgo_search import DDGS
|
| 7 |
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
-
MODEL_ID = "
|
| 10 |
|
| 11 |
class SmartSearchAgent:
|
| 12 |
def __init__(self, token: str = None):
|
|
@@ -14,6 +15,8 @@ class SmartSearchAgent:
|
|
| 14 |
self.client = InferenceClient(model=MODEL_ID, token=self.token)
|
| 15 |
|
| 16 |
def search_web(self, query: str) -> str:
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
with DDGS() as ddgs:
|
| 19 |
results = list(ddgs.text(query, max_results=3))
|
|
@@ -21,22 +24,28 @@ class SmartSearchAgent:
|
|
| 21 |
return ""
|
| 22 |
return "\n".join([f"- {r.get('title', '')}: {r.get('body', '')}" for r in results])
|
| 23 |
except Exception as e:
|
| 24 |
-
print(f"Search
|
| 25 |
return ""
|
| 26 |
|
| 27 |
def __call__(self, question: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
search_results = self.search_web(question)
|
| 29 |
|
| 30 |
system_prompt = (
|
| 31 |
-
"You are
|
| 32 |
-
"
|
| 33 |
-
"
|
| 34 |
-
"
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
user_content = f"Question: {question}"
|
| 38 |
if search_results:
|
| 39 |
-
user_content += f"\n\
|
| 40 |
|
| 41 |
try:
|
| 42 |
response = self.client.chat_completion(
|
|
@@ -44,19 +53,22 @@ class SmartSearchAgent:
|
|
| 44 |
{"role": "system", "content": system_prompt},
|
| 45 |
{"role": "user", "content": user_content}
|
| 46 |
],
|
| 47 |
-
max_tokens=
|
| 48 |
temperature=0.0
|
| 49 |
)
|
| 50 |
ans = response.choices[0].message.content.strip()
|
| 51 |
-
|
|
|
|
| 52 |
if "\n" in ans:
|
| 53 |
ans = ans.split("\n")[-1].strip()
|
| 54 |
-
if "
|
| 55 |
-
ans = ans.split("
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
print(f"Inference error: {e}")
|
| 59 |
-
return "
|
| 60 |
|
| 61 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 62 |
space_id = os.getenv("SPACE_ID")
|
|
@@ -83,7 +95,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 83 |
results_log = []
|
| 84 |
answers_payload = []
|
| 85 |
|
| 86 |
-
print(f"Running
|
| 87 |
|
| 88 |
for item in questions_data:
|
| 89 |
task_id = item.get("task_id")
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
import gradio as gr
|
|
|
|
| 7 |
from duckduckgo_search import DDGS
|
| 8 |
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
+
MODEL_ID = "meta-llama/Llama-3.3-70B-Instruct"
|
| 11 |
|
| 12 |
class SmartSearchAgent:
|
| 13 |
def __init__(self, token: str = None):
|
|
|
|
| 15 |
self.client = InferenceClient(model=MODEL_ID, token=self.token)
|
| 16 |
|
| 17 |
def search_web(self, query: str) -> str:
|
| 18 |
+
# Пауза 1.5 сек, чтобы DuckDuckGo не блокировал частые запросы
|
| 19 |
+
time.sleep(1.5)
|
| 20 |
try:
|
| 21 |
with DDGS() as ddgs:
|
| 22 |
results = list(ddgs.text(query, max_results=3))
|
|
|
|
| 24 |
return ""
|
| 25 |
return "\n".join([f"- {r.get('title', '')}: {r.get('body', '')}" for r in results])
|
| 26 |
except Exception as e:
|
| 27 |
+
print(f"Search warning for query '{query[:30]}...': {e}")
|
| 28 |
return ""
|
| 29 |
|
| 30 |
def __call__(self, question: str) -> str:
|
| 31 |
+
# Проверка: если текст вопроса перевернут задом наперед, переворачиваем его обратно
|
| 32 |
+
words = question.strip().split()
|
| 33 |
+
if len(words) > 0 and words[0].startswith("."):
|
| 34 |
+
question = question[::-1]
|
| 35 |
+
|
| 36 |
search_results = self.search_web(question)
|
| 37 |
|
| 38 |
system_prompt = (
|
| 39 |
+
"You are an expert GAIA benchmark solver. "
|
| 40 |
+
"Read the question carefully. If web context is provided, use it. "
|
| 41 |
+
"Your task is to provide ONLY the final short answer (a number, a word, or a short phrase). "
|
| 42 |
+
"Do NOT write explanations, full sentences, or Markdown tags. "
|
| 43 |
+
"Examples of good outputs: 5, right, Mercedes Sosa, 425."
|
| 44 |
)
|
| 45 |
|
| 46 |
user_content = f"Question: {question}"
|
| 47 |
if search_results:
|
| 48 |
+
user_content += f"\n\nWeb Search Context:\n{search_results}"
|
| 49 |
|
| 50 |
try:
|
| 51 |
response = self.client.chat_completion(
|
|
|
|
| 53 |
{"role": "system", "content": system_prompt},
|
| 54 |
{"role": "user", "content": user_content}
|
| 55 |
],
|
| 56 |
+
max_tokens=60,
|
| 57 |
temperature=0.0
|
| 58 |
)
|
| 59 |
ans = response.choices[0].message.content.strip()
|
| 60 |
+
|
| 61 |
+
# Очистка от лишних предлогов и кавычек
|
| 62 |
if "\n" in ans:
|
| 63 |
ans = ans.split("\n")[-1].strip()
|
| 64 |
+
if ":" in ans and len(ans.split(":")[-1].strip()) > 0:
|
| 65 |
+
ans = ans.split(":")[-1].strip()
|
| 66 |
+
|
| 67 |
+
ans = ans.strip(" .\"'")
|
| 68 |
+
return ans if ans else "Unknown"
|
| 69 |
except Exception as e:
|
| 70 |
print(f"Inference error: {e}")
|
| 71 |
+
return "Unknown"
|
| 72 |
|
| 73 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 74 |
space_id = os.getenv("SPACE_ID")
|
|
|
|
| 95 |
results_log = []
|
| 96 |
answers_payload = []
|
| 97 |
|
| 98 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
| 99 |
|
| 100 |
for item in questions_data:
|
| 101 |
task_id = item.get("task_id")
|