|
|
import os |
|
|
import gradio as gr |
|
|
import requests |
|
|
import pandas as pd |
|
|
from smolagents import CodeAgent, HfApiModel, VisitWebpageTool |
|
|
|
|
|
|
|
|
token = os.getenv("HF_TOKEN") |
|
|
|
|
|
model = HfApiModel(model_id="Qwen/Qwen2.5-72B-Instruct", token=token) |
|
|
|
|
|
class AlfredAgent: |
|
|
def __init__(self): |
|
|
|
|
|
CUSTOM_SYSTEM_PROMPT = """You are a GAIA expert. Solve the task using Python code. |
|
|
{{managed_agents_descriptions}} |
|
|
{{authorized_imports}} |
|
|
|
|
|
Rules: |
|
|
1. Use web_search or visit_webpage for information. |
|
|
2. If you find a file (audio/video), search for its name on the web to get details. |
|
|
3. The LAST LINE of your code must be the final answer variable. |
|
|
4. Final answer must be short (e.g., '15', 'Paris').""" |
|
|
|
|
|
self.agent = CodeAgent( |
|
|
tools=[VisitWebpageTool()], |
|
|
model=model, |
|
|
max_steps=12, |
|
|
add_base_tools=True, |
|
|
additional_authorized_imports=['requests', 'bs4', 'pandas', 'json', 'math', 're'], |
|
|
system_prompt=CUSTOM_SYSTEM_PROMPT |
|
|
) |
|
|
|
|
|
def __call__(self, question: str) -> str: |
|
|
try: |
|
|
|
|
|
result = self.agent.run(question) |
|
|
ans = str(result).strip() |
|
|
|
|
|
|
|
|
if len(ans) > 50 and "\n" in ans: |
|
|
ans = ans.split('\n')[-1].replace("Final Answer:", "").strip() |
|
|
|
|
|
return ans[:50] |
|
|
except Exception as e: |
|
|
return "Unknown" |
|
|
|
|
|
|
|
|
def run_and_submit_all(profile: gr.OAuthProfile | None): |
|
|
if profile is None: return "Giriş gerekli.", None |
|
|
|
|
|
questions_url = "https://agents-course-unit4-scoring.hf.space/questions" |
|
|
submit_url = "https://agents-course-unit4-scoring.hf.space/submit" |
|
|
|
|
|
agent = AlfredAgent() |
|
|
|
|
|
try: |
|
|
response = requests.get(questions_url, timeout=15) |
|
|
questions_data = response.json() |
|
|
except: return "Sorular alınamadı.", None |
|
|
|
|
|
answers_payload = [] |
|
|
results_log = [] |
|
|
|
|
|
for item in questions_data: |
|
|
task_id = item.get("task_id") |
|
|
answer = agent(item.get("question")) |
|
|
|
|
|
clean_ans = str(answer).split('\n')[-1].replace("Final Answer:", "").strip(" .\"'") |
|
|
|
|
|
answers_payload.append({"task_id": task_id, "submitted_answer": clean_ans[:100]}) |
|
|
results_log.append({"Task ID": task_id, "Submitted Answer": clean_ans[:100]}) |
|
|
|
|
|
submission_data = { |
|
|
"username": profile.username, |
|
|
"agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main", |
|
|
"answers": answers_payload |
|
|
} |
|
|
|
|
|
try: |
|
|
res = requests.post(submit_url, json=submission_data, timeout=60).json() |
|
|
return f"Skor: {res.get('score')}% ({res.get('correct_count')}/20)", pd.DataFrame(results_log) |
|
|
except: return "Gönderim başarısız.", pd.DataFrame(results_log) |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.LoginButton() |
|
|
btn = gr.Button("Sınavı Başlat") |
|
|
out = gr.Textbox(label="Sonuç") |
|
|
tab = gr.DataFrame() |
|
|
btn.click(run_and_submit_all, outputs=[out, tab]) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |