jarguello76 commited on
Commit
6c9f543
·
verified ·
1 Parent(s): ae365e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -113
app.py CHANGED
@@ -2,122 +2,118 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
- from langchain_core.messages import HumanMessage
6
- from tools import build_graph
7
 
8
- # Constants
 
 
 
 
 
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
- class BasicAgent:
12
-
13
- def __init__(self, csv_file_path: str = "embeddings.csv"):
14
- print("BasicAgent initialized.")
15
- self.graph = build_graph(csv_file_path=csv_file_path)
16
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def __call__(self, question: str) -> str:
18
- print(f"Agent received question (first 50 chars): {question[:50]}...")
19
-
20
- # Wrap the question in a HumanMessage
21
- messages = [HumanMessage(content=question)]
22
- messages = self.graph.invoke({"messages": messages})
23
-
24
- # Extract answer and remove "FINAL ANSWER: " prefix
25
- answer = messages['messages'][-1].content
26
- if answer.startswith("FINAL ANSWER: "):
27
- return answer[14:]
28
- else:
29
- # Look for FINAL ANSWER in the response
30
- lines = answer.split('\n')
31
- for line in lines:
32
- if line.strip().startswith("FINAL ANSWER:"):
33
- return line.strip()[14:].strip()
34
- return answer
35
 
 
36
  def run_and_submit_all(profile: gr.OAuthProfile | None):
37
- """
38
- Fetches all questions, runs the BasicAgent on them, submits all answers,
39
- and displays the results.
40
- """
41
- if not profile:
42
- print("User not logged in.")
43
- return "Please Login to Hugging Face with the button.", None
44
-
45
- username = f"{profile.username}"
46
- print(f"User logged in: {username}")
47
-
48
  space_id = os.getenv("SPACE_ID")
49
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Local Development"
50
-
 
 
 
 
51
  api_url = DEFAULT_API_URL
52
  questions_url = f"{api_url}/questions"
53
  submit_url = f"{api_url}/submit"
54
-
55
  try:
56
- agent = BasicAgent() # Make sure your CSV file is named "embeddings.csv" or update the path
57
  except Exception as e:
58
- print(f"Error instantiating agent: {e}")
59
  return f"Error initializing agent: {e}", None
60
-
61
- # Fetch Questions
62
- print(f"Fetching questions from: {questions_url}")
 
63
  try:
64
  response = requests.get(questions_url, timeout=15)
65
  response.raise_for_status()
66
  questions_data = response.json()
67
-
68
  if not questions_data:
69
  return "Fetched questions list is empty or invalid format.", None
70
-
71
  print(f"Fetched {len(questions_data)} questions.")
72
  except Exception as e:
73
- print(f"Error fetching questions: {e}")
74
  return f"Error fetching questions: {e}", None
75
-
76
  results_log = []
77
  answers_payload = []
78
- print(f"Running agent on {len(questions_data)} questions...")
79
-
80
  for item in questions_data:
81
  task_id = item.get("task_id")
82
  question_text = item.get("question")
83
-
84
  if not task_id or question_text is None:
85
- print(f"Skipping item with missing task_id or question: {item}")
86
  continue
87
-
88
  try:
89
  submitted_answer = agent(question_text)
90
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
91
- results_log.append({
92
- "Task ID": task_id,
93
- "Question": question_text,
94
- "Submitted Answer": submitted_answer
95
- })
96
- print(f"Completed task {task_id}: {submitted_answer}")
97
-
98
  except Exception as e:
99
- print(f"Error running agent on task {task_id}: {e}")
100
- results_log.append({
101
- "Task ID": task_id,
102
- "Question": question_text,
103
- "Submitted Answer": f"AGENT ERROR: {e}"
104
- })
105
-
106
  if not answers_payload:
107
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
108
-
109
- submission_data = {
110
- "username": username.strip(),
111
- "agent_code": agent_code,
112
- "answers": answers_payload
113
- }
114
-
115
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
116
  try:
117
  response = requests.post(submit_url, json=submission_data, timeout=60)
118
  response.raise_for_status()
119
  result_data = response.json()
120
-
121
  final_status = (
122
  f"Submission Successful!\n"
123
  f"User: {result_data.get('username')}\n"
@@ -125,56 +121,57 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
125
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
126
  f"Message: {result_data.get('message', 'No message received.')}"
127
  )
128
-
129
- print("Submission successful.")
130
- results_df = pd.DataFrame(results_log)
131
- return final_status, results_df
132
-
133
  except Exception as e:
134
- print(f"Submission failed: {e}")
135
- status_message = f"Submission Failed: {e}"
136
- results_df = pd.DataFrame(results_log)
137
- return status_message, results_df
138
 
 
139
  with gr.Blocks() as demo:
140
- gr.Markdown("# Basic Agent Evaluation Runner")
 
141
  gr.Markdown("""
142
- **Instructions:**
143
- 1. Make sure your `embeddings.csv` file is in the same directory
144
- 2. Log in to your Hugging Face account using the button below
145
- 3. Click 'Run Evaluation & Submit All Answers' to start the evaluation
146
-
147
- **Note:** The evaluation process may take several minutes depending on the number of questions.
148
  """)
149
-
150
  gr.LoginButton()
151
-
152
- run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
 
 
 
 
 
 
153
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
154
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
155
-
156
  run_button.click(
157
  fn=run_and_submit_all,
158
  outputs=[status_output, results_table]
159
  )
160
 
 
161
  if __name__ == "__main__":
162
  print("\n" + "-"*30 + " App Starting " + "-"*30)
163
-
164
- space_host = os.getenv("SPACE_HOST")
165
- space_id = os.getenv("SPACE_ID")
166
-
167
- if space_host:
168
- print(f" SPACE_HOST found: {space_host}")
169
- print(f" Runtime URL: https://{space_host}.hf.space")
 
 
 
 
 
 
170
  else:
171
- print("ℹ️ Running locally")
172
-
173
- if space_id:
174
- print(f"✅ SPACE_ID found: {space_id}")
175
- print(f" Repo URL: https://huggingface.co/spaces/{space_id}")
176
-
177
  print("-"*(60 + len(" App Starting ")) + "\n")
178
- print("Launching Gradio Interface...")
179
-
180
- demo.launch(debug=True, share=False)
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ import re
 
6
 
7
+ # --- Ensure HF Hub token is available in the Space ---
8
+ token = gr.get_oauth_token()
9
+ if token:
10
+ os.environ["agents_token"] = token.token
11
+
12
+ # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
 
15
+ # --- LangChain Agent Definition ---
16
+ from langchain.llms import HuggingFaceHub
17
+ from langchain.agents import initialize_agent, Tool
18
+ from langchain.agents.agent_types import AgentType
19
+ from langchain.tools import tool
20
+ from duckduckgo_search import DDGS
21
+
22
+ @tool
23
+ def search_web(query: str) -> str:
24
+ with DDGS() as ddgs:
25
+ results = list(ddgs.text(query, max_results=3))
26
+ return results[0]["body"] if results else "No result"
27
+
28
+ @tool
29
+ def run_code_snippet(code: str) -> str:
30
+ try:
31
+ local_env = {}
32
+ exec(code, {}, local_env)
33
+ result = [v for v in local_env.values() if isinstance(v, (int, float, str))]
34
+ return str(result[0]) if result else "0"
35
+ except Exception as e:
36
+ return f"Error: {str(e)}"
37
+
38
+ class GAIAAgent:
39
+ def __init__(self):
40
+ print("Initializing LangChain agent with Hugging Face model...")
41
+ self.llm = HuggingFaceHub(
42
+ repo_id="google/flan-t5-xl", # You can change this to another HF model
43
+ model_kwargs={"temperature": 0.3, "max_length": 256}
44
+ )
45
+ self.tools = [search_web, run_code_snippet]
46
+ self.agent = initialize_agent(
47
+ self.tools,
48
+ self.llm,
49
+ agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
50
+ verbose=True
51
+ )
52
+
53
  def __call__(self, question: str) -> str:
54
+ try:
55
+ result = self.agent.run(question)
56
+ return f"FINAL ANSWER: {self.clean_answer(result)}"
57
+ except Exception as e:
58
+ return f"FINAL ANSWER: Error: {e}"
59
+
60
+ def clean_answer(self, ans: str) -> str:
61
+ ans = re.sub(r"[\$,%]", "", ans)
62
+ return ans.strip()
 
 
 
 
 
 
 
 
63
 
64
+ # --- Evaluation & Submission Pipeline ---
65
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
 
 
 
 
 
 
 
 
 
 
66
  space_id = os.getenv("SPACE_ID")
67
+ if profile:
68
+ username = f"{profile.username}"
69
+ print(f"User logged in: {username}")
70
+ else:
71
+ return "Please Login to Hugging Face with the button.", None
72
+
73
  api_url = DEFAULT_API_URL
74
  questions_url = f"{api_url}/questions"
75
  submit_url = f"{api_url}/submit"
76
+
77
  try:
78
+ agent = GAIAAgent()
79
  except Exception as e:
 
80
  return f"Error initializing agent: {e}", None
81
+
82
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
83
+ print(agent_code)
84
+
85
  try:
86
  response = requests.get(questions_url, timeout=15)
87
  response.raise_for_status()
88
  questions_data = response.json()
 
89
  if not questions_data:
90
  return "Fetched questions list is empty or invalid format.", None
 
91
  print(f"Fetched {len(questions_data)} questions.")
92
  except Exception as e:
 
93
  return f"Error fetching questions: {e}", None
94
+
95
  results_log = []
96
  answers_payload = []
 
 
97
  for item in questions_data:
98
  task_id = item.get("task_id")
99
  question_text = item.get("question")
 
100
  if not task_id or question_text is None:
 
101
  continue
 
102
  try:
103
  submitted_answer = agent(question_text)
104
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
105
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
 
 
106
  except Exception as e:
107
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
108
+
 
 
 
 
 
109
  if not answers_payload:
110
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
111
+
112
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
 
 
 
 
113
  try:
114
  response = requests.post(submit_url, json=submission_data, timeout=60)
115
  response.raise_for_status()
116
  result_data = response.json()
 
117
  final_status = (
118
  f"Submission Successful!\n"
119
  f"User: {result_data.get('username')}\n"
 
121
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
122
  f"Message: {result_data.get('message', 'No message received.')}"
123
  )
124
+ return final_status, pd.DataFrame(results_log)
 
 
 
 
125
  except Exception as e:
126
+ return f"Submission Failed: {e}", pd.DataFrame(results_log)
 
 
 
127
 
128
+ # --- Gradio UI ---
129
  with gr.Blocks() as demo:
130
+ gr.Markdown("# GAIA Agent Evaluation Runner")
131
+
132
  gr.Markdown("""
133
+ **Instructions:**
134
+ 1. Clone this space and modify the agent logic as needed.
135
+ 2. Log in to Hugging Face to associate your submission.
136
+ 3. Click the button below to fetch questions, run your agent, and submit results.
 
 
137
  """)
138
+
139
  gr.LoginButton()
140
+
141
+ gr.Markdown("## 🔍 Try a Question Preview")
142
+ preview_input = gr.Textbox(label="Your GAIA-style question")
143
+ preview_output = gr.Textbox(label="Agent's Response", lines=2)
144
+ preview_button = gr.Button("Preview Answer")
145
+ preview_button.click(lambda q: GAIAAgent()(q), inputs=preview_input, outputs=preview_output)
146
+
147
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
148
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
149
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
150
+
151
  run_button.click(
152
  fn=run_and_submit_all,
153
  outputs=[status_output, results_table]
154
  )
155
 
156
+ # --- Main Entry ---
157
  if __name__ == "__main__":
158
  print("\n" + "-"*30 + " App Starting " + "-"*30)
159
+ space_host_startup = os.getenv("SPACE_HOST")
160
+ space_id_startup = os.getenv("SPACE_ID")
161
+
162
+ if space_host_startup:
163
+ print(f"✅ SPACE_HOST found: {space_host_startup}")
164
+ print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
165
+ else:
166
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
167
+
168
+ if space_id_startup:
169
+ print(f"✅ SPACE_ID found: {space_id_startup}")
170
+ print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
171
+ print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
172
  else:
173
+ print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
174
+
 
 
 
 
175
  print("-"*(60 + len(" App Starting ")) + "\n")
176
+ print("Launching Gradio Interface for GAIA Agent Evaluation...")
177
+ demo.launch(debug=True, share=False)