Spaces:
Sleeping
Sleeping
File size: 13,318 Bytes
0ba39a4 42874ea 0ba39a4 42874ea 0ba39a4 42874ea 0ba39a4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | {
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "0659aeaf",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/rex/envs/hf_311/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI\n",
"import os\n",
"\n",
"os.environ['HF_TOKEN'] = 'hf_'\n",
"llm = HuggingFaceInferenceAPI(model_name='Qwen/Qwen2.5-Coder-32B-Instruct')"
]
},
{
"cell_type": "markdown",
"id": "833fba3e",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 8,
"id": "312823b0",
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core.agent.workflow import AgentWorkflow\n",
"from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI\n",
"from llama_index.tools.wikipedia import WikipediaToolSpec\n",
"from llama_index.core.tools import FunctionTool\n",
"from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec\n",
"from llama_index.tools.code_interpreter import CodeInterpreterToolSpec\n",
"from tools import read_python_file_tool, audio_to_text_tool, read_xlsx_file_tool, read_png_file_tool\n",
"\n",
"# (Keep Constants as is)\n",
"# --- Constants ---\n",
"DEFAULT_API_URL = \"https://agents-course-unit4-scoring.hf.space\"\n",
"FETCH_QUESTIONS_LOCAL = True\n",
"\n",
"system_prompt=\"\"\"You are a general AI assistant. I will ask you a question. Report your thoughts, and return\n",
"your answer.\n",
"\n",
"YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of\n",
"numbers and/or strings.\n",
"If you are asked for a number, don’t use comma to write your number neither use units such as $ or percent\n",
"sign unless specified otherwise.\n",
"If you are asked for a string, don’t use articles, neither abbreviations (e.g. for cities), and write the digits in\n",
"plain text unless specified otherwise.\n",
"If you are asked for a comma separated list, apply the above rules depending of whether the element to be put\n",
"in the list is a number or a string.\n",
"\n",
"Here is the question: \n",
"\"\"\"\n",
"\n",
"llm = HuggingFaceInferenceAPI(model_name='Qwen/Qwen3-32B', num_output=10000, temperature=0.01)\n",
"wikipedia_tools = WikipediaToolSpec().to_tool_list()\n",
"search_tool = FunctionTool.from_defaults(DuckDuckGoSearchToolSpec().duckduckgo_full_search)\n",
"agent = AgentWorkflow.from_tools_or_functions(\n",
" tools_or_functions=[search_tool, read_python_file_tool, audio_to_text_tool, read_xlsx_file_tool, read_png_file_tool] + wikipedia_tools,\n",
" llm=llm,\n",
" #system_prompt=system_prompt,\n",
" verbose=False,\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "9813de74",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Right\n"
]
}
],
"source": [
"item = {\n",
" \"task_id\": \"2d83110e-a098-4ebb-9987-066c06fa42d0\",\n",
" \"question\": \".rewsna eht sa \\\"tfel\\\" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI\",\n",
" \"Level\": \"1\",\n",
" \"file_name\": \"\"\n",
" }\n",
"question_text = item.get(\"question\")\n",
"prompt = system_prompt + question_text\n",
"if item.get(\"file_name\", \"\") != \"\":\n",
" file_path = os.path.join(\"files\", item.get(\"file_name\", \"\"))\n",
" prompt += f'\\nHere is the attached file you might need to answer the question: files/{file_path}'\n",
"r = await agent.run(prompt)\n",
"print(r.response.blocks[0].text)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "da5b2551",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ian Rose\n"
]
}
],
"source": [
"import re\n",
"\n",
"question = 'Who nominated the only Featured Article on English Wikipedia about a dinosaur that was promoted in November 2016?'\n",
"r = await agent.run(system_prompt + question)\n",
"print(r.response.blocks[0].text)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "24ceb99e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bartłomiej\n"
]
}
],
"source": [
"import re\n",
"\n",
"question = 'Who did the actor who played Ray in the Polish-language version of Everybody Loves Raymond play in Magda M.? Give only the first name.'\n",
"r = await agent.run(system_prompt + question)\n",
"print(r.response.blocks[0].text)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "31f8d4b6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The chess position image file is missing or inaccessible. Please provide the correct file path or describe the position.\n"
]
}
],
"source": [
"item = {\n",
" \"task_id\": \"cca530fc-4052-43b2-b130-b30968d8aa44\",\n",
" \"question\": \"Review the chess position provided in the image. It is black's turn. Provide the correct next move for black which guarantees a win. Please provide your response in algebraic notation.\",\n",
" \"Level\": \"1\",\n",
" \"file_name\": \"cca530fc-4052-43b2-b130-b30968d8aa44.png\"\n",
" }\n",
"\n",
"question_text = item.get(\"question\")\n",
"prompt = system_prompt + question_text\n",
"if item.get(\"file_name\", \"\") != \"\":\n",
" file_path = os.path.join(\"files\", item.get(\"file_name\", \"\"))\n",
" prompt += f'\\nHere is the attached file you might need to answer the question: files/{file_path}'\n",
"r = await agent.run(prompt)\n",
"print(r.response.blocks[0].text)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "f342112e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
}
],
"source": [
"item = {\n",
" \"task_id\": \"f918266a-b3e0-4914-865d-4faa564f1aef\",\n",
" \"question\": \"What is the final numeric output from the attached Python code?\",\n",
" \"Level\": \"1\",\n",
" \"file_name\": \"f918266a-b3e0-4914-865d-4faa564f1aef.py\"\n",
" }\n",
"\n",
"question_text = item.get(\"question\")\n",
"prompt = system_prompt + question_text\n",
"if item.get(\"file_name\", \"\") != \"\":\n",
" prompt += f'\\nHere is the attached file you might need to answer the question: files/{item.get(\"file_name\", \"\")}'\n",
"r = await agent.run(prompt)\n",
"print(r.response.blocks[0].text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "247038e8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cornstarch, granulated sugar, freshly squeezed lemon juice, pure vanilla extract, ripe strawberries\n"
]
}
],
"source": [
"item = {\n",
" \"task_id\": \"99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3\",\n",
" \"question\": \"Hi, I'm making a pie but I could use some help with my shopping list. I have everything I need for the crust, but I'm not sure about the filling. I got the recipe from my friend Aditi, but she left it as a voice memo and the speaker on my phone is buzzing so I can't quite make out what she's saying. Could you please listen to the recipe and list all of the ingredients that my friend described? I only want the ingredients for the filling, as I have everything I need to make my favorite pie crust. I've attached the recipe as Strawberry pie.mp3.\\n\\nIn your response, please only list the ingredients, not any measurements. So if the recipe calls for \\\"a pinch of salt\\\" or \\\"two cups of ripe strawberries\\\" the ingredients on the list would be \\\"salt\\\" and \\\"ripe strawberries\\\".\\n\\nPlease format your response as a comma separated list of ingredients. Also, please alphabetize the ingredients.\",\n",
" \"Level\": \"1\",\n",
" \"file_name\": \"99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3.mp3\"\n",
" }\n",
"\n",
"question_text = item.get(\"question\")\n",
"prompt = system_prompt + question_text\n",
"if item.get(\"file_name\", \"\") != \"\":\n",
" prompt += f'\\nHere is the attched file you might need to answer the question: files/{item.get(\"file_name\", \"\")}'\n",
"r = await agent.run(prompt)\n",
"print(r.response.blocks[0].text)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "59110ca7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"132,133,134,197,245\n"
]
}
],
"source": [
"item = {\n",
" \"task_id\": \"1f975693-876d-457b-a649-393859e79bf3\",\n",
" \"question\": \"Hi, I was out sick from my classes on Friday, so I'm trying to figure out what I need to study for my Calculus mid-term next week. My friend from class sent me an audio recording of Professor Willowbrook giving out the recommended reading for the test, but my headphones are broken :(\\n\\nCould you please listen to the recording for me and tell me the page numbers I'm supposed to go over? I've attached a file called Homework.mp3 that has the recording. Please provide just the page numbers as a comma-delimited list. And please provide the list in ascending order.\",\n",
" \"Level\": \"1\",\n",
" \"file_name\": \"1f975693-876d-457b-a649-393859e79bf3.mp3\"\n",
" }\n",
"\n",
"question_text = item.get(\"question\")\n",
"prompt = system_prompt + question_text\n",
"if item.get(\"file_name\", \"\") != \"\":\n",
" prompt += f'\\nHere is the additional file you might need to answer the question: files/{item.get(\"file_name\", \"\")}'\n",
"r = await agent.run(prompt)\n",
"print(r.response.blocks[0].text)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3e61988b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"89706.00\n"
]
}
],
"source": [
"item = {\n",
" \"task_id\": \"7bd855d8-463d-4ed5-93ca-5fe35145f733\",\n",
" \"question\": \"The attached Excel file contains the sales of menu items for a local fast-food chain. What were the total sales that the chain made from food (not including drinks)? Express your answer in USD with two decimal places.\",\n",
" \"Level\": \"1\",\n",
" \"file_name\": \"7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsx\"\n",
" }\n",
"\n",
"question_text = item.get(\"question\")\n",
"prompt = system_prompt + question_text\n",
"if item.get(\"file_name\", \"\") != \"\":\n",
" prompt += f'\\nHere is the additional file you might need to answer the question: files/{item.get(\"file_name\", \"\")}'\n",
"r = await agent.run(prompt)\n",
"print(r.response.blocks[0].text)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "ecc8d8be",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'132,133,134,197,245'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"submitted_answer = \"\"\"<think>\n",
"Okay, let me see. The user needs the page numbers from the audio recording for their Calculus mid-term. I used the audio_to_text tool to convert the MP3 file to text. The transcription mentions pages 245, 197, 132, 133, and 134. Now I need to list these in ascending order. Let me check the numbers again.\n",
"\n",
"Pages mentioned are 132, 133, 134, 197, and 245. Wait, the user wants them in ascending order. Let me sort them: 132, 133, 134, 197, 245. That's correct. The answer should be a comma-separated list without any extra text. Just the numbers in order. I need to make sure there are no duplicates and that each page is listed once. The user also wants them in ascending order, so I'll arrange them from smallest to largest. Yep, that's right. So the final answer is 132,133,134,197,245.\n",
"</think>\n",
"\n",
"132,133,134,197,245\"\"\"\n",
"\n",
"submitted_answer.split(\"</think>\")[-1].strip()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "hf_311",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|