smiquensi commited on
Commit
6d148d5
·
verified ·
1 Parent(s): 16ed1b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -95
app.py CHANGED
@@ -1,16 +1,12 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
6
  from smolagents import CodeAgent
7
  from smolagents.models import InferenceClientModel
8
  from smolagents.tools import DuckDuckGoSearchTool
9
 
10
- # --- Constants ---
11
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
-
13
- # --- Basic Agent Definition ---
14
  class BasicAgent:
15
  def __init__(self):
16
  model = InferenceClientModel(model_id="google/flan-t5-large")
@@ -19,7 +15,7 @@ class BasicAgent:
19
  print("🤖 Agente inteligente inicializado.")
20
 
21
  def __call__(self, question: str) -> str:
22
- print(f"❓ Recibida pregunta: {question[:50]}...")
23
  try:
24
  answer = self.agent.run(question).strip()
25
  print(f"✅ Respuesta generada: {answer}")
@@ -28,140 +24,98 @@ class BasicAgent:
28
  print(f"❌ Error del agente: {e}")
29
  return f"AGENT ERROR: {e}"
30
 
31
- # --- Evaluation and Submission ---
 
 
32
  def run_and_submit_all(profile: gr.OAuthProfile | None):
33
  space_id = os.getenv("SPACE_ID")
34
 
35
  if profile:
36
- username = f"{profile.username}"
37
- print(f"User logged in: {username}")
38
  else:
39
- print("User not logged in.")
40
- return "Please Login to Hugging Face with the button.", None
41
 
42
  api_url = DEFAULT_API_URL
43
  questions_url = f"{api_url}/questions"
44
  submit_url = f"{api_url}/submit"
 
45
 
46
  try:
47
  agent = BasicAgent()
48
  except Exception as e:
49
- print(f"Error instantiating agent: {e}")
50
- return f"Error initializing agent: {e}", None
51
-
52
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
53
- print(agent_code)
54
 
55
- print(f"Fetching questions from: {questions_url}")
56
  try:
57
  response = requests.get(questions_url, timeout=15)
58
  response.raise_for_status()
59
  questions_data = response.json()
60
- if not questions_data:
61
- print("Fetched questions list is empty.")
62
- return "Fetched questions list is empty or invalid format.", None
63
- print(f"Fetched {len(questions_data)} questions.")
64
- except requests.exceptions.RequestException as e:
65
- print(f"Error fetching questions: {e}")
66
- return f"Error fetching questions: {e}", None
67
- except requests.exceptions.JSONDecodeError as e:
68
- print(f"Error decoding JSON response from questions endpoint: {e}")
69
- print(f"Response text: {response.text[:500]}")
70
- return f"Error decoding server response for questions: {e}", None
71
  except Exception as e:
72
- print(f"An unexpected error occurred fetching questions: {e}")
73
- return f"An unexpected error occurred fetching questions: {e}", None
74
 
 
75
  results_log = []
76
  answers_payload = []
77
- print(f"Running agent on {len(questions_data)} questions...")
78
  for item in questions_data:
79
  task_id = item.get("task_id")
80
  question_text = item.get("question")
81
  if not task_id or question_text is None:
82
- print(f"Skipping item with missing task_id or question: {item}")
83
  continue
84
  try:
85
  submitted_answer = agent(question_text)
86
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
87
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
88
  except Exception as e:
89
- print(f"Error running agent on task {task_id}: {e}")
90
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
91
-
92
- if not answers_payload:
93
- print("Agent did not produce any answers to submit.")
94
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
 
 
 
 
 
 
95
 
96
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
97
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
98
- print(status_update)
99
-
100
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
101
  try:
102
  response = requests.post(submit_url, json=submission_data, timeout=60)
103
  response.raise_for_status()
104
  result_data = response.json()
105
  final_status = (
106
- f"Submission Successful!\n"
107
- f"User: {result_data.get('username')}\n"
108
- f"Overall Score: {result_data.get('score', 'N/A')}% "
109
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
110
- f"Message: {result_data.get('message', 'No message received.')}"
111
  )
112
- print("Submission successful.")
113
- results_df = pd.DataFrame(results_log)
114
- return final_status, results_df
115
- except requests.exceptions.HTTPError as e:
116
- error_detail = f"Server responded with status {e.response.status_code}."
117
- try:
118
- error_json = e.response.json()
119
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
120
- except requests.exceptions.JSONDecodeError:
121
- error_detail += f" Response: {e.response.text[:500]}"
122
- status_message = f"Submission Failed: {error_detail}"
123
- print(status_message)
124
- results_df = pd.DataFrame(results_log)
125
- return status_message, results_df
126
- except requests.exceptions.Timeout:
127
- status_message = "Submission Failed: The request timed out."
128
- print(status_message)
129
- results_df = pd.DataFrame(results_log)
130
- return status_message, results_df
131
- except requests.exceptions.RequestException as e:
132
- status_message = f"Submission Failed: Network error - {e}"
133
- print(status_message)
134
- results_df = pd.DataFrame(results_log)
135
- return status_message, results_df
136
  except Exception as e:
137
- status_message = f"An unexpected error occurred during submission: {e}"
138
- print(status_message)
139
- results_df = pd.DataFrame(results_log)
140
- return status_message, results_df
141
 
142
- # --- Gradio Interface ---
143
  with gr.Blocks() as demo:
144
- gr.Markdown("# Basic Agent Evaluation Runner")
145
  gr.Markdown("""
146
- **Instructions:**
147
-
148
- 1. Log in to your Hugging Face account using the button below.
149
- 2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
150
- ---
151
- This app uses a basic agent. Improve its logic and tools to boost performance!
152
  """)
153
 
154
  gr.LoginButton()
 
 
 
155
 
156
- run_button = gr.Button("Run Evaluation & Submit All Answers")
157
-
158
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
159
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
160
-
161
- run_button.click(
162
- fn=run_and_submit_all,
163
- outputs=[status_output, results_table]
164
- )
165
 
166
  if __name__ == "__main__":
167
- demo.launch(debug=True, share=False)
 
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
  from smolagents import CodeAgent
6
  from smolagents.models import InferenceClientModel
7
  from smolagents.tools import DuckDuckGoSearchTool
8
 
9
+ # --- Agente Inteligente usando flan-t5-large + DuckDuckGo ---
 
 
 
10
  class BasicAgent:
11
  def __init__(self):
12
  model = InferenceClientModel(model_id="google/flan-t5-large")
 
15
  print("🤖 Agente inteligente inicializado.")
16
 
17
  def __call__(self, question: str) -> str:
18
+ print(f"❓ Pregunta recibida: {question[:50]}...")
19
  try:
20
  answer = self.agent.run(question).strip()
21
  print(f"✅ Respuesta generada: {answer}")
 
24
  print(f"❌ Error del agente: {e}")
25
  return f"AGENT ERROR: {e}"
26
 
27
+ # --- Evaluación y envío GAIA ---
28
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
29
+
30
  def run_and_submit_all(profile: gr.OAuthProfile | None):
31
  space_id = os.getenv("SPACE_ID")
32
 
33
  if profile:
34
+ username = profile.username
35
+ print(f"👤 Usuario: {username}")
36
  else:
37
+ return "⚠️ Por favor, inicia sesión en Hugging Face antes de enviar.", None
 
38
 
39
  api_url = DEFAULT_API_URL
40
  questions_url = f"{api_url}/questions"
41
  submit_url = f"{api_url}/submit"
42
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
43
 
44
  try:
45
  agent = BasicAgent()
46
  except Exception as e:
47
+ return f"Error al crear el agente: {e}", None
 
 
 
 
48
 
49
+ # Descargar preguntas
50
  try:
51
  response = requests.get(questions_url, timeout=15)
52
  response.raise_for_status()
53
  questions_data = response.json()
 
 
 
 
 
 
 
 
 
 
 
54
  except Exception as e:
55
+ return f" Error al descargar preguntas: {e}", None
 
56
 
57
+ # Responder preguntas
58
  results_log = []
59
  answers_payload = []
60
+
61
  for item in questions_data:
62
  task_id = item.get("task_id")
63
  question_text = item.get("question")
64
  if not task_id or question_text is None:
 
65
  continue
66
  try:
67
  submitted_answer = agent(question_text)
68
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
69
+ results_log.append({
70
+ "Task ID": task_id,
71
+ "Question": question_text,
72
+ "Submitted Answer": submitted_answer
73
+ })
74
  except Exception as e:
75
+ results_log.append({
76
+ "Task ID": task_id,
77
+ "Question": question_text,
78
+ "Submitted Answer": f"AGENT ERROR: {e}"
79
+ })
80
+
81
+ # Enviar respuestas
82
+ submission_data = {
83
+ "username": username,
84
+ "agent_code": agent_code,
85
+ "answers": answers_payload
86
+ }
87
 
 
 
 
 
 
88
  try:
89
  response = requests.post(submit_url, json=submission_data, timeout=60)
90
  response.raise_for_status()
91
  result_data = response.json()
92
  final_status = (
93
+ f" ¡Envío realizado con éxito!\n"
94
+ f"👤 Usuario: {result_data.get('username')}\n"
95
+ f"📊 Puntuación: {result_data.get('score', 'N/A')}% "
96
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correctas)\n"
97
+ f"📬 Mensaje: {result_data.get('message', 'Sin mensaje.')}"
98
  )
99
+ return final_status, pd.DataFrame(results_log)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  except Exception as e:
101
+ return f" Error durante el envío: {e}", pd.DataFrame(results_log)
 
 
 
102
 
103
+ # --- Interfaz Gradio ---
104
  with gr.Blocks() as demo:
105
+ gr.Markdown("# 🧠 Evaluador Agente GAIA - Curso Hugging Face")
106
  gr.Markdown("""
107
+ 1. Inicia sesión en Hugging Face.
108
+ 2. Pulsa el botón para ejecutar tu agente y enviar las respuestas.
109
+ 3. Espera unos minutos y revisa la puntuación.
 
 
 
110
  """)
111
 
112
  gr.LoginButton()
113
+ run_button = gr.Button("▶️ Ejecutar y Enviar Respuestas")
114
+ status_output = gr.Textbox(label="Resultado", lines=6, interactive=False)
115
+ results_table = gr.DataFrame(label="Respuestas Generadas")
116
 
117
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
 
 
 
 
 
118
 
119
  if __name__ == "__main__":
120
+ print("🚀 Lanzando interfaz...")
121
+ demo.launch(debug=True)