jjvelezo commited on
Commit
50e5a10
·
verified ·
1 Parent(s): b8ac549

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -132
app.py CHANGED
@@ -2,182 +2,106 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
- from agent import DuckDuckGoAgent
6
 
7
- # --- Constants ---
8
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
- # --- Basic Agent Definition ---
11
- class BasicAgent:
12
- def __init__(self):
13
- print("BasicAgent initialized.")
14
- def __call__(self, question: str) -> str:
15
- print(f"Agent received question (first 50 chars): {question[:50]}...")
16
- fixed_answer = "This is a default answer."
17
- print(f"Agent returning fixed answer: {fixed_answer}")
18
- return fixed_answer
19
 
 
20
  def run_and_submit_all(profile: gr.OAuthProfile | None):
21
  """
22
- Fetches all questions, runs the DuckDuckGoAgent on them, submits all answers,
23
- and displays the results.
24
  """
25
- space_id = os.getenv("SPACE_ID")
26
 
27
  if profile:
28
  username = f"{profile.username}"
29
- print(f"User logged in: {username}")
30
  else:
31
- print("User not logged in.")
32
- return "Please Login to Hugging Face with the button.", None
33
 
34
  api_url = DEFAULT_API_URL
35
  questions_url = f"{api_url}/questions"
 
36
  submit_url = f"{api_url}/submit"
37
 
38
- # 1. Instantiate Agent
39
  try:
40
- agent = DuckDuckGoAgent() # Instanciando DuckDuckGoAgent
 
41
  except Exception as e:
42
- print(f"Error instantiating agent: {e}")
43
- return f"Error initializing agent: {e}", None
44
-
45
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
46
- print(agent_code)
47
 
48
- # 2. Fetch Questions
49
- print(f"Fetching questions from: {questions_url}")
50
  try:
51
  response = requests.get(questions_url, timeout=15)
52
  response.raise_for_status()
53
  questions_data = response.json()
54
  if not questions_data:
55
- print("Fetched questions list is empty.")
56
- return "Fetched questions list is empty or invalid format.", None
57
- print(f"Fetched {len(questions_data)} questions.")
58
- except requests.exceptions.RequestException as e:
59
- print(f"Error fetching questions: {e}")
60
- return f"Error fetching questions: {e}", None
61
 
62
- # 3. Run Agent
63
  results_log = []
64
  answers_payload = []
65
- print(f"Running agent on {len(questions_data)} questions...")
66
  for item in questions_data:
67
  task_id = item.get("task_id")
68
- question_text = item.get("question")
 
 
 
69
  if not task_id or question_text is None:
70
- print(f"Skipping item with missing task_id or question: {item}")
71
  continue
72
  try:
73
- submitted_answer = agent(question_text) # Usando el agente DuckDuckGo
74
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
75
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
76
  except Exception as e:
77
- print(f"Error running agent on task {task_id}: {e}")
78
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
79
 
80
- if not answers_payload:
81
- print("Agent did not produce any answers to submit.")
82
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
83
-
84
- # 4. Prepare Submission
85
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
86
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
87
- print(status_update)
88
-
89
- # 5. Submit
90
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
91
  try:
92
  response = requests.post(submit_url, json=submission_data, timeout=60)
93
  response.raise_for_status()
94
  result_data = response.json()
95
- final_status = (
96
- f"Submission Successful!\n"
97
- f"User: {result_data.get('username')}\n"
98
- f"Overall Score: {result_data.get('score', 'N/A')}% "
99
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
100
- f"Message: {result_data.get('message', 'No message received.')}"
101
- )
102
- print("Submission successful.")
103
- results_df = pd.DataFrame(results_log)
104
- return final_status, results_df
105
- except requests.exceptions.HTTPError as e:
106
- error_detail = f"Server responded with status {e.response.status_code}."
107
- try:
108
- error_json = e.response.json()
109
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
110
- except requests.exceptions.JSONDecodeError:
111
- error_detail += f" Response: {e.response.text[:500]}"
112
- status_message = f"Submission Failed: {error_detail}"
113
- print(status_message)
114
- results_df = pd.DataFrame(results_log)
115
- return status_message, results_df
116
- except requests.exceptions.Timeout:
117
- status_message = "Submission Failed: The request timed out."
118
- print(status_message)
119
- results_df = pd.DataFrame(results_log)
120
- return status_message, results_df
121
  except requests.exceptions.RequestException as e:
122
- status_message = f"Submission Failed: Network error - {e}"
123
- print(status_message)
124
- results_df = pd.DataFrame(results_log)
125
- return status_message, results_df
126
- except Exception as e:
127
- status_message = f"An unexpected error occurred during submission: {e}"
128
- print(status_message)
129
- results_df = pd.DataFrame(results_log)
130
- return status_message, results_df
131
-
132
 
133
-
134
- # --- Build Gradio Interface using Blocks ---
135
  with gr.Blocks() as demo:
136
- gr.Markdown("# DuckDuckGo Agent Evaluation Runner")
137
- gr.Markdown(
138
- """
139
- **Instructions:**
140
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
141
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
142
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
143
- ---
144
- **Disclaimers:**
145
- Once clicking on the "submit button, it can take quite some time (this is the time for the agent to go through all the questions).
146
- This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution.
147
- """
148
- )
149
-
150
- gr.LoginButton()
151
 
152
- run_button = gr.Button("Run Evaluation & Print Results")
 
 
153
 
154
- status_output = gr.Textbox(label="Run Status / Evaluation Result", lines=5, interactive=False)
155
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
 
 
156
 
157
- run_button.click(
158
- fn=run_and_submit_all,
159
- outputs=[status_output, results_table]
160
- )
161
 
162
  if __name__ == "__main__":
163
- print("\n" + "-"*30 + " App Starting " + "-"*30)
164
- space_host_startup = os.getenv("SPACE_HOST")
165
- space_id_startup = os.getenv("SPACE_ID")
166
-
167
- if space_host_startup:
168
- print(f"✅ SPACE_HOST found: {space_host_startup}")
169
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
170
- else:
171
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
172
-
173
- if space_id_startup:
174
- print(f"✅ SPACE_ID found: {space_id_startup}")
175
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
176
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
177
- else:
178
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
179
-
180
- print("-"*(60 + len(" App Starting ")) + "\n")
181
-
182
- print("Launching Gradio Interface for DuckDuckGo Agent Evaluation...")
183
- demo.launch(debug=True, share=False)
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ from dotenv import load_dotenv
6
 
7
+ load_dotenv() # Cargar variables de entorno
 
8
 
9
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
 
 
 
 
 
 
 
10
 
11
+ # Función para ejecutar y enviar todas las respuestas
12
  def run_and_submit_all(profile: gr.OAuthProfile | None):
13
  """
14
+ Recoge todas las preguntas, ejecuta el agente sobre ellas, envía las respuestas y muestra los resultados.
 
15
  """
16
+ space_id = os.getenv("SPACE_ID") # ID del espacio para enlaces al código
17
 
18
  if profile:
19
  username = f"{profile.username}"
20
+ print(f"Usuario logueado: {username}")
21
  else:
22
+ print("Usuario no logueado.")
23
+ return "Por favor, inicia sesión en Hugging Face.", None
24
 
25
  api_url = DEFAULT_API_URL
26
  questions_url = f"{api_url}/questions"
27
+ attachments_url = f"{api_url}/files/"
28
  submit_url = f"{api_url}/submit"
29
 
30
+ # Crear agente (modificado)
31
  try:
32
+ print("Iniciando agente...")
33
+ agent = agent.BasicAgent() # Usar el agente principal
34
  except Exception as e:
35
+ print(f"Error al iniciar el agente: {e}")
36
+ return f"Error al iniciar el agente: {e}", None
 
 
 
37
 
38
+ # 2. Recoger las preguntas
39
+ print(f"Recogiendo preguntas desde: {questions_url}")
40
  try:
41
  response = requests.get(questions_url, timeout=15)
42
  response.raise_for_status()
43
  questions_data = response.json()
44
  if not questions_data:
45
+ print("La lista de preguntas está vacía.")
46
+ return "La lista de preguntas está vacía.", None
47
+ print(f"Se recogieron {len(questions_data)} preguntas.")
48
+ except Exception as e:
49
+ print(f"Error recogiendo preguntas: {e}")
50
+ return f"Error recogiendo preguntas: {e}", None
51
 
52
+ # 3. Ejecutar el agente
53
  results_log = []
54
  answers_payload = []
55
+ print(f"Ejecutando el agente sobre {len(questions_data)} preguntas...")
56
  for item in questions_data:
57
  task_id = item.get("task_id")
58
+ question_text = item.get("question", "")
59
+ attachment_b64 = item.get("attachment_b64", "")
60
+ if attachment_b64:
61
+ question_text = f"{question_text}\n\n[ATTACHMENT:]\n{attachment_b64}"
62
  if not task_id or question_text is None:
63
+ print(f"Saltando tarea con ID o pregunta faltante: {item}")
64
  continue
65
  try:
66
+ submitted_answer = agent.forward(question_text) # Respuesta del agente
67
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
68
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
69
  except Exception as e:
70
+ print(f"Error ejecutando agente en tarea {task_id}: {e}")
71
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
72
 
73
+ # 4. Enviar las respuestas
74
+ submission_data = {"username": username.strip(), "agent_code": "agent_code_placeholder", "answers": answers_payload}
 
 
 
 
 
 
 
 
 
75
  try:
76
  response = requests.post(submit_url, json=submission_data, timeout=60)
77
  response.raise_for_status()
78
  result_data = response.json()
79
+ final_status = f"¡Envío exitoso!\nUsuario: {result_data.get('username')}\nPuntaje final: {result_data.get('score', 'N/A')}%"
80
+ print("Envío exitoso.")
81
+ return final_status, pd.DataFrame(results_log)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  except requests.exceptions.RequestException as e:
83
+ print(f"Error al enviar respuestas: {e}")
84
+ return f"Error al enviar respuestas: {e}", pd.DataFrame(results_log)
 
 
 
 
 
 
 
 
85
 
86
+ # Interfaz Gradio
 
87
  with gr.Blocks() as demo:
88
+ gr.Markdown("# Evaluación Básica del Agente")
89
+ gr.Markdown("""
90
+ **Instrucciones:**
91
+ 1. Modifica este espacio con tu lógica de agente y las herramientas necesarias.
92
+ 2. Inicia sesión en Hugging Face con el botón abajo.
93
+ 3. Haz clic en 'Ejecutar Evaluación y Enviar Todas las Respuestas' para obtener resultados.
 
 
 
 
 
 
 
 
 
94
 
95
+ **Aviso:**
96
+ Puede tomar tiempo procesar las respuestas, así que ten paciencia.
97
+ """)
98
 
99
+ gr.LoginButton()
100
+ run_button = gr.Button("Ejecutar Evaluación y Enviar Todas las Respuestas")
101
+ status_output = gr.Textbox(label="Resultado de Ejecución / Envío", lines=5, interactive=False)
102
+ results_table = gr.DataFrame(label="Preguntas y Respuestas del Agente", wrap=True)
103
 
104
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
105
 
106
  if __name__ == "__main__":
107
+ demo.launch(debug=True, share=False)