import requests import json import gradio as gr from huggingface_hub import InferenceClient from datetime import datetime class GaiaAgent: def __init__(self, model_name="mistralai/Mixtral-8x7B-Instruct-v0.1"): self.client = InferenceClient(model=model_name) self.system_prompt = """You are solving GAIA benchmark questions. Follow these rules: - Think step by step. - End with: FINAL ANSWER: [answer]. - Answer must be short: a number, few words, or comma-separated list. - No units ($, %) unless specified. - No articles (a, an, the) in strings.""" def get_questions(self, split="validation"): try: url = f"https://huggingface.co/datasets/gaia-benchmark/GAIA/resolve/main/{split}/metadata.jsonl" print(f"Intentando cargar preguntas desde: {url}") # Agrega esta línea response = requests.get(url) response.raise_for_status() # Esto levantará una excepción para códigos de estado HTTP erróneos (4xx o 5xx) print("Preguntas cargadas exitosamente.") # Agrega esta línea return [json.loads(line) for line in response.text.splitlines()] except requests.exceptions.RequestException as req_err: # Captura errores específicos de requests print(f"Error de red o HTTP al cargar preguntas: {req_err}") if hasattr(req_err, 'response') and req_err.response is not None: print(f"Estado HTTP: {req_err.response.status_code}") print(f"Contenido de la respuesta: {req_err.response.text}") return [] except json.JSONDecodeError as json_err: # Captura errores de decodificación JSON print(f"Error al decodificar JSON de las preguntas: {json_err}") print(f"Contenido problemático: {response.text[:500]}...") # Imprime el inicio del contenido return [] except Exception as e: print(f"Error inesperado al cargar preguntas: {e}") return [] def answer_question(self, question): prompt = f"{self.system_prompt}\nQuestion: {question['question']}\nReasoning:" response = self.client.text_generation( prompt, max_new_tokens=500, temperature=0.1, do_sample=False ) return { "task_id": question["task_id"], "answer": self._extract_final_answer(response), "reasoning": response } def _extract_final_answer(self, text): if "FINAL ANSWER:" in text: return text.split("FINAL ANSWER:")[-1].strip() return text.strip() def run_evaluation(hf_username, full_name, question_limit=5): agent = GaiaAgent() questions = agent.get_questions()[:question_limit] if not questions: return "Error: No se pudieron cargar las preguntas", "" results = [] for q in questions: result = agent.answer_question(q) results.append(f"**Pregunta {q['task_id']}:**\n{q['question']}\n\n**Respuesta:** {result['answer']}\n\n---") # Simular generación de certificado certificate_info = f""" ⭐ Certificado GAIA Benchmark ⭐ Nombre: {full_name} Usuario HF: {hf_username} Fecha: {datetime.now().strftime('%Y-%m-%d')} Preguntas respondidas: {len(results)} ¡Felicitaciones por completar el benchmark! """ return certificate_info, "\n".join(results) # Interfaz Gradio with gr.Blocks(title="Agente GAIA Benchmark") as demo: gr.Markdown(""" # 🌟 Agente para GAIA Benchmark Ejecuta evaluación y obtén tu certificado """) with gr.Row(): with gr.Column(): hf_username = gr.Textbox(label="Usuario de Hugging Face", visible=False) full_name = gr.Textbox(label="Nombre completo (aparecerá en el certificado)") question_slider = gr.Slider(1, 20, value=5, label="Número de preguntas a evaluar") submit_btn = gr.Button("Ejecutar Evaluación", variant="primary") with gr.Column(): certificate_output = gr.Textbox(label="Certificado", interactive=False) answers_output = gr.Markdown(label="Respuestas Generadas") # Integración con login de HF demo.load( lambda: gr.Textbox(value=gr.Request().username if gr.Request() else ""), outputs=hf_username ) submit_btn.click( run_evaluation, inputs=[hf_username, full_name, question_slider], outputs=[certificate_output, answers_output] ) if __name__ == "__main__": demo.launch()