File size: 3,507 Bytes
10e9b7d
 
eccf8e4
3c4371f
30c5055
 
51cceff
30c5055
5fa53b8
1100086
30c5055
07c43b9
31243f4
5fa53b8
 
07d2845
68877f8
6a4f6b4
5fa53b8
 
 
 
 
0519fb9
6a3b6b0
6b00115
6a3b6b0
5fa53b8
07d2845
5fa53b8
7946e03
07c43b9
 
4ed0eb8
07c43b9
5fa53b8
51cceff
5fa53b8
 
 
 
 
 
 
af175c6
b0c1e59
4021bf3
51cceff
30c5055
51cceff
 
 
 
 
 
30c5055
eccf8e4
31243f4
 
51cceff
e80aab9
7d65c66
51cceff
b0c1e59
31243f4
 
51cceff
 
 
af175c6
51cceff
 
af175c6
51cceff
 
 
 
 
e80aab9
51cceff
 
 
 
 
 
 
 
 
 
 
 
e80aab9
 
51cceff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import gradio as gr
import requests
import pandas as pd
from smolagents import CodeAgent, HfApiModel, VisitWebpageTool

# Model ve Token Ayarları
token = os.getenv("HF_TOKEN")
# 1. Modeli Qwen olarak değiştiriyoruz (Kota engeline takılmaz)
model = HfApiModel(model_id="Qwen/Qwen2.5-72B-Instruct", token=token)

class AlfredAgent:
    def __init__(self):
        # En basit, kütüphanenin en sevdiği prompt yapısı
        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, # Adım sayısını 12'de tutalım ki model yorulmasın
            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:
            # Modelin doğal akışını bozmadan çalıştır
            result = self.agent.run(question)
            ans = str(result).strip()
            
            # Sadece basit bir temizlik (Eğer çok uzunsa son satırı al)
            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"

# --- Gönderim ve Arayüz Fonksiyonu ---
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"))
        # Temiz ve kısa cevap formatı
        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)

# Gradio Arayüzü
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()