Vlad Iliescu commited on
Commit
ed82353
·
1 Parent(s): 11dcea3

feat: initial very ugly submission code (still prettier than the default tho)

Browse files
Files changed (2) hide show
  1. .gitignore +3 -0
  2. notebooks/01-vi-questions.ipynb +246 -0
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .env
2
+ downloaded_files
3
+ .idea
notebooks/01-vi-questions.ipynb ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "id": "initial_id",
6
+ "metadata": {
7
+ "collapsed": true
8
+ },
9
+ "source": [
10
+ "import json\n",
11
+ "from pathlib import Path\n",
12
+ "\n",
13
+ "import pandas as pd\n",
14
+ "import requests\n",
15
+ "from dotenv import dotenv_values\n",
16
+ "\n",
17
+ "test_api_base = \"https://agents-course-unit4-scoring.hf.space\"\n",
18
+ "\n",
19
+ "def get_random_question():\n",
20
+ " url = f\"{test_api_base}/random-question\"\n",
21
+ "\n",
22
+ "\n",
23
+ " try:\n",
24
+ " # Fetch the random question\n",
25
+ " response = requests.get(url, timeout=10)\n",
26
+ " response.raise_for_status()\n",
27
+ " question_data = response.json()\n",
28
+ "\n",
29
+ " # Check if there's an associated file to download\n",
30
+ " if question_data.get(\"file_name\") and question_data.get(\"task_id\"):\n",
31
+ " task_id = question_data[\"task_id\"]\n",
32
+ " file_url = f\"{test_api_base}/files/{task_id}\"\n",
33
+ "\n",
34
+ " # Create a directory for downloaded files if it doesn't exist\n",
35
+ " download_dir = Path(\"downloaded_files\")\n",
36
+ " download_dir.mkdir(exist_ok=True)\n",
37
+ "\n",
38
+ " # Download the file\n",
39
+ " file_response = requests.get(file_url, timeout=30)\n",
40
+ " file_response.raise_for_status()\n",
41
+ "\n",
42
+ " # Get filename from content-disposition header or use task_id\n",
43
+ " content_disposition = file_response.headers.get('content-disposition', '')\n",
44
+ " if 'filename=' in content_disposition:\n",
45
+ " filename = content_disposition.split('filename=')[1].strip('\"')\n",
46
+ " else:\n",
47
+ " # Default filename with extension based on content-type\n",
48
+ " content_type = file_response.headers.get('content-type', '')\n",
49
+ " ext = '.bin' # default\n",
50
+ " if 'excel' in content_type or 'spreadsheet' in content_type:\n",
51
+ " ext = '.xlsx'\n",
52
+ " elif 'csv' in content_type:\n",
53
+ " ext = '.csv'\n",
54
+ " elif 'json' in content_type:\n",
55
+ " ext = '.json'\n",
56
+ " elif 'text' in content_type:\n",
57
+ " ext = '.txt'\n",
58
+ " filename = f\"{task_id}{ext}\"\n",
59
+ "\n",
60
+ " file_path = download_dir / filename\n",
61
+ "\n",
62
+ " # Save the file\n",
63
+ " with open(file_path, 'wb') as f:\n",
64
+ " f.write(file_response.content)\n",
65
+ "\n",
66
+ " # Add the file path to the question data\n",
67
+ " question_data['downloaded_file_path'] = str(file_path)\n",
68
+ " print(f\"Downloaded file to: {file_path}\")\n",
69
+ "\n",
70
+ " return question_data\n",
71
+ "\n",
72
+ " except requests.exceptions.RequestException as e:\n",
73
+ " print(f\"Error fetching question: {e}\")\n",
74
+ " return None\n",
75
+ " except json.JSONDecodeError as e:\n",
76
+ " print(f\"Error parsing JSON response: {e}\")\n",
77
+ " return None\n",
78
+ " except Exception as e:\n",
79
+ " print(f\"Unexpected error: {e}\")\n",
80
+ " return None"
81
+ ],
82
+ "outputs": [],
83
+ "execution_count": null
84
+ },
85
+ {
86
+ "metadata": {},
87
+ "cell_type": "code",
88
+ "source": [
89
+ "# if __name__ == \"__main__\":\n",
90
+ "question = get_random_question()\n",
91
+ "if question:\n",
92
+ " print(f\"Task ID: {question.get('task_id')}\")\n",
93
+ " print(f\"Question: {question.get('question')}\")\n",
94
+ " print(f\"Level: {question.get('Level')}\")\n",
95
+ " if 'downloaded_file_path' in question:\n",
96
+ " print(f\"Downloaded file: {question['downloaded_file_path']}\")"
97
+ ],
98
+ "id": "55d7941445304e9b",
99
+ "outputs": [],
100
+ "execution_count": null
101
+ },
102
+ {
103
+ "metadata": {},
104
+ "cell_type": "code",
105
+ "source": [
106
+ "from smolagents import AzureOpenAIServerModel, CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool\n",
107
+ "\n",
108
+ "config = dotenv_values()\n",
109
+ "\n",
110
+ "model = AzureOpenAIServerModel(\n",
111
+ " model_id=config[\"AZURE_OPENAI_CHAT_MODEL\"],\n",
112
+ " api_key=config[\"AZURE_OPENAI_API_KEY\"],\n",
113
+ " api_version=config[\"AZURE_OPENAI_API_VERSION\"],\n",
114
+ " azure_endpoint=config[\"AZURE_OPENAI_API_BASE\"],\n",
115
+ ")\n",
116
+ "\n",
117
+ "agent = CodeAgent(tools=[], model=model, max_steps=10, verbosity_level=0)"
118
+ ],
119
+ "id": "99393f634f21563f",
120
+ "outputs": [],
121
+ "execution_count": null
122
+ },
123
+ {
124
+ "metadata": {},
125
+ "cell_type": "code",
126
+ "source": "agent.run(question.get(\"question\"))",
127
+ "id": "e761157828f8ebb1",
128
+ "outputs": [],
129
+ "execution_count": null
130
+ },
131
+ {
132
+ "metadata": {},
133
+ "cell_type": "code",
134
+ "source": "",
135
+ "id": "47901ff79b3bed13",
136
+ "outputs": [],
137
+ "execution_count": null
138
+ },
139
+ {
140
+ "metadata": {},
141
+ "cell_type": "markdown",
142
+ "source": "## Question Processing",
143
+ "id": "1290570b730fda4"
144
+ },
145
+ {
146
+ "metadata": {},
147
+ "cell_type": "code",
148
+ "source": [
149
+ "response = requests.get(f\"{test_api_base}/questions\", timeout=15)\n",
150
+ "response.raise_for_status()\n",
151
+ "\n",
152
+ "questions_data = response.json()"
153
+ ],
154
+ "id": "9f6fe414bc8fb090",
155
+ "outputs": [],
156
+ "execution_count": null
157
+ },
158
+ {
159
+ "metadata": {},
160
+ "cell_type": "code",
161
+ "source": "questions_data",
162
+ "id": "ca50d2e29bfbc34a",
163
+ "outputs": [],
164
+ "execution_count": null
165
+ },
166
+ {
167
+ "metadata": {},
168
+ "cell_type": "code",
169
+ "source": [
170
+ "\n",
171
+ "def run_agents(questions_data: list[{}]):\n",
172
+ " answers = []\n",
173
+ " results_log = []\n",
174
+ " for question_data in questions_data:\n",
175
+ " task_id = question_data.get(\"task_id\")\n",
176
+ " question_text = question_data.get(\"question\")\n",
177
+ " level = question_data.get(\"Level\")\n",
178
+ " print(f\"Task ID: {task_id}, Question: {question_text}, Level: {level}\")\n",
179
+ "\n",
180
+ " answer = agent.run(task=question_text)\n",
181
+ " print(answer)\n",
182
+ "\n",
183
+ " answers.append({\"task_id\": task_id, \"submitted_answer\": answer})\n",
184
+ " results_log.append({\"Task ID\": task_id, \"Question\": question_text, \"Answer\": answer})\n",
185
+ "\n",
186
+ " submission_data = {\n",
187
+ " \"username\": \"vladi\",\n",
188
+ " \"agent_code\": \"https://huggingface.co/spaces/vladi/AgentsGAIAFun\",\n",
189
+ " \"answers\": answers\n",
190
+ " }\n",
191
+ "\n",
192
+ " return submission_data, results_log\n",
193
+ "\n",
194
+ "def submit_answers(submission_data: dict):\n",
195
+ " print(f\"Submitting {len(submission_data['answers'])} answers\")\n",
196
+ "\n",
197
+ " response = requests.post(f\"{test_api_base}/submit\", json=submission_data, timeout=60)\n",
198
+ " response.raise_for_status()\n",
199
+ " result_data = response.json()\n",
200
+ "\n",
201
+ " return result_data\n",
202
+ "\n",
203
+ "\n",
204
+ "submission_data, results_log = run_agents(questions_data[:4])\n",
205
+ "results_df = pd.DataFrame(results_log)\n",
206
+ "\n",
207
+ "result = submit_answers(submission_data)\n",
208
+ "\n",
209
+ "print(results_df)\n",
210
+ "print(result)\n"
211
+ ],
212
+ "id": "74bce95503481798",
213
+ "outputs": [],
214
+ "execution_count": null
215
+ },
216
+ {
217
+ "metadata": {},
218
+ "cell_type": "code",
219
+ "source": "results_df",
220
+ "id": "57e1c5515e9bf8a1",
221
+ "outputs": [],
222
+ "execution_count": null
223
+ }
224
+ ],
225
+ "metadata": {
226
+ "kernelspec": {
227
+ "display_name": "Python 3",
228
+ "language": "python",
229
+ "name": "python3"
230
+ },
231
+ "language_info": {
232
+ "codemirror_mode": {
233
+ "name": "ipython",
234
+ "version": 2
235
+ },
236
+ "file_extension": ".py",
237
+ "mimetype": "text/x-python",
238
+ "name": "python",
239
+ "nbconvert_exporter": "python",
240
+ "pygments_lexer": "ipython2",
241
+ "version": "2.7.6"
242
+ }
243
+ },
244
+ "nbformat": 4,
245
+ "nbformat_minor": 5
246
+ }