Visionkambanje commited on
Commit
d93eb8a
·
verified ·
1 Parent(s): 36becbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -199
app.py CHANGED
@@ -1,147 +1,88 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
6
- import re
7
 
8
- from smolagents import CodeAgent, DuckDuckGoSearchTool,InferenceClientModel , PythonInterpreterTool, WikipediaSearchTool, FinalAnswerTool
9
-
10
- # (Keep Constants as is)
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
-
14
- # --- Basic Agent Definition ---
15
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
16
 
17
- import os
18
- from smolagents import InferenceClientModel
19
 
 
20
  model = InferenceClientModel(
21
  model_id="Qwen/Qwen1.5-1.8B-Chat", # example HF chat model
22
- token=os.getenv("HF_TOKEN") # only needed if private model
23
  )
24
 
25
- try:
26
- import ddgs
27
- except ImportError:
28
- from duckduckgo_search import DDGS
29
- import sys, types
30
- ddgs = types.SimpleNamespace(DDGS=DDGS)
31
- sys.modules["ddgs"] = ddgs
32
-
33
  class BasicAgent:
34
- def __init__(self, api_key: str = None):
35
- if not api_key:
36
- print("Did not receive API key.")
37
- else:
38
- print(f"First 5 characters of the provided API key: {api_key[:5]}")
39
- os.environ["HF_TOKEN"] = api_key
40
- print("BasicAgent initialized.")
 
 
 
 
 
 
41
  def __call__(self, question: str) -> str:
42
  print(f"Agent received question (first 50 chars): {question[:50]}...")
43
- agent = CodeAgent(tools=[DuckDuckGoSearchTool(),PythonInterpreterTool(),WikipediaSearchTool(),FinalAnswerTool()],model=OpenAIServerModel(model_id="gpt-4o"))
44
- answer=agent.run(question)
 
 
45
  return answer
46
-
47
- def run_and_submit_all(api_key: str, profile: gr.OAuthProfile | None = None):
48
- """
49
- Fetches all questions, runs the BasicAgent on them, submits all answers,
50
- and displays the results.
51
- """
52
- # --- Determine HF Space Runtime URL and Repo URL ---
53
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
54
-
55
- if profile:
56
- username= f"{profile.username}"
57
- print(f"User logged in: {username}")
58
- else:
59
- print("User not logged in.")
60
- return "Please Login to Hugging Face with the button.", None
61
-
62
- api_url = DEFAULT_API_URL
63
- questions_url = f"{api_url}/questions"
64
- submit_url = f"{api_url}/submit"
65
-
66
- # 1. Instantiate Agent ( modify this part to create your agent)
67
- try:
68
- agent = BasicAgent(model=model)
69
- except Exception as e:
70
- print(f"Error instantiating agent: {e}")
71
- return f"Error initializing agent: {e}", None
72
- # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
73
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
74
- print(agent_code)
75
-
76
- # 2. Fetch Questions
77
- print(f"Fetching questions from: {questions_url}")
78
  try:
79
  response = requests.get(questions_url, timeout=15)
80
  response.raise_for_status()
81
  questions_data = response.json()
82
  if not questions_data:
83
- print("Fetched questions list is empty.")
84
- return "Fetched questions list is empty or invalid format.", None
85
- print(f"Fetched {len(questions_data)} questions.")
86
- except requests.exceptions.RequestException as e:
87
- print(f"Error fetching questions: {e}")
88
- return f"Error fetching questions: {e}", None
89
- except requests.exceptions.JSONDecodeError as e:
90
- print(f"Error decoding JSON response from questions endpoint: {e}")
91
- print(f"Response text: {response.text[:500]}")
92
- return f"Error decoding server response for questions: {e}", None
93
  except Exception as e:
94
- print(f"An unexpected error occurred fetching questions: {e}")
95
- return f"An unexpected error occurred fetching questions: {e}", None
96
-
97
- # 3. Run your Agent
98
  results_log = []
99
  answers_payload = []
100
- print(f"Running agent on {len(questions_data)} questions...")
101
  for item in questions_data:
102
  task_id = item.get("task_id")
103
  question_text = item.get("question")
104
  if not task_id or question_text is None:
105
- print(f"Skipping item with missing task_id or question: {item}")
106
  continue
107
- try:
108
- submitted_answer = agent(question_text)
109
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
110
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
 
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
- except Exception as e:
131
- print(f"Error running agent on task {task_id}: {e}")
132
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
133
-
134
  if not answers_payload:
135
- print("Agent did not produce any answers to submit.")
136
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
137
-
138
- # 4. Prepare Submission
 
 
139
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
140
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
141
- print(status_update)
142
-
143
- # 5. Submit
144
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
145
  try:
146
  response = requests.post(submit_url, json=submission_data, timeout=60)
147
  response.raise_for_status()
@@ -153,106 +94,29 @@ def run_and_submit_all(api_key: str, profile: gr.OAuthProfile | None = None):
153
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
154
  f"Message: {result_data.get('message', 'No message received.')}"
155
  )
156
- print("Submission successful.")
157
- results_df = pd.DataFrame(results_log)
158
- return final_status, results_df
159
- except requests.exceptions.HTTPError as e:
160
- error_detail = f"Server responded with status {e.response.status_code}."
161
- try:
162
- error_json = e.response.json()
163
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
164
- except requests.exceptions.JSONDecodeError:
165
- error_detail += f" Response: {e.response.text[:500]}"
166
- status_message = f"Submission Failed: {error_detail}"
167
- print(status_message)
168
- results_df = pd.DataFrame(results_log)
169
- return status_message, results_df
170
- except requests.exceptions.Timeout:
171
- status_message = "Submission Failed: The request timed out."
172
- print(status_message)
173
- results_df = pd.DataFrame(results_log)
174
- return status_message, results_df
175
- except requests.exceptions.RequestException as e:
176
- status_message = f"Submission Failed: Network error - {e}"
177
- print(status_message)
178
- results_df = pd.DataFrame(results_log)
179
- return status_message, results_df
180
  except Exception as e:
181
- status_message = f"An unexpected error occurred during submission: {e}"
182
- print(status_message)
183
- results_df = pd.DataFrame(results_log)
184
- return status_message, results_df
185
-
186
-
187
- # --- Build Gradio Interface using Blocks ---
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
 
 
202
  with gr.Blocks() as demo:
203
  gr.Markdown("# Basic Agent Evaluation Runner")
204
- gr.Markdown(
205
- """
206
- **Instructions:**
207
-
208
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
209
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
210
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
211
-
212
- ---
213
- **Disclaimers:**
214
- 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).
215
- This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
216
- """
217
- )
218
-
219
 
220
  gr.LoginButton()
221
-
222
  run_button = gr.Button("Run Evaluation & Submit All Answers")
223
-
224
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
225
- # Removed max_rows=10 from DataFrame constructor
226
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
227
-
228
  run_button.click(
229
  fn=run_and_submit_all,
230
- input=(HF_Api_key)
231
  outputs=[status_output, results_table]
232
-
233
-
234
  )
235
-
236
  if __name__ == "__main__":
237
- print("\n" + "-"*30 + " App Starting " + "-"*30)
238
- # Check for SPACE_HOST and SPACE_ID at startup for information
239
- space_host_startup = os.getenv("SPACE_HOST")
240
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
241
-
242
- if space_host_startup:
243
- print(f"✅ SPACE_HOST found: {space_host_startup}")
244
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
245
- else:
246
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
247
-
248
- if space_id_startup: # Print repo URLs if SPACE_ID is found
249
- print(f"✅ SPACE_ID found: {space_id_startup}")
250
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
251
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
252
- else:
253
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
254
-
255
- print("-"*(60 + len(" App Starting ")) + "\n")
256
-
257
- print("Launching Gradio Interface for Basic Agent Evaluation...")
258
- demo.launch(debug=True, share=False)
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
 
5
 
6
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, PythonInterpreterTool, WikipediaSearchTool, FinalAnswerTool
7
+
 
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
 
 
10
 
11
+ # --- Hugging Face API Key ---
12
+ HF_API_KEY = "your_huggingface_api_key_here" # <-- Replace with your key
13
 
14
+ # --- Initialize Hugging Face Model ---
15
  model = InferenceClientModel(
16
  model_id="Qwen/Qwen1.5-1.8B-Chat", # example HF chat model
17
+ token=HF_API_KEY
18
  )
19
 
20
+ # --- Basic Agent Definition ---
 
 
 
 
 
 
 
21
  class BasicAgent:
22
+ def __init__(self, model):
23
+ self.model = model
24
+ self.agent = CodeAgent(
25
+ tools=[
26
+ DuckDuckGoSearchTool(),
27
+ PythonInterpreterTool(),
28
+ WikipediaSearchTool(),
29
+ FinalAnswerTool()
30
+ ],
31
+ model=self.model
32
+ )
33
+ print("BasicAgent initialized with Hugging Face model.")
34
+
35
  def __call__(self, question: str) -> str:
36
  print(f"Agent received question (first 50 chars): {question[:50]}...")
37
+ try:
38
+ answer = self.agent.run(question)
39
+ except Exception as e:
40
+ answer = f"AGENT ERROR: {e}"
41
  return answer
42
+
43
+ # --- Run & Submit Function ---
44
+ def run_and_submit_all(profile: gr.OAuthProfile | None = None):
45
+ if not profile:
46
+ return "Please login to Hugging Face with the button.", None
47
+ username = profile.username
48
+ print(f"User logged in: {username}")
49
+
50
+ # Initialize agent
51
+ agent = BasicAgent(model=model)
52
+
53
+ # Fetch questions
54
+ questions_url = f"{DEFAULT_API_URL}/questions"
55
+ submit_url = f"{DEFAULT_API_URL}/submit"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ return "Fetched questions list is empty or invalid format.", None
 
 
 
 
 
 
 
 
 
62
  except Exception as e:
63
+ return f"Error fetching questions: {e}", None
64
+
65
+ # Run agent on all questions
 
66
  results_log = []
67
  answers_payload = []
 
68
  for item in questions_data:
69
  task_id = item.get("task_id")
70
  question_text = item.get("question")
71
  if not task_id or question_text is None:
 
72
  continue
73
+ submitted_answer = agent(question_text)
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
 
 
 
 
 
 
 
 
 
 
 
 
77
  if not answers_payload:
 
78
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
79
+
80
+ # Prepare submission
81
+ space_id = os.getenv("SPACE_ID", "your_space_id_here") # optional fallback
82
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
83
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
84
+
85
+ # Submit
 
 
 
86
  try:
87
  response = requests.post(submit_url, json=submission_data, timeout=60)
88
  response.raise_for_status()
 
94
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
95
  f"Message: {result_data.get('message', 'No message received.')}"
96
  )
97
+ return final_status, pd.DataFrame(results_log)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  except Exception as e:
99
+ return f"Submission Failed: {e}", pd.DataFrame(results_log)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
+ # --- Gradio Interface ---
102
  with gr.Blocks() as demo:
103
  gr.Markdown("# Basic Agent Evaluation Runner")
104
+ gr.Markdown("""
105
+ **Instructions:**
106
+ 1. Login with your Hugging Face account.
107
+ 2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, and submit.
108
+ """)
 
 
 
 
 
 
 
 
 
 
109
 
110
  gr.LoginButton()
 
111
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
112
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
113
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
114
+
115
  run_button.click(
116
  fn=run_and_submit_all,
117
+ inputs=(),
118
  outputs=[status_output, results_table]
 
 
119
  )
120
+
121
  if __name__ == "__main__":
122
+ demo.launch(debug=True, share=False)