File size: 9,431 Bytes
10e9b7d
 
eccf8e4
3c4371f
13def30
 
10e9b7d
13def30
e80aab9
13def30
 
 
31243f4
13def30
 
 
 
 
 
 
 
 
 
 
 
31243f4
13def30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c4371f
 
13def30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e80aab9
3c4371f
13def30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eccf8e4
13def30
 
7d65c66
13def30
 
7d65c66
13def30
 
 
 
 
 
 
 
31243f4
13def30
 
 
31243f4
13def30
 
 
 
31243f4
13def30
 
 
 
31243f4
13def30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e80aab9
13def30
e80aab9
13def30
 
 
 
 
 
 
 
e80aab9
13def30
 
 
 
 
 
 
 
 
e80aab9
 
13def30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31243f4
e80aab9
 
 
13def30
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import os
import gradio as gr
import requests
import pandas as pd
import google.generativeai as genai
from typing import Optional

API_URL = "https://agents-course-unit4-scoring.hf.space"

class ImprovedGAIAgent:
    """Agente melhorado para o GAIA com Chain of Thought."""
    
    def __init__(self):
        print("🚀 Inicializando agente melhorado...")
        
        # Configura Gemini
        api_key = os.getenv("GOOGLE_API_KEY")
        if api_key:
            genai.configure(api_key=api_key)
            self.model = genai.GenerativeModel("gemini-3.1-flash-lite")
            print("✅ Gemini configurado")
        else:
            self.model = None
            print("⚠️ GOOGLE_API_KEY não encontrada")
    
    def __call__(self, question: str) -> str:
        """Responde à pergunta com Chain of Thought."""
        
        # Se não tiver modelo, retorna N/A
        if not self.model:
            return "N/A"
        
        # Prompt melhorado com instruções claras
        prompt = f"""
        You are an AI assistant solving GAIA benchmark questions.
        
        Question: {question}
        
        Instructions:
        1. THINK STEP BY STEP about the problem.
        2. Show your reasoning briefly.
        3. END with the final answer in the format: FINAL ANSWER: [answer]
        
        Important rules:
        - If it's a number, just output the number
        - If it's a string, output just the string
        - If it's a date, output in YYYY-MM-DD format
        - No markdown, no extra text after FINAL ANSWER
        
        Let me solve this step by step:
        """
        
        try:
            response = self.model.generate_content(prompt)
            text = response.text.strip()
            
            # Extrai a resposta final
            if "FINAL ANSWER:" in text:
                answer = text.split("FINAL ANSWER:")[-1].strip()
            else:
                # Fallback: pega a última linha
                lines = [l.strip() for l in text.split('\n') if l.strip()]
                answer = lines[-1] if lines else text
            
            # Remove markdown e aspas extras
            answer = answer.strip('"').strip("'").strip()
            answer = answer.replace("```", "").strip()
            
            print(f"✅ Resposta: {answer}")
            return answer if answer else "N/A"
            
        except Exception as e:
            print(f"❌ Erro no modelo: {e}")
            return "N/A"


class SearchEnhancedAgent:
    """Agente com ferramenta de busca (se disponível)."""
    
    def __init__(self):
        print("🚀 Inicializando agente com busca...")
        
        # Configura Gemini
        api_key = os.getenv("GOOGLE_API_KEY")
        if api_key:
            genai.configure(api_key=api_key)
            self.model = genai.GenerativeModel("gemini-3.1-flash-lite")
            print("✅ Gemini configurado")
        else:
            self.model = None
            print("⚠️ GOOGLE_API_KEY não encontrada")
        
        # Tenta importar ferramentas de busca
        self.search_tool = None
        try:
            from duckduckgo_search import DDGS
            self.search_tool = DDGS()
            print("✅ DuckDuckGo configurado")
        except ImportError:
            print("⚠️ DuckDuckGo não disponível")
    
    def search(self, query: str) -> str:
        """Faz busca na web."""
        if not self.search_tool:
            return ""
        try:
            results = self.search_tool.text(query, max_results=3)
            return "\n".join([f"- {r['body']}" for r in results])
        except Exception as e:
            print(f"⚠️ Erro na busca: {e}")
            return ""
    
    def __call__(self, question: str) -> str:
        """Responde à pergunta com busca se necessário."""
        
        if not self.model:
            return "N/A"
        
        # Verifica se precisa de busca
        search_terms = ["who", "what", "when", "where", "which", "how"]
        needs_search = any(term in question.lower() for term in search_terms)
        
        search_results = ""
        if needs_search and self.search_tool:
            print("🔍 Buscando informações...")
            search_results = self.search(question)
        
        prompt = f"""
        You are an AI assistant solving GAIA benchmark questions.
        
        Question: {question}
        
        {f"Search results:\n{search_results}\n" if search_results else ""}
        
        Instructions:
        1. Use the search results if available.
        2. Think step by step.
        3. END with FINAL ANSWER: [answer]
        
        Rules:
        - Numbers: just the number
        - Strings: just the text
        - Dates: YYYY-MM-DD
        - No markdown after FINAL ANSWER
        
        Let me solve this:
        """
        
        try:
            response = self.model.generate_content(prompt)
            text = response.text.strip()
            
            if "FINAL ANSWER:" in text:
                answer = text.split("FINAL ANSWER:")[-1].strip()
            else:
                lines = [l.strip() for l in text.split('\n') if l.strip()]
                answer = lines[-1] if lines else text
            
            answer = answer.strip('"').strip("'").strip()
            answer = answer.replace("```", "").strip()
            
            print(f"✅ Resposta: {answer}")
            return answer if answer else "N/A"
            
        except Exception as e:
            print(f"❌ Erro: {e}")
            return "N/A"


# ===== FUNÇÃO PRINCIPAL =====
def run_and_submit(username: str, use_search: bool = True):
    """Executa o agente e submete as respostas."""
    
    if not username or not username.strip():
        return "❌ Digite seu username do Hugging Face.", None
    
    username = username.strip()
    print(f"\n👤 Usuário: {username}")
    
    # Escolhe o agente
    if use_search:
        agent = SearchEnhancedAgent()
    else:
        agent = ImprovedGAIAgent()
    
    # Busca perguntas
    try:
        print("📥 Buscando perguntas...")
        response = requests.get(f"{API_URL}/questions", timeout=15)
        response.raise_for_status()
        questions = response.json()
        print(f"✅ {len(questions)} perguntas carregadas")
    except Exception as e:
        return f"❌ Erro ao buscar perguntas: {e}", None
    
    # Processa perguntas
    results = []
    answers = []
    correct = 0
    
    for i, item in enumerate(questions, 1):
        task_id = item.get("task_id")
        question = item.get("question")
        
        if not task_id:
            continue
        
        print(f"\n[{i}/{len(questions)}] Task {task_id[:8]}...")
        print(f"   Pergunta: {question[:100]}...")
        
        try:
            answer = agent(question)
            answers.append({"task_id": task_id, "submitted_answer": answer})
            results.append({"Task ID": task_id, "Resposta": answer})
            print(f"   ✅ Resposta: {answer}")
        except Exception as e:
            error_msg = f"ERRO: {e}"
            results.append({"Task ID": task_id, "Resposta": error_msg})
            print(f"   ❌ {error_msg}")
    
    if not answers:
        return "❌ Nenhuma resposta gerada.", pd.DataFrame(results)
    
    # Submete
    space_id = os.getenv("SPACE_ID", "seu-usuario/seu-space")
    payload = {
        "username": username,
        "agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
        "answers": answers
    }
    
    print(f"\n📤 Submetendo {len(answers)} respostas...")
    
    try:
        response = requests.post(f"{API_URL}/submit", json=payload, timeout=120)
        response.raise_for_status()
        data = response.json()
        
        status = f"""
✅ SUBMISSÃO CONCLUÍDA!
👤 Usuário: {data.get('username', username)}
📊 Score: {data.get('score', 'N/A')}%
✅ Acertos: {data.get('correct_count', '?')}/{data.get('total_attempted', '?')}
📝 Mensagem: {data.get('message', '')}

📋 Detalhes:
- Total perguntas: {len(questions)}
- Respostas submetidas: {len(answers)}
- Agente: {'com busca' if use_search else 'sem busca'}
"""
        return status, pd.DataFrame(results)
        
    except Exception as e:
        return f"❌ Erro na submissão: {e}", pd.DataFrame(results)


# ===== INTERFACE GRADIO =====
with gr.Blocks(title="GAIA Agent - Busca+CoT") as demo:
    gr.Markdown("""
    # 🎯 GAIA Agent - Versão Melhorada
    
    **Agente com Chain of Thought e busca na web!**
    
    Instruções:
    1. Digite seu username do Hugging Face
    2. Selecione se quer usar busca na web
    3. Clique em Executar
    4. Veja seu score!
    """)
    
    with gr.Row():
        username_input = gr.Textbox(
            label="Seu username do Hugging Face",
            placeholder="ex: nayaracardoso",
            scale=2
        )
        search_checkbox = gr.Checkbox(
            label="🔍 Usar busca na web",
            value=True
        )
        run_btn = gr.Button("🚀 Executar", variant="primary", scale=1)
    
    status_output = gr.Textbox(label="Status", lines=15)
    results_table = gr.DataFrame(label="Resultados")
    
    run_btn.click(
        fn=run_and_submit,
        inputs=[username_input, search_checkbox],
        outputs=[status_output, results_table]
    )

if __name__ == "__main__":
    demo.launch()