Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,42 @@ import os
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
| 5 |
-
from
|
|
|
|
| 6 |
|
| 7 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 8 |
-
MODEL_ID = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def create_agent(token: str):
|
| 11 |
-
|
| 12 |
-
model = LiteLLMModel(model_id=MODEL_ID, api_key=token)
|
| 13 |
search_tool = DuckDuckGoSearchTool()
|
| 14 |
|
| 15 |
agent = CodeAgent(
|
|
@@ -20,7 +48,6 @@ def create_agent(token: str):
|
|
| 20 |
return agent
|
| 21 |
|
| 22 |
def solve_question(agent, question: str) -> str:
|
| 23 |
-
# Автоматический переворот для вопросов-инверсий
|
| 24 |
if question.strip().startswith(".") or "ecnetnes" in question:
|
| 25 |
question = question[::-1]
|
| 26 |
|
|
@@ -39,7 +66,7 @@ def solve_question(agent, question: str) -> str:
|
|
| 39 |
ans = ans.split("\n")[-1].strip()
|
| 40 |
return ans.strip(" .\"'") if ans else "0"
|
| 41 |
except Exception as e:
|
| 42 |
-
print(f"Agent error
|
| 43 |
return "0"
|
| 44 |
|
| 45 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
@@ -60,7 +87,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 60 |
|
| 61 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "https://huggingface.co"
|
| 62 |
|
| 63 |
-
# Fetch Questions
|
| 64 |
try:
|
| 65 |
res = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
|
| 66 |
res.raise_for_status()
|
|
@@ -83,7 +109,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 83 |
answers_payload.append({"task_id": task_id, "submitted_answer": ans})
|
| 84 |
results_log.append({"Task ID": task_id, "Question": q, "Submitted Answer": ans})
|
| 85 |
|
| 86 |
-
# Submit
|
| 87 |
try:
|
| 88 |
sub_res = requests.post(
|
| 89 |
f"{DEFAULT_API_URL}/submit",
|
|
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
| 5 |
+
from huggingface_hub import InferenceClient
|
| 6 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
| 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
|
| 14 |
+
self.client = InferenceClient(model=model_id, token=token)
|
| 15 |
+
|
| 16 |
+
def __call__(self, messages, **kwargs):
|
| 17 |
+
try:
|
| 18 |
+
formatted_messages = []
|
| 19 |
+
for m in messages:
|
| 20 |
+
if isinstance(m, dict):
|
| 21 |
+
formatted_messages.append(m)
|
| 22 |
+
else:
|
| 23 |
+
role = getattr(m, "role", "user")
|
| 24 |
+
content = getattr(m, "content", str(m))
|
| 25 |
+
formatted_messages.append({"role": role, "content": str(content)})
|
| 26 |
+
|
| 27 |
+
response = self.client.chat_completion(
|
| 28 |
+
messages=formatted_messages,
|
| 29 |
+
max_tokens=200,
|
| 30 |
+
temperature=0.0
|
| 31 |
+
)
|
| 32 |
+
return response.choices[0].message
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"Model error: {e}")
|
| 35 |
+
class Choice:
|
| 36 |
+
content = "0"
|
| 37 |
+
return Choice()
|
| 38 |
|
| 39 |
def create_agent(token: str):
|
| 40 |
+
model = CustomHFModel(model_id=MODEL_ID, token=token)
|
|
|
|
| 41 |
search_tool = DuckDuckGoSearchTool()
|
| 42 |
|
| 43 |
agent = CodeAgent(
|
|
|
|
| 48 |
return agent
|
| 49 |
|
| 50 |
def solve_question(agent, question: str) -> str:
|
|
|
|
| 51 |
if question.strip().startswith(".") or "ecnetnes" in question:
|
| 52 |
question = question[::-1]
|
| 53 |
|
|
|
|
| 66 |
ans = ans.split("\n")[-1].strip()
|
| 67 |
return ans.strip(" .\"'") if ans else "0"
|
| 68 |
except Exception as e:
|
| 69 |
+
print(f"Agent error: {e}")
|
| 70 |
return "0"
|
| 71 |
|
| 72 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
| 87 |
|
| 88 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "https://huggingface.co"
|
| 89 |
|
|
|
|
| 90 |
try:
|
| 91 |
res = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
|
| 92 |
res.raise_for_status()
|
|
|
|
| 109 |
answers_payload.append({"task_id": task_id, "submitted_answer": ans})
|
| 110 |
results_log.append({"Task ID": task_id, "Question": q, "Submitted Answer": ans})
|
| 111 |
|
|
|
|
| 112 |
try:
|
| 113 |
sub_res = requests.post(
|
| 114 |
f"{DEFAULT_API_URL}/submit",
|