dlaima commited on
Commit
480c00a
·
verified ·
1 Parent(s): 1319806

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -152
app.py CHANGED
@@ -2,202 +2,111 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
- import openai
6
- from smolagents import CodeAgent
7
- from smolagents import DuckDuckGoSearchTool
8
 
9
-
10
- # --- Constants ---
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
13
- # --- SmolCodeAgent Definition with system prompt and DuckDuckGo tool ---
14
- class SmolCodeAgent:
15
- SYSTEM_PROMPT = """
16
- You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:
 
 
 
 
 
17
  FINAL ANSWER: [YOUR FINAL ANSWER].
18
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list
19
- of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list,
20
- apply the above rules depending of whether the element to be put in the list is a number or a string.
21
- """
22
-
23
- def __init__(self, api_key=None, model="gpt-4o"):
24
- if api_key is None:
25
- api_key = os.getenv("OPENAI_API_KEY")
26
- if not api_key:
27
- raise RuntimeError("OPENAI_API_KEY not set in environment.")
28
- openai.api_key = api_key
29
- self.model = model
30
-
31
- # Add DuckDuckGoSearchTool to tools
32
- self.tools = [DuckDuckGoSearchTool()]
33
- self.agent = CodeAgent(model=self.model, tools=self.tools)
34
 
35
  def __call__(self, question: str) -> str:
36
- print(f"SmolCodeAgent received question (first 50 chars): {question[:50]}...")
37
- try:
38
- full_prompt = self.SYSTEM_PROMPT.strip() + "\n\nQuestion: " + question
39
- answer = self.agent.run(full_prompt)
40
- print(f"SmolCodeAgent answer (first 100 chars): {answer[:100]}...")
41
- return answer
42
- except Exception as e:
43
- error_msg = f"AGENT ERROR: {e}"
44
- print(error_msg)
45
- return error_msg
46
 
47
- # --- Main logic to run the agent on all questions and submit ---
48
  def run_and_submit_all(profile: gr.OAuthProfile | None):
49
  space_id = os.getenv("SPACE_ID")
50
-
51
  if profile:
52
  username = profile.username
53
  print(f"User logged in: {username}")
54
  else:
55
- print("User not logged in.")
56
  return "Please Login to Hugging Face with the button.", None
57
 
58
- api_url = DEFAULT_API_URL
59
- questions_url = f"{api_url}/questions"
60
- submit_url = f"{api_url}/submit"
61
 
62
- # Instantiate your SmolCodeAgent here
63
  try:
64
- agent = SmolCodeAgent()
65
  except Exception as e:
66
- print(f"Error instantiating agent: {e}")
67
  return f"Error initializing agent: {e}", None
68
 
69
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
70
- print(f"Agent code URL: {agent_code}")
71
-
72
  # Fetch Questions
73
- print(f"Fetching questions from: {questions_url}")
74
  try:
75
- response = requests.get(questions_url, timeout=15)
76
- response.raise_for_status()
77
- questions_data = response.json()
78
- if not questions_data:
79
- print("Fetched questions list is empty.")
80
- return "Fetched questions list is empty or invalid format.", None
81
- print(f"Fetched {len(questions_data)} questions.")
82
  except Exception as e:
83
- print(f"Error fetching questions: {e}")
84
- return f"Error fetching questions: {e}", None
85
 
86
- # Run the agent on each question
87
  results_log = []
88
  answers_payload = []
89
- print(f"Running agent on {len(questions_data)} questions...")
90
  for item in questions_data:
91
  task_id = item.get("task_id")
92
- question_text = item.get("question")
93
- if not task_id or question_text is None:
94
- print(f"Skipping item with missing task_id or question: {item}")
95
  continue
96
  try:
97
- submitted_answer = agent(question_text)
98
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
99
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
100
  except Exception as e:
101
- print(f"Error running agent on task {task_id}: {e}")
102
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
103
 
104
  if not answers_payload:
105
- print("Agent did not produce any answers to submit.")
106
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
107
 
108
- # Prepare submission data
109
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
110
- print(f"Submitting {len(answers_payload)} answers for user '{username}'...")
 
 
111
 
112
- # Submit answers
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"
120
- f"Overall Score: {result_data.get('score', 'N/A')}% "
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
- print("Submission successful.")
125
- results_df = pd.DataFrame(results_log)
126
- return final_status, results_df
127
- except requests.exceptions.HTTPError as e:
128
- error_detail = f"Server responded with status {e.response.status_code}."
129
- try:
130
- error_json = e.response.json()
131
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
132
- except Exception:
133
- error_detail += f" Response: {e.response.text[:500]}"
134
- status_message = f"Submission Failed: {error_detail}"
135
- print(status_message)
136
- results_df = pd.DataFrame(results_log)
137
- return status_message, results_df
138
- except requests.exceptions.Timeout:
139
- status_message = "Submission Failed: The request timed out."
140
- print(status_message)
141
- results_df = pd.DataFrame(results_log)
142
- return status_message, results_df
143
- except requests.exceptions.RequestException as e:
144
- status_message = f"Submission Failed: Network error - {e}"
145
- print(status_message)
146
- results_df = pd.DataFrame(results_log)
147
- return status_message, results_df
148
  except Exception as e:
149
- status_message = f"An unexpected error occurred during submission: {e}"
150
- print(status_message)
151
- results_df = pd.DataFrame(results_log)
152
- return status_message, results_df
153
-
154
 
155
- # --- Gradio UI ---
156
  with gr.Blocks() as demo:
157
- gr.Markdown("# Basic Agent Evaluation Runner with SmolCodeAgent")
158
- gr.Markdown(
159
- """
160
- **Instructions:**
161
- 1. Clone this space, modify the agent logic and tools.
162
- 2. Log in to your Hugging Face account using the button below.
163
- 3. Click 'Run Evaluation & Submit All Answers' to run the agent on all questions and submit.
164
- ---
165
- **Note:**
166
- Submissions may take time while the agent processes questions.
167
- """
168
- )
169
-
170
- login_btn = gr.LoginButton()
171
  run_button = gr.Button("Run Evaluation & Submit All Answers")
172
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
173
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
174
 
175
- # We need to pass the login profile to the function
176
- run_button.click(
177
- fn=run_and_submit_all,
178
- inputs=[login_btn],
179
- outputs=[status_output, results_table]
180
- )
181
 
182
  if __name__ == "__main__":
183
- print("\n" + "-"*30 + " App Starting " + "-"*30)
184
- space_host_startup = os.getenv("SPACE_HOST")
185
- space_id_startup = os.getenv("SPACE_ID")
186
-
187
- if space_host_startup:
188
- print(f"✅ SPACE_HOST found: {space_host_startup}")
189
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
190
- else:
191
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
192
-
193
- if space_id_startup:
194
- print(f"✅ SPACE_ID found: {space_id_startup}")
195
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
196
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
197
- else:
198
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
199
-
200
- print("-"*(60 + len(" App Starting ")) + "\n")
201
-
202
- print("Launching Gradio Interface for Basic Agent Evaluation...")
203
- demo.launch(debug=True, share=False)
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ from smolagents import CodeAgent, DuckDuckGoSearchTool
6
+ from smolagents.models import OpenAIServerModel
 
7
 
8
+ # Constants
 
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
+ # === Define the Smol Agent ===
12
+ class MyAgent:
13
+ def __init__(self):
14
+ self.model = OpenAIServerModel() # Assumes OpenAI-compatible API via env vars
15
+ self.agent = CodeAgent(
16
+ tools=[DuckDuckGoSearchTool()],
17
+ model=self.model,
18
+ system_message="""You are a general AI assistant. I will ask you a question.
19
+ Report your thoughts, and finish your answer with the following template:
20
  FINAL ANSWER: [YOUR FINAL ANSWER].
21
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list
22
+ of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""
23
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  def __call__(self, question: str) -> str:
26
+ return self.agent.run(question)
 
 
 
 
 
 
 
 
 
27
 
28
+ # === Submission Logic ===
29
  def run_and_submit_all(profile: gr.OAuthProfile | None):
30
  space_id = os.getenv("SPACE_ID")
 
31
  if profile:
32
  username = profile.username
33
  print(f"User logged in: {username}")
34
  else:
 
35
  return "Please Login to Hugging Face with the button.", None
36
 
37
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
38
+ questions_url = f"{DEFAULT_API_URL}/questions"
39
+ submit_url = f"{DEFAULT_API_URL}/submit"
40
 
 
41
  try:
42
+ agent = MyAgent()
43
  except Exception as e:
 
44
  return f"Error initializing agent: {e}", None
45
 
 
 
 
46
  # Fetch Questions
 
47
  try:
48
+ res = requests.get(questions_url, timeout=15)
49
+ res.raise_for_status()
50
+ questions_data = res.json()
 
 
 
 
51
  except Exception as e:
52
+ return f"Failed to fetch questions: {e}", None
 
53
 
 
54
  results_log = []
55
  answers_payload = []
56
+
57
  for item in questions_data:
58
  task_id = item.get("task_id")
59
+ question = item.get("question")
60
+ if not task_id or question is None:
 
61
  continue
62
  try:
63
+ answer = agent(question)
64
+ results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": answer})
65
+ answers_payload.append({"task_id": task_id, "submitted_answer": answer})
66
  except Exception as e:
67
+ results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": f"ERROR: {e}"})
 
68
 
69
  if not answers_payload:
70
+ return "No answers generated.", pd.DataFrame(results_log)
 
71
 
72
+ submission_data = {
73
+ "username": username,
74
+ "agent_code": agent_code,
75
+ "answers": answers_payload
76
+ }
77
 
 
78
  try:
79
+ res = requests.post(submit_url, json=submission_data, timeout=60)
80
+ res.raise_for_status()
81
+ result_data = res.json()
82
+ summary = (
83
  f"Submission Successful!\n"
84
  f"User: {result_data.get('username')}\n"
85
+ f"Score: {result_data.get('score', '?')}% "
86
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')})\n"
87
+ f"Message: {result_data.get('message', '')}"
88
  )
89
+ return summary, pd.DataFrame(results_log)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  except Exception as e:
91
+ return f"Submission failed: {e}", pd.DataFrame(results_log)
 
 
 
 
92
 
93
+ # === Gradio UI ===
94
  with gr.Blocks() as demo:
95
+ gr.Markdown("# Agent Evaluation Runner (SmolAgents)")
96
+ gr.Markdown("""
97
+ **Instructions:**
98
+ 1. Clone this space and customize your agent.
99
+ 2. Log in with Hugging Face.
100
+ 3. Click 'Run Evaluation' to answer and submit.
101
+ """)
102
+
103
+ gr.LoginButton()
 
 
 
 
 
104
  run_button = gr.Button("Run Evaluation & Submit All Answers")
105
+ status_output = gr.Textbox(label="Status", lines=4, interactive=False)
106
+ results_table = gr.DataFrame(label="Results", wrap=True)
107
 
108
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
 
 
109
 
110
  if __name__ == "__main__":
111
+ print("Launching...")
112
+ demo.launch(debug=True)