cowrycode commited on
Commit
eef5eca
·
verified ·
1 Parent(s): dbf8fd9

Update app_old.py

Browse files
Files changed (1) hide show
  1. app_old.py +314 -1
app_old.py CHANGED
@@ -1 +1,314 @@
1
- dsfv
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import requests
4
+ import inspect
5
+ import pandas as pd
6
+
7
+ import asyncio
8
+ import nest_asyncio
9
+ from typing import List, Dict, Any
10
+ from llama_index.core.agent import ReActAgent
11
+ from llama_index.core.agent.workflow import AgentWorkflow
12
+ from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
13
+ from youtube_tool import youtube_transcript_tool, youtube_transcript_snippet_tool
14
+ #from multiple_tools import round_to_two_decimals_tool, text_inverter_tool, google_web_search_tool, wikipedia_search_tool
15
+ from multiple_tools import round_to_two_decimals_tool, text_inverter_tool, google_web_search_tool, wikipedia_search_tool, transcribe_audio_tool, excel_food_sales_sum_tool, parse_file_and_summarize_tool, solve_chess_image_tool, vegetable_classifier_tool
16
+ from agent import smart_agent
17
+ from llama_index.llms.openai import OpenAI
18
+ import re
19
+ #-----------------------------------------------------------------
20
+
21
+
22
+ # (Keep Constants as is)
23
+ # --- Constants ---
24
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
25
+ HF_key = os.getenv("HF_TOKEN")
26
+ OpenAI_key = os.getenv("OPEN_AI_TOKEN")
27
+
28
+ # --- Basic Agent Definition ---
29
+
30
+ # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
31
+ class BasicAgent:
32
+ def __init__(self):
33
+ print("BasicAgent initialized. . . .")
34
+
35
+ #self.llm = OpenAI(model="gpt-4o-mini", temperature=0.2, api_key=OpenAI_key)
36
+ # self.system_prompt = (
37
+ # "You are a helpful AI assistant completing GAIA benchmark tasks.\n"
38
+ # "You MUST use the tools provided when needed.\n"
39
+ # "If you already have enough information, respond directly with:\n"
40
+ # "<answer>\n"
41
+ # "Once you output '<answer>', stop reasoning and do not call any tool.\n"
42
+ # )
43
+ self.system_prompt = (
44
+ "You are a helpful assistant tasked with answering questions using a set of tools.\n"
45
+ "Your final answer must strictly follow this format:\n"
46
+ "FINAL ANSWER: [ANSWER]\n"
47
+ "Only write the answer in that exact format. Do not explain anything. Do not include any other text. \n"
48
+ "If you are provided with a similar question and its final answer, and the current question is **exactly the same**, then simply return the same final answer without using any tools. \n"
49
+ "Only use tools if the current question is different from the similar one. \n"
50
+ "Examples: \n"
51
+ "- FINAL ANSWER: FunkMonk \n"
52
+ "- FINAL ANSWER: Paris \n"
53
+ "- FINAL ANSWER: 128 \n"
54
+ " \n"
55
+ "Once you output 'FINAL ANSWER', stop reasoning and do not call any tool.\n"
56
+ "If you do not follow this format exactly, your response will be considered incorrect. \n"
57
+ )
58
+ self.llm = HuggingFaceInferenceAPI(
59
+ model_name="deepseek-ai/DeepSeek-R1-0528",
60
+ token=HF_key,
61
+ provider="auto"
62
+ )
63
+ #self.llm = OpenAI(model="gpt-4o", temperature=0.1, api_key=OpenAI_key)
64
+ # self.system_prompt = (
65
+ # "You are a helpful AI assistant completing GAIA benchmark tasks.\n"
66
+ # "You MUST use the tools provided to answer the user's question. Do not answer from your own knowledge.\n"
67
+ # "Carefully analyze the question to determine the most appropriate tool to use.\n"
68
+ # "Here are guidelines for using the tools:\n"
69
+ # "- Use 'wikipedia_search_tool' to find factual information about topics, events, people, etc. (e.g., 'Use wikipedia_search to find the population of France').\n"
70
+ # "- Use 'youtube_transcript_tool' to extract transcripts from YouTube videos when the question requires understanding the video content. (e.g., 'Use youtube_transcript to summarize the key points of this video').\n"
71
+ # "- Use 'transcribe_audio_tool' to transcribe uploaded audio files. (e.g., 'Use audio_transcriber to get the text from this audio recording').\n"
72
+ # "- Use 'solve_chess_image_tool' to analyze and solve chess puzzles from images. (e.g., 'Use chess_image_solver to determine the best move in this chess position').\n"
73
+ # "- Use 'parse_file_and_summarize_tool' to parse and analyze data from Excel or CSV files. (e.g., 'Use file_parser to calculate the average sales from this data').\n"
74
+ # "- Use 'vegetable_classifier_tool' to classify a list of food items and extract only the vegetables. (e.g., 'Use vegetable_classifier_2022 to get a list of the vegetables in this grocery list').\n"
75
+ # "- Use 'excel_food_sales_sum_tool' to extract total food sales from excel files. (e.g., 'Use excel_food_sales_sum to calculate the total food sales').\n"
76
+ # "- Use 'google_web_search_tool' to find factual information about topics, events, people, from the web if not spificied to be fund in wikipedia etc. (e.g., 'find the population of France').\n"
77
+ # "Do NOT guess or make up answers. If a tool cannot provide the answer, truthfully respond that you were unable to find the information.\n"
78
+ # "Use the tools to research or calculate the answer.\n"
79
+ # "If a tool fails, explain the reason for the failure instead of hallucinating an answer.\n"
80
+ # "Provide concise and direct answers as requested in the questions. Do not add extra information unless explicitly asked for.\n"
81
+ # "For example, if asked for a number, return only the number. If asked for a list, return only the list.\n"
82
+ # )
83
+ self.agent = AgentWorkflow.from_tools_or_functions(
84
+ [
85
+ wikipedia_search_tool, youtube_transcript_tool, youtube_transcript_snippet_tool, round_to_two_decimals_tool, text_inverter_tool, google_web_search_tool,transcribe_audio_tool, excel_food_sales_sum_tool, parse_file_and_summarize_tool, solve_chess_image_tool, vegetable_classifier_tool
86
+ ],
87
+ llm=self.llm,
88
+ system_prompt=self.system_prompt,
89
+ )
90
+ def extract_answer(self, text: str) -> str:
91
+ match = re.search(r"(?<=<answer>)(.*?)(?=</answer>)", text)
92
+ return match.group(1) if match else ""
93
+
94
+ async def run(self, question: str) -> str:
95
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
96
+ # answer = await self.agent.run(question)
97
+ answer = await self.agent.run(
98
+ f"{question}\n\nIf you have enough information, respond with a concise final answer.",
99
+ max_iterations=10
100
+ )
101
+ return str(answer)
102
+ #return self.extract_answer(str(answer));
103
+ # if hasattr(answer, "output"):
104
+ # print(f"Agent returning answer: {answer}")
105
+ # return str(answer.output)
106
+ # else:
107
+ # print(f"Agent returning answer: {answer}")
108
+ # return str(answer)
109
+
110
+ def __call__(self, question: str) -> str:
111
+ return asyncio.run(self.run(question))
112
+
113
+
114
+
115
+
116
+ def run_and_submit_all( profile: gr.OAuthProfile | None):
117
+ """
118
+ Fetches all questions, runs the BasicAgent on them, submits all answers,
119
+ and displays the results.
120
+ """
121
+ # --- Determine HF Space Runtime URL and Repo URL ---
122
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
123
+
124
+ if profile:
125
+ username= f"{profile.username}"
126
+ print(f"User logged in: {username}")
127
+ else:
128
+ print("User not logged in.")
129
+ return "Please Login to Hugging Face with the button.", None
130
+
131
+ api_url = DEFAULT_API_URL
132
+ questions_url = f"{api_url}/questions"
133
+ submit_url = f"{api_url}/submit"
134
+
135
+ # 1. Instantiate Agent ( modify this part to create your agent)
136
+ try:
137
+ agent = BasicAgent()
138
+ except Exception as e:
139
+ print(f"Error instantiating agent: {e}")
140
+ return f"Error initializing agent: {e}", None
141
+ #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)
142
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
143
+ print(agent_code)
144
+
145
+ # 2. Fetch Questions
146
+ print(f"Fetching questions from: {questions_url}")
147
+ try:
148
+ response = requests.get(questions_url, timeout=15)
149
+ response.raise_for_status()
150
+ questions_data = response.json()
151
+ if not questions_data:
152
+ print("Fetched questions list is empty.")
153
+ return "Fetched questions list is empty or invalid format.", None
154
+ print(f"Fetched {len(questions_data)} questions.")
155
+ except requests.exceptions.RequestException as e:
156
+ print(f"Error fetching questions: {e}")
157
+ return f"Error fetching questions: {e}", None
158
+ except requests.exceptions.JSONDecodeError as e:
159
+ print(f"Error decoding JSON response from questions endpoint: {e}")
160
+ print(f"Response text: {response.text[:500]}")
161
+ return f"Error decoding server response for questions: {e}", None
162
+ except Exception as e:
163
+ print(f"An unexpected error occurred fetching questions: {e}")
164
+ return f"An unexpected error occurred fetching questions: {e}", None
165
+
166
+ # 3. Run your Agent
167
+ results_log = []
168
+ # answers_payload = []
169
+ # print(f"Running agent on {len(questions_data)} questions...")
170
+ # for item in questions_data:
171
+ # task_id = item.get("task_id")
172
+ # question_text = item.get("question")
173
+ # if not task_id or question_text is None:
174
+ # print(f"Skipping item with missing task_id or question: {item}")
175
+ # continue
176
+ # try:
177
+ # submitted_answer = agent(question_text)
178
+ # answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
179
+ # results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
180
+ # except Exception as e:
181
+ # print(f"Error running agent on task {task_id}: {e}")
182
+ # results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
183
+
184
+ # if not answers_payload:
185
+ # print("Agent did not produce any answers to submit.")
186
+ # return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
187
+
188
+ #3A
189
+
190
+ async def run_all_questions(questions_data):
191
+ answers_payload = []
192
+ for item in questions_data:
193
+ task_id = item.get("task_id")
194
+ question_text = item.get("question")
195
+ if not task_id or question_text is None:
196
+ print(f"Skipping item with missing task_id or question: {item}")
197
+ continue
198
+ try:
199
+ answer = await agent.run(question_text) # await coroutine
200
+ answers_payload.append({"task_id": task_id, "submitted_answer": answer})
201
+ print(f"Answered Task {task_id}:: {answer}")
202
+ except Exception as e:
203
+ answers_payload.append({"task_id": task_id, "submitted_answer": f"AGENT ERROR: {e}"})
204
+ print(f"Error on Task {task_id}: {e}")
205
+ return answers_payload
206
+
207
+ answers_payload = asyncio.run(run_all_questions(questions_data))
208
+ #answers_payload = run_all_questions(questions_data)
209
+
210
+ #3B
211
+
212
+ # 4. Prepare Submission
213
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
214
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
215
+ print(status_update)
216
+
217
+ # 5. Submit
218
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
219
+ try:
220
+ response = requests.post(submit_url, json=submission_data, timeout=60)
221
+ response.raise_for_status()
222
+ result_data = response.json()
223
+ final_status = (
224
+ f"Submission Successful!\n"
225
+ f"User: {result_data.get('username')}\n"
226
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
227
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
228
+ f"Message: {result_data.get('message', 'No message received.')}"
229
+ )
230
+ print("Submission successful.")
231
+ results_df = pd.DataFrame(results_log)
232
+ return final_status, results_df
233
+ except requests.exceptions.HTTPError as e:
234
+ error_detail = f"Server responded with status {e.response.status_code}."
235
+ try:
236
+ error_json = e.response.json()
237
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
238
+ except requests.exceptions.JSONDecodeError:
239
+ error_detail += f" Response: {e.response.text[:500]}"
240
+ status_message = f"Submission Failed: {error_detail}"
241
+ print(status_message)
242
+ results_df = pd.DataFrame(results_log)
243
+ return status_message, results_df
244
+ except requests.exceptions.Timeout:
245
+ status_message = "Submission Failed: The request timed out."
246
+ print(status_message)
247
+ results_df = pd.DataFrame(results_log)
248
+ return status_message, results_df
249
+ except requests.exceptions.RequestException as e:
250
+ status_message = f"Submission Failed: Network error - {e}"
251
+ print(status_message)
252
+ results_df = pd.DataFrame(results_log)
253
+ return status_message, results_df
254
+ except Exception as e:
255
+ status_message = f"An unexpected error occurred during submission: {e}"
256
+ print(status_message)
257
+ results_df = pd.DataFrame(results_log)
258
+ return status_message, results_df
259
+
260
+
261
+ # --- Build Gradio Interface using Blocks ---
262
+ with gr.Blocks() as demo:
263
+ gr.Markdown("# Basic Agent Evaluation Runner")
264
+ gr.Markdown(
265
+ """
266
+ **Instructions:**
267
+
268
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
269
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
270
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
271
+
272
+ ---
273
+ **Disclaimers:**
274
+ 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).
275
+ 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.
276
+ """
277
+ )
278
+
279
+ gr.LoginButton()
280
+
281
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
282
+
283
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
284
+ # Removed max_rows=10 from DataFrame constructor
285
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
286
+
287
+ run_button.click(
288
+ fn=run_and_submit_all,
289
+ outputs=[status_output, results_table]
290
+ )
291
+
292
+ if __name__ == "__main__":
293
+ print("\n" + "-"*30 + " App Starting " + "-"*30)
294
+ # Check for SPACE_HOST and SPACE_ID at startup for information
295
+ space_host_startup = os.getenv("SPACE_HOST")
296
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
297
+
298
+ if space_host_startup:
299
+ print(f"✅ SPACE_HOST found: {space_host_startup}")
300
+ print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
301
+ else:
302
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
303
+
304
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
305
+ print(f"✅ SPACE_ID found: {space_id_startup}")
306
+ print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
307
+ print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
308
+ else:
309
+ print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
310
+
311
+ print("-"*(60 + len(" App Starting ")) + "\n")
312
+
313
+ print("Launching Gradio Interface for Basic Agent Evaluation...")
314
+ demo.launch(debug=True, share=False)