zineb-chgari commited on
Commit
908bb13
·
verified ·
1 Parent(s): 4b8b9e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +242 -26
app.py CHANGED
@@ -8,34 +8,250 @@ from smolagents import ToolCallingAgent
8
  from smolagents import DuckDuckGoSearchTool
9
  from smolagents import Tool
10
  import traceback
11
- from huggingface_hub import InferenceClient, HfApi
12
- from huggingface_hub.utils import HfHubHTTPError
13
  # (Keep Constants as is)
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
  print("Token loaded:", bool(os.getenv("chatbotagenthf")))
17
- from huggingface_hub import InferenceClient, HfApi
18
- api_key=os.getenv("chatbotagenthf")
19
- api = HfApi()
20
- client = InferenceClient(token=api_key)
21
-
22
- # Example list of chat/instruction models to test
23
- models = [
24
- "mistralai/Mistral-7B-Instruct-v0.3",
25
- "HuggingFaceH4/zephyr-7b-beta",
26
- "google/gemma-2b-it",
27
- "tiiuae/falcon-7b-instruct",
28
- "meta-llama/Llama-3.1-8B-Instruct"
29
- ]
30
-
31
- client = InferenceClient()
32
- api = HfApi()
33
-
34
- for model in models:
35
- print(f"🔎 Testing {model}...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  try:
37
- # just check if model exists and is accessible
38
- api.model_info(model, token="your_HF_token_here")
39
- print(f"✅ Accessible: {model}")
40
- except HfHubHTTPError as e:
41
- print(f" Not accessible: {model} -> {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  from smolagents import DuckDuckGoSearchTool
9
  from smolagents import Tool
10
  import traceback
 
 
11
  # (Keep Constants as is)
12
  # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
  print("Token loaded:", bool(os.getenv("chatbotagenthf")))
15
+ custom_prompt = """
16
+ You are an intelligent AI agent participating in the GALA (Generative Agent Learning Assignment).
17
+ Your goal is to answer each question accurately, concisely, and factually.
18
+
19
+ Instructions:
20
+ 1. Analyze the user's question carefully.
21
+ 2. If you can answer it directly from your own knowledge, do so in one or two sentences.
22
+ 3. If the question requires recent, factual, or numeric data (e.g. "current CEO", "latest version", "in 2025", etc.), use the DuckDuckGoSearchTool to find the answer.
23
+ 4. When using a tool, be precise in your query and summarize the result clearly.
24
+ 5. Always provide clean, readable text without markdown or citations unless explicitly requested.
25
+ 6. If a question is ambiguous, briefly explain your assumption before answering.
26
+ 7. Never refuse to answer unless the question is unrelated to the task or violates safety rules.
27
+
28
+ Format:
29
+ - Respond directly with the final answer only.
30
+ - Do not include reasoning steps or technical metadata.
31
+
32
+ Example behaviors:
33
+ User: Who is the CEO of OpenAI?
34
+ → Use search tool → "The CEO of OpenAI is Sam Altman."
35
+
36
+ User: What is the capital of Japan?
37
+ → Direct answer → "The capital of Japan is Tokyo."
38
+
39
+ Remember: you are being evaluated for correctness and clarity.
40
+ """
41
+
42
+ @Tool
43
+ def calculatorTool(query:str) -> str:
44
+ try:
45
+ result=eval(query)
46
+ return str(result)
47
+ except Exception as e:
48
+ return f"error while calculating {e}"
49
+ CalculatorTool=Tool(
50
+ name="calculator",
51
+ description="it calculates basic arithmetics",
52
+ func=calculatorTool
53
+ )
54
+ class BasicAgent:
55
+ def __init__(self):
56
+ try:
57
+ api_key=os.getenv("chatbotagenthf")
58
+ model =InferenceClientModel(model_id="HuggingFaceH4/zephyr-7b-beta",token=api_key)
59
+
60
+ self.agent = ToolCallingAgent(
61
+ tools=[DuckDuckGoSearchTool()],
62
+ model=model
63
+ )
64
+
65
+ print("✅ Agent initialized successfully.")
66
+
67
+ except Exception as e:
68
+ print(f"❌ Error initializing agent: {e}")
69
+ traceback.print_exc()
70
+ raise e
71
+
72
+ def __call__(self, question: str) -> str:
73
+ try:
74
+ print(f"\n➡️ Agent received question: {question[:80]}")
75
+ agent_answer = self.agent.run(question)
76
+ print(f"✅ Agent returning answer: {agent_answer}")
77
+ return agent_answer
78
+ except Exception as e:
79
+ print(f"❌ Agent encountered an error: {e}")
80
+ traceback.print_exc()
81
+ return f"Error generating answer: {e}"
82
+
83
+
84
+ def run_and_submit_all( profile: gr.OAuthProfile | None):
85
+ """
86
+ Fetches all questions, runs the BasicAgent on them, submits all answers,
87
+ and displays the results.
88
+ """
89
+ # --- Determine HF Space Runtime URL and Repo URL ---
90
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
91
+
92
+ if profile:
93
+ username= f"{profile.username}"
94
+ print(f"User logged in: {username}")
95
+ else:
96
+ print("User not logged in.")
97
+ return "Please Login to Hugging Face with the button.", None
98
+
99
+ api_url = DEFAULT_API_URL
100
+ questions_url = f"{api_url}/questions"
101
+ submit_url = f"{api_url}/submit"
102
+
103
+ # 1. Instantiate Agent ( modify this part to create your agent)
104
+ try:
105
+ agent = BasicAgent()
106
+ except Exception as e:
107
+ print(f"Error instantiating agent: {e}")
108
+ return f"Error initializing agent: {e}", None
109
+ # 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)
110
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
111
+ print(agent_code)
112
+
113
+ # 2. Fetch Questions
114
+ print(f"Fetching questions from: {questions_url}")
115
  try:
116
+ response = requests.get(questions_url, timeout=15)
117
+ response.raise_for_status()
118
+ questions_data = response.json()
119
+ if not questions_data:
120
+ print("Fetched questions list is empty.")
121
+ return "Fetched questions list is empty or invalid format.", None
122
+ print(f"Fetched {len(questions_data)} questions.")
123
+ except requests.exceptions.RequestException as e:
124
+ print(f"Error fetching questions: {e}")
125
+ return f"Error fetching questions: {e}", None
126
+ except requests.exceptions.JSONDecodeError as e:
127
+ print(f"Error decoding JSON response from questions endpoint: {e}")
128
+ print(f"Response text: {response.text[:500]}")
129
+ return f"Error decoding server response for questions: {e}", None
130
+ except Exception as e:
131
+ print(f"An unexpected error occurred fetching questions: {e}")
132
+ return f"An unexpected error occurred fetching questions: {e}", None
133
+
134
+ # 3. Run your Agent
135
+ results_log = []
136
+ answers_payload = []
137
+ print(f"Running agent on {len(questions_data)} questions...")
138
+ for item in questions_data:
139
+ task_id = item.get("task_id")
140
+ question_text = item.get("question")
141
+ if not task_id or question_text is None:
142
+ print(f"Skipping item with missing task_id or question: {item}")
143
+ continue
144
+ try:
145
+ submitted_answer = agent(question_text)
146
+ answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
147
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
148
+ except Exception as e:
149
+ print(f"Error running agent on task {task_id}: {e}")
150
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
151
+
152
+ if not answers_payload:
153
+ print("Agent did not produce any answers to submit.")
154
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
155
+
156
+ # 4. Prepare Submission
157
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
158
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
159
+ print(status_update)
160
+
161
+ # 5. Submit
162
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
163
+ try:
164
+ response = requests.post(submit_url, json=submission_data, timeout=60)
165
+ response.raise_for_status()
166
+ result_data = response.json()
167
+ final_status = (
168
+ f"Submission Successful!\n"
169
+ f"User: {result_data.get('username')}\n"
170
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
171
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
172
+ f"Message: {result_data.get('message', 'No message received.')}"
173
+ )
174
+ print("Submission successful.")
175
+ results_df = pd.DataFrame(results_log)
176
+ return final_status, results_df
177
+ except requests.exceptions.HTTPError as e:
178
+ error_detail = f"Server responded with status {e.response.status_code}."
179
+ try:
180
+ error_json = e.response.json()
181
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
182
+ except requests.exceptions.JSONDecodeError:
183
+ error_detail += f" Response: {e.response.text[:500]}"
184
+ status_message = f"Submission Failed: {error_detail}"
185
+ print(status_message)
186
+ results_df = pd.DataFrame(results_log)
187
+ return status_message, results_df
188
+ except requests.exceptions.Timeout:
189
+ status_message = "Submission Failed: The request timed out."
190
+ print(status_message)
191
+ results_df = pd.DataFrame(results_log)
192
+ return status_message, results_df
193
+ except requests.exceptions.RequestException as e:
194
+ status_message = f"Submission Failed: Network error - {e}"
195
+ print(status_message)
196
+ results_df = pd.DataFrame(results_log)
197
+ return status_message, results_df
198
+ except Exception as e:
199
+ status_message = f"An unexpected error occurred during submission: {e}"
200
+ print(status_message)
201
+ results_df = pd.DataFrame(results_log)
202
+ return status_message, results_df
203
+
204
+
205
+ # --- Build Gradio Interface using Blocks ---
206
+ with gr.Blocks() as demo:
207
+ gr.Markdown("# Basic Agent Evaluation Runner")
208
+ gr.Markdown(
209
+ """
210
+ **Instructions:**
211
+
212
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
213
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
214
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
215
+
216
+ ---
217
+ **Disclaimers:**
218
+ 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).
219
+ 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.
220
+ """
221
+ )
222
+
223
+ gr.LoginButton()
224
+
225
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
226
+
227
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
228
+ # Removed max_rows=10 from DataFrame constructor
229
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
230
+
231
+ run_button.click(
232
+ fn=run_and_submit_all,
233
+ outputs=[status_output, results_table]
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
+ print("Launching Gradio Interface for Basic Agent Evaluation...")
257
+ demo.launch(debug=True, share=False)