Darsh1234Tayal commited on
Commit
c3409a2
·
verified ·
1 Parent(s): e00de7e

Upload folder using huggingface_hub

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gradio/certificate.pem +31 -0
  2. .ipynb_checkpoints/Untitled-checkpoint.ipynb +6 -0
  3. .ipynb_checkpoints/Untitled1-checkpoint.ipynb +6 -0
  4. .ipynb_checkpoints/advanced project-checkpoint.ipynb +6 -0
  5. .ipynb_checkpoints/day3-checkpoint.ipynb +615 -0
  6. .ipynb_checkpoints/day4-checkpoint.ipynb +848 -0
  7. .ipynb_checkpoints/optimized-checkpoint.cpp +65 -0
  8. README.md +3 -9
  9. Untitled.ipynb +207 -0
  10. Untitled1.ipynb +251 -0
  11. advanced project.ipynb +225 -0
  12. community-contributions/.ipynb_checkpoints/Code_Converter(Added_Java_Support)(Open_Ai_Only)-checkpoint.ipynb +795 -0
  13. community-contributions/Code_Converter(Added_Java_Support)(Open_Ai_Only).ipynb +824 -0
  14. community-contributions/Day 3 using gemini.ipynb +493 -0
  15. community-contributions/code_documentation_generator.ipynb +433 -0
  16. community-contributions/day4-gemini-included.ipynb +908 -0
  17. community-contributions/day4_extra_deepseek_and_hf_inference_provider_added.ipynb +0 -0
  18. community-contributions/day4_with_inference_provider.ipynb +925 -0
  19. community-contributions/day5-homework.ipynb +565 -0
  20. community-contributions/doc_string_exercise/README.md +29 -0
  21. community-contributions/doc_string_exercise/data/original_file.py +19 -0
  22. community-contributions/doc_string_exercise/generate_doc_string.py +85 -0
  23. community-contributions/doc_string_exercise/utils.py +147 -0
  24. community-contributions/ems_week4_docupy.ipynb +869 -0
  25. community-contributions/ems_week4_trading.ipynb +528 -0
  26. community-contributions/unit-tests-generator.ipynb +462 -0
  27. community-contributions/w4_lang_converter.py +346 -0
  28. community-contributions/week4-day4-challenge.ipynb +689 -0
  29. community-contributions/week4-day5-code-commenter.ipynb +401 -0
  30. day3.ipynb +785 -0
  31. day4.ipynb +848 -0
  32. optimized.cpp +65 -0
  33. requirements.txt +2 -0
  34. simple.cpp +7 -0
  35. week6/.ipynb_checkpoints/Untitled-checkpoint.ipynb +6 -0
  36. week6/.ipynb_checkpoints/day1-checkpoint.ipynb +437 -0
  37. week6/.ipynb_checkpoints/day2-checkpoint.ipynb +632 -0
  38. week6/.ipynb_checkpoints/day3-checkpoint.ipynb +926 -0
  39. week6/.ipynb_checkpoints/day4-checkpoint.ipynb +406 -0
  40. week6/.ipynb_checkpoints/day4-results-checkpoint.ipynb +0 -0
  41. week6/.ipynb_checkpoints/day5-checkpoint.ipynb +555 -0
  42. week6/.ipynb_checkpoints/day5-results-checkpoint.ipynb +0 -0
  43. week6/.ipynb_checkpoints/items-checkpoint.py +103 -0
  44. week6/.ipynb_checkpoints/lite-checkpoint.ipynb +424 -0
  45. week6/.ipynb_checkpoints/loaders-checkpoint.py +81 -0
  46. week6/.ipynb_checkpoints/similarity search-checkpoint.ipynb +6 -0
  47. week6/.ipynb_checkpoints/untitled-checkpoint.txt +0 -0
  48. week6/Untitled.ipynb +103 -0
  49. week6/__pycache__/items.cpython-311.pyc +0 -0
  50. week6/community-contributions/ems_week6_day4_gemini_results.ipynb +313 -0
.gradio/certificate.pem ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
3
+ TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
4
+ cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
5
+ WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
6
+ ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
7
+ MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
8
+ h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
9
+ 0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
10
+ A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
11
+ T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH
12
+ B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC
13
+ B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SHzUv
14
+ KBds0pjBqAlkd25HN7rOrFleaJ1/ctaJxQZBKT5ZPt0m9STJEadao0xAH0ahmbWn
15
+ OlFuhjuefXKnEgV4We0+UXgVCwOPjdAvBbI+e0ocS3MFEvzG6uBQE3xDk3SzynTn
16
+ jh8BCNAw1FtxNrQHusEwMFxIt4I7mKZ9YIqioymCzLq9gwQbooMDQaHWBfEbwrbw
17
+ qHyGO0aoSCqI3Haadr8faqU9GY/rOPNk3sgrDQoo//fb4hVC1CLQJ13hef4Y53CI
18
+ rU7m2Ys6xt0nUW7/vGT1M0NPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNV
19
+ HRMBAf8EBTADAQH/MB0GA1UdDgQWBBR5tFnme7bl5AFzgAiIyBpY9umbbjANBgkq
20
+ hkiG9w0BAQsFAAOCAgEAVR9YqbyyqFDQDLHYGmkgJykIrGF1XIpu+ILlaS/V9lZL
21
+ ubhzEFnTIZd+50xx+7LSYK05qAvqFyFWhfFQDlnrzuBZ6brJFe+GnY+EgPbk6ZGQ
22
+ 3BebYhtF8GaV0nxvwuo77x/Py9auJ/GpsMiu/X1+mvoiBOv/2X/qkSsisRcOj/KK
23
+ NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5
24
+ ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur
25
+ TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC
26
+ jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc
27
+ oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq
28
+ 4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA
29
+ mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
30
+ emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
31
+ -----END CERTIFICATE-----
.ipynb_checkpoints/Untitled-checkpoint.ipynb ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [],
3
+ "metadata": {},
4
+ "nbformat": 4,
5
+ "nbformat_minor": 5
6
+ }
.ipynb_checkpoints/Untitled1-checkpoint.ipynb ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [],
3
+ "metadata": {},
4
+ "nbformat": 4,
5
+ "nbformat_minor": 5
6
+ }
.ipynb_checkpoints/advanced project-checkpoint.ipynb ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [],
3
+ "metadata": {},
4
+ "nbformat": 4,
5
+ "nbformat_minor": 5
6
+ }
.ipynb_checkpoints/day3-checkpoint.ipynb ADDED
@@ -0,0 +1,615 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Code Generator\n",
9
+ "\n",
10
+ "The requirement: use a Frontier model to generate high performance C++ code from Python code\n"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "markdown",
15
+ "id": "d5ccb926-7b49-44a4-99ab-8ef20b5778c0",
16
+ "metadata": {},
17
+ "source": [
18
+ "<table style=\"margin: 0; text-align: left;\">\n",
19
+ " <tr>\n",
20
+ " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
21
+ " <img src=\"../resources.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
22
+ " </td>\n",
23
+ " <td>\n",
24
+ " <h2 style=\"color:#f71;\">Reminder: fetch latest code</h2>\n",
25
+ " <span style=\"color:#f71;\">I'm continually improving these labs, adding more examples and exercises.\n",
26
+ " At the start of each week, it's worth checking you have the latest code.<br/>\n",
27
+ " First do a <a href=\"https://chatgpt.com/share/6734e705-3270-8012-a074-421661af6ba9\">git pull and merge your changes as needed</a>. Any problems? Try asking ChatGPT to clarify how to merge - or contact me!<br/><br/>\n",
28
+ " After you've pulled the code, from the llm_engineering directory, in an Anaconda prompt (PC) or Terminal (Mac), run:<br/>\n",
29
+ " <code>conda env update --f environment.yml --prune</code><br/>\n",
30
+ " Or if you used virtualenv rather than Anaconda, then run this from your activated environment in a Powershell (PC) or Terminal (Mac):<br/>\n",
31
+ " <code>pip install -r requirements.txt</code>\n",
32
+ " <br/>Then restart the kernel (Kernel menu >> Restart Kernel and Clear Outputs Of All Cells) to pick up the changes.\n",
33
+ " </span>\n",
34
+ " </td>\n",
35
+ " </tr>\n",
36
+ "</table>"
37
+ ]
38
+ },
39
+ {
40
+ "cell_type": "markdown",
41
+ "id": "d90e04a2-5b8a-4fd5-9db8-27c02f033313",
42
+ "metadata": {},
43
+ "source": [
44
+ "<table style=\"margin: 0; text-align: left;\">\n",
45
+ " <tr>\n",
46
+ " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
47
+ " <img src=\"../important.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
48
+ " </td>\n",
49
+ " <td>\n",
50
+ " <h1 style=\"color:#900;\">Important Note</h1>\n",
51
+ " <span style=\"color:#900;\">\n",
52
+ " In this lab, I use GPT-4o and Claude-3.5-Sonnet, which are the slightly higher priced models. The costs are still low, but if you'd prefer to keep costs ultra low, please make the suggested switches to the models (3 cells down from here).\n",
53
+ " </span>\n",
54
+ " </td>\n",
55
+ " </tr>\n",
56
+ "</table>"
57
+ ]
58
+ },
59
+ {
60
+ "cell_type": "code",
61
+ "execution_count": null,
62
+ "id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
63
+ "metadata": {},
64
+ "outputs": [],
65
+ "source": [
66
+ "# imports\n",
67
+ "\n",
68
+ "import os\n",
69
+ "import io\n",
70
+ "import sys\n",
71
+ "from dotenv import load_dotenv\n",
72
+ "from openai import OpenAI\n",
73
+ "import google.generativeai\n",
74
+ "import anthropic\n",
75
+ "from IPython.display import Markdown, display, update_display\n",
76
+ "import gradio as gr\n",
77
+ "import subprocess"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "execution_count": null,
83
+ "id": "4f672e1c-87e9-4865-b760-370fa605e614",
84
+ "metadata": {},
85
+ "outputs": [],
86
+ "source": [
87
+ "# environment\n",
88
+ "\n",
89
+ "load_dotenv(override=True)\n",
90
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
91
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')"
92
+ ]
93
+ },
94
+ {
95
+ "cell_type": "code",
96
+ "execution_count": null,
97
+ "id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
98
+ "metadata": {},
99
+ "outputs": [],
100
+ "source": [
101
+ "# initialize\n",
102
+ "# NOTE - option to use ultra-low cost models by uncommenting last 2 lines\n",
103
+ "\n",
104
+ "openai = OpenAI()\n",
105
+ "claude = anthropic.Anthropic()\n",
106
+ "OPENAI_MODEL = \"gpt-4o\"\n",
107
+ "CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\"\n",
108
+ "\n",
109
+ "# Want to keep costs ultra-low? Uncomment these lines:\n",
110
+ "# OPENAI_MODEL = \"gpt-4o-mini\"\n",
111
+ "# CLAUDE_MODEL = \"claude-3-haiku-20240307\""
112
+ ]
113
+ },
114
+ {
115
+ "cell_type": "code",
116
+ "execution_count": null,
117
+ "id": "6896636f-923e-4a2c-9d6c-fac07828a201",
118
+ "metadata": {},
119
+ "outputs": [],
120
+ "source": [
121
+ "system_message = \"You are an assistant that reimplements Python code in high performance C++ for an M1 Mac. \"\n",
122
+ "system_message += \"Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments. \"\n",
123
+ "system_message += \"The C++ response needs to produce an identical output in the fastest possible time.\""
124
+ ]
125
+ },
126
+ {
127
+ "cell_type": "code",
128
+ "execution_count": null,
129
+ "id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
130
+ "metadata": {},
131
+ "outputs": [],
132
+ "source": [
133
+ "def user_prompt_for(python):\n",
134
+ " user_prompt = \"Rewrite this Python code in C++ with the fastest possible implementation that produces identical output in the least time. \"\n",
135
+ " user_prompt += \"Respond only with C++ code; do not explain your work other than a few comments. \"\n",
136
+ " user_prompt += \"Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\\n\\n\"\n",
137
+ " user_prompt += python\n",
138
+ " return user_prompt"
139
+ ]
140
+ },
141
+ {
142
+ "cell_type": "code",
143
+ "execution_count": null,
144
+ "id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
145
+ "metadata": {},
146
+ "outputs": [],
147
+ "source": [
148
+ "def messages_for(python):\n",
149
+ " return [\n",
150
+ " {\"role\": \"system\", \"content\": system_message},\n",
151
+ " {\"role\": \"user\", \"content\": user_prompt_for(python)}\n",
152
+ " ]"
153
+ ]
154
+ },
155
+ {
156
+ "cell_type": "code",
157
+ "execution_count": null,
158
+ "id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
159
+ "metadata": {},
160
+ "outputs": [],
161
+ "source": [
162
+ "# write to a file called optimized.cpp\n",
163
+ "\n",
164
+ "def write_output(cpp):\n",
165
+ " code = cpp.replace(\"```cpp\",\"\").replace(\"```\",\"\")\n",
166
+ " with open(\"optimized.cpp\", \"w\") as f:\n",
167
+ " f.write(code)"
168
+ ]
169
+ },
170
+ {
171
+ "cell_type": "code",
172
+ "execution_count": null,
173
+ "id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
174
+ "metadata": {},
175
+ "outputs": [],
176
+ "source": [
177
+ "def optimize_gpt(python): \n",
178
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n",
179
+ " reply = \"\"\n",
180
+ " for chunk in stream:\n",
181
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
182
+ " reply += fragment\n",
183
+ " print(fragment, end='', flush=True)\n",
184
+ " write_output(reply)"
185
+ ]
186
+ },
187
+ {
188
+ "cell_type": "code",
189
+ "execution_count": null,
190
+ "id": "7cd84ad8-d55c-4fe0-9eeb-1895c95c4a9d",
191
+ "metadata": {},
192
+ "outputs": [],
193
+ "source": [
194
+ "def optimize_claude(python):\n",
195
+ " result = claude.messages.stream(\n",
196
+ " model=CLAUDE_MODEL,\n",
197
+ " max_tokens=2000,\n",
198
+ " system=system_message,\n",
199
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
200
+ " )\n",
201
+ " reply = \"\"\n",
202
+ " with result as stream:\n",
203
+ " for text in stream.text_stream:\n",
204
+ " reply += text\n",
205
+ " print(text, end=\"\", flush=True)\n",
206
+ " write_output(reply)"
207
+ ]
208
+ },
209
+ {
210
+ "cell_type": "code",
211
+ "execution_count": null,
212
+ "id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
213
+ "metadata": {},
214
+ "outputs": [],
215
+ "source": [
216
+ "pi = \"\"\"\n",
217
+ "import time\n",
218
+ "\n",
219
+ "def calculate(iterations, param1, param2):\n",
220
+ " result = 1.0\n",
221
+ " for i in range(1, iterations+1):\n",
222
+ " j = i * param1 - param2\n",
223
+ " result -= (1/j)\n",
224
+ " j = i * param1 + param2\n",
225
+ " result += (1/j)\n",
226
+ " return result\n",
227
+ "\n",
228
+ "start_time = time.time()\n",
229
+ "result = calculate(100_000_000, 4, 1) * 4\n",
230
+ "end_time = time.time()\n",
231
+ "\n",
232
+ "print(f\"Result: {result:.12f}\")\n",
233
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
234
+ "\"\"\""
235
+ ]
236
+ },
237
+ {
238
+ "cell_type": "code",
239
+ "execution_count": null,
240
+ "id": "7fe1cd4b-d2c5-4303-afed-2115a3fef200",
241
+ "metadata": {},
242
+ "outputs": [],
243
+ "source": [
244
+ "exec(pi)"
245
+ ]
246
+ },
247
+ {
248
+ "cell_type": "code",
249
+ "execution_count": null,
250
+ "id": "105db6f9-343c-491d-8e44-3a5328b81719",
251
+ "metadata": {},
252
+ "outputs": [],
253
+ "source": [
254
+ "optimize_gpt(pi)"
255
+ ]
256
+ },
257
+ {
258
+ "cell_type": "code",
259
+ "execution_count": null,
260
+ "id": "bf26ee95-0c77-491d-9a91-579a1e96a8a3",
261
+ "metadata": {},
262
+ "outputs": [],
263
+ "source": [
264
+ "exec(pi)"
265
+ ]
266
+ },
267
+ {
268
+ "cell_type": "markdown",
269
+ "id": "bf8f8018-f64d-425c-a0e1-d7862aa9592d",
270
+ "metadata": {},
271
+ "source": [
272
+ "# Compiling C++ and executing\n",
273
+ "\n",
274
+ "This next cell contains the command to compile a C++ file on my M1 Mac. \n",
275
+ "It compiles the file `optimized.cpp` into an executable called `optimized` \n",
276
+ "Then it runs the program called `optimized`\n",
277
+ "\n",
278
+ "In the next lab (day4), a student has contributed a full solution that compiles to efficient code on Mac, PC and Linux!\n",
279
+ "\n",
280
+ "You can wait for this, or you can google (or ask ChatGPT!) for how to do this on your platform, then replace the lines below.\n",
281
+ "If you're not comfortable with this step, you can skip it for sure - I'll show you exactly how it performs on my Mac.\n",
282
+ "\n",
283
+ "\n",
284
+ "OR alternatively: student Sandeep K.G. points out that you can run Python and C++ code online to test it out that way. Thank you Sandeep! \n",
285
+ "> Not an exact comparison but you can still get the idea of performance difference.\n",
286
+ "> For example here: https://www.programiz.com/cpp-programming/online-compiler/"
287
+ ]
288
+ },
289
+ {
290
+ "cell_type": "code",
291
+ "execution_count": null,
292
+ "id": "4194e40c-04ab-4940-9d64-b4ad37c5bb40",
293
+ "metadata": {},
294
+ "outputs": [],
295
+ "source": [
296
+ "# Compile C++ and run the executable\n",
297
+ "\n",
298
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
299
+ "!./optimized"
300
+ ]
301
+ },
302
+ {
303
+ "cell_type": "code",
304
+ "execution_count": null,
305
+ "id": "983a11fe-e24d-4c65-8269-9802c5ef3ae6",
306
+ "metadata": {},
307
+ "outputs": [],
308
+ "source": [
309
+ "optimize_claude(pi)"
310
+ ]
311
+ },
312
+ {
313
+ "cell_type": "code",
314
+ "execution_count": null,
315
+ "id": "d5a766f9-3d23-4bb4-a1d4-88ec44b61ddf",
316
+ "metadata": {},
317
+ "outputs": [],
318
+ "source": [
319
+ "# Repeat for Claude - again, use the right approach for your platform\n",
320
+ "\n",
321
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
322
+ "!./optimized"
323
+ ]
324
+ },
325
+ {
326
+ "cell_type": "code",
327
+ "execution_count": null,
328
+ "id": "c3b497b3-f569-420e-b92e-fb0f49957ce0",
329
+ "metadata": {},
330
+ "outputs": [],
331
+ "source": [
332
+ "python_hard = \"\"\"# Be careful to support large number sizes\n",
333
+ "\n",
334
+ "def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
335
+ " value = seed\n",
336
+ " while True:\n",
337
+ " value = (a * value + c) % m\n",
338
+ " yield value\n",
339
+ " \n",
340
+ "def max_subarray_sum(n, seed, min_val, max_val):\n",
341
+ " lcg_gen = lcg(seed)\n",
342
+ " random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
343
+ " max_sum = float('-inf')\n",
344
+ " for i in range(n):\n",
345
+ " current_sum = 0\n",
346
+ " for j in range(i, n):\n",
347
+ " current_sum += random_numbers[j]\n",
348
+ " if current_sum > max_sum:\n",
349
+ " max_sum = current_sum\n",
350
+ " return max_sum\n",
351
+ "\n",
352
+ "def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
353
+ " total_sum = 0\n",
354
+ " lcg_gen = lcg(initial_seed)\n",
355
+ " for _ in range(20):\n",
356
+ " seed = next(lcg_gen)\n",
357
+ " total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
358
+ " return total_sum\n",
359
+ "\n",
360
+ "# Parameters\n",
361
+ "n = 10000 # Number of random numbers\n",
362
+ "initial_seed = 42 # Initial seed for the LCG\n",
363
+ "min_val = -10 # Minimum value of random numbers\n",
364
+ "max_val = 10 # Maximum value of random numbers\n",
365
+ "\n",
366
+ "# Timing the function\n",
367
+ "import time\n",
368
+ "start_time = time.time()\n",
369
+ "result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
370
+ "end_time = time.time()\n",
371
+ "\n",
372
+ "print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
373
+ "print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
374
+ "\"\"\""
375
+ ]
376
+ },
377
+ {
378
+ "cell_type": "code",
379
+ "execution_count": null,
380
+ "id": "dab5e4bc-276c-4555-bd4c-12c699d5e899",
381
+ "metadata": {},
382
+ "outputs": [],
383
+ "source": [
384
+ "exec(python_hard)"
385
+ ]
386
+ },
387
+ {
388
+ "cell_type": "code",
389
+ "execution_count": null,
390
+ "id": "e8d24ed5-2c15-4f55-80e7-13a3952b3cb8",
391
+ "metadata": {},
392
+ "outputs": [],
393
+ "source": [
394
+ "optimize_gpt(python_hard)"
395
+ ]
396
+ },
397
+ {
398
+ "cell_type": "code",
399
+ "execution_count": null,
400
+ "id": "e0b3d073-88a2-40b2-831c-6f0c345c256f",
401
+ "metadata": {},
402
+ "outputs": [],
403
+ "source": [
404
+ "# Replace this with the right C++ compile + execute command for your platform\n",
405
+ "\n",
406
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
407
+ "!./optimized"
408
+ ]
409
+ },
410
+ {
411
+ "cell_type": "code",
412
+ "execution_count": null,
413
+ "id": "e9305446-1d0c-4b51-866a-b8c1e299bf5c",
414
+ "metadata": {},
415
+ "outputs": [],
416
+ "source": [
417
+ "optimize_claude(python_hard)"
418
+ ]
419
+ },
420
+ {
421
+ "cell_type": "code",
422
+ "execution_count": null,
423
+ "id": "0c181036-8193-4fdd-aef3-fc513b218d43",
424
+ "metadata": {},
425
+ "outputs": [],
426
+ "source": [
427
+ "# Replace this with the right C++ compile + execute command for your platform\n",
428
+ "\n",
429
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
430
+ "!./optimized"
431
+ ]
432
+ },
433
+ {
434
+ "cell_type": "code",
435
+ "execution_count": null,
436
+ "id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
437
+ "metadata": {},
438
+ "outputs": [],
439
+ "source": [
440
+ "def stream_gpt(python): \n",
441
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n",
442
+ " reply = \"\"\n",
443
+ " for chunk in stream:\n",
444
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
445
+ " reply += fragment\n",
446
+ " yield reply.replace('```cpp\\n','').replace('```','')"
447
+ ]
448
+ },
449
+ {
450
+ "cell_type": "code",
451
+ "execution_count": null,
452
+ "id": "8669f56b-8314-4582-a167-78842caea131",
453
+ "metadata": {},
454
+ "outputs": [],
455
+ "source": [
456
+ "def stream_claude(python):\n",
457
+ " result = claude.messages.stream(\n",
458
+ " model=CLAUDE_MODEL,\n",
459
+ " max_tokens=2000,\n",
460
+ " system=system_message,\n",
461
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
462
+ " )\n",
463
+ " reply = \"\"\n",
464
+ " with result as stream:\n",
465
+ " for text in stream.text_stream:\n",
466
+ " reply += text\n",
467
+ " yield reply.replace('```cpp\\n','').replace('```','')"
468
+ ]
469
+ },
470
+ {
471
+ "cell_type": "code",
472
+ "execution_count": null,
473
+ "id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
474
+ "metadata": {},
475
+ "outputs": [],
476
+ "source": [
477
+ "def optimize(python, model):\n",
478
+ " if model==\"GPT\":\n",
479
+ " result = stream_gpt(python)\n",
480
+ " elif model==\"Claude\":\n",
481
+ " result = stream_claude(python)\n",
482
+ " else:\n",
483
+ " raise ValueError(\"Unknown model\")\n",
484
+ " for stream_so_far in result:\n",
485
+ " yield stream_so_far "
486
+ ]
487
+ },
488
+ {
489
+ "cell_type": "code",
490
+ "execution_count": null,
491
+ "id": "f1ddb38e-6b0a-4c37-baa4-ace0b7de887a",
492
+ "metadata": {},
493
+ "outputs": [],
494
+ "source": [
495
+ "with gr.Blocks() as ui:\n",
496
+ " with gr.Row():\n",
497
+ " python = gr.Textbox(label=\"Python code:\", lines=10, value=python_hard)\n",
498
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
499
+ " with gr.Row():\n",
500
+ " model = gr.Dropdown([\"GPT\", \"Claude\"], label=\"Select model\", value=\"GPT\")\n",
501
+ " convert = gr.Button(\"Convert code\")\n",
502
+ "\n",
503
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
504
+ "\n",
505
+ "ui.launch(inbrowser=True)"
506
+ ]
507
+ },
508
+ {
509
+ "cell_type": "code",
510
+ "execution_count": null,
511
+ "id": "19bf2bff-a822-4009-a539-f003b1651383",
512
+ "metadata": {},
513
+ "outputs": [],
514
+ "source": [
515
+ "def execute_python(code):\n",
516
+ " try:\n",
517
+ " output = io.StringIO()\n",
518
+ " sys.stdout = output\n",
519
+ " exec(code)\n",
520
+ " finally:\n",
521
+ " sys.stdout = sys.__stdout__\n",
522
+ " return output.getvalue()"
523
+ ]
524
+ },
525
+ {
526
+ "cell_type": "code",
527
+ "execution_count": null,
528
+ "id": "77f3ab5d-fcfb-4d3f-8728-9cacbf833ea6",
529
+ "metadata": {},
530
+ "outputs": [],
531
+ "source": [
532
+ "# You'll need to change the code in the try block to compile the C++ code for your platform\n",
533
+ "# I pasted this into Claude's chat UI with a request for it to give me a version for an Intel PC,\n",
534
+ "# and it responded with something that looks perfect - you can try a similar approach for your platform.\n",
535
+ "\n",
536
+ "# M1 Mac version to compile and execute optimized C++ code:\n",
537
+ "\n",
538
+ "def execute_cpp(code):\n",
539
+ " write_output(code)\n",
540
+ " try:\n",
541
+ " compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", \"optimized\", \"optimized.cpp\"]\n",
542
+ " compile_result = subprocess.run(compile_cmd, check=True, text=True, capture_output=True)\n",
543
+ " run_cmd = [\"./optimized\"]\n",
544
+ " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
545
+ " return run_result.stdout\n",
546
+ " except subprocess.CalledProcessError as e:\n",
547
+ " return f\"An error occurred:\\n{e.stderr}\""
548
+ ]
549
+ },
550
+ {
551
+ "cell_type": "code",
552
+ "execution_count": null,
553
+ "id": "9a2274f1-d03b-42c0-8dcc-4ce159b18442",
554
+ "metadata": {},
555
+ "outputs": [],
556
+ "source": [
557
+ "css = \"\"\"\n",
558
+ ".python {background-color: #306998;}\n",
559
+ ".cpp {background-color: #050;}\n",
560
+ "\"\"\""
561
+ ]
562
+ },
563
+ {
564
+ "cell_type": "code",
565
+ "execution_count": null,
566
+ "id": "f1303932-160c-424b-97a8-d28c816721b2",
567
+ "metadata": {},
568
+ "outputs": [],
569
+ "source": [
570
+ "with gr.Blocks(css=css) as ui:\n",
571
+ " gr.Markdown(\"## Convert code from Python to C++\")\n",
572
+ " with gr.Row():\n",
573
+ " python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
574
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
575
+ " with gr.Row():\n",
576
+ " model = gr.Dropdown([\"GPT\", \"Claude\"], label=\"Select model\", value=\"GPT\")\n",
577
+ " with gr.Row():\n",
578
+ " convert = gr.Button(\"Convert code\")\n",
579
+ " with gr.Row():\n",
580
+ " python_run = gr.Button(\"Run Python\")\n",
581
+ " cpp_run = gr.Button(\"Run C++\")\n",
582
+ " with gr.Row():\n",
583
+ " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
584
+ " cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
585
+ "\n",
586
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
587
+ " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
588
+ " cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
589
+ "\n",
590
+ "ui.launch(inbrowser=True)"
591
+ ]
592
+ }
593
+ ],
594
+ "metadata": {
595
+ "kernelspec": {
596
+ "display_name": "Python 3 (ipykernel)",
597
+ "language": "python",
598
+ "name": "python3"
599
+ },
600
+ "language_info": {
601
+ "codemirror_mode": {
602
+ "name": "ipython",
603
+ "version": 3
604
+ },
605
+ "file_extension": ".py",
606
+ "mimetype": "text/x-python",
607
+ "name": "python",
608
+ "nbconvert_exporter": "python",
609
+ "pygments_lexer": "ipython3",
610
+ "version": "3.11.11"
611
+ }
612
+ },
613
+ "nbformat": 4,
614
+ "nbformat_minor": 5
615
+ }
.ipynb_checkpoints/day4-checkpoint.ipynb ADDED
@@ -0,0 +1,848 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Code Generator\n",
9
+ "\n",
10
+ "The requirement: use an Open Source model to generate high performance C++ code from Python code\n",
11
+ "\n",
12
+ "To replicate this, you'll need to set up a HuggingFace endpoint as I do in the video. It's simple to do, and it's quite satisfying to see the results!\n",
13
+ "\n",
14
+ "It's also an important part of your learning; this is the first example of deploying an open source model to be behind an API. We'll return to this in Week 8, but this should plant a seed in your mind for what's involved in moving open source models into production."
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "markdown",
19
+ "id": "22e1567b-33fd-49e7-866e-4b635d15715a",
20
+ "metadata": {},
21
+ "source": [
22
+ "<table style=\"margin: 0; text-align: left;\">\n",
23
+ " <tr>\n",
24
+ " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
25
+ " <img src=\"../important.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
26
+ " </td>\n",
27
+ " <td>\n",
28
+ " <h1 style=\"color:#900;\">Important - Pause Endpoints when not in use</h1>\n",
29
+ " <span style=\"color:#900;\">\n",
30
+ " If you do decide to use HuggingFace endpoints for this project, you should stop or pause the endpoints when you are done to avoid accruing unnecessary running cost. The costs are very low as long as you only run the endpoint when you're using it. Navigate to the HuggingFace endpoint UI <a href=\"https://ui.endpoints.huggingface.co/\">here,</a> open your endpoint, and click Pause to put it on pause so you no longer pay for it. \n",
31
+ "Many thanks to student John L. for raising this.\n",
32
+ "<br/><br/>\n",
33
+ "In week 8 we will use Modal instead of HuggingFace endpoints; with Modal you only pay for the time that you use it and you should get free credits.\n",
34
+ " </span>\n",
35
+ " </td>\n",
36
+ " </tr>\n",
37
+ "</table>"
38
+ ]
39
+ },
40
+ {
41
+ "cell_type": "code",
42
+ "execution_count": null,
43
+ "id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
44
+ "metadata": {},
45
+ "outputs": [],
46
+ "source": [
47
+ "# imports\n",
48
+ "\n",
49
+ "import os\n",
50
+ "import io\n",
51
+ "import sys\n",
52
+ "import json\n",
53
+ "import requests\n",
54
+ "from dotenv import load_dotenv\n",
55
+ "from openai import OpenAI\n",
56
+ "import google.generativeai\n",
57
+ "import anthropic\n",
58
+ "from IPython.display import Markdown, display, update_display\n",
59
+ "import gradio as gr\n",
60
+ "import subprocess"
61
+ ]
62
+ },
63
+ {
64
+ "cell_type": "code",
65
+ "execution_count": null,
66
+ "id": "4f672e1c-87e9-4865-b760-370fa605e614",
67
+ "metadata": {},
68
+ "outputs": [],
69
+ "source": [
70
+ "# environment\n",
71
+ "\n",
72
+ "load_dotenv(override=True)\n",
73
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
74
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
75
+ "os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
76
+ ]
77
+ },
78
+ {
79
+ "cell_type": "code",
80
+ "execution_count": null,
81
+ "id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
82
+ "metadata": {},
83
+ "outputs": [],
84
+ "source": [
85
+ "# initialize\n",
86
+ "\n",
87
+ "openai = OpenAI()\n",
88
+ "claude = anthropic.Anthropic()\n",
89
+ "OPENAI_MODEL = \"gpt-4o\"\n",
90
+ "CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\""
91
+ ]
92
+ },
93
+ {
94
+ "cell_type": "code",
95
+ "execution_count": null,
96
+ "id": "6896636f-923e-4a2c-9d6c-fac07828a201",
97
+ "metadata": {},
98
+ "outputs": [],
99
+ "source": [
100
+ "system_message = \"You are an assistant that reimplements Python code in high performance C++ for an M1 Mac. \"\n",
101
+ "system_message += \"Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments. \"\n",
102
+ "system_message += \"The C++ response needs to produce an identical output in the fastest possible time. Keep implementations of random number generators identical so that results match exactly.\""
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": null,
108
+ "id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
109
+ "metadata": {},
110
+ "outputs": [],
111
+ "source": [
112
+ "def user_prompt_for(python):\n",
113
+ " user_prompt = \"Rewrite this Python code in C++ with the fastest possible implementation that produces identical output in the least time. \"\n",
114
+ " user_prompt += \"Respond only with C++ code; do not explain your work other than a few comments. \"\n",
115
+ " user_prompt += \"Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\\n\\n\"\n",
116
+ " user_prompt += python\n",
117
+ " return user_prompt"
118
+ ]
119
+ },
120
+ {
121
+ "cell_type": "code",
122
+ "execution_count": null,
123
+ "id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
124
+ "metadata": {},
125
+ "outputs": [],
126
+ "source": [
127
+ "def messages_for(python):\n",
128
+ " return [\n",
129
+ " {\"role\": \"system\", \"content\": system_message},\n",
130
+ " {\"role\": \"user\", \"content\": user_prompt_for(python)}\n",
131
+ " ]"
132
+ ]
133
+ },
134
+ {
135
+ "cell_type": "code",
136
+ "execution_count": null,
137
+ "id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
138
+ "metadata": {},
139
+ "outputs": [],
140
+ "source": [
141
+ "# write to a file called optimized.cpp\n",
142
+ "\n",
143
+ "def write_output(cpp):\n",
144
+ " code = cpp.replace(\"```cpp\",\"\").replace(\"```\",\"\")\n",
145
+ " with open(\"optimized.cpp\", \"w\") as f:\n",
146
+ " f.write(code)"
147
+ ]
148
+ },
149
+ {
150
+ "cell_type": "code",
151
+ "execution_count": null,
152
+ "id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
153
+ "metadata": {},
154
+ "outputs": [],
155
+ "source": [
156
+ "def optimize_gpt(python): \n",
157
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n",
158
+ " reply = \"\"\n",
159
+ " for chunk in stream:\n",
160
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
161
+ " reply += fragment\n",
162
+ " print(fragment, end='', flush=True)\n",
163
+ " write_output(reply)"
164
+ ]
165
+ },
166
+ {
167
+ "cell_type": "code",
168
+ "execution_count": null,
169
+ "id": "7cd84ad8-d55c-4fe0-9eeb-1895c95c4a9d",
170
+ "metadata": {},
171
+ "outputs": [],
172
+ "source": [
173
+ "def optimize_claude(python):\n",
174
+ " result = claude.messages.stream(\n",
175
+ " model=CLAUDE_MODEL,\n",
176
+ " max_tokens=2000,\n",
177
+ " system=system_message,\n",
178
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
179
+ " )\n",
180
+ " reply = \"\"\n",
181
+ " with result as stream:\n",
182
+ " for text in stream.text_stream:\n",
183
+ " reply += text\n",
184
+ " print(text, end=\"\", flush=True)\n",
185
+ " write_output(reply)"
186
+ ]
187
+ },
188
+ {
189
+ "cell_type": "code",
190
+ "execution_count": null,
191
+ "id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
192
+ "metadata": {},
193
+ "outputs": [],
194
+ "source": [
195
+ "pi = \"\"\"\n",
196
+ "import time\n",
197
+ "\n",
198
+ "def calculate(iterations, param1, param2):\n",
199
+ " result = 1.0\n",
200
+ " for i in range(1, iterations+1):\n",
201
+ " j = i * param1 - param2\n",
202
+ " result -= (1/j)\n",
203
+ " j = i * param1 + param2\n",
204
+ " result += (1/j)\n",
205
+ " return result\n",
206
+ "\n",
207
+ "start_time = time.time()\n",
208
+ "result = calculate(100_000_000, 4, 1) * 4\n",
209
+ "end_time = time.time()\n",
210
+ "\n",
211
+ "print(f\"Result: {result:.12f}\")\n",
212
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
213
+ "\"\"\""
214
+ ]
215
+ },
216
+ {
217
+ "cell_type": "code",
218
+ "execution_count": null,
219
+ "id": "7fe1cd4b-d2c5-4303-afed-2115a3fef200",
220
+ "metadata": {},
221
+ "outputs": [],
222
+ "source": [
223
+ "exec(pi)"
224
+ ]
225
+ },
226
+ {
227
+ "cell_type": "code",
228
+ "execution_count": null,
229
+ "id": "105db6f9-343c-491d-8e44-3a5328b81719",
230
+ "metadata": {},
231
+ "outputs": [],
232
+ "source": [
233
+ "optimize_gpt(pi)"
234
+ ]
235
+ },
236
+ {
237
+ "cell_type": "code",
238
+ "execution_count": null,
239
+ "id": "bf26ee95-0c77-491d-9a91-579a1e96a8a3",
240
+ "metadata": {},
241
+ "outputs": [],
242
+ "source": [
243
+ "exec(pi)"
244
+ ]
245
+ },
246
+ {
247
+ "cell_type": "code",
248
+ "execution_count": null,
249
+ "id": "4194e40c-04ab-4940-9d64-b4ad37c5bb40",
250
+ "metadata": {},
251
+ "outputs": [],
252
+ "source": [
253
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
254
+ "!./optimized"
255
+ ]
256
+ },
257
+ {
258
+ "cell_type": "code",
259
+ "execution_count": null,
260
+ "id": "983a11fe-e24d-4c65-8269-9802c5ef3ae6",
261
+ "metadata": {},
262
+ "outputs": [],
263
+ "source": [
264
+ "optimize_claude(pi)"
265
+ ]
266
+ },
267
+ {
268
+ "cell_type": "code",
269
+ "execution_count": null,
270
+ "id": "d5a766f9-3d23-4bb4-a1d4-88ec44b61ddf",
271
+ "metadata": {},
272
+ "outputs": [],
273
+ "source": [
274
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
275
+ "!./optimized"
276
+ ]
277
+ },
278
+ {
279
+ "cell_type": "code",
280
+ "execution_count": null,
281
+ "id": "c3b497b3-f569-420e-b92e-fb0f49957ce0",
282
+ "metadata": {},
283
+ "outputs": [],
284
+ "source": [
285
+ "python_hard = \"\"\"# Be careful to support large number sizes\n",
286
+ "\n",
287
+ "def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
288
+ " value = seed\n",
289
+ " while True:\n",
290
+ " value = (a * value + c) % m\n",
291
+ " yield value\n",
292
+ " \n",
293
+ "def max_subarray_sum(n, seed, min_val, max_val):\n",
294
+ " lcg_gen = lcg(seed)\n",
295
+ " random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
296
+ " max_sum = float('-inf')\n",
297
+ " for i in range(n):\n",
298
+ " current_sum = 0\n",
299
+ " for j in range(i, n):\n",
300
+ " current_sum += random_numbers[j]\n",
301
+ " if current_sum > max_sum:\n",
302
+ " max_sum = current_sum\n",
303
+ " return max_sum\n",
304
+ "\n",
305
+ "def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
306
+ " total_sum = 0\n",
307
+ " lcg_gen = lcg(initial_seed)\n",
308
+ " for _ in range(20):\n",
309
+ " seed = next(lcg_gen)\n",
310
+ " total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
311
+ " return total_sum\n",
312
+ "\n",
313
+ "# Parameters\n",
314
+ "n = 10000 # Number of random numbers\n",
315
+ "initial_seed = 42 # Initial seed for the LCG\n",
316
+ "min_val = -10 # Minimum value of random numbers\n",
317
+ "max_val = 10 # Maximum value of random numbers\n",
318
+ "\n",
319
+ "# Timing the function\n",
320
+ "import time\n",
321
+ "start_time = time.time()\n",
322
+ "result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
323
+ "end_time = time.time()\n",
324
+ "\n",
325
+ "print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
326
+ "print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
327
+ "\"\"\""
328
+ ]
329
+ },
330
+ {
331
+ "cell_type": "code",
332
+ "execution_count": null,
333
+ "id": "dab5e4bc-276c-4555-bd4c-12c699d5e899",
334
+ "metadata": {},
335
+ "outputs": [],
336
+ "source": [
337
+ "exec(python_hard)"
338
+ ]
339
+ },
340
+ {
341
+ "cell_type": "code",
342
+ "execution_count": null,
343
+ "id": "e8d24ed5-2c15-4f55-80e7-13a3952b3cb8",
344
+ "metadata": {},
345
+ "outputs": [],
346
+ "source": [
347
+ "optimize_gpt(python_hard)"
348
+ ]
349
+ },
350
+ {
351
+ "cell_type": "code",
352
+ "execution_count": null,
353
+ "id": "e0b3d073-88a2-40b2-831c-6f0c345c256f",
354
+ "metadata": {},
355
+ "outputs": [],
356
+ "source": [
357
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
358
+ "!./optimized"
359
+ ]
360
+ },
361
+ {
362
+ "cell_type": "code",
363
+ "execution_count": null,
364
+ "id": "e9305446-1d0c-4b51-866a-b8c1e299bf5c",
365
+ "metadata": {},
366
+ "outputs": [],
367
+ "source": [
368
+ "optimize_claude(python_hard)"
369
+ ]
370
+ },
371
+ {
372
+ "cell_type": "code",
373
+ "execution_count": null,
374
+ "id": "0c181036-8193-4fdd-aef3-fc513b218d43",
375
+ "metadata": {},
376
+ "outputs": [],
377
+ "source": [
378
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
379
+ "!./optimized"
380
+ ]
381
+ },
382
+ {
383
+ "cell_type": "code",
384
+ "execution_count": null,
385
+ "id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
386
+ "metadata": {},
387
+ "outputs": [],
388
+ "source": [
389
+ "def stream_gpt(python): \n",
390
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n",
391
+ " reply = \"\"\n",
392
+ " for chunk in stream:\n",
393
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
394
+ " reply += fragment\n",
395
+ " yield reply.replace('```cpp\\n','').replace('```','')"
396
+ ]
397
+ },
398
+ {
399
+ "cell_type": "code",
400
+ "execution_count": null,
401
+ "id": "8669f56b-8314-4582-a167-78842caea131",
402
+ "metadata": {},
403
+ "outputs": [],
404
+ "source": [
405
+ "def stream_claude(python):\n",
406
+ " result = claude.messages.stream(\n",
407
+ " model=CLAUDE_MODEL,\n",
408
+ " max_tokens=2000,\n",
409
+ " system=system_message,\n",
410
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
411
+ " )\n",
412
+ " reply = \"\"\n",
413
+ " with result as stream:\n",
414
+ " for text in stream.text_stream:\n",
415
+ " reply += text\n",
416
+ " yield reply.replace('```cpp\\n','').replace('```','')"
417
+ ]
418
+ },
419
+ {
420
+ "cell_type": "code",
421
+ "execution_count": null,
422
+ "id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
423
+ "metadata": {},
424
+ "outputs": [],
425
+ "source": [
426
+ "def optimize(python, model):\n",
427
+ " if model==\"GPT\":\n",
428
+ " result = stream_gpt(python)\n",
429
+ " elif model==\"Claude\":\n",
430
+ " result = stream_claude(python)\n",
431
+ " else:\n",
432
+ " raise ValueError(\"Unknown model\")\n",
433
+ " for stream_so_far in result:\n",
434
+ " yield stream_so_far "
435
+ ]
436
+ },
437
+ {
438
+ "cell_type": "code",
439
+ "execution_count": null,
440
+ "id": "f1ddb38e-6b0a-4c37-baa4-ace0b7de887a",
441
+ "metadata": {},
442
+ "outputs": [],
443
+ "source": [
444
+ "with gr.Blocks() as ui:\n",
445
+ " with gr.Row():\n",
446
+ " python = gr.Textbox(label=\"Python code:\", lines=10, value=python_hard)\n",
447
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
448
+ " with gr.Row():\n",
449
+ " model = gr.Dropdown([\"GPT\", \"Claude\"], label=\"Select model\", value=\"GPT\")\n",
450
+ " convert = gr.Button(\"Convert code\")\n",
451
+ "\n",
452
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
453
+ "\n",
454
+ "ui.launch(inbrowser=True)"
455
+ ]
456
+ },
457
+ {
458
+ "cell_type": "code",
459
+ "execution_count": null,
460
+ "id": "19bf2bff-a822-4009-a539-f003b1651383",
461
+ "metadata": {},
462
+ "outputs": [],
463
+ "source": [
464
+ "def execute_python(code):\n",
465
+ " try:\n",
466
+ " output = io.StringIO()\n",
467
+ " sys.stdout = output\n",
468
+ " exec(code)\n",
469
+ " finally:\n",
470
+ " sys.stdout = sys.__stdout__\n",
471
+ " return output.getvalue()"
472
+ ]
473
+ },
474
+ {
475
+ "cell_type": "code",
476
+ "execution_count": null,
477
+ "id": "77f3ab5d-fcfb-4d3f-8728-9cacbf833ea6",
478
+ "metadata": {},
479
+ "outputs": [],
480
+ "source": [
481
+ "def execute_cpp(code):\n",
482
+ " write_output(code)\n",
483
+ " compiler_cmd = [\"clang++\", \"-O3\", \"-std=c++17\", \"-march=armv8.3-a\", \"-o\", \"optimized\", \"optimized.cpp\"]\n",
484
+ " try:\n",
485
+ " compile_result = subprocess.run(compiler_cmd, check=True, text=True, capture_output=True)\n",
486
+ " run_cmd = [\"./optimized\"]\n",
487
+ " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
488
+ " return run_result.stdout\n",
489
+ " except subprocess.CalledProcessError as e:\n",
490
+ " return f\"An error occurred:\\n{e.stderr}\""
491
+ ]
492
+ },
493
+ {
494
+ "cell_type": "code",
495
+ "execution_count": null,
496
+ "id": "9a2274f1-d03b-42c0-8dcc-4ce159b18442",
497
+ "metadata": {},
498
+ "outputs": [],
499
+ "source": [
500
+ "css = \"\"\"\n",
501
+ ".python {background-color: #306998;}\n",
502
+ ".cpp {background-color: #050;}\n",
503
+ "\"\"\""
504
+ ]
505
+ },
506
+ {
507
+ "cell_type": "code",
508
+ "execution_count": null,
509
+ "id": "f1303932-160c-424b-97a8-d28c816721b2",
510
+ "metadata": {},
511
+ "outputs": [],
512
+ "source": [
513
+ "with gr.Blocks(css=css) as ui:\n",
514
+ " gr.Markdown(\"## Convert code from Python to C++\")\n",
515
+ " with gr.Row():\n",
516
+ " python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
517
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
518
+ " with gr.Row():\n",
519
+ " model = gr.Dropdown([\"GPT\", \"Claude\"], label=\"Select model\", value=\"GPT\")\n",
520
+ " with gr.Row():\n",
521
+ " convert = gr.Button(\"Convert code\")\n",
522
+ " with gr.Row():\n",
523
+ " python_run = gr.Button(\"Run Python\")\n",
524
+ " cpp_run = gr.Button(\"Run C++\")\n",
525
+ " with gr.Row():\n",
526
+ " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
527
+ " cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
528
+ "\n",
529
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
530
+ " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
531
+ " cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
532
+ "\n",
533
+ "ui.launch(inbrowser=True)"
534
+ ]
535
+ },
536
+ {
537
+ "cell_type": "code",
538
+ "execution_count": null,
539
+ "id": "bb8c5b4e-ec51-4f21-b3f8-6aa94fede86d",
540
+ "metadata": {},
541
+ "outputs": [],
542
+ "source": [
543
+ "from huggingface_hub import login, InferenceClient\n",
544
+ "from transformers import AutoTokenizer"
545
+ ]
546
+ },
547
+ {
548
+ "cell_type": "code",
549
+ "execution_count": null,
550
+ "id": "13347633-4606-4e38-9927-80c39e65c1f1",
551
+ "metadata": {},
552
+ "outputs": [],
553
+ "source": [
554
+ "hf_token = os.environ['HF_TOKEN']\n",
555
+ "login(hf_token, add_to_git_credential=True)"
556
+ ]
557
+ },
558
+ {
559
+ "cell_type": "code",
560
+ "execution_count": null,
561
+ "id": "ef60a4df-6267-4ebd-8eed-dcb917af0a5e",
562
+ "metadata": {},
563
+ "outputs": [],
564
+ "source": [
565
+ "code_qwen = \"Qwen/CodeQwen1.5-7B-Chat\"\n",
566
+ "code_gemma = \"google/codegemma-7b-it\"\n",
567
+ "CODE_QWEN_URL = \"https://h1vdol7jxhje3mpn.us-east-1.aws.endpoints.huggingface.cloud\"\n",
568
+ "CODE_GEMMA_URL = \"https://c5hggiyqachmgnqg.us-east-1.aws.endpoints.huggingface.cloud\""
569
+ ]
570
+ },
571
+ {
572
+ "cell_type": "code",
573
+ "execution_count": null,
574
+ "id": "695ce389-a903-4533-a2f1-cd9e2a6af8f2",
575
+ "metadata": {},
576
+ "outputs": [],
577
+ "source": [
578
+ "tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
579
+ "messages = messages_for(pi)\n",
580
+ "text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)"
581
+ ]
582
+ },
583
+ {
584
+ "cell_type": "code",
585
+ "execution_count": null,
586
+ "id": "d4548e96-0b32-4793-bdd6-1b072c2f26ab",
587
+ "metadata": {},
588
+ "outputs": [],
589
+ "source": [
590
+ "print(text)"
591
+ ]
592
+ },
593
+ {
594
+ "cell_type": "code",
595
+ "execution_count": null,
596
+ "id": "bb2a126b-09e7-4966-bc97-0ef5c2cc7896",
597
+ "metadata": {},
598
+ "outputs": [],
599
+ "source": [
600
+ "client = InferenceClient(CODE_QWEN_URL, token=hf_token)\n",
601
+ "stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)\n",
602
+ "for r in stream:\n",
603
+ " print(r.token.text, end = \"\")"
604
+ ]
605
+ },
606
+ {
607
+ "cell_type": "code",
608
+ "execution_count": null,
609
+ "id": "127a52e5-ad85-42b7-a0f5-9afda5efe090",
610
+ "metadata": {},
611
+ "outputs": [],
612
+ "source": [
613
+ "def stream_code_qwen(python):\n",
614
+ " tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
615
+ " messages = messages_for(python)\n",
616
+ " text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
617
+ " client = InferenceClient(CODE_QWEN_URL, token=hf_token)\n",
618
+ " stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)\n",
619
+ " result = \"\"\n",
620
+ " for r in stream:\n",
621
+ " result += r.token.text\n",
622
+ " yield result "
623
+ ]
624
+ },
625
+ {
626
+ "cell_type": "code",
627
+ "execution_count": null,
628
+ "id": "a82387d1-7651-4923-995b-fe18356fcaa6",
629
+ "metadata": {},
630
+ "outputs": [],
631
+ "source": [
632
+ "def optimize(python, model):\n",
633
+ " if model==\"GPT\":\n",
634
+ " result = stream_gpt(python)\n",
635
+ " elif model==\"Claude\":\n",
636
+ " result = stream_claude(python)\n",
637
+ " elif model==\"CodeQwen\":\n",
638
+ " result = stream_code_qwen(python)\n",
639
+ " else:\n",
640
+ " raise ValueError(\"Unknown model\")\n",
641
+ " for stream_so_far in result:\n",
642
+ " yield stream_so_far "
643
+ ]
644
+ },
645
+ {
646
+ "cell_type": "markdown",
647
+ "id": "4b0a6a97-5b8a-4a9b-8ee0-7561e0ced673",
648
+ "metadata": {},
649
+ "source": [
650
+ "<table style=\"margin: 0; text-align: left;\">\n",
651
+ " <tr>\n",
652
+ " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
653
+ " <img src=\"../thankyou.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
654
+ " </td>\n",
655
+ " <td>\n",
656
+ " <h2 style=\"color:#090;\">Thank you to @CloudLlama for an amazing contribution</h2>\n",
657
+ " <span style=\"color:#090;\">\n",
658
+ " A student has contributed a chunk of code to improve this, in the next 2 cells. You can now select which Python porgram to run,\n",
659
+ " and a compiler is automatically selected that will work on PC, Windows and Mac. Massive thank you @CloudLlama!\n",
660
+ " </span>\n",
661
+ " </td>\n",
662
+ " </tr>\n",
663
+ "</table>"
664
+ ]
665
+ },
666
+ {
667
+ "cell_type": "code",
668
+ "execution_count": null,
669
+ "id": "4ba311ec-c16a-4fe0-946b-4b940704cf65",
670
+ "metadata": {},
671
+ "outputs": [],
672
+ "source": [
673
+ "def select_sample_program(sample_program):\n",
674
+ " if sample_program==\"pi\":\n",
675
+ " return pi\n",
676
+ " elif sample_program==\"python_hard\":\n",
677
+ " return python_hard\n",
678
+ " else:\n",
679
+ " return \"Type your Python program here\""
680
+ ]
681
+ },
682
+ {
683
+ "cell_type": "code",
684
+ "execution_count": null,
685
+ "id": "e42286bc-085c-45dc-b101-234308e58269",
686
+ "metadata": {},
687
+ "outputs": [],
688
+ "source": [
689
+ "import platform\n",
690
+ "\n",
691
+ "VISUAL_STUDIO_2022_TOOLS = \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\Common7\\Tools\\\\VsDevCmd.bat\"\n",
692
+ "VISUAL_STUDIO_2019_TOOLS = \"C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2019\\\\BuildTools\\\\Common7\\\\Tools\\\\VsDevCmd.bat\"\n",
693
+ "\n",
694
+ "simple_cpp = \"\"\"\n",
695
+ "#include <iostream>\n",
696
+ "\n",
697
+ "int main() {\n",
698
+ " std::cout << \"Hello\";\n",
699
+ " return 0;\n",
700
+ "}\n",
701
+ "\"\"\"\n",
702
+ "\n",
703
+ "def run_cmd(command_to_run):\n",
704
+ " try:\n",
705
+ " run_result = subprocess.run(command_to_run, check=True, text=True, capture_output=True)\n",
706
+ " return run_result.stdout if run_result.stdout else \"SUCCESS\"\n",
707
+ " except:\n",
708
+ " return \"\"\n",
709
+ "\n",
710
+ "def c_compiler_cmd(filename_base):\n",
711
+ " my_platform = platform.system()\n",
712
+ " my_compiler = []\n",
713
+ "\n",
714
+ " try:\n",
715
+ " with open(\"simple.cpp\", \"w\") as f:\n",
716
+ " f.write(simple_cpp)\n",
717
+ " \n",
718
+ " if my_platform == \"Windows\":\n",
719
+ " if os.path.isfile(VISUAL_STUDIO_2022_TOOLS):\n",
720
+ " if os.path.isfile(\"./simple.exe\"):\n",
721
+ " os.remove(\"./simple.exe\")\n",
722
+ " compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
723
+ " if run_cmd(compile_cmd):\n",
724
+ " if run_cmd([\"./simple.exe\"]) == \"Hello\":\n",
725
+ " my_compiler = [\"Windows\", \"Visual Studio 2022\", [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
726
+ " \n",
727
+ " if not my_compiler:\n",
728
+ " if os.path.isfile(VISUAL_STUDIO_2019_TOOLS):\n",
729
+ " if os.path.isfile(\"./simple.exe\"):\n",
730
+ " os.remove(\"./simple.exe\")\n",
731
+ " compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
732
+ " if run_cmd(compile_cmd):\n",
733
+ " if run_cmd([\"./simple.exe\"]) == \"Hello\":\n",
734
+ " my_compiler = [\"Windows\", \"Visual Studio 2019\", [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
735
+ " \n",
736
+ " if not my_compiler:\n",
737
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
738
+ " \n",
739
+ " elif my_platform == \"Linux\":\n",
740
+ " if os.path.isfile(\"./simple\"):\n",
741
+ " os.remove(\"./simple\")\n",
742
+ " compile_cmd = [\"g++\", \"simple.cpp\", \"-o\", \"simple\"]\n",
743
+ " if run_cmd(compile_cmd):\n",
744
+ " if run_cmd([\"./simple\"]) == \"Hello\":\n",
745
+ " my_compiler = [\"Linux\", \"GCC (g++)\", [\"g++\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\" ]]\n",
746
+ " \n",
747
+ " if not my_compiler:\n",
748
+ " if os.path.isfile(\"./simple\"):\n",
749
+ " os.remove(\"./simple\")\n",
750
+ " compile_cmd = [\"clang++\", \"simple.cpp\", \"-o\", \"simple\"]\n",
751
+ " if run_cmd(compile_cmd):\n",
752
+ " if run_cmd([\"./simple\"]) == \"Hello\":\n",
753
+ " my_compiler = [\"Linux\", \"Clang++\", [\"clang++\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\"]]\n",
754
+ " \n",
755
+ " if not my_compiler:\n",
756
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
757
+ " \n",
758
+ " elif my_platform == \"Darwin\":\n",
759
+ " if os.path.isfile(\"./simple\"):\n",
760
+ " os.remove(\"./simple\")\n",
761
+ " compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", \"simple\", \"simple.cpp\"]\n",
762
+ " if run_cmd(compile_cmd):\n",
763
+ " if run_cmd([\"./simple\"]) == \"Hello\":\n",
764
+ " my_compiler = [\"Macintosh\", \"Clang++\", [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", f\"{filename_base}\", f\"{filename_base}.cpp\"]]\n",
765
+ " \n",
766
+ " if not my_compiler:\n",
767
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
768
+ " except:\n",
769
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
770
+ " \n",
771
+ " if my_compiler:\n",
772
+ " return my_compiler\n",
773
+ " else:\n",
774
+ " return [\"Unknown\", \"Unavailable\", []]\n"
775
+ ]
776
+ },
777
+ {
778
+ "cell_type": "code",
779
+ "execution_count": null,
780
+ "id": "f9ca2e6f-60c1-4e5f-b570-63c75b2d189b",
781
+ "metadata": {},
782
+ "outputs": [],
783
+ "source": [
784
+ "compiler_cmd = c_compiler_cmd(\"optimized\")\n",
785
+ "\n",
786
+ "with gr.Blocks(css=css) as ui:\n",
787
+ " gr.Markdown(\"## Convert code from Python to C++\")\n",
788
+ " with gr.Row():\n",
789
+ " python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
790
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
791
+ " with gr.Row():\n",
792
+ " with gr.Column():\n",
793
+ " sample_program = gr.Radio([\"pi\", \"python_hard\"], label=\"Sample program\", value=\"python_hard\")\n",
794
+ " model = gr.Dropdown([\"GPT\", \"Claude\", \"CodeQwen\"], label=\"Select model\", value=\"GPT\")\n",
795
+ " with gr.Column():\n",
796
+ " architecture = gr.Radio([compiler_cmd[0]], label=\"Architecture\", interactive=False, value=compiler_cmd[0])\n",
797
+ " compiler = gr.Radio([compiler_cmd[1]], label=\"Compiler\", interactive=False, value=compiler_cmd[1])\n",
798
+ " with gr.Row():\n",
799
+ " convert = gr.Button(\"Convert code\")\n",
800
+ " with gr.Row():\n",
801
+ " python_run = gr.Button(\"Run Python\")\n",
802
+ " if not compiler_cmd[1] == \"Unavailable\":\n",
803
+ " cpp_run = gr.Button(\"Run C++\")\n",
804
+ " else:\n",
805
+ " cpp_run = gr.Button(\"No compiler to run C++\", interactive=False)\n",
806
+ " with gr.Row():\n",
807
+ " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
808
+ " cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
809
+ "\n",
810
+ " sample_program.change(select_sample_program, inputs=[sample_program], outputs=[python])\n",
811
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
812
+ " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
813
+ " cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
814
+ "\n",
815
+ "ui.launch(inbrowser=True)"
816
+ ]
817
+ },
818
+ {
819
+ "cell_type": "code",
820
+ "execution_count": null,
821
+ "id": "9d0ad093-425b-488e-8c3f-67f729dd9c06",
822
+ "metadata": {},
823
+ "outputs": [],
824
+ "source": []
825
+ }
826
+ ],
827
+ "metadata": {
828
+ "kernelspec": {
829
+ "display_name": "Python 3 (ipykernel)",
830
+ "language": "python",
831
+ "name": "python3"
832
+ },
833
+ "language_info": {
834
+ "codemirror_mode": {
835
+ "name": "ipython",
836
+ "version": 3
837
+ },
838
+ "file_extension": ".py",
839
+ "mimetype": "text/x-python",
840
+ "name": "python",
841
+ "nbconvert_exporter": "python",
842
+ "pygments_lexer": "ipython3",
843
+ "version": "3.11.11"
844
+ }
845
+ },
846
+ "nbformat": 4,
847
+ "nbformat_minor": 5
848
+ }
.ipynb_checkpoints/optimized-checkpoint.cpp ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <vector>
3
+ #include <chrono>
4
+ #include <limits>
5
+ #include <iomanip>
6
+
7
+ class LCG {
8
+ private:
9
+ uint64_t value;
10
+ const uint64_t a = 1664525;
11
+ const uint64_t c = 1013904223;
12
+ const uint64_t m = 1ULL << 32;
13
+
14
+ public:
15
+ LCG(uint64_t seed) : value(seed) {}
16
+
17
+ uint64_t next() {
18
+ value = (a * value + c) % m;
19
+ return value;
20
+ }
21
+ };
22
+
23
+ int64_t max_subarray_sum(int n, uint64_t seed, int min_val, int max_val) {
24
+ LCG lcg(seed);
25
+ std::vector<int> random_numbers(n);
26
+ for (int i = 0; i < n; ++i) {
27
+ random_numbers[i] = static_cast<int>(lcg.next() % (max_val - min_val + 1) + min_val);
28
+ }
29
+
30
+ int64_t max_sum = std::numeric_limits<int64_t>::min();
31
+ int64_t current_sum = 0;
32
+ for (int i = 0; i < n; ++i) {
33
+ current_sum = std::max(static_cast<int64_t>(random_numbers[i]), current_sum + random_numbers[i]);
34
+ max_sum = std::max(max_sum, current_sum);
35
+ }
36
+ return max_sum;
37
+ }
38
+
39
+ int64_t total_max_subarray_sum(int n, uint64_t initial_seed, int min_val, int max_val) {
40
+ int64_t total_sum = 0;
41
+ LCG lcg(initial_seed);
42
+ for (int i = 0; i < 20; ++i) {
43
+ uint64_t seed = lcg.next();
44
+ total_sum += max_subarray_sum(n, seed, min_val, max_val);
45
+ }
46
+ return total_sum;
47
+ }
48
+
49
+ int main() {
50
+ int n = 10000;
51
+ uint64_t initial_seed = 42;
52
+ int min_val = -10;
53
+ int max_val = 10;
54
+
55
+ auto start_time = std::chrono::high_resolution_clock::now();
56
+ int64_t result = total_max_subarray_sum(n, initial_seed, min_val, max_val);
57
+ auto end_time = std::chrono::high_resolution_clock::now();
58
+
59
+ auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
60
+
61
+ std::cout << "Total Maximum Subarray Sum (20 runs): " << result << std::endl;
62
+ std::cout << "Execution Time: " << std::fixed << std::setprecision(6) << duration.count() / 1e6 << " seconds" << std::endl;
63
+
64
+ return 0;
65
+ }
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: 'AI Girlfriend Project By Darsh '
3
- emoji: 🚀
4
- colorFrom: yellow
5
- colorTo: blue
6
  sdk: gradio
7
- sdk_version: 5.33.0
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: AI_Girlfriend_Project_by_Darsh_
3
+ app_file: advanced project.ipynb
 
 
4
  sdk: gradio
5
+ sdk_version: 5.23.1
 
 
6
  ---
 
 
Untitled.ipynb ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 8,
6
+ "id": "01032c82-239c-48bf-82dc-cf91a2dd4698",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import ollama\n",
11
+ "import gradio as gr"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "execution_count": 9,
17
+ "id": "d17e8da0-72e6-4d11-9f99-64d83d6bb77b",
18
+ "metadata": {},
19
+ "outputs": [],
20
+ "source": [
21
+ "system_prompt_llama = \"You are an assistant that solves a quadratic equation provided. Just write the steps and the final answer and nothing else\"\n",
22
+ "user_prompt_llama = \"This is the quadratic equation you have to solve: \"\n",
23
+ "user_prompt_deepseek = \"This is the quadratic equation you have to solve: \"\n",
24
+ "system_prompt_deepseek = \"You are an assistant that solves a quadratic equation provided. Just write the steps and the final answer and nothing else\""
25
+ ]
26
+ },
27
+ {
28
+ "cell_type": "code",
29
+ "execution_count": 10,
30
+ "id": "e5ed88b2-090f-4364-b334-c6ff66676d82",
31
+ "metadata": {},
32
+ "outputs": [],
33
+ "source": [
34
+ "default_equation = \"x² - 5x + 6\""
35
+ ]
36
+ },
37
+ {
38
+ "cell_type": "code",
39
+ "execution_count": 11,
40
+ "id": "3aa1ed93-15ea-4d97-a00d-dbda21450ad7",
41
+ "metadata": {},
42
+ "outputs": [],
43
+ "source": [
44
+ "people_dict = {}"
45
+ ]
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": 12,
50
+ "id": "9361e9e6-99eb-45b0-ba31-3cf1b0865936",
51
+ "metadata": {},
52
+ "outputs": [],
53
+ "source": [
54
+ "def llama(equation):\n",
55
+ " message_ = [{'role':'system', 'content':system_prompt_llama}, {'role':'user', 'content':user_prompt_llama+equation}]\n",
56
+ " res = ollama.chat(model=\"llama3.2\", messages=message_)\n",
57
+ " return res['message']['content']\n",
58
+ " \n",
59
+ "def deepseek(equation):\n",
60
+ " message_ = [{'role':'system', 'content':system_prompt_deepseek}, {'role':'user', 'content':user_prompt_deepseek+equation}]\n",
61
+ " res = ollama.chat(model=\"deepseek-r1:1.5b\", messages=message_)\n",
62
+ " return res['message']['content']\n",
63
+ " "
64
+ ]
65
+ },
66
+ {
67
+ "cell_type": "code",
68
+ "execution_count": 13,
69
+ "id": "e6ddaf09-996f-4cfa-af98-49bf1d7d5ab3",
70
+ "metadata": {},
71
+ "outputs": [
72
+ {
73
+ "name": "stdout",
74
+ "output_type": "stream",
75
+ "text": [
76
+ "* Running on local URL: http://127.0.0.1:7872\n",
77
+ "* Running on public URL: https://7a723fc085cc9ad458.gradio.live\n",
78
+ "\n",
79
+ "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n"
80
+ ]
81
+ },
82
+ {
83
+ "data": {
84
+ "text/html": [
85
+ "<div><iframe src=\"https://7a723fc085cc9ad458.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
86
+ ],
87
+ "text/plain": [
88
+ "<IPython.core.display.HTML object>"
89
+ ]
90
+ },
91
+ "metadata": {},
92
+ "output_type": "display_data"
93
+ },
94
+ {
95
+ "data": {
96
+ "text/plain": []
97
+ },
98
+ "execution_count": 13,
99
+ "metadata": {},
100
+ "output_type": "execute_result"
101
+ }
102
+ ],
103
+ "source": [
104
+ "with gr.Blocks() as ui:\n",
105
+ " gr.Markdown(\"## This is a quadratic equation solver\")\n",
106
+ " with gr.Row():\n",
107
+ " llama_question = gr.Textbox(label=\"Llama's Question\",value=default_equation,lines=2)\n",
108
+ " deepseek_question = gr.Textbox(label=\"Deepseek's Question\",value=default_equation,lines=2)\n",
109
+ " with gr.Row():\n",
110
+ " llama_answer = gr.TextArea(label=\"Llama's Answer\",lines=10)\n",
111
+ " deepseek_answer = gr.TextArea(label=\"Deepseek's Answer\",lines=10)\n",
112
+ " with gr.Row():\n",
113
+ " llama_solver = gr.Button(\"Solve Llama\")\n",
114
+ " deepseek_solver = gr.Button(\"Solve Deepseek\")\n",
115
+ " llama_solver.click(llama, inputs=[llama_question], outputs=[llama_answer]) \n",
116
+ " deepseek_solver.click(deepseek, inputs=[deepseek_question], outputs=[deepseek_answer]) \n",
117
+ "\n",
118
+ "ui.launch(share=True)"
119
+ ]
120
+ },
121
+ {
122
+ "cell_type": "code",
123
+ "execution_count": 7,
124
+ "id": "0b9ea66f-a97f-4de2-bc60-fd7abbb4cc25",
125
+ "metadata": {},
126
+ "outputs": [
127
+ {
128
+ "name": "stdout",
129
+ "output_type": "stream",
130
+ "text": [
131
+ "* Running on local URL: http://127.0.0.1:7862\n",
132
+ "\n",
133
+ "To create a public link, set `share=True` in `launch()`.\n"
134
+ ]
135
+ },
136
+ {
137
+ "data": {
138
+ "text/html": [
139
+ "<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
140
+ ],
141
+ "text/plain": [
142
+ "<IPython.core.display.HTML object>"
143
+ ]
144
+ },
145
+ "metadata": {},
146
+ "output_type": "display_data"
147
+ },
148
+ {
149
+ "data": {
150
+ "text/plain": []
151
+ },
152
+ "execution_count": 7,
153
+ "metadata": {},
154
+ "output_type": "execute_result"
155
+ }
156
+ ],
157
+ "source": [
158
+ "import gradio as gr\n",
159
+ "\n",
160
+ "# A simple function that reacts to a selected mood\n",
161
+ "def respond_to_mood(mood):\n",
162
+ " return f\"You selected '{mood}'. I hope your day goes well!\"\n",
163
+ "\n",
164
+ "\n",
165
+ "# Gradio Interface\n",
166
+ "with gr.Blocks() as ui:\n",
167
+ " with gr.Row():\n",
168
+ " area = gr.Textbox(label=\"Answer here\")\n",
169
+ " inputs=gr.Radio([\"Happy\", \"Sad\", \"Excited\", \"Angry\"], label=\"How are you feeling today?\")\n",
170
+ " inputs.change(respond_to_mood, inputs=[inputs], outputs=[area])\n",
171
+ "ui.launch()\n",
172
+ " \n",
173
+ " \n",
174
+ "\n"
175
+ ]
176
+ },
177
+ {
178
+ "cell_type": "code",
179
+ "execution_count": null,
180
+ "id": "d5b05061-61da-4d2a-8d30-8cff97b7db35",
181
+ "metadata": {},
182
+ "outputs": [],
183
+ "source": []
184
+ }
185
+ ],
186
+ "metadata": {
187
+ "kernelspec": {
188
+ "display_name": "Python 3 (ipykernel)",
189
+ "language": "python",
190
+ "name": "python3"
191
+ },
192
+ "language_info": {
193
+ "codemirror_mode": {
194
+ "name": "ipython",
195
+ "version": 3
196
+ },
197
+ "file_extension": ".py",
198
+ "mimetype": "text/x-python",
199
+ "name": "python",
200
+ "nbconvert_exporter": "python",
201
+ "pygments_lexer": "ipython3",
202
+ "version": "3.11.11"
203
+ }
204
+ },
205
+ "nbformat": 4,
206
+ "nbformat_minor": 5
207
+ }
Untitled1.ipynb ADDED
@@ -0,0 +1,251 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 39,
6
+ "id": "438bfe5a-1519-463b-a9ce-8598c6fb50cd",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import ollama\n",
11
+ "import gradio as gr\n",
12
+ "import time"
13
+ ]
14
+ },
15
+ {
16
+ "cell_type": "code",
17
+ "execution_count": 40,
18
+ "id": "0c9f64a2-acd3-438e-9854-e9daced0d1c4",
19
+ "metadata": {},
20
+ "outputs": [],
21
+ "source": [
22
+ "class person_dict:\n",
23
+ " def __init__(self, name, messages:list, safety_checker:bool=True):\n",
24
+ " self.name = name\n",
25
+ " self.safety_checker = True\n",
26
+ " self.messages = messages\n",
27
+ " \n",
28
+ " \n",
29
+ "default_system_prompt = \"You are the girlfriend of a man named \"\n",
30
+ "\n",
31
+ "people_list = []"
32
+ ]
33
+ },
34
+ {
35
+ "cell_type": "code",
36
+ "execution_count": 41,
37
+ "id": "2162a08c-0309-45fb-9d43-923a3d915e56",
38
+ "metadata": {},
39
+ "outputs": [],
40
+ "source": [
41
+ "inappropriate_words = [\n",
42
+ " \"porn\", \"sex\", \"nude\", \"blowjob\", \"anal\", \"vagina\", \"penis\", \"boobs\",\n",
43
+ " \"kill\", \"murder\", \"shoot\", \"bomb\", \"rape\", \"lynch\", \"gas them\", \"hang them\", \"burn them\",\n",
44
+ " \"suicide\", \"kill myself\", \"end my life\", \"cut myself\", \"jump off\", \"overdose\", \"slit my wrist\",\n",
45
+ " \"cocaine\", \"weed\", \"heroin\", \"meth\", \"lsd\", \"crack\", \"ecstasy\", \"sell drugs\", \"buy drugs\"\n",
46
+ "]\n",
47
+ "\n",
48
+ " \n"
49
+ ]
50
+ },
51
+ {
52
+ "cell_type": "code",
53
+ "execution_count": 42,
54
+ "id": "498b61d9-d250-486d-8e46-7a18854c2181",
55
+ "metadata": {},
56
+ "outputs": [],
57
+ "source": [
58
+ "def appender(mess, mes_list, user=True):\n",
59
+ " if user:\n",
60
+ " mes_list.append({'role':'user', 'content':mess})\n",
61
+ " else:\n",
62
+ " mess_list.append({'role':'assistant', 'content':mess})\n",
63
+ " "
64
+ ]
65
+ },
66
+ {
67
+ "cell_type": "code",
68
+ "execution_count": 65,
69
+ "id": "090f2abf-3b7c-4a90-b81f-18ea70419589",
70
+ "metadata": {},
71
+ "outputs": [],
72
+ "source": [
73
+ "def chat(id, mess):\n",
74
+ " messag = people_list[int(id)].messages\n",
75
+ " if people_list[int(id)].safety_checker == True:\n",
76
+ " for word in inappropriate_words:\n",
77
+ " if word in mess:\n",
78
+ " return \"<SAFETY CHECKER MESSAGE> This message will not be responded to, because safety checker was enabled on your account. Please try some different message\"\n",
79
+ " else:\n",
80
+ " appender(mess, people_list[int(id)].messages)\n",
81
+ " res = ollama.chat(model=\"llama3.2\", messages=messag)\n",
82
+ " appender(res['message']['content'], people_list[int(id)].messages, user=False)\n",
83
+ " return res['message']['content']\n",
84
+ " \n",
85
+ " "
86
+ ]
87
+ },
88
+ {
89
+ "cell_type": "code",
90
+ "execution_count": 76,
91
+ "id": "1e9cc3a5-7485-458f-abd3-3bf8bf2ebcc5",
92
+ "metadata": {},
93
+ "outputs": [],
94
+ "source": [
95
+ "def show_signup():\n",
96
+ " return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)\n",
97
+ "\n",
98
+ "def show_login():\n",
99
+ " return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)\n",
100
+ "\n",
101
+ "def show_chat():\n",
102
+ " return gr.update(visible = False), gr.update(visible=False), gr.update(visible=True)\n",
103
+ "\n",
104
+ "\n",
105
+ "def login(id: int, person_name):\n",
106
+ " if int(id) <= len(people_list) - 1:\n",
107
+ " if people_list[int(id)].name == person_name:\n",
108
+ " # Call show_chat() and unpack its 3 return values\n",
109
+ " return \"You are successfully logged in!\", id, *show_chat()\n",
110
+ " else:\n",
111
+ " return \"Sorry, the name doesn't match!\", None, *show_login()\n",
112
+ " else:\n",
113
+ " return \"This ID doesn't exist!\", None, *show_login()\n",
114
+ "\n",
115
+ "\n",
116
+ "def sign_in(person_name, safety_checker):\n",
117
+ " if safety_checker == \"Yes\":\n",
118
+ " new_person = person_dict(name=person_name, messages=[{'role':'system', 'content':default_system_prompt+person_name+\". You have to keep him happy\"}], safety_checker=True)\n",
119
+ " people_list.append(new_person)\n",
120
+ " return f\"Hey {person_name}! You have successfully created an account! Your id is: {len(people_list) - 1} Keep it safe!\"\n",
121
+ " else:\n",
122
+ " new_person = person_dict(name=person_name, messages=[{'role':'system', 'content':default_system_prompt+person_name+\". You have to keep him happy\"}], safety_checker=False)\n",
123
+ " people_list.append(new_person)\n",
124
+ " return f\"Hey {person_name}! You have successfully created an account! Your id is: {len(people_list) - 1} Keep it safe!\"\n",
125
+ "\n",
126
+ "with gr.Blocks() as ui:\n",
127
+ " user_id = gr.State()\n",
128
+ " with gr.Column(visible=True) as login_page:\n",
129
+ " gr.Markdown(\"## 🔐 Please LogIn to your account:\")\n",
130
+ " id = gr.Textbox(label=\"Please enter your id\", lines=1)\n",
131
+ " name = gr.Textbox(label=\"Please enter your name\", lines=2)\n",
132
+ " message = gr.Label()\n",
133
+ " log_in_but = gr.Button(\"Submit\")\n",
134
+ " sign_up = gr.Button(\"New to us? Sign up instead\")\n",
135
+ " with gr.Column(visible=False) as sign_up_page:\n",
136
+ " gr.Markdown(\"## 🔐 Please create your account:\")\n",
137
+ " name1 = gr.Textbox(label=\"Please enter your name\", lines=2)\n",
138
+ " gr.Markdown(\"Note safety checker cannot be changed!\")\n",
139
+ " safety_checker = gr.Radio([\"Yes\", \"No\"], label = \"Safety Checker\")\n",
140
+ " message1 = gr.Label()\n",
141
+ " sign_up_but = gr.Button(\"Submit\")\n",
142
+ " log_in = gr.Button(\"Already have an account? Go to LogIn.\")\n",
143
+ " with gr.Column(visible = False) as chat_page:\n",
144
+ " user_input = gr.Textbox(label=\"Enter Your Message:\", lines=5)\n",
145
+ " sub = gr.Button(\"Submit\")\n",
146
+ " assi = gr.TextArea(label=\"Your GirlFriend's response:\", lines=10)\n",
147
+ " log_in_but.click(fn=login, inputs=[id, name], outputs=[message, user_id, login_page, sign_up_page, chat_page])\n",
148
+ " sign_up.click(fn=show_signup, inputs=[], outputs=[login_page, sign_up_page, chat_page])\n",
149
+ " log_in.click(fn=show_login, inputs=[], outputs=[login_page, sign_up_page, chat_page])\n",
150
+ " sign_up_but.click(fn=sign_in, inputs=[name1, safety_checker], outputs=[message1])\n",
151
+ " sub.click(fn=chat, inputs=[user_id, user_input], outputs=[assi])\n",
152
+ "\n",
153
+ "\n",
154
+ " \n",
155
+ " \n",
156
+ " "
157
+ ]
158
+ },
159
+ {
160
+ "cell_type": "code",
161
+ "execution_count": 77,
162
+ "id": "644ed09f-21cd-4b63-a7bf-58d00925fabf",
163
+ "metadata": {},
164
+ "outputs": [
165
+ {
166
+ "name": "stdout",
167
+ "output_type": "stream",
168
+ "text": [
169
+ "* Running on local URL: http://127.0.0.1:7886\n",
170
+ "\n",
171
+ "To create a public link, set `share=True` in `launch()`.\n"
172
+ ]
173
+ },
174
+ {
175
+ "data": {
176
+ "text/html": [
177
+ "<div><iframe src=\"http://127.0.0.1:7886/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
178
+ ],
179
+ "text/plain": [
180
+ "<IPython.core.display.HTML object>"
181
+ ]
182
+ },
183
+ "metadata": {},
184
+ "output_type": "display_data"
185
+ },
186
+ {
187
+ "data": {
188
+ "text/plain": []
189
+ },
190
+ "execution_count": 77,
191
+ "metadata": {},
192
+ "output_type": "execute_result"
193
+ }
194
+ ],
195
+ "source": [
196
+ "ui.launch()"
197
+ ]
198
+ },
199
+ {
200
+ "cell_type": "code",
201
+ "execution_count": 75,
202
+ "id": "bbeccb9b-44c9-4172-9020-67a1b4dfc34e",
203
+ "metadata": {},
204
+ "outputs": [
205
+ {
206
+ "data": {
207
+ "text/plain": [
208
+ "{'role': 'system',\n",
209
+ " 'content': 'You are the girlfriend of a man named Harsh. You have to keep him happy'}"
210
+ ]
211
+ },
212
+ "execution_count": 75,
213
+ "metadata": {},
214
+ "output_type": "execute_result"
215
+ }
216
+ ],
217
+ "source": [
218
+ "people_list[2].messages"
219
+ ]
220
+ },
221
+ {
222
+ "cell_type": "code",
223
+ "execution_count": null,
224
+ "id": "67b6f8e1-31c5-4922-b880-6ee7cb5580e5",
225
+ "metadata": {},
226
+ "outputs": [],
227
+ "source": []
228
+ }
229
+ ],
230
+ "metadata": {
231
+ "kernelspec": {
232
+ "display_name": "Python 3 (ipykernel)",
233
+ "language": "python",
234
+ "name": "python3"
235
+ },
236
+ "language_info": {
237
+ "codemirror_mode": {
238
+ "name": "ipython",
239
+ "version": 3
240
+ },
241
+ "file_extension": ".py",
242
+ "mimetype": "text/x-python",
243
+ "name": "python",
244
+ "nbconvert_exporter": "python",
245
+ "pygments_lexer": "ipython3",
246
+ "version": "3.11.11"
247
+ }
248
+ },
249
+ "nbformat": 4,
250
+ "nbformat_minor": 5
251
+ }
advanced project.ipynb ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "13fc27bd-8eb2-41a7-9216-cec1b802975f",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import ollama\n",
11
+ "import gradio as gr"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "execution_count": 2,
17
+ "id": "9fb99cdf-44c5-4b2b-aa04-8aee5f07140a",
18
+ "metadata": {},
19
+ "outputs": [],
20
+ "source": [
21
+ "class person_dict:\n",
22
+ " def __init__(self, name, messages: list, safety_checker: bool = True):\n",
23
+ " self.name = name\n",
24
+ " self.safety_checker = safety_checker \n",
25
+ " self.messages = messages\n",
26
+ "\n",
27
+ "\n",
28
+ "default_system_prompt = \"You are the girlfriend of a man named \"\n",
29
+ "\n",
30
+ "people_list = []"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": 3,
36
+ "id": "13257d96-ba64-4b58-8077-8827ca1f3921",
37
+ "metadata": {},
38
+ "outputs": [],
39
+ "source": [
40
+ "inappropriate_words = [\n",
41
+ " \"porn\", \"sex\", \"nude\", \"blowjob\", \"anal\", \"vagina\", \"penis\", \"boobs\",\n",
42
+ " \"kill\", \"murder\", \"shoot\", \"bomb\", \"rape\", \"lynch\", \"gas them\", \"hang them\", \"burn them\",\n",
43
+ " \"suicide\", \"kill myself\", \"end my life\", \"cut myself\", \"jump off\", \"overdose\", \"slit my wrist\",\n",
44
+ " \"cocaine\", \"weed\", \"heroin\", \"meth\", \"lsd\", \"crack\", \"ecstasy\", \"sell drugs\", \"buy drugs\"]"
45
+ ]
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": 4,
50
+ "id": "e8a0d538-1b02-4217-b729-733be838ec36",
51
+ "metadata": {},
52
+ "outputs": [],
53
+ "source": [
54
+ "def appender(mess, mes_list, user=True):\n",
55
+ " if user:\n",
56
+ " mes_list.append({'role': 'user', 'content': mess})\n",
57
+ " else:\n",
58
+ " mes_list.append({'role': 'assistant', 'content': mess})"
59
+ ]
60
+ },
61
+ {
62
+ "cell_type": "code",
63
+ "execution_count": 5,
64
+ "id": "983b960c-5309-47d8-8fe5-346df540fc54",
65
+ "metadata": {},
66
+ "outputs": [],
67
+ "source": [
68
+ "def chat(id, mess):\n",
69
+ " id = int(id)\n",
70
+ " person = people_list[id]\n",
71
+ " messag = person.messages\n",
72
+ "\n",
73
+ " \n",
74
+ " if person.safety_checker:\n",
75
+ " for word in inappropriate_words:\n",
76
+ " if word.lower() in mess.lower(): # case insensitive check\n",
77
+ " return \"<SAFETY CHECKER MESSAGE> This message will not be responded to, because safety checker was enabled on your account. Please try a different message.\"\n",
78
+ "\n",
79
+ " \n",
80
+ " appender(mess, messag)\n",
81
+ " res = ollama.chat(model=\"llama3.2\", messages=messag)\n",
82
+ " appender(res['message']['content'], messag, user=False)\n",
83
+ " return res['message']['content']"
84
+ ]
85
+ },
86
+ {
87
+ "cell_type": "code",
88
+ "execution_count": 6,
89
+ "id": "e60b3e81-7224-481f-b025-a161ac74eb6b",
90
+ "metadata": {},
91
+ "outputs": [
92
+ {
93
+ "name": "stdout",
94
+ "output_type": "stream",
95
+ "text": [
96
+ "* Running on local URL: http://127.0.0.1:7860\n",
97
+ "\n",
98
+ "To create a public link, set `share=True` in `launch()`.\n"
99
+ ]
100
+ },
101
+ {
102
+ "data": {
103
+ "text/html": [
104
+ "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
105
+ ],
106
+ "text/plain": [
107
+ "<IPython.core.display.HTML object>"
108
+ ]
109
+ },
110
+ "metadata": {},
111
+ "output_type": "display_data"
112
+ },
113
+ {
114
+ "data": {
115
+ "text/plain": []
116
+ },
117
+ "execution_count": 6,
118
+ "metadata": {},
119
+ "output_type": "execute_result"
120
+ }
121
+ ],
122
+ "source": [
123
+ "def show_signup():\n",
124
+ " return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)\n",
125
+ "\n",
126
+ "def show_login():\n",
127
+ " return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)\n",
128
+ "\n",
129
+ "def show_chat():\n",
130
+ " return gr.update(visible = False), gr.update(visible=False), gr.update(visible=True)\n",
131
+ "\n",
132
+ "\n",
133
+ "def login(id: int, person_name):\n",
134
+ " if int(id) <= len(people_list) - 1:\n",
135
+ " if people_list[int(id)].name == person_name:\n",
136
+ " # Call show_chat() and unpack its 3 return values\n",
137
+ " return \"You are successfully logged in!\", id, *show_chat()\n",
138
+ " else:\n",
139
+ " return \"Sorry, the name doesn't match!\", None, *show_login()\n",
140
+ " else:\n",
141
+ " return \"This ID doesn't exist!\", None, *show_login()\n",
142
+ "\n",
143
+ "\n",
144
+ "def sign_in(person_name, safety_checker):\n",
145
+ " if safety_checker == \"Yes\":\n",
146
+ " new_person = person_dict(name=person_name, messages=[{'role':'system', 'content':default_system_prompt+person_name+\". You have to keep him happy. Only respond to what he told you. Do not make things up!\"}], safety_checker=True)\n",
147
+ " people_list.append(new_person)\n",
148
+ " return f\"Hey {person_name}! You have successfully created an account! Your id is: {len(people_list) - 1} Keep it safe!\"\n",
149
+ " else:\n",
150
+ " new_person = person_dict(name=person_name, messages=[{'role':'system', 'content':default_system_prompt+person_name+\". You have to keep him happy. Only respond to what he told you. Do not make things up!\"}], safety_checker=False)\n",
151
+ " people_list.append(new_person)\n",
152
+ " return f\"Hey {person_name}! You have successfully created an account! Your id is: {len(people_list) - 1} Keep it safe!\"\n",
153
+ "\n",
154
+ "with gr.Blocks() as ui:\n",
155
+ " user_id = gr.State()\n",
156
+ " with gr.Column(visible=True) as login_page:\n",
157
+ " gr.Markdown(\"## 🔐 Please LogIn to your account:\")\n",
158
+ " id = gr.Textbox(label=\"Please enter your id\", lines=1)\n",
159
+ " name = gr.Textbox(label=\"Please enter your name\", lines=2)\n",
160
+ " message = gr.Label()\n",
161
+ " log_in_but = gr.Button(\"Submit\")\n",
162
+ " sign_up = gr.Button(\"New to us? Sign up instead\")\n",
163
+ " with gr.Column(visible=False) as sign_up_page:\n",
164
+ " gr.Markdown(\"## 🔐 Please create your account:\")\n",
165
+ " name1 = gr.Textbox(label=\"Please enter your name\", lines=2)\n",
166
+ " gr.Markdown(\"Note safety checker cannot be changed!\")\n",
167
+ " safety_checker = gr.Radio([\"Yes\", \"No\"], label = \"Safety Checker\")\n",
168
+ " message1 = gr.Label()\n",
169
+ " sign_up_but = gr.Button(\"Submit\")\n",
170
+ " log_in = gr.Button(\"Already have an account? Go to LogIn.\")\n",
171
+ " with gr.Column(visible = False) as chat_page:\n",
172
+ " user_input = gr.Textbox(label=\"Enter Your Message:\", lines=5)\n",
173
+ " sub = gr.Button(\"Submit\")\n",
174
+ " assi = gr.TextArea(label=\"Your GirlFriend's response:\", lines=10)\n",
175
+ " log_in_but.click(fn=login, inputs=[id, name], outputs=[message, user_id, login_page, sign_up_page, chat_page])\n",
176
+ " sign_up.click(fn=show_signup, inputs=[], outputs=[login_page, sign_up_page, chat_page])\n",
177
+ " log_in.click(fn=show_login, inputs=[], outputs=[login_page, sign_up_page, chat_page])\n",
178
+ " sign_up_but.click(fn=sign_in, inputs=[name1, safety_checker], outputs=[message1])\n",
179
+ " sub.click(fn=chat, inputs=[user_id, user_input], outputs=[assi])\n",
180
+ "\n",
181
+ "\n",
182
+ "ui.launch()"
183
+ ]
184
+ },
185
+ {
186
+ "cell_type": "code",
187
+ "execution_count": null,
188
+ "id": "8c9a6068-880e-4c94-84ec-809cfb3729ce",
189
+ "metadata": {},
190
+ "outputs": [],
191
+ "source": [
192
+ "person_list[0].name"
193
+ ]
194
+ },
195
+ {
196
+ "cell_type": "code",
197
+ "execution_count": null,
198
+ "id": "1aebd290-e2a5-4702-850c-109b92f027d2",
199
+ "metadata": {},
200
+ "outputs": [],
201
+ "source": []
202
+ }
203
+ ],
204
+ "metadata": {
205
+ "kernelspec": {
206
+ "display_name": "Python 3 (ipykernel)",
207
+ "language": "python",
208
+ "name": "python3"
209
+ },
210
+ "language_info": {
211
+ "codemirror_mode": {
212
+ "name": "ipython",
213
+ "version": 3
214
+ },
215
+ "file_extension": ".py",
216
+ "mimetype": "text/x-python",
217
+ "name": "python",
218
+ "nbconvert_exporter": "python",
219
+ "pygments_lexer": "ipython3",
220
+ "version": "3.11.11"
221
+ }
222
+ },
223
+ "nbformat": 4,
224
+ "nbformat_minor": 5
225
+ }
community-contributions/.ipynb_checkpoints/Code_Converter(Added_Java_Support)(Open_Ai_Only)-checkpoint.ipynb ADDED
@@ -0,0 +1,795 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Code Generator\n",
9
+ "\n",
10
+ "The requirement: use a Frontier model to generate high performance C++ code from Python code\n"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": 1,
16
+ "id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "# imports\n",
21
+ "\n",
22
+ "import os\n",
23
+ "import io\n",
24
+ "import sys\n",
25
+ "from dotenv import load_dotenv\n",
26
+ "from openai import OpenAI\n",
27
+ "import google.generativeai\n",
28
+ "import anthropic\n",
29
+ "from IPython.display import Markdown, display, update_display\n",
30
+ "import gradio as gr\n",
31
+ "import subprocess"
32
+ ]
33
+ },
34
+ {
35
+ "cell_type": "code",
36
+ "execution_count": 2,
37
+ "id": "4f672e1c-87e9-4865-b760-370fa605e614",
38
+ "metadata": {},
39
+ "outputs": [],
40
+ "source": [
41
+ "# environment\n",
42
+ "\n",
43
+ "load_dotenv()\n",
44
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
45
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')"
46
+ ]
47
+ },
48
+ {
49
+ "cell_type": "code",
50
+ "execution_count": 28,
51
+ "id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
52
+ "metadata": {},
53
+ "outputs": [],
54
+ "source": [
55
+ "# initialize\n",
56
+ "# NOTE - option to use ultra-low cost models by uncommenting last 2 lines\n",
57
+ "\n",
58
+ "openai = OpenAI()\n",
59
+ "# claude = anthropic.Anthropic()\n",
60
+ "OPENAI_MODEL = \"gpt-4o\"\n",
61
+ "# CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\"\n",
62
+ "\n",
63
+ "# Want to keep costs ultra-low? Uncomment these lines:\n",
64
+ "# OPENAI_MODEL = \"gpt-4o-mini\"\n",
65
+ "# CLAUDE_MODEL = \"claude-3-haiku-20240307\""
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "execution_count": 4,
71
+ "id": "6896636f-923e-4a2c-9d6c-fac07828a201",
72
+ "metadata": {},
73
+ "outputs": [],
74
+ "source": [
75
+ "# system_message = \"You are an assistant that reimplements Python code in high-performance C++ and Java for an M1 Mac. \"\n",
76
+ "# system_message += \"Respond only with C++ and Java code; use comments sparingly and do not provide any explanation other than occasional comments. \"\n",
77
+ "# system_message += \"The C++ and Java responses need to produce identical output in the fastest possible time.\"\n"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "execution_count": 5,
83
+ "id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
84
+ "metadata": {},
85
+ "outputs": [],
86
+ "source": [
87
+ "# def user_prompt_for(python):\n",
88
+ "# user_prompt = \"Rewrite this Python code in C++ and Java with the fastest possible implementation that produces identical output in the least time. \"\n",
89
+ "# user_prompt += \"Respond only with C++ and Java code; do not explain your work other than a few comments. \"\n",
90
+ "# user_prompt += \"Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip for C++, and import required packages for Java.\\n\\n\"\n",
91
+ "# user_prompt += python\n",
92
+ "# return user_prompt\n"
93
+ ]
94
+ },
95
+ {
96
+ "cell_type": "code",
97
+ "execution_count": 6,
98
+ "id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
99
+ "metadata": {},
100
+ "outputs": [],
101
+ "source": [
102
+ "# def messages_for(python):\n",
103
+ "# return [\n",
104
+ "# {\"role\": \"system\", \"content\": system_message}, # Includes the updated system message with C++ and Java\n",
105
+ "# {\"role\": \"user\", \"content\": user_prompt_for(python)} # Calls the updated user prompt function for C++ and Java\n",
106
+ "# ]\n"
107
+ ]
108
+ },
109
+ {
110
+ "cell_type": "code",
111
+ "execution_count": 29,
112
+ "id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
113
+ "metadata": {},
114
+ "outputs": [],
115
+ "source": [
116
+ "def write_output(code, file_name):\n",
117
+ " \"\"\"Write the generated code to a file.\"\"\"\n",
118
+ " with open(file_name, \"w\") as f:\n",
119
+ " f.write(code)\n"
120
+ ]
121
+ },
122
+ {
123
+ "cell_type": "code",
124
+ "execution_count": 30,
125
+ "id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
126
+ "metadata": {},
127
+ "outputs": [],
128
+ "source": [
129
+ "def system_message_for(language):\n",
130
+ " \"\"\"Create a system message tailored for the requested language.\"\"\"\n",
131
+ " return (\n",
132
+ " f\"You are an assistant that reimplements Python code in high-performance {language.upper()} for an M1 Mac. \"\n",
133
+ " f\"Respond only with {language.upper()} code; do not explain your work other than occasional comments. \"\n",
134
+ " \"Pay attention to number types to ensure no overflows and include all necessary packages.\\n\\n\"\n",
135
+ " )\n",
136
+ "\n",
137
+ "def user_prompt_for(python):\n",
138
+ " \"\"\"Generate the user prompt.\"\"\"\n",
139
+ " return (\n",
140
+ " \"Rewrite this Python code in the requested language with the fastest possible implementation that produces \"\n",
141
+ " \"identical output in the least time. Use appropriate syntax for the language.\\n\\n\" + python\n",
142
+ " )\n",
143
+ "\n",
144
+ "def messages_for(python, language):\n",
145
+ " \"\"\"Generate the messages for GPT.\"\"\"\n",
146
+ " return [\n",
147
+ " {\"role\": \"system\", \"content\": system_message_for(language)},\n",
148
+ " {\"role\": \"user\", \"content\": user_prompt_for(python)},\n",
149
+ " ]\n",
150
+ " "
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": 31,
156
+ "id": "3ec5a816-7bf4-4daa-b0c9-f04edb1c0140",
157
+ "metadata": {},
158
+ "outputs": [],
159
+ "source": [
160
+ "def optimize_gpt(python, language=\"cpp\"):\n",
161
+ " \"\"\"Optimize the given Python code and generate C++ or Java output.\"\"\"\n",
162
+ " code = \"\"\n",
163
+ " for chunk in stream_gpt(python, language):\n",
164
+ " print(chunk, end=\"\") # Stream the output\n",
165
+ " code = chunk # Store the final code\n",
166
+ " \n",
167
+ " file_name = f\"optimized.{language}\"\n",
168
+ " write_output(code, file_name)\n",
169
+ " print(f\"\\nCode written to {file_name}.\")"
170
+ ]
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "execution_count": null,
175
+ "id": "8adf0436-cf0e-429c-bd35-c3d551631b27",
176
+ "metadata": {},
177
+ "outputs": [],
178
+ "source": []
179
+ },
180
+ {
181
+ "cell_type": "code",
182
+ "execution_count": null,
183
+ "id": "7cd84ad8-d55c-4fe0-9eeb-1895c95c4a9d",
184
+ "metadata": {},
185
+ "outputs": [],
186
+ "source": [
187
+ "# def optimize_claude(python):\n",
188
+ "# result = claude.messages.stream(\n",
189
+ "# model=CLAUDE_MODEL,\n",
190
+ "# max_tokens=2000,\n",
191
+ "# system=system_message,\n",
192
+ "# messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
193
+ "# )\n",
194
+ "# reply = \"\"\n",
195
+ "# with result as stream:\n",
196
+ "# for text in stream.text_stream:\n",
197
+ "# reply += text\n",
198
+ "# print(text, end=\"\", flush=True)\n",
199
+ "# write_output(reply)"
200
+ ]
201
+ },
202
+ {
203
+ "cell_type": "code",
204
+ "execution_count": 32,
205
+ "id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
206
+ "metadata": {},
207
+ "outputs": [],
208
+ "source": [
209
+ "pi = \"\"\"\n",
210
+ "import time\n",
211
+ "\n",
212
+ "def calculate(iterations, param1, param2):\n",
213
+ " result = 1.0\n",
214
+ " for i in range(1, iterations+1):\n",
215
+ " j = i * param1 - param2\n",
216
+ " result -= (1/j)\n",
217
+ " j = i * param1 + param2\n",
218
+ " result += (1/j)\n",
219
+ " return result\n",
220
+ "\n",
221
+ "start_time = time.time()\n",
222
+ "result = calculate(100_000_000, 4, 1) * 4\n",
223
+ "end_time = time.time()\n",
224
+ "\n",
225
+ "print(f\"Result: {result:.12f}\")\n",
226
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
227
+ "\"\"\""
228
+ ]
229
+ },
230
+ {
231
+ "cell_type": "code",
232
+ "execution_count": 10,
233
+ "id": "7fe1cd4b-d2c5-4303-afed-2115a3fef200",
234
+ "metadata": {},
235
+ "outputs": [
236
+ {
237
+ "name": "stdout",
238
+ "output_type": "stream",
239
+ "text": [
240
+ "Result: 3.141592658589\n",
241
+ "Execution Time: 46.954224 seconds\n"
242
+ ]
243
+ }
244
+ ],
245
+ "source": [
246
+ "exec(pi)"
247
+ ]
248
+ },
249
+ {
250
+ "cell_type": "code",
251
+ "execution_count": null,
252
+ "id": "57ea7f4b-a862-4805-a074-2019314cbd4a",
253
+ "metadata": {},
254
+ "outputs": [],
255
+ "source": [
256
+ "optimize_gpt(python_code, language=\"java\")\n"
257
+ ]
258
+ },
259
+ {
260
+ "cell_type": "code",
261
+ "execution_count": null,
262
+ "id": "105db6f9-343c-491d-8e44-3a5328b81719",
263
+ "metadata": {},
264
+ "outputs": [],
265
+ "source": [
266
+ "optimize_gpt(python_code, language=\"cpp\")\n"
267
+ ]
268
+ },
269
+ {
270
+ "cell_type": "code",
271
+ "execution_count": null,
272
+ "id": "bf26ee95-0c77-491d-9a91-579a1e96a8a3",
273
+ "metadata": {},
274
+ "outputs": [],
275
+ "source": [
276
+ "exec(pi)"
277
+ ]
278
+ },
279
+ {
280
+ "cell_type": "markdown",
281
+ "id": "bf8f8018-f64d-425c-a0e1-d7862aa9592d",
282
+ "metadata": {},
283
+ "source": [
284
+ "# Compiling C++ and executing\n",
285
+ "\n",
286
+ "You can use any platform now (Windows,Mac,Linux) i have added compatiblity in this"
287
+ ]
288
+ },
289
+ {
290
+ "cell_type": "code",
291
+ "execution_count": null,
292
+ "id": "4194e40c-04ab-4940-9d64-b4ad37c5bb40",
293
+ "metadata": {},
294
+ "outputs": [],
295
+ "source": [
296
+ "import subprocess\n",
297
+ "import platform\n",
298
+ "\n",
299
+ "def compile_and_run(language=\"cpp\"):\n",
300
+ " \"\"\"Compile and run the generated code.\"\"\"\n",
301
+ " is_windows = platform.system() == \"Windows\"\n",
302
+ " \n",
303
+ " if language == \"cpp\":\n",
304
+ " if is_windows:\n",
305
+ " # Windows: Use g++ (requires MinGW or equivalent installed)\n",
306
+ " compile_command = [\"g++\", \"-O3\", \"-std=c++17\", \"-o\", \"optimized.exe\", \"optimized.cpp\"]\n",
307
+ " execute_command = [\"optimized.exe\"]\n",
308
+ " else:\n",
309
+ " # Non-Windows: Use clang++\n",
310
+ " compile_command = [\n",
311
+ " \"clang++\", \"-O3\", \"-std=c++17\", \"-march=armv8.3-a\", \"-o\", \"optimized\", \"optimized.cpp\"\n",
312
+ " ]\n",
313
+ " execute_command = [\"./optimized\"]\n",
314
+ " elif language == \"java\":\n",
315
+ " # Both Windows and non-Windows use the same Java commands\n",
316
+ " compile_command = [\"javac\", \"optimized.java\"]\n",
317
+ " execute_command = [\"java\", \"optimized\"]\n",
318
+ " else:\n",
319
+ " raise ValueError(\"Unsupported language. Choose 'cpp' or 'java'.\")\n",
320
+ "\n",
321
+ " # Compile\n",
322
+ " try:\n",
323
+ " subprocess.run(compile_command, check=True, shell=is_windows)\n",
324
+ " print(f\"{language.upper()} compilation successful.\")\n",
325
+ " except subprocess.CalledProcessError as e:\n",
326
+ " print(f\"{language.upper()} compilation failed:\\n{e}\")\n",
327
+ " return\n",
328
+ "\n",
329
+ " # Run\n",
330
+ " try:\n",
331
+ " output = subprocess.run(\n",
332
+ " execute_command, capture_output=True, text=True, shell=is_windows\n",
333
+ " )\n",
334
+ " print(f\"{language.upper()} execution output:\\n{output.stdout}\")\n",
335
+ " except subprocess.CalledProcessError as e:\n",
336
+ " print(f\"{language.upper()} execution failed:\\n{e.stderr}\")\n"
337
+ ]
338
+ },
339
+ {
340
+ "cell_type": "code",
341
+ "execution_count": null,
342
+ "id": "240ae457-ec5a-4268-9e7d-e782c2113f02",
343
+ "metadata": {},
344
+ "outputs": [],
345
+ "source": [
346
+ "compile_and_run(language=\"cpp\")\n"
347
+ ]
348
+ },
349
+ {
350
+ "cell_type": "code",
351
+ "execution_count": null,
352
+ "id": "983a11fe-e24d-4c65-8269-9802c5ef3ae6",
353
+ "metadata": {},
354
+ "outputs": [],
355
+ "source": [
356
+ "# optimize_claude(pi)"
357
+ ]
358
+ },
359
+ {
360
+ "cell_type": "code",
361
+ "execution_count": null,
362
+ "id": "d5a766f9-3d23-4bb4-a1d4-88ec44b61ddf",
363
+ "metadata": {},
364
+ "outputs": [],
365
+ "source": [
366
+ "# Repeat for Claude - again, use the right approach for your platform\n",
367
+ "\n",
368
+ "# !clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
369
+ "# !./optimized"
370
+ ]
371
+ },
372
+ {
373
+ "cell_type": "code",
374
+ "execution_count": 26,
375
+ "id": "c3b497b3-f569-420e-b92e-fb0f49957ce0",
376
+ "metadata": {},
377
+ "outputs": [],
378
+ "source": [
379
+ "python_hard = \"\"\"# Be careful to support large number sizes\n",
380
+ "\n",
381
+ "def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
382
+ " value = seed\n",
383
+ " while True:\n",
384
+ " value = (a * value + c) % m\n",
385
+ " yield value\n",
386
+ " \n",
387
+ "def max_subarray_sum(n, seed, min_val, max_val):\n",
388
+ " lcg_gen = lcg(seed)\n",
389
+ " random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
390
+ " max_sum = float('-inf')\n",
391
+ " for i in range(n):\n",
392
+ " current_sum = 0\n",
393
+ " for j in range(i, n):\n",
394
+ " current_sum += random_numbers[j]\n",
395
+ " if current_sum > max_sum:\n",
396
+ " max_sum = current_sum\n",
397
+ " return max_sum\n",
398
+ "\n",
399
+ "def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
400
+ " total_sum = 0\n",
401
+ " lcg_gen = lcg(initial_seed)\n",
402
+ " for _ in range(20):\n",
403
+ " seed = next(lcg_gen)\n",
404
+ " total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
405
+ " return total_sum\n",
406
+ "\n",
407
+ "# Parameters\n",
408
+ "n = 10000 # Number of random numbers\n",
409
+ "initial_seed = 42 # Initial seed for the LCG\n",
410
+ "min_val = -10 # Minimum value of random numbers\n",
411
+ "max_val = 10 # Maximum value of random numbers\n",
412
+ "\n",
413
+ "# Timing the function\n",
414
+ "import time\n",
415
+ "start_time = time.time()\n",
416
+ "result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
417
+ "end_time = time.time()\n",
418
+ "\n",
419
+ "print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
420
+ "print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
421
+ "\"\"\""
422
+ ]
423
+ },
424
+ {
425
+ "cell_type": "code",
426
+ "execution_count": null,
427
+ "id": "dab5e4bc-276c-4555-bd4c-12c699d5e899",
428
+ "metadata": {},
429
+ "outputs": [],
430
+ "source": [
431
+ "exec(python_hard)"
432
+ ]
433
+ },
434
+ {
435
+ "cell_type": "code",
436
+ "execution_count": null,
437
+ "id": "e8d24ed5-2c15-4f55-80e7-13a3952b3cb8",
438
+ "metadata": {},
439
+ "outputs": [],
440
+ "source": [
441
+ "# optimize_gpt(python_hard)"
442
+ ]
443
+ },
444
+ {
445
+ "cell_type": "code",
446
+ "execution_count": null,
447
+ "id": "e0b3d073-88a2-40b2-831c-6f0c345c256f",
448
+ "metadata": {},
449
+ "outputs": [],
450
+ "source": [
451
+ "# # Replace this with the right C++ compile + execute command for your platform\n",
452
+ "\n",
453
+ "# !clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
454
+ "# !./optimized"
455
+ ]
456
+ },
457
+ {
458
+ "cell_type": "code",
459
+ "execution_count": null,
460
+ "id": "e9305446-1d0c-4b51-866a-b8c1e299bf5c",
461
+ "metadata": {},
462
+ "outputs": [],
463
+ "source": [
464
+ "# optimize_claude(python_hard)"
465
+ ]
466
+ },
467
+ {
468
+ "cell_type": "code",
469
+ "execution_count": null,
470
+ "id": "0c181036-8193-4fdd-aef3-fc513b218d43",
471
+ "metadata": {},
472
+ "outputs": [],
473
+ "source": [
474
+ "# Replace this with the right C++ compile + execute command for your platform\n",
475
+ "\n",
476
+ "# !clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
477
+ "# !./optimized"
478
+ ]
479
+ },
480
+ {
481
+ "cell_type": "code",
482
+ "execution_count": 19,
483
+ "id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
484
+ "metadata": {},
485
+ "outputs": [],
486
+ "source": [
487
+ "def stream_gpt(python, language=\"cpp\"):\n",
488
+ " \"\"\"Stream the GPT output for the requested language.\"\"\"\n",
489
+ " if language not in [\"cpp\", \"java\"]:\n",
490
+ " raise ValueError(\"Invalid language specified. Choose 'cpp' or 'java'.\")\n",
491
+ " \n",
492
+ " # Stream response\n",
493
+ " stream = openai.ChatCompletion.create(\n",
494
+ " model=OPENAI_MODEL, messages=messages_for(python, language), stream=True\n",
495
+ " )\n",
496
+ " reply = \"\"\n",
497
+ " code_block = f\"```{language}\\n\" # Detect code block for the language\n",
498
+ "\n",
499
+ " for chunk in stream:\n",
500
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
501
+ " reply += fragment\n",
502
+ " \n",
503
+ " # Clean the streamed reply\n",
504
+ " cleaned_reply = reply.replace(code_block, \"\").replace(\"```\", \"\")\n",
505
+ " yield cleaned_reply\n"
506
+ ]
507
+ },
508
+ {
509
+ "cell_type": "code",
510
+ "execution_count": 20,
511
+ "id": "8669f56b-8314-4582-a167-78842caea131",
512
+ "metadata": {},
513
+ "outputs": [],
514
+ "source": [
515
+ "# def stream_claude(python):\n",
516
+ "# result = claude.messages.stream(\n",
517
+ "# model=CLAUDE_MODEL,\n",
518
+ "# max_tokens=2000,\n",
519
+ "# system=system_message,\n",
520
+ "# messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
521
+ "# )\n",
522
+ "# reply = \"\"\n",
523
+ "# with result as stream:\n",
524
+ "# for text in stream.text_stream:\n",
525
+ "# reply += text\n",
526
+ "# yield reply.replace('```cpp\\n','').replace('```','')"
527
+ ]
528
+ },
529
+ {
530
+ "cell_type": "code",
531
+ "execution_count": 21,
532
+ "id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
533
+ "metadata": {},
534
+ "outputs": [],
535
+ "source": [
536
+ "def optimize(python, model=\"GPT\", language=\"cpp\"):\n",
537
+ " \"\"\"\n",
538
+ " Optimize the given Python code using the specified model (GPT or Claude) and generate the output\n",
539
+ " in the requested programming language.\n",
540
+ "\n",
541
+ " Args:\n",
542
+ " python (str): The Python code to optimize.\n",
543
+ " model (str): The model to use (\"GPT\" or \"Claude\").\n",
544
+ " language (str): The target programming language (\"cpp\" or \"java\").\n",
545
+ "\n",
546
+ " Yields:\n",
547
+ " str: The streamed output of the generated code.\n",
548
+ " \"\"\"\n",
549
+ " if model == \"GPT\":\n",
550
+ " result = stream_gpt(python, language=language)\n",
551
+ " \n",
552
+ " else:\n",
553
+ " raise ValueError(\"Unknown model. Please choose 'GPT' or 'Claude'.\")\n",
554
+ "\n",
555
+ " for stream_so_far in result:\n",
556
+ " yield stream_so_far\n"
557
+ ]
558
+ },
559
+ {
560
+ "cell_type": "code",
561
+ "execution_count": null,
562
+ "id": "f1ddb38e-6b0a-4c37-baa4-ace0b7de887a",
563
+ "metadata": {},
564
+ "outputs": [],
565
+ "source": [
566
+ "# import gradio as gr\n",
567
+ "\n",
568
+ "# # Assuming `optimize` is already defined and imported\n",
569
+ "# # python_hard should be a pre-defined Python code snippet for the default value\n",
570
+ "\n",
571
+ "# with gr.Blocks() as ui:\n",
572
+ "# with gr.Row():\n",
573
+ "# python = gr.Textbox(label=\"Python code:\", lines=10, value=\"\") # Default value can be set here\n",
574
+ "# cpp = gr.Textbox(label=\"Converted code:\", lines=10, interactive=False) # Output box\n",
575
+ "\n",
576
+ "# with gr.Row():\n",
577
+ "# model = gr.Dropdown([\"GPT\", \"Claude\"], label=\"Select model\", value=\"GPT\") # Default is GPT\n",
578
+ "# language = gr.Dropdown([\"cpp\", \"java\"], label=\"Target language\", value=\"cpp\") # Default is C++\n",
579
+ "# convert = gr.Button(\"Convert code\")\n",
580
+ "\n",
581
+ "# # Connect the button to the optimize function\n",
582
+ "# def convert_code(python, model, language):\n",
583
+ "# result = \"\"\n",
584
+ "# for output in optimize(python, model=model, language=language):\n",
585
+ "# result = output # Collect the last streamed result\n",
586
+ "# return result\n",
587
+ "\n",
588
+ "# convert.click(\n",
589
+ "# fn=convert_code,\n",
590
+ "# inputs=[python, model, language], # Inputs from UI\n",
591
+ "# outputs=[cpp], # Output to the C++/Java box\n",
592
+ "# )\n",
593
+ "\n",
594
+ "# ui.launch(inbrowser=True)\n"
595
+ ]
596
+ },
597
+ {
598
+ "cell_type": "code",
599
+ "execution_count": 22,
600
+ "id": "19bf2bff-a822-4009-a539-f003b1651383",
601
+ "metadata": {},
602
+ "outputs": [],
603
+ "source": [
604
+ "import io\n",
605
+ "import sys\n",
606
+ "\n",
607
+ "def execute_python(code):\n",
608
+ " \"\"\"\n",
609
+ " Execute Python code dynamically and capture its output.\n",
610
+ "\n",
611
+ " Args:\n",
612
+ " code (str): The Python code to execute.\n",
613
+ "\n",
614
+ " Returns:\n",
615
+ " str: The captured standard output of the executed code.\n",
616
+ "\n",
617
+ " Raises:\n",
618
+ " Exception: If the execution of the code raises an error.\n",
619
+ " \"\"\"\n",
620
+ " output = io.StringIO()\n",
621
+ " try:\n",
622
+ " sys.stdout = output # Redirect standard output to the StringIO object\n",
623
+ " exec(code, {}) # Execute code with an empty global context for safety\n",
624
+ " except Exception as e:\n",
625
+ " return f\"Error during execution: {str(e)}\"\n",
626
+ " finally:\n",
627
+ " sys.stdout = sys.__stdout__ # Restore standard output\n",
628
+ "\n",
629
+ " return output.getvalue()\n"
630
+ ]
631
+ },
632
+ {
633
+ "cell_type": "code",
634
+ "execution_count": 23,
635
+ "id": "77f3ab5d-fcfb-4d3f-8728-9cacbf833ea6",
636
+ "metadata": {},
637
+ "outputs": [],
638
+ "source": [
639
+ "# You'll need to change the code in the try block to compile the C++ code for your platform\n",
640
+ "# I pasted this into Claude's chat UI with a request for it to give me a version for an Intel PC,\n",
641
+ "# and it responded with something that looks perfect - you can try a similar approach for your platform.\n",
642
+ "\n",
643
+ "# M1 Mac version to compile and execute optimized C++ code:\n",
644
+ "\n",
645
+ "def execute_cpp(code):\n",
646
+ " write_output(code)\n",
647
+ " try:\n",
648
+ " compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", \"optimized\", \"optimized.cpp\"]\n",
649
+ " compile_result = subprocess.run(compile_cmd, check=True, text=True, capture_output=True)\n",
650
+ " run_cmd = [\"./optimized\"]\n",
651
+ " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
652
+ " return run_result.stdout\n",
653
+ " except subprocess.CalledProcessError as e:\n",
654
+ " return f\"An error occurred:\\n{e.stderr}\""
655
+ ]
656
+ },
657
+ {
658
+ "cell_type": "code",
659
+ "execution_count": 36,
660
+ "id": "9645b5c4-41a1-4a88-a5e6-cf618864af04",
661
+ "metadata": {},
662
+ "outputs": [],
663
+ "source": [
664
+ "\n",
665
+ "def execute_java(code):\n",
666
+ " \"\"\"Compile and execute Java code dynamically.\"\"\"\n",
667
+ " write_output(code, \"Optimized.java\")\n",
668
+ " try:\n",
669
+ " # Compile the Java code\n",
670
+ " compile_cmd = [\"javac\", \"Optimized.java\"]\n",
671
+ " subprocess.run(compile_cmd, check=True, text=True, capture_output=True)\n",
672
+ " \n",
673
+ " # Run the compiled Java program\n",
674
+ " run_cmd = [\"java\", \"Optimized\"]\n",
675
+ " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
676
+ " return run_result.stdout # Return the output\n",
677
+ " except subprocess.CalledProcessError as e:\n",
678
+ " return f\"Error during compilation or execution:\\n{e.stderr}\""
679
+ ]
680
+ },
681
+ {
682
+ "cell_type": "code",
683
+ "execution_count": 24,
684
+ "id": "9a2274f1-d03b-42c0-8dcc-4ce159b18442",
685
+ "metadata": {},
686
+ "outputs": [],
687
+ "source": [
688
+ "# css = \"\"\"\n",
689
+ "# .python {background-color: #306998;}\n",
690
+ "# .cpp {background-color: #050;}\n",
691
+ "# \"\"\""
692
+ ]
693
+ },
694
+ {
695
+ "cell_type": "code",
696
+ "execution_count": 40,
697
+ "id": "f1303932-160c-424b-97a8-d28c816721b2",
698
+ "metadata": {},
699
+ "outputs": [
700
+ {
701
+ "data": {
702
+ "text/html": [
703
+ "<div><iframe src=\"http://127.0.0.1:7864/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
704
+ ],
705
+ "text/plain": [
706
+ "<IPython.core.display.HTML object>"
707
+ ]
708
+ },
709
+ "metadata": {},
710
+ "output_type": "display_data"
711
+ },
712
+ {
713
+ "data": {
714
+ "text/plain": []
715
+ },
716
+ "execution_count": 40,
717
+ "metadata": {},
718
+ "output_type": "execute_result"
719
+ }
720
+ ],
721
+ "source": [
722
+ "css = \"\"\"\n",
723
+ ".python {background-color: #306998;}\n",
724
+ ".cpp {background-color: #050;}\n",
725
+ ".java {background-color: #b07219;}\n",
726
+ "\"\"\"\n",
727
+ "\n",
728
+ "with gr.Blocks(css=css) as ui:\n",
729
+ " gr.Markdown(\"## Convert code from Python to C++, Java, or Run Directly\")\n",
730
+ " \n",
731
+ " with gr.Row():\n",
732
+ " python = gr.Textbox(label=\"Python code:\", value=\"print('Hello from Python!')\", lines=10)\n",
733
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
734
+ " java = gr.Textbox(label=\"Java code:\", lines=10)\n",
735
+ " \n",
736
+ " with gr.Row():\n",
737
+ " model = gr.Dropdown([\"GPT\"], label=\"Select model\", value=\"GPT\")\n",
738
+ " \n",
739
+ " with gr.Row():\n",
740
+ " convert_cpp = gr.Button(\"Convert to C++\")\n",
741
+ " convert_java = gr.Button(\"Convert to Java\")\n",
742
+ " \n",
743
+ " with gr.Row():\n",
744
+ " python_run = gr.Button(\"Run Python\")\n",
745
+ " cpp_run = gr.Button(\"Run C++\")\n",
746
+ " java_run = gr.Button(\"Run Java\")\n",
747
+ " \n",
748
+ " with gr.Row():\n",
749
+ " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
750
+ " cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
751
+ " java_out = gr.TextArea(label=\"Java result:\", elem_classes=[\"java\"])\n",
752
+ "\n",
753
+ " # Add C++ and Java conversion\n",
754
+ " convert_cpp.click(optimize, inputs=[python, model], outputs=[cpp])\n",
755
+ " convert_java.click(optimize, inputs=[python, model], outputs=[java])\n",
756
+ " \n",
757
+ " # Add execution buttons for each language\n",
758
+ " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
759
+ " cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
760
+ " java_run.click(execute_java, inputs=[java], outputs=[java_out])\n",
761
+ "\n",
762
+ "ui.launch(inbrowser=True)\n"
763
+ ]
764
+ },
765
+ {
766
+ "cell_type": "code",
767
+ "execution_count": null,
768
+ "id": "1e910bc5-b343-48e8-9da5-2ce8e2ab888e",
769
+ "metadata": {},
770
+ "outputs": [],
771
+ "source": []
772
+ }
773
+ ],
774
+ "metadata": {
775
+ "kernelspec": {
776
+ "display_name": "Python 3 (ipykernel)",
777
+ "language": "python",
778
+ "name": "python3"
779
+ },
780
+ "language_info": {
781
+ "codemirror_mode": {
782
+ "name": "ipython",
783
+ "version": 3
784
+ },
785
+ "file_extension": ".py",
786
+ "mimetype": "text/x-python",
787
+ "name": "python",
788
+ "nbconvert_exporter": "python",
789
+ "pygments_lexer": "ipython3",
790
+ "version": "3.11.11"
791
+ }
792
+ },
793
+ "nbformat": 4,
794
+ "nbformat_minor": 5
795
+ }
community-contributions/Code_Converter(Added_Java_Support)(Open_Ai_Only).ipynb ADDED
@@ -0,0 +1,824 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Code Generator\n",
9
+ "\n",
10
+ "The requirement: use a Frontier model to generate high performance C++ code from Python code\n"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": 1,
16
+ "id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "# imports\n",
21
+ "\n",
22
+ "import os\n",
23
+ "import io\n",
24
+ "import sys\n",
25
+ "from dotenv import load_dotenv\n",
26
+ "from openai import OpenAI\n",
27
+ "import google.generativeai\n",
28
+ "import anthropic\n",
29
+ "from IPython.display import Markdown, display, update_display\n",
30
+ "import gradio as gr\n",
31
+ "import subprocess"
32
+ ]
33
+ },
34
+ {
35
+ "cell_type": "code",
36
+ "execution_count": 2,
37
+ "id": "4f672e1c-87e9-4865-b760-370fa605e614",
38
+ "metadata": {},
39
+ "outputs": [],
40
+ "source": [
41
+ "# environment\n",
42
+ "\n",
43
+ "load_dotenv()\n",
44
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
45
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')"
46
+ ]
47
+ },
48
+ {
49
+ "cell_type": "code",
50
+ "execution_count": 28,
51
+ "id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
52
+ "metadata": {},
53
+ "outputs": [],
54
+ "source": [
55
+ "# initialize\n",
56
+ "# NOTE - option to use ultra-low cost models by uncommenting last 2 lines\n",
57
+ "\n",
58
+ "openai = OpenAI()\n",
59
+ "# claude = anthropic.Anthropic()\n",
60
+ "OPENAI_MODEL = \"gpt-4o\"\n",
61
+ "# CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\"\n",
62
+ "\n",
63
+ "# Want to keep costs ultra-low? Uncomment these lines:\n",
64
+ "# OPENAI_MODEL = \"gpt-4o-mini\"\n",
65
+ "# CLAUDE_MODEL = \"claude-3-haiku-20240307\""
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "execution_count": 4,
71
+ "id": "6896636f-923e-4a2c-9d6c-fac07828a201",
72
+ "metadata": {},
73
+ "outputs": [],
74
+ "source": [
75
+ "# system_message = \"You are an assistant that reimplements Python code in high-performance C++ and Java for an M1 Mac. \"\n",
76
+ "# system_message += \"Respond only with C++ and Java code; use comments sparingly and do not provide any explanation other than occasional comments. \"\n",
77
+ "# system_message += \"The C++ and Java responses need to produce identical output in the fastest possible time.\"\n"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "execution_count": 5,
83
+ "id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
84
+ "metadata": {},
85
+ "outputs": [],
86
+ "source": [
87
+ "# def user_prompt_for(python):\n",
88
+ "# user_prompt = \"Rewrite this Python code in C++ and Java with the fastest possible implementation that produces identical output in the least time. \"\n",
89
+ "# user_prompt += \"Respond only with C++ and Java code; do not explain your work other than a few comments. \"\n",
90
+ "# user_prompt += \"Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip for C++, and import required packages for Java.\\n\\n\"\n",
91
+ "# user_prompt += python\n",
92
+ "# return user_prompt\n"
93
+ ]
94
+ },
95
+ {
96
+ "cell_type": "code",
97
+ "execution_count": 6,
98
+ "id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
99
+ "metadata": {},
100
+ "outputs": [],
101
+ "source": [
102
+ "# def messages_for(python):\n",
103
+ "# return [\n",
104
+ "# {\"role\": \"system\", \"content\": system_message}, # Includes the updated system message with C++ and Java\n",
105
+ "# {\"role\": \"user\", \"content\": user_prompt_for(python)} # Calls the updated user prompt function for C++ and Java\n",
106
+ "# ]\n"
107
+ ]
108
+ },
109
+ {
110
+ "cell_type": "code",
111
+ "execution_count": 29,
112
+ "id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
113
+ "metadata": {},
114
+ "outputs": [],
115
+ "source": [
116
+ "def write_output(code, file_name):\n",
117
+ " \"\"\"Write the generated code to a file.\"\"\"\n",
118
+ " with open(file_name, \"w\") as f:\n",
119
+ " f.write(code)\n"
120
+ ]
121
+ },
122
+ {
123
+ "cell_type": "code",
124
+ "execution_count": 30,
125
+ "id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
126
+ "metadata": {},
127
+ "outputs": [],
128
+ "source": [
129
+ "def system_message_for(language):\n",
130
+ " \"\"\"Create a system message tailored for the requested language.\"\"\"\n",
131
+ " return (\n",
132
+ " f\"You are an assistant that reimplements Python code in high-performance {language.upper()} for an M1 Mac. \"\n",
133
+ " f\"Respond only with {language.upper()} code; do not explain your work other than occasional comments. \"\n",
134
+ " \"Pay attention to number types to ensure no overflows and include all necessary packages.\\n\\n\"\n",
135
+ " )\n",
136
+ "\n",
137
+ "def user_prompt_for(python):\n",
138
+ " \"\"\"Generate the user prompt.\"\"\"\n",
139
+ " return (\n",
140
+ " \"Rewrite this Python code in the requested language with the fastest possible implementation that produces \"\n",
141
+ " \"identical output in the least time. Use appropriate syntax for the language.\\n\\n\" + python\n",
142
+ " )\n",
143
+ "\n",
144
+ "def messages_for(python, language):\n",
145
+ " \"\"\"Generate the messages for GPT.\"\"\"\n",
146
+ " return [\n",
147
+ " {\"role\": \"system\", \"content\": system_message_for(language)},\n",
148
+ " {\"role\": \"user\", \"content\": user_prompt_for(python)},\n",
149
+ " ]\n",
150
+ " "
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": 31,
156
+ "id": "3ec5a816-7bf4-4daa-b0c9-f04edb1c0140",
157
+ "metadata": {},
158
+ "outputs": [],
159
+ "source": [
160
+ "def optimize_gpt(python, language=\"cpp\"):\n",
161
+ " \"\"\"Optimize the given Python code and generate C++ or Java output.\"\"\"\n",
162
+ " code = \"\"\n",
163
+ " for chunk in stream_gpt(python, language):\n",
164
+ " print(chunk, end=\"\") # Stream the output\n",
165
+ " code = chunk # Store the final code\n",
166
+ " \n",
167
+ " file_name = f\"optimized.{language}\"\n",
168
+ " write_output(code, file_name)\n",
169
+ " print(f\"\\nCode written to {file_name}.\")"
170
+ ]
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "execution_count": null,
175
+ "id": "8adf0436-cf0e-429c-bd35-c3d551631b27",
176
+ "metadata": {},
177
+ "outputs": [],
178
+ "source": []
179
+ },
180
+ {
181
+ "cell_type": "code",
182
+ "execution_count": null,
183
+ "id": "7cd84ad8-d55c-4fe0-9eeb-1895c95c4a9d",
184
+ "metadata": {},
185
+ "outputs": [],
186
+ "source": [
187
+ "# def optimize_claude(python):\n",
188
+ "# result = claude.messages.stream(\n",
189
+ "# model=CLAUDE_MODEL,\n",
190
+ "# max_tokens=2000,\n",
191
+ "# system=system_message,\n",
192
+ "# messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
193
+ "# )\n",
194
+ "# reply = \"\"\n",
195
+ "# with result as stream:\n",
196
+ "# for text in stream.text_stream:\n",
197
+ "# reply += text\n",
198
+ "# print(text, end=\"\", flush=True)\n",
199
+ "# write_output(reply)"
200
+ ]
201
+ },
202
+ {
203
+ "cell_type": "code",
204
+ "execution_count": 32,
205
+ "id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
206
+ "metadata": {},
207
+ "outputs": [],
208
+ "source": [
209
+ "pi = \"\"\"\n",
210
+ "import time\n",
211
+ "\n",
212
+ "def calculate(iterations, param1, param2):\n",
213
+ " result = 1.0\n",
214
+ " for i in range(1, iterations+1):\n",
215
+ " j = i * param1 - param2\n",
216
+ " result -= (1/j)\n",
217
+ " j = i * param1 + param2\n",
218
+ " result += (1/j)\n",
219
+ " return result\n",
220
+ "\n",
221
+ "start_time = time.time()\n",
222
+ "result = calculate(100_000_000, 4, 1) * 4\n",
223
+ "end_time = time.time()\n",
224
+ "\n",
225
+ "print(f\"Result: {result:.12f}\")\n",
226
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
227
+ "\"\"\""
228
+ ]
229
+ },
230
+ {
231
+ "cell_type": "code",
232
+ "execution_count": 10,
233
+ "id": "7fe1cd4b-d2c5-4303-afed-2115a3fef200",
234
+ "metadata": {},
235
+ "outputs": [
236
+ {
237
+ "name": "stdout",
238
+ "output_type": "stream",
239
+ "text": [
240
+ "Result: 3.141592658589\n",
241
+ "Execution Time: 46.954224 seconds\n"
242
+ ]
243
+ }
244
+ ],
245
+ "source": [
246
+ "exec(pi)"
247
+ ]
248
+ },
249
+ {
250
+ "cell_type": "code",
251
+ "execution_count": null,
252
+ "id": "57ea7f4b-a862-4805-a074-2019314cbd4a",
253
+ "metadata": {},
254
+ "outputs": [],
255
+ "source": [
256
+ "optimize_gpt(python_code, language=\"java\")\n"
257
+ ]
258
+ },
259
+ {
260
+ "cell_type": "code",
261
+ "execution_count": null,
262
+ "id": "105db6f9-343c-491d-8e44-3a5328b81719",
263
+ "metadata": {},
264
+ "outputs": [],
265
+ "source": [
266
+ "optimize_gpt(python_code, language=\"cpp\")\n"
267
+ ]
268
+ },
269
+ {
270
+ "cell_type": "code",
271
+ "execution_count": null,
272
+ "id": "bf26ee95-0c77-491d-9a91-579a1e96a8a3",
273
+ "metadata": {},
274
+ "outputs": [],
275
+ "source": [
276
+ "exec(pi)"
277
+ ]
278
+ },
279
+ {
280
+ "cell_type": "markdown",
281
+ "id": "bf8f8018-f64d-425c-a0e1-d7862aa9592d",
282
+ "metadata": {},
283
+ "source": [
284
+ "# Compiling C++ and executing\n",
285
+ "\n",
286
+ "You can use any platform now (Windows,Mac,Linux) i have added compatiblity in this"
287
+ ]
288
+ },
289
+ {
290
+ "cell_type": "code",
291
+ "execution_count": null,
292
+ "id": "4194e40c-04ab-4940-9d64-b4ad37c5bb40",
293
+ "metadata": {},
294
+ "outputs": [],
295
+ "source": [
296
+ "import subprocess\n",
297
+ "import platform\n",
298
+ "\n",
299
+ "def compile_and_run(language=\"cpp\"):\n",
300
+ " \"\"\"Compile and run the generated code.\"\"\"\n",
301
+ " is_windows = platform.system() == \"Windows\"\n",
302
+ " \n",
303
+ " if language == \"cpp\":\n",
304
+ " if is_windows:\n",
305
+ " # Windows: Use g++ (requires MinGW or equivalent installed)\n",
306
+ " compile_command = [\"g++\", \"-O3\", \"-std=c++17\", \"-o\", \"optimized.exe\", \"optimized.cpp\"]\n",
307
+ " execute_command = [\"optimized.exe\"]\n",
308
+ " else:\n",
309
+ " # Non-Windows: Use clang++\n",
310
+ " compile_command = [\n",
311
+ " \"clang++\", \"-O3\", \"-std=c++17\", \"-march=armv8.3-a\", \"-o\", \"optimized\", \"optimized.cpp\"\n",
312
+ " ]\n",
313
+ " execute_command = [\"./optimized\"]\n",
314
+ " elif language == \"java\":\n",
315
+ " # Both Windows and non-Windows use the same Java commands\n",
316
+ " compile_command = [\"javac\", \"optimized.java\"]\n",
317
+ " execute_command = [\"java\", \"optimized\"]\n",
318
+ " else:\n",
319
+ " raise ValueError(\"Unsupported language. Choose 'cpp' or 'java'.\")\n",
320
+ "\n",
321
+ " # Compile\n",
322
+ " try:\n",
323
+ " subprocess.run(compile_command, check=True, shell=is_windows)\n",
324
+ " print(f\"{language.upper()} compilation successful.\")\n",
325
+ " except subprocess.CalledProcessError as e:\n",
326
+ " print(f\"{language.upper()} compilation failed:\\n{e}\")\n",
327
+ " return\n",
328
+ "\n",
329
+ " # Run\n",
330
+ " try:\n",
331
+ " output = subprocess.run(\n",
332
+ " execute_command, capture_output=True, text=True, shell=is_windows\n",
333
+ " )\n",
334
+ " print(f\"{language.upper()} execution output:\\n{output.stdout}\")\n",
335
+ " except subprocess.CalledProcessError as e:\n",
336
+ " print(f\"{language.upper()} execution failed:\\n{e.stderr}\")\n"
337
+ ]
338
+ },
339
+ {
340
+ "cell_type": "code",
341
+ "execution_count": null,
342
+ "id": "240ae457-ec5a-4268-9e7d-e782c2113f02",
343
+ "metadata": {},
344
+ "outputs": [],
345
+ "source": [
346
+ "compile_and_run(language=\"cpp\")\n"
347
+ ]
348
+ },
349
+ {
350
+ "cell_type": "code",
351
+ "execution_count": null,
352
+ "id": "983a11fe-e24d-4c65-8269-9802c5ef3ae6",
353
+ "metadata": {},
354
+ "outputs": [],
355
+ "source": [
356
+ "# optimize_claude(pi)"
357
+ ]
358
+ },
359
+ {
360
+ "cell_type": "code",
361
+ "execution_count": null,
362
+ "id": "d5a766f9-3d23-4bb4-a1d4-88ec44b61ddf",
363
+ "metadata": {},
364
+ "outputs": [],
365
+ "source": [
366
+ "# Repeat for Claude - again, use the right approach for your platform\n",
367
+ "\n",
368
+ "# !clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
369
+ "# !./optimized"
370
+ ]
371
+ },
372
+ {
373
+ "cell_type": "code",
374
+ "execution_count": 26,
375
+ "id": "c3b497b3-f569-420e-b92e-fb0f49957ce0",
376
+ "metadata": {},
377
+ "outputs": [],
378
+ "source": [
379
+ "python_hard = \"\"\"# Be careful to support large number sizes\n",
380
+ "\n",
381
+ "def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
382
+ " value = seed\n",
383
+ " while True:\n",
384
+ " value = (a * value + c) % m\n",
385
+ " yield value\n",
386
+ " \n",
387
+ "def max_subarray_sum(n, seed, min_val, max_val):\n",
388
+ " lcg_gen = lcg(seed)\n",
389
+ " random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
390
+ " max_sum = float('-inf')\n",
391
+ " for i in range(n):\n",
392
+ " current_sum = 0\n",
393
+ " for j in range(i, n):\n",
394
+ " current_sum += random_numbers[j]\n",
395
+ " if current_sum > max_sum:\n",
396
+ " max_sum = current_sum\n",
397
+ " return max_sum\n",
398
+ "\n",
399
+ "def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
400
+ " total_sum = 0\n",
401
+ " lcg_gen = lcg(initial_seed)\n",
402
+ " for _ in range(20):\n",
403
+ " seed = next(lcg_gen)\n",
404
+ " total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
405
+ " return total_sum\n",
406
+ "\n",
407
+ "# Parameters\n",
408
+ "n = 10000 # Number of random numbers\n",
409
+ "initial_seed = 42 # Initial seed for the LCG\n",
410
+ "min_val = -10 # Minimum value of random numbers\n",
411
+ "max_val = 10 # Maximum value of random numbers\n",
412
+ "\n",
413
+ "# Timing the function\n",
414
+ "import time\n",
415
+ "start_time = time.time()\n",
416
+ "result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
417
+ "end_time = time.time()\n",
418
+ "\n",
419
+ "print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
420
+ "print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
421
+ "\"\"\""
422
+ ]
423
+ },
424
+ {
425
+ "cell_type": "code",
426
+ "execution_count": null,
427
+ "id": "dab5e4bc-276c-4555-bd4c-12c699d5e899",
428
+ "metadata": {},
429
+ "outputs": [],
430
+ "source": [
431
+ "exec(python_hard)"
432
+ ]
433
+ },
434
+ {
435
+ "cell_type": "code",
436
+ "execution_count": null,
437
+ "id": "e8d24ed5-2c15-4f55-80e7-13a3952b3cb8",
438
+ "metadata": {},
439
+ "outputs": [],
440
+ "source": [
441
+ "# optimize_gpt(python_hard)"
442
+ ]
443
+ },
444
+ {
445
+ "cell_type": "code",
446
+ "execution_count": null,
447
+ "id": "e0b3d073-88a2-40b2-831c-6f0c345c256f",
448
+ "metadata": {},
449
+ "outputs": [],
450
+ "source": [
451
+ "# # Replace this with the right C++ compile + execute command for your platform\n",
452
+ "\n",
453
+ "# !clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
454
+ "# !./optimized"
455
+ ]
456
+ },
457
+ {
458
+ "cell_type": "code",
459
+ "execution_count": null,
460
+ "id": "e9305446-1d0c-4b51-866a-b8c1e299bf5c",
461
+ "metadata": {},
462
+ "outputs": [],
463
+ "source": [
464
+ "# optimize_claude(python_hard)"
465
+ ]
466
+ },
467
+ {
468
+ "cell_type": "code",
469
+ "execution_count": null,
470
+ "id": "0c181036-8193-4fdd-aef3-fc513b218d43",
471
+ "metadata": {},
472
+ "outputs": [],
473
+ "source": [
474
+ "# Replace this with the right C++ compile + execute command for your platform\n",
475
+ "\n",
476
+ "# !clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
477
+ "# !./optimized"
478
+ ]
479
+ },
480
+ {
481
+ "cell_type": "code",
482
+ "execution_count": 19,
483
+ "id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
484
+ "metadata": {},
485
+ "outputs": [],
486
+ "source": [
487
+ "def stream_gpt(python, language=\"cpp\"):\n",
488
+ " \"\"\"Stream the GPT output for the requested language.\"\"\"\n",
489
+ " if language not in [\"cpp\", \"java\"]:\n",
490
+ " raise ValueError(\"Invalid language specified. Choose 'cpp' or 'java'.\")\n",
491
+ " \n",
492
+ " # Stream response\n",
493
+ " stream = openai.ChatCompletion.create(\n",
494
+ " model=OPENAI_MODEL, messages=messages_for(python, language), stream=True\n",
495
+ " )\n",
496
+ " reply = \"\"\n",
497
+ " code_block = f\"```{language}\\n\" # Detect code block for the language\n",
498
+ "\n",
499
+ " for chunk in stream:\n",
500
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
501
+ " reply += fragment\n",
502
+ " \n",
503
+ " # Clean the streamed reply\n",
504
+ " cleaned_reply = reply.replace(code_block, \"\").replace(\"```\", \"\")\n",
505
+ " yield cleaned_reply\n"
506
+ ]
507
+ },
508
+ {
509
+ "cell_type": "code",
510
+ "execution_count": 20,
511
+ "id": "8669f56b-8314-4582-a167-78842caea131",
512
+ "metadata": {},
513
+ "outputs": [],
514
+ "source": [
515
+ "# def stream_claude(python):\n",
516
+ "# result = claude.messages.stream(\n",
517
+ "# model=CLAUDE_MODEL,\n",
518
+ "# max_tokens=2000,\n",
519
+ "# system=system_message,\n",
520
+ "# messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
521
+ "# )\n",
522
+ "# reply = \"\"\n",
523
+ "# with result as stream:\n",
524
+ "# for text in stream.text_stream:\n",
525
+ "# reply += text\n",
526
+ "# yield reply.replace('```cpp\\n','').replace('```','')"
527
+ ]
528
+ },
529
+ {
530
+ "cell_type": "code",
531
+ "execution_count": 21,
532
+ "id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
533
+ "metadata": {},
534
+ "outputs": [],
535
+ "source": [
536
+ "def optimize(python, model=\"GPT\", language=\"cpp\"):\n",
537
+ " \"\"\"\n",
538
+ " Optimize the given Python code using the specified model (GPT or Claude) and generate the output\n",
539
+ " in the requested programming language.\n",
540
+ "\n",
541
+ " Args:\n",
542
+ " python (str): The Python code to optimize.\n",
543
+ " model (str): The model to use (\"GPT\" or \"Claude\").\n",
544
+ " language (str): The target programming language (\"cpp\" or \"java\").\n",
545
+ "\n",
546
+ " Yields:\n",
547
+ " str: The streamed output of the generated code.\n",
548
+ " \"\"\"\n",
549
+ " if model == \"GPT\":\n",
550
+ " result = stream_gpt(python, language=language)\n",
551
+ " \n",
552
+ " else:\n",
553
+ " raise ValueError(\"Unknown model. Please choose 'GPT' or 'Claude'.\")\n",
554
+ "\n",
555
+ " for stream_so_far in result:\n",
556
+ " yield stream_so_far\n"
557
+ ]
558
+ },
559
+ {
560
+ "cell_type": "code",
561
+ "execution_count": null,
562
+ "id": "f1ddb38e-6b0a-4c37-baa4-ace0b7de887a",
563
+ "metadata": {},
564
+ "outputs": [],
565
+ "source": [
566
+ "# import gradio as gr\n",
567
+ "\n",
568
+ "# # Assuming `optimize` is already defined and imported\n",
569
+ "# # python_hard should be a pre-defined Python code snippet for the default value\n",
570
+ "\n",
571
+ "# with gr.Blocks() as ui:\n",
572
+ "# with gr.Row():\n",
573
+ "# python = gr.Textbox(label=\"Python code:\", lines=10, value=\"\") # Default value can be set here\n",
574
+ "# cpp = gr.Textbox(label=\"Converted code:\", lines=10, interactive=False) # Output box\n",
575
+ "\n",
576
+ "# with gr.Row():\n",
577
+ "# model = gr.Dropdown([\"GPT\", \"Claude\"], label=\"Select model\", value=\"GPT\") # Default is GPT\n",
578
+ "# language = gr.Dropdown([\"cpp\", \"java\"], label=\"Target language\", value=\"cpp\") # Default is C++\n",
579
+ "# convert = gr.Button(\"Convert code\")\n",
580
+ "\n",
581
+ "# # Connect the button to the optimize function\n",
582
+ "# def convert_code(python, model, language):\n",
583
+ "# result = \"\"\n",
584
+ "# for output in optimize(python, model=model, language=language):\n",
585
+ "# result = output # Collect the last streamed result\n",
586
+ "# return result\n",
587
+ "\n",
588
+ "# convert.click(\n",
589
+ "# fn=convert_code,\n",
590
+ "# inputs=[python, model, language], # Inputs from UI\n",
591
+ "# outputs=[cpp], # Output to the C++/Java box\n",
592
+ "# )\n",
593
+ "\n",
594
+ "# ui.launch(inbrowser=True)\n"
595
+ ]
596
+ },
597
+ {
598
+ "cell_type": "code",
599
+ "execution_count": 22,
600
+ "id": "19bf2bff-a822-4009-a539-f003b1651383",
601
+ "metadata": {},
602
+ "outputs": [],
603
+ "source": [
604
+ "import io\n",
605
+ "import sys\n",
606
+ "\n",
607
+ "def execute_python(code):\n",
608
+ " \"\"\"\n",
609
+ " Execute Python code dynamically and capture its output.\n",
610
+ "\n",
611
+ " Args:\n",
612
+ " code (str): The Python code to execute.\n",
613
+ "\n",
614
+ " Returns:\n",
615
+ " str: The captured standard output of the executed code.\n",
616
+ "\n",
617
+ " Raises:\n",
618
+ " Exception: If the execution of the code raises an error.\n",
619
+ " \"\"\"\n",
620
+ " output = io.StringIO()\n",
621
+ " try:\n",
622
+ " sys.stdout = output # Redirect standard output to the StringIO object\n",
623
+ " exec(code, {}) # Execute code with an empty global context for safety\n",
624
+ " except Exception as e:\n",
625
+ " return f\"Error during execution: {str(e)}\"\n",
626
+ " finally:\n",
627
+ " sys.stdout = sys.__stdout__ # Restore standard output\n",
628
+ "\n",
629
+ " return output.getvalue()\n"
630
+ ]
631
+ },
632
+ {
633
+ "cell_type": "code",
634
+ "execution_count": 23,
635
+ "id": "77f3ab5d-fcfb-4d3f-8728-9cacbf833ea6",
636
+ "metadata": {},
637
+ "outputs": [],
638
+ "source": [
639
+ "# You'll need to change the code in the try block to compile the C++ code for your platform\n",
640
+ "# I pasted this into Claude's chat UI with a request for it to give me a version for an Intel PC,\n",
641
+ "# and it responded with something that looks perfect - you can try a similar approach for your platform.\n",
642
+ "\n",
643
+ "# M1 Mac version to compile and execute optimized C++ code:\n",
644
+ "\n",
645
+ "def execute_cpp(code):\n",
646
+ " write_output(code)\n",
647
+ " try:\n",
648
+ " compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", \"optimized\", \"optimized.cpp\"]\n",
649
+ " compile_result = subprocess.run(compile_cmd, check=True, text=True, capture_output=True)\n",
650
+ " run_cmd = [\"./optimized\"]\n",
651
+ " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
652
+ " return run_result.stdout\n",
653
+ " except subprocess.CalledProcessError as e:\n",
654
+ " return f\"An error occurred:\\n{e.stderr}\""
655
+ ]
656
+ },
657
+ {
658
+ "cell_type": "code",
659
+ "execution_count": 36,
660
+ "id": "9645b5c4-41a1-4a88-a5e6-cf618864af04",
661
+ "metadata": {},
662
+ "outputs": [],
663
+ "source": [
664
+ "\n",
665
+ "def execute_java(code):\n",
666
+ " \"\"\"Compile and execute Java code dynamically.\"\"\"\n",
667
+ " write_output(code, \"Optimized.java\")\n",
668
+ " try:\n",
669
+ " # Compile the Java code\n",
670
+ " compile_cmd = [\"javac\", \"Optimized.java\"]\n",
671
+ " subprocess.run(compile_cmd, check=True, text=True, capture_output=True)\n",
672
+ " \n",
673
+ " # Run the compiled Java program\n",
674
+ " run_cmd = [\"java\", \"Optimized\"]\n",
675
+ " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
676
+ " return run_result.stdout # Return the output\n",
677
+ " except subprocess.CalledProcessError as e:\n",
678
+ " return f\"Error during compilation or execution:\\n{e.stderr}\""
679
+ ]
680
+ },
681
+ {
682
+ "cell_type": "code",
683
+ "execution_count": 24,
684
+ "id": "9a2274f1-d03b-42c0-8dcc-4ce159b18442",
685
+ "metadata": {},
686
+ "outputs": [],
687
+ "source": [
688
+ "# css = \"\"\"\n",
689
+ "# .python {background-color: #306998;}\n",
690
+ "# .cpp {background-color: #050;}\n",
691
+ "# \"\"\""
692
+ ]
693
+ },
694
+ {
695
+ "cell_type": "code",
696
+ "execution_count": 40,
697
+ "id": "f1303932-160c-424b-97a8-d28c816721b2",
698
+ "metadata": {},
699
+ "outputs": [
700
+ {
701
+ "data": {
702
+ "text/html": [
703
+ "<div><iframe src=\"http://127.0.0.1:7864/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
704
+ ],
705
+ "text/plain": [
706
+ "<IPython.core.display.HTML object>"
707
+ ]
708
+ },
709
+ "metadata": {},
710
+ "output_type": "display_data"
711
+ },
712
+ {
713
+ "data": {
714
+ "text/plain": []
715
+ },
716
+ "execution_count": 40,
717
+ "metadata": {},
718
+ "output_type": "execute_result"
719
+ }
720
+ ],
721
+ "source": [
722
+ "css = \"\"\"\n",
723
+ ".python {background-color: #306998;}\n",
724
+ ".cpp {background-color: #050;}\n",
725
+ ".java {background-color: #b07219;}\n",
726
+ "\"\"\"\n",
727
+ "\n",
728
+ "with gr.Blocks(css=css) as ui:\n",
729
+ " gr.Markdown(\"## Convert code from Python to C++, Java, or Run Directly\")\n",
730
+ " \n",
731
+ " with gr.Row():\n",
732
+ " python = gr.Textbox(label=\"Python code:\", value=\"print('Hello from Python!')\", lines=10)\n",
733
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
734
+ " java = gr.Textbox(label=\"Java code:\", lines=10)\n",
735
+ " \n",
736
+ " with gr.Row():\n",
737
+ " model = gr.Dropdown([\"GPT\"], label=\"Select model\", value=\"GPT\")\n",
738
+ " \n",
739
+ " with gr.Row():\n",
740
+ " convert_cpp = gr.Button(\"Convert to C++\")\n",
741
+ " convert_java = gr.Button(\"Convert to Java\")\n",
742
+ " \n",
743
+ " with gr.Row():\n",
744
+ " python_run = gr.Button(\"Run Python\")\n",
745
+ " cpp_run = gr.Button(\"Run C++\")\n",
746
+ " java_run = gr.Button(\"Run Java\")\n",
747
+ " \n",
748
+ " with gr.Row():\n",
749
+ " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
750
+ " cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
751
+ " java_out = gr.TextArea(label=\"Java result:\", elem_classes=[\"java\"])\n",
752
+ "\n",
753
+ " # Add C++ and Java conversion\n",
754
+ " convert_cpp.click(optimize, inputs=[python, model], outputs=[cpp])\n",
755
+ " convert_java.click(optimize, inputs=[python, model], outputs=[java])\n",
756
+ " \n",
757
+ " # Add execution buttons for each language\n",
758
+ " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
759
+ " cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
760
+ " java_run.click(execute_java, inputs=[java], outputs=[java_out])\n",
761
+ "\n",
762
+ "ui.launch(inbrowser=True)\n"
763
+ ]
764
+ },
765
+ {
766
+ "cell_type": "code",
767
+ "execution_count": 1,
768
+ "id": "1e910bc5-b343-48e8-9da5-2ce8e2ab888e",
769
+ "metadata": {},
770
+ "outputs": [
771
+ {
772
+ "name": "stdout",
773
+ "output_type": "stream",
774
+ "text": [
775
+ "Entering context\n",
776
+ "Using Resource\n",
777
+ "Exiting context\n"
778
+ ]
779
+ }
780
+ ],
781
+ "source": [
782
+ "class MyContext:\n",
783
+ " def __enter__(self):\n",
784
+ " print(\"Entering context\")\n",
785
+ " return \"Resource\"\n",
786
+ " \n",
787
+ " def __exit__(self, exc_type, exc_val, exc_tb):\n",
788
+ " print(\"Exiting context\")\n",
789
+ "\n",
790
+ "with MyContext() as x:\n",
791
+ " print(\"Using\", x)\n"
792
+ ]
793
+ },
794
+ {
795
+ "cell_type": "code",
796
+ "execution_count": null,
797
+ "id": "997f1aaa-53bf-4958-b99b-07389e7ccbbe",
798
+ "metadata": {},
799
+ "outputs": [],
800
+ "source": []
801
+ }
802
+ ],
803
+ "metadata": {
804
+ "kernelspec": {
805
+ "display_name": "Python 3 (ipykernel)",
806
+ "language": "python",
807
+ "name": "python3"
808
+ },
809
+ "language_info": {
810
+ "codemirror_mode": {
811
+ "name": "ipython",
812
+ "version": 3
813
+ },
814
+ "file_extension": ".py",
815
+ "mimetype": "text/x-python",
816
+ "name": "python",
817
+ "nbconvert_exporter": "python",
818
+ "pygments_lexer": "ipython3",
819
+ "version": "3.11.11"
820
+ }
821
+ },
822
+ "nbformat": 4,
823
+ "nbformat_minor": 5
824
+ }
community-contributions/Day 3 using gemini.ipynb ADDED
@@ -0,0 +1,493 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "3d3cb3c4-9046-4f64-9188-ee20ae324fd1",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Code Generator\n",
9
+ "\n",
10
+ "The requirement: use a Frontier model to generate high performance C++ code from Python code\n",
11
+ "\n",
12
+ "# Important Note\n",
13
+ "Used an open-source model gemini-1.5-pro ,can try 2.0 flash too\n"
14
+ ]
15
+ },
16
+ {
17
+ "cell_type": "code",
18
+ "execution_count": null,
19
+ "id": "6f2c3e03-f38a-4bf2-98e8-696fb3d428c9",
20
+ "metadata": {},
21
+ "outputs": [],
22
+ "source": [
23
+ "# imports\n",
24
+ "\n",
25
+ "import os\n",
26
+ "import io\n",
27
+ "import sys\n",
28
+ "from dotenv import load_dotenv\n",
29
+ "import google.generativeai\n",
30
+ "from IPython.display import Markdown, display, update_display\n",
31
+ "import gradio as gr\n",
32
+ "import subprocess"
33
+ ]
34
+ },
35
+ {
36
+ "cell_type": "code",
37
+ "execution_count": null,
38
+ "id": "e437f3d1-39c4-47fd-919f-c2119d602d72",
39
+ "metadata": {},
40
+ "outputs": [],
41
+ "source": [
42
+ "# environment\n",
43
+ "\n",
44
+ "load_dotenv()\n",
45
+ "google_api_key = os.getenv('GOOGLE_API_KEY')\n",
46
+ "if google_api_key:\n",
47
+ " print(f\"Google API Key exists\")\n",
48
+ "else:\n",
49
+ " print(\"Google API Key not set\")"
50
+ ]
51
+ },
52
+ {
53
+ "cell_type": "code",
54
+ "execution_count": null,
55
+ "id": "1724ddb6-0059-46a3-bcf9-587c0c93cb2a",
56
+ "metadata": {},
57
+ "outputs": [],
58
+ "source": [
59
+ "google.generativeai.configure()\n"
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "code",
64
+ "execution_count": null,
65
+ "id": "b62738c1-9857-40fc-91e8-dfd46483ea50",
66
+ "metadata": {},
67
+ "outputs": [],
68
+ "source": [
69
+ "system_message = \"You are an assistant that reimplements Python code in high performance C++ for an Windows system. \"\n",
70
+ "system_message += \"Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments. \"\n",
71
+ "system_message += \"The C++ response needs to produce an identical output in the fastest possible time.\""
72
+ ]
73
+ },
74
+ {
75
+ "cell_type": "code",
76
+ "execution_count": null,
77
+ "id": "bd431141-8602-4c68-9a1d-a7c0a6f13fa3",
78
+ "metadata": {},
79
+ "outputs": [],
80
+ "source": [
81
+ "def user_prompt_for(python):\n",
82
+ " user_prompt = \"Rewrite this Python code in C++ with the fastest possible implementation that produces identical output in the least time. \"\n",
83
+ " user_prompt += \"Respond only with C++ code; do not explain your work other than a few comments. \"\n",
84
+ " user_prompt += \"Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\\n\\n\"\n",
85
+ " user_prompt += python\n",
86
+ " return user_prompt"
87
+ ]
88
+ },
89
+ {
90
+ "cell_type": "code",
91
+ "execution_count": null,
92
+ "id": "d5f48451-4cd4-46ea-a41d-531a3c7db2a8",
93
+ "metadata": {},
94
+ "outputs": [],
95
+ "source": [
96
+ "def messages_for(python):\n",
97
+ " return [\n",
98
+ " {\"role\": \"system\", \"content\": system_message},\n",
99
+ " {\"role\": \"user\", \"content\": user_prompt_for(python)}\n",
100
+ " ]"
101
+ ]
102
+ },
103
+ {
104
+ "cell_type": "code",
105
+ "execution_count": null,
106
+ "id": "83fd2170-14ea-4fb6-906e-c3c5cfce1ecc",
107
+ "metadata": {},
108
+ "outputs": [],
109
+ "source": [
110
+ "# write to a file called optimized.cpp\n",
111
+ "\n",
112
+ "def write_output(cpp):\n",
113
+ " code = cpp.replace(\"```cpp\",\"\").replace(\"```\",\"\")\n",
114
+ " with open(\"optimized.cpp\", \"w\") as f:\n",
115
+ " f.write(code)"
116
+ ]
117
+ },
118
+ {
119
+ "cell_type": "code",
120
+ "execution_count": null,
121
+ "id": "1ff08067-c9df-4981-8ab5-99eb2c2fd2c7",
122
+ "metadata": {},
123
+ "outputs": [],
124
+ "source": [
125
+ "def optimize_google(python):\n",
126
+ " # Initialize empty reply string\n",
127
+ " reply = \"\"\n",
128
+ " \n",
129
+ " # The API for Gemini has a slightly different structure\n",
130
+ " gemini = google.generativeai.GenerativeModel(\n",
131
+ " model_name='gemini-1.5-pro',\n",
132
+ " system_instruction=system_message\n",
133
+ " )\n",
134
+ " \n",
135
+ " response = gemini.generate_content(\n",
136
+ " user_prompt_for(python),\n",
137
+ " stream=True\n",
138
+ " )\n",
139
+ " \n",
140
+ " # Process the stream\n",
141
+ " for chunk in response:\n",
142
+ " # Extract text from the chunk\n",
143
+ " if chunk.text:\n",
144
+ " reply += chunk.text\n",
145
+ " print(chunk.text, end=\"\", flush=True)\n",
146
+ " \n",
147
+ " # Write the complete response to output\n",
148
+ " write_output(reply)\n",
149
+ " \n",
150
+ " # return reply"
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": null,
156
+ "id": "8e8c7ba2-4ee9-4523-b0f1-cc7a91798bba",
157
+ "metadata": {},
158
+ "outputs": [],
159
+ "source": [
160
+ "pi = \"\"\"\n",
161
+ "import time\n",
162
+ "\n",
163
+ "def calculate(iterations, param1, param2):\n",
164
+ " result = 1.0\n",
165
+ " for i in range(1, iterations+1):\n",
166
+ " j = i * param1 - param2\n",
167
+ " result -= (1/j)\n",
168
+ " j = i * param1 + param2\n",
169
+ " result += (1/j)\n",
170
+ " return result\n",
171
+ "\n",
172
+ "start_time = time.time()\n",
173
+ "result = calculate(100_000_000, 4, 1) * 4\n",
174
+ "end_time = time.time()\n",
175
+ "\n",
176
+ "print(f\"Result: {result:.12f}\")\n",
177
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
178
+ "\"\"\""
179
+ ]
180
+ },
181
+ {
182
+ "cell_type": "code",
183
+ "execution_count": null,
184
+ "id": "78d1afb7-ed6b-4a03-b36d-4ce8249c592e",
185
+ "metadata": {},
186
+ "outputs": [],
187
+ "source": [
188
+ "exec(pi)"
189
+ ]
190
+ },
191
+ {
192
+ "cell_type": "code",
193
+ "execution_count": null,
194
+ "id": "1fe1d0b6-7cc7-423b-bc4b-741a0c48c106",
195
+ "metadata": {},
196
+ "outputs": [],
197
+ "source": [
198
+ "optimize_google(pi)"
199
+ ]
200
+ },
201
+ {
202
+ "cell_type": "code",
203
+ "execution_count": null,
204
+ "id": "d28b4ac9-0909-4b35-aee1-97613a133e8e",
205
+ "metadata": {},
206
+ "outputs": [],
207
+ "source": [
208
+ "exec(pi) #Execution Time: 16.209231 seconds"
209
+ ]
210
+ },
211
+ {
212
+ "cell_type": "markdown",
213
+ "id": "7d0443a3-3ca2-4a7a-a6c3-c94d0aa54603",
214
+ "metadata": {},
215
+ "source": [
216
+ "# Compiling C++ and executing\n",
217
+ "\n",
218
+ "This next cell contains the command to compile a C++ file on Windows system. \n",
219
+ "It compiles the file `optimized.cpp` into an executable called `optimized` \n",
220
+ "Then it runs the program called `optimized`\n",
221
+ "\n",
222
+ "The way to compile for mac users is \\\n",
223
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp \\\n",
224
+ "!./optimized"
225
+ ]
226
+ },
227
+ {
228
+ "cell_type": "code",
229
+ "execution_count": null,
230
+ "id": "9b5cfc70-df1f-44a7-b4ae-fd934f715930",
231
+ "metadata": {},
232
+ "outputs": [],
233
+ "source": [
234
+ "!g++ -o optimized optimized.cpp\n",
235
+ "!.\\optimized #Execution Time: 3.661196 seconds"
236
+ ]
237
+ },
238
+ {
239
+ "cell_type": "code",
240
+ "execution_count": null,
241
+ "id": "e30fcbdf-82cf-4d50-9690-92dae69d5127",
242
+ "metadata": {},
243
+ "outputs": [],
244
+ "source": [
245
+ "python_hard = \"\"\"\n",
246
+ "def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
247
+ " value = seed\n",
248
+ " while True:\n",
249
+ " value = (a * value + c) % m\n",
250
+ " yield value\n",
251
+ " \n",
252
+ "def max_subarray_sum(n, seed, min_val, max_val):\n",
253
+ " lcg_gen = lcg(seed)\n",
254
+ " random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
255
+ " max_sum = float('-inf')\n",
256
+ " for i in range(n):\n",
257
+ " current_sum = 0\n",
258
+ " for j in range(i, n):\n",
259
+ " current_sum += random_numbers[j]\n",
260
+ " if current_sum > max_sum:\n",
261
+ " max_sum = current_sum\n",
262
+ " return max_sum\n",
263
+ "\n",
264
+ "def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
265
+ " total_sum = 0\n",
266
+ " lcg_gen = lcg(initial_seed)\n",
267
+ " for _ in range(20):\n",
268
+ " seed = next(lcg_gen)\n",
269
+ " total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
270
+ " return total_sum\n",
271
+ "\n",
272
+ "# Parameters\n",
273
+ "n = 10000 # Number of random numbers\n",
274
+ "initial_seed = 42 # Initial seed for the LCG\n",
275
+ "min_val = -10 # Minimum value of random numbers\n",
276
+ "max_val = 10 # Maximum value of random numbers\n",
277
+ "\n",
278
+ "# Timing the function\n",
279
+ "import time\n",
280
+ "start_time = time.time()\n",
281
+ "result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
282
+ "end_time = time.time()\n",
283
+ "\n",
284
+ "print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
285
+ "print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
286
+ "\"\"\""
287
+ ]
288
+ },
289
+ {
290
+ "cell_type": "code",
291
+ "execution_count": null,
292
+ "id": "2e8e111c-6f69-4ed0-8f86-8ed5982aa065",
293
+ "metadata": {},
294
+ "outputs": [],
295
+ "source": [
296
+ "exec(python_hard) #Execution Time: 62.297366 seconds"
297
+ ]
298
+ },
299
+ {
300
+ "cell_type": "code",
301
+ "execution_count": null,
302
+ "id": "38038ac1-5cdf-49d7-a286-a5871d5af583",
303
+ "metadata": {},
304
+ "outputs": [],
305
+ "source": [
306
+ "optimize_google(python_hard)"
307
+ ]
308
+ },
309
+ {
310
+ "cell_type": "code",
311
+ "execution_count": null,
312
+ "id": "08cb9619-b8ae-42e7-9375-4b3918c37fd0",
313
+ "metadata": {},
314
+ "outputs": [],
315
+ "source": [
316
+ "!g++ -o optimized optimized.cpp\n",
317
+ "!.\\optimized"
318
+ ]
319
+ },
320
+ {
321
+ "cell_type": "code",
322
+ "execution_count": null,
323
+ "id": "acd17a0d-f9f1-45a6-8151-916d8e6b9e4f",
324
+ "metadata": {},
325
+ "outputs": [],
326
+ "source": [
327
+ "def stream_google(python):\n",
328
+ " # Initialize empty reply string\n",
329
+ " reply = \"\"\n",
330
+ " \n",
331
+ " # The API for Gemini has a slightly different structure\n",
332
+ " gemini = google.generativeai.GenerativeModel(\n",
333
+ " model_name='gemini-1.5-pro',\n",
334
+ " system_instruction=system_message\n",
335
+ " )\n",
336
+ " \n",
337
+ " response = gemini.generate_content(\n",
338
+ " user_prompt_for(python),\n",
339
+ " stream=True\n",
340
+ " )\n",
341
+ " \n",
342
+ " # Process the stream\n",
343
+ " for chunk in response:\n",
344
+ " # Extract text from the chunk\n",
345
+ " if chunk.text:\n",
346
+ " reply += chunk.text\n",
347
+ " yield reply.replace('```cpp\\n','').replace('```','')\n"
348
+ ]
349
+ },
350
+ {
351
+ "cell_type": "code",
352
+ "execution_count": null,
353
+ "id": "c3177229-d6cf-4df2-81a7-9e1f3b229c19",
354
+ "metadata": {},
355
+ "outputs": [],
356
+ "source": [
357
+ "def optimize(python, model):\n",
358
+ " result=stream_google(python)\n",
359
+ " for stream_so_far in result:\n",
360
+ " yield stream_so_far "
361
+ ]
362
+ },
363
+ {
364
+ "cell_type": "code",
365
+ "execution_count": null,
366
+ "id": "c2476c2d-9218-4d30-bcc9-9cc5271c3a00",
367
+ "metadata": {},
368
+ "outputs": [],
369
+ "source": [
370
+ "with gr.Blocks() as ui:\n",
371
+ " with gr.Row():\n",
372
+ " python = gr.Textbox(label=\"Python code:\", lines=10, value=pi)\n",
373
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
374
+ " with gr.Row():\n",
375
+ " model = gr.Dropdown([\"Google\"], label=\"Select model\", value=\"Google\")\n",
376
+ " convert = gr.Button(\"Convert code\")\n",
377
+ "\n",
378
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
379
+ "\n",
380
+ "ui.launch(inbrowser=True)"
381
+ ]
382
+ },
383
+ {
384
+ "cell_type": "code",
385
+ "execution_count": null,
386
+ "id": "a30de175-af4e-428a-8942-1c41997c01f1",
387
+ "metadata": {},
388
+ "outputs": [],
389
+ "source": [
390
+ "def execute_python(code):\n",
391
+ " try:\n",
392
+ " output = io.StringIO()\n",
393
+ " sys.stdout = output\n",
394
+ " exec(code)\n",
395
+ " finally:\n",
396
+ " sys.stdout = sys.__stdout__\n",
397
+ " return output.getvalue()"
398
+ ]
399
+ },
400
+ {
401
+ "cell_type": "code",
402
+ "execution_count": null,
403
+ "id": "20c6316d-b090-42c5-9be9-7d5a178b97b3",
404
+ "metadata": {},
405
+ "outputs": [],
406
+ "source": [
407
+ "def execute_cpp(code):\n",
408
+ " write_output(code)\n",
409
+ " try:\n",
410
+ " # compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", \"optimized\", \"optimized.cpp\"]\n",
411
+ " compile_cmd = [\"g++\", \"-o\", \"optimized\", \"optimized.cpp\"]\n",
412
+ " compile_result = subprocess.run(compile_cmd, check=True, text=True, capture_output=True)\n",
413
+ " run_cmd = [\"./optimized\"]\n",
414
+ " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
415
+ " return run_result.stdout\n",
416
+ " except subprocess.CalledProcessError as e:\n",
417
+ " return f\"An error occurred:\\n{e.stderr}\""
418
+ ]
419
+ },
420
+ {
421
+ "cell_type": "code",
422
+ "execution_count": null,
423
+ "id": "950a459f-3ef6-4afd-9e83-f01c032aa21b",
424
+ "metadata": {},
425
+ "outputs": [],
426
+ "source": [
427
+ "css = \"\"\"\n",
428
+ ".python {background-color: #306998;}\n",
429
+ ".cpp {background-color: #050;}\n",
430
+ "\"\"\""
431
+ ]
432
+ },
433
+ {
434
+ "cell_type": "code",
435
+ "execution_count": null,
436
+ "id": "bc3d90ba-716c-4b8f-989f-46c2447c42fa",
437
+ "metadata": {},
438
+ "outputs": [],
439
+ "source": [
440
+ "with gr.Blocks(css=css) as ui:\n",
441
+ " gr.Markdown(\"## Convert code from Python to C++\")\n",
442
+ " with gr.Row():\n",
443
+ " python = gr.Textbox(label=\"Python code:\", value=pi, lines=10)\n",
444
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
445
+ " with gr.Row():\n",
446
+ " model = gr.Dropdown([\"Google\"], label=\"Select model\", value=\"Google\")\n",
447
+ " with gr.Row():\n",
448
+ " convert = gr.Button(\"Convert code\")\n",
449
+ " with gr.Row():\n",
450
+ " python_run = gr.Button(\"Run Python\")\n",
451
+ " cpp_run = gr.Button(\"Run C++\")\n",
452
+ " with gr.Row():\n",
453
+ " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
454
+ " cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
455
+ "\n",
456
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
457
+ " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
458
+ " cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
459
+ "\n",
460
+ "ui.launch(inbrowser=True)"
461
+ ]
462
+ },
463
+ {
464
+ "cell_type": "code",
465
+ "execution_count": null,
466
+ "id": "c12f6115-e8a9-494e-95ce-2566854c0aa2",
467
+ "metadata": {},
468
+ "outputs": [],
469
+ "source": []
470
+ }
471
+ ],
472
+ "metadata": {
473
+ "kernelspec": {
474
+ "display_name": "Python 3 (ipykernel)",
475
+ "language": "python",
476
+ "name": "python3"
477
+ },
478
+ "language_info": {
479
+ "codemirror_mode": {
480
+ "name": "ipython",
481
+ "version": 3
482
+ },
483
+ "file_extension": ".py",
484
+ "mimetype": "text/x-python",
485
+ "name": "python",
486
+ "nbconvert_exporter": "python",
487
+ "pygments_lexer": "ipython3",
488
+ "version": "3.11.11"
489
+ }
490
+ },
491
+ "nbformat": 4,
492
+ "nbformat_minor": 5
493
+ }
community-contributions/code_documentation_generator.ipynb ADDED
@@ -0,0 +1,433 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "05432987-80bc-4aa5-8c05-277861e19307",
6
+ "metadata": {},
7
+ "source": [
8
+ "## Adds docstrings/comments to code and generates code summary"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "markdown",
13
+ "id": "e706f175-1e83-4d2c-8613-056b2e532624",
14
+ "metadata": {},
15
+ "source": [
16
+ "### Model Usage \n",
17
+ "\n",
18
+ "- **Open Source Models:**\n",
19
+ "\n",
20
+ " - Deployed via Endpoint: Hosted on a server and accessed remotely (Qwen 1.5-7)\n",
21
+ " - Run Locally on Machine: Executed directly on a local device (Ollama running Llama 3.2-1B)\n",
22
+ "\n",
23
+ "- **Closed Source Models:** \n",
24
+ " - Accessed through API key authentication: (OpenAI, Anthropic). \n"
25
+ ]
26
+ },
27
+ {
28
+ "cell_type": "code",
29
+ "execution_count": null,
30
+ "id": "9ed667df-6660-4ba3-80c5-4c1c8f7e63f3",
31
+ "metadata": {},
32
+ "outputs": [],
33
+ "source": [
34
+ "# imports\n",
35
+ "\n",
36
+ "import os\n",
37
+ "import io\n",
38
+ "import sys \n",
39
+ "import json\n",
40
+ "import requests\n",
41
+ "from dotenv import load_dotenv\n",
42
+ "from openai import OpenAI\n",
43
+ "import google.generativeai\n",
44
+ "import anthropic\n",
45
+ "import ollama\n",
46
+ "from IPython.display import Markdown, display, update_display\n",
47
+ "import gradio as gr\n",
48
+ "from huggingface_hub import login, InferenceClient\n",
49
+ "from transformers import AutoTokenizer, pipeline"
50
+ ]
51
+ },
52
+ {
53
+ "cell_type": "code",
54
+ "execution_count": null,
55
+ "id": "c9dd4bf1-48cf-44dc-9d04-0ec6e8189a3c",
56
+ "metadata": {},
57
+ "outputs": [],
58
+ "source": [
59
+ "# environment\n",
60
+ "\n",
61
+ "load_dotenv()\n",
62
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')\n",
63
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY')\n",
64
+ "CODE_QWEN_URL = os.environ['CODE_QWEN_URL'] \n",
65
+ "BIGBIRD_PEGASUS_URL = os.environ['BIGBIRD_PEGASUS_URL']\n",
66
+ "HF_TOKEN = os.environ['HF_TOKEN']"
67
+ ]
68
+ },
69
+ {
70
+ "cell_type": "code",
71
+ "execution_count": null,
72
+ "id": "71f671d6-50a7-43cf-9e04-52a159d67dab",
73
+ "metadata": {},
74
+ "outputs": [],
75
+ "source": [
76
+ "!ollama pull llama3.2:1b"
77
+ ]
78
+ },
79
+ {
80
+ "cell_type": "code",
81
+ "execution_count": null,
82
+ "id": "8e6f8f35-477d-4014-8fe9-874b5aee0061",
83
+ "metadata": {},
84
+ "outputs": [],
85
+ "source": [
86
+ "openai = OpenAI()\n",
87
+ "claude = anthropic.Anthropic()"
88
+ ]
89
+ },
90
+ {
91
+ "cell_type": "code",
92
+ "execution_count": null,
93
+ "id": "ae34b79c-425a-4f04-821a-8f1d9868b146",
94
+ "metadata": {},
95
+ "outputs": [],
96
+ "source": [
97
+ "OPENAI_MODEL = \"gpt-4o-mini\"\n",
98
+ "CLAUDE_MODEL = \"claude-3-haiku-20240307\"\n",
99
+ "LLAMA_MODEL = \"llama3.2:1b\""
100
+ ]
101
+ },
102
+ {
103
+ "cell_type": "code",
104
+ "execution_count": null,
105
+ "id": "80e6d920-3c94-48c4-afd8-518f415ab777",
106
+ "metadata": {},
107
+ "outputs": [],
108
+ "source": [
109
+ "code_qwen = \"Qwen/CodeQwen1.5-7B-Chat\"\n",
110
+ "bigbird_pegasus = \"google/bigbird-pegasus-large-arxiv\"\n",
111
+ "login(HF_TOKEN, add_to_git_credential=True)"
112
+ ]
113
+ },
114
+ {
115
+ "cell_type": "code",
116
+ "execution_count": null,
117
+ "id": "314cd8e3-2c10-4149-9818-4e6b0c05b871",
118
+ "metadata": {},
119
+ "outputs": [],
120
+ "source": [
121
+ "# Uses Llama to Check Which Language the Code is Written In\n",
122
+ "system_message_comments = \"You are an assistant designed to add docstrings and helpful comments to code for documentation purposes.\"\n",
123
+ "system_message_comments += \"Respond back with properly formatted code, including docstrings and comments. Keep comments concise. \"\n",
124
+ "system_message_comments += \"Do not respond with greetings, or any such extra output\""
125
+ ]
126
+ },
127
+ {
128
+ "cell_type": "code",
129
+ "execution_count": null,
130
+ "id": "66fa09e4-1b79-4f53-9bb7-904d515b2f26",
131
+ "metadata": {},
132
+ "outputs": [],
133
+ "source": [
134
+ "system_message_summary = \"You are an assistant designed to summarise code for documentation purposes. You are not to display code again.\"\n",
135
+ "system_message_summary += \"Respond back with a properly crafted summary, mentioning key details regarding to the code, such as workflow, code language.\"\n",
136
+ "system_message_summary += \"Do not respond with greetings, or any such extra output. Do not respond in Markdown. Be thorough, keep explanation level at undergraduate level.\""
137
+ ]
138
+ },
139
+ {
140
+ "cell_type": "code",
141
+ "execution_count": null,
142
+ "id": "ea405820-f9d1-4cf1-b465-9ae5cd9016f6",
143
+ "metadata": {},
144
+ "outputs": [],
145
+ "source": [
146
+ "def user_prompt_for(code):\n",
147
+ " user_prompt = \"Rewrite this code to include helpful comments and docstrings. \"\n",
148
+ " user_prompt += \"Respond only with code.\\n\"\n",
149
+ " user_prompt += code\n",
150
+ " return user_prompt"
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": null,
156
+ "id": "26c9be56-1d4f-43e5-9bc4-eb5b76da8071",
157
+ "metadata": {},
158
+ "outputs": [],
159
+ "source": [
160
+ "def user_prompt_for_summary(code):\n",
161
+ " user_prompt = \"Return the summary of the code.\\n\"\n",
162
+ " user_prompt += code\n",
163
+ " return user_prompt"
164
+ ]
165
+ },
166
+ {
167
+ "cell_type": "code",
168
+ "execution_count": null,
169
+ "id": "c0ac22cb-dc96-4ae1-b00d-2747572f6945",
170
+ "metadata": {},
171
+ "outputs": [],
172
+ "source": [
173
+ "def messages_for(code):\n",
174
+ " messages = [\n",
175
+ " {\"role\": \"system\", \"content\": system_message_comments},\n",
176
+ " {\"role\":\"user\", \"content\" : user_prompt_for(code)}\n",
177
+ " ]\n",
178
+ " return messages"
179
+ ]
180
+ },
181
+ {
182
+ "cell_type": "code",
183
+ "execution_count": null,
184
+ "id": "eae1a8b4-68a8-4cd5-849e-0ecabd166a0c",
185
+ "metadata": {},
186
+ "outputs": [],
187
+ "source": [
188
+ "def messages_for_summary(code):\n",
189
+ " messages = [\n",
190
+ " {\"role\": \"system\", \"content\": system_message_summary},\n",
191
+ " {\"role\":\"user\", \"content\" : user_prompt_for_summary(code)}\n",
192
+ " ]\n",
193
+ " return messages"
194
+ ]
195
+ },
196
+ {
197
+ "cell_type": "code",
198
+ "execution_count": null,
199
+ "id": "5eb726dd-e09e-4011-8eb6-4d20f2830ff5",
200
+ "metadata": {},
201
+ "outputs": [],
202
+ "source": [
203
+ "func = \"\"\"\n",
204
+ "import time\n",
205
+ "\n",
206
+ "def calculate(iterations, param1, param2):\n",
207
+ " result = 1.0\n",
208
+ " for i in range(1, iterations+1):\n",
209
+ " j = i * param1 - param2\n",
210
+ " result -= (1/j)\n",
211
+ " j = i * param1 + param2\n",
212
+ " result += (1/j)\n",
213
+ " return result\n",
214
+ "\n",
215
+ "start_time = time.time()\n",
216
+ "result = calculate(100_000_000, 4, 1) * 4\n",
217
+ "end_time = time.time()\n",
218
+ "\n",
219
+ "print(f\"Result: {result:.12f}\")\n",
220
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
221
+ "\"\"\""
222
+ ]
223
+ },
224
+ {
225
+ "cell_type": "code",
226
+ "execution_count": null,
227
+ "id": "f61943b2-c939-4910-a670-58abaf464bb6",
228
+ "metadata": {},
229
+ "outputs": [],
230
+ "source": [
231
+ "def call_llama(code):\n",
232
+ " # commented code\n",
233
+ " messages = messages_for(code)\n",
234
+ " response1 = ollama.chat(model=LLAMA_MODEL, messages=messages)\n",
235
+ "\n",
236
+ " # summary\n",
237
+ " messages = messages_for_summary(code)\n",
238
+ " response2 = ollama.chat(model=LLAMA_MODEL, messages=messages)\n",
239
+ " \n",
240
+ " return response1['message']['content'],response2['message']['content']"
241
+ ]
242
+ },
243
+ {
244
+ "cell_type": "code",
245
+ "execution_count": null,
246
+ "id": "696fb97e-807e-40ed-b0e1-beb82d1108a6",
247
+ "metadata": {},
248
+ "outputs": [],
249
+ "source": [
250
+ "def call_claude(code):\n",
251
+ " # commented code\n",
252
+ " message1 = claude.messages.create(\n",
253
+ " model=CLAUDE_MODEL,\n",
254
+ " system=system_message_comments,\n",
255
+ " messages=([{\"role\": \"user\", \"content\":user_prompt_for(code)}]),\n",
256
+ " max_tokens=500\n",
257
+ " )\n",
258
+ "\n",
259
+ " # summary\n",
260
+ " message2 = claude.messages.create(\n",
261
+ " model=CLAUDE_MODEL,\n",
262
+ " system=system_message_summary,\n",
263
+ " messages=([{\"role\": \"user\", \"content\":user_prompt_for_summary(code)}]),\n",
264
+ " max_tokens=500\n",
265
+ " )\n",
266
+ " \n",
267
+ " return message1.content[0].text,message2.content[0].text"
268
+ ]
269
+ },
270
+ {
271
+ "cell_type": "code",
272
+ "execution_count": null,
273
+ "id": "4bf1db64-86fa-42a1-98dd-3df74607f8db",
274
+ "metadata": {},
275
+ "outputs": [],
276
+ "source": [
277
+ "def call_gpt(code):\n",
278
+ " # commented code\n",
279
+ " completion1 = openai.chat.completions.create(\n",
280
+ " model=OPENAI_MODEL,\n",
281
+ " messages=messages_for(code),\n",
282
+ " )\n",
283
+ "\n",
284
+ " #summary\n",
285
+ " completion2 = openai.chat.completions.create(\n",
286
+ " model=OPENAI_MODEL,\n",
287
+ " messages=messages_for_summary(code),\n",
288
+ " )\n",
289
+ " \n",
290
+ " return completion1.choices[0].message.content,completion2.choices[0].message.content"
291
+ ]
292
+ },
293
+ {
294
+ "cell_type": "code",
295
+ "execution_count": null,
296
+ "id": "6863dc42-cbcd-4a95-8b0a-cfbcbfed0764",
297
+ "metadata": {},
298
+ "outputs": [],
299
+ "source": [
300
+ "def call_codeqwen(code):\n",
301
+ " # commented code\n",
302
+ " tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
303
+ " messages = messages_for(code)\n",
304
+ " text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
305
+ " client = InferenceClient(CODE_QWEN_URL, token=HF_TOKEN)\n",
306
+ " response1 = client.text_generation(text, details=True, max_new_tokens=1000)\n",
307
+ "\n",
308
+ " # summary\n",
309
+ " tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
310
+ " messages = messages_for_summary(code)\n",
311
+ " text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
312
+ " client = InferenceClient(CODE_QWEN_URL, token=HF_TOKEN)\n",
313
+ " response2 = client.text_generation(text, details=True, max_new_tokens=1000)\n",
314
+ " \n",
315
+ " return response1.generated_text ,response2.generated_text "
316
+ ]
317
+ },
318
+ {
319
+ "cell_type": "code",
320
+ "execution_count": null,
321
+ "id": "06d05c02-45e4-47da-b70b-cf433dfaca4c",
322
+ "metadata": {},
323
+ "outputs": [],
324
+ "source": [
325
+ "def create_docs(code,model):\n",
326
+ " if model == \"Llama\":\n",
327
+ " comments,summary = call_llama(code)\n",
328
+ " elif model == \"Claude\":\n",
329
+ " comments,summary = call_claude(code)\n",
330
+ " elif model == \"GPT\":\n",
331
+ " comments,summary = call_gpt(code)\n",
332
+ " elif model == \"CodeQwen\":\n",
333
+ " comments,summary = call_codeqwen(code)\n",
334
+ " else:\n",
335
+ " raise ValueError(\"Unknown Model\")\n",
336
+ " return comments,summary"
337
+ ]
338
+ },
339
+ {
340
+ "cell_type": "code",
341
+ "execution_count": null,
342
+ "id": "1b4ea289-5da9-4b0e-b4d4-f8f01e466839",
343
+ "metadata": {},
344
+ "outputs": [],
345
+ "source": [
346
+ "css = \"\"\"\n",
347
+ ".comments {background-color: #00599C;}\n",
348
+ ".summary {background-color: #008B8B;}\n",
349
+ "\"\"\""
350
+ ]
351
+ },
352
+ {
353
+ "cell_type": "code",
354
+ "execution_count": null,
355
+ "id": "89ad7c7b-b881-45d3-aadc-d7206af578fb",
356
+ "metadata": {},
357
+ "outputs": [],
358
+ "source": [
359
+ "with gr.Blocks(css=css) as ui:\n",
360
+ " gr.Markdown(\"### Code Documentation and Formatting\")\n",
361
+ " with gr.Row():\n",
362
+ " code = gr.Textbox(label=\"Input Code: \", value=func, lines=10)\n",
363
+ " with gr.Row():\n",
364
+ " model = gr.Dropdown([\"GPT\",\"Claude\",\"Llama\",\"CodeQwen\"],label=\"Select model\",value=\"GPT\")\n",
365
+ " with gr.Row():\n",
366
+ " docs = gr.Button(\"Add Comments and Sumarise Code\")\n",
367
+ " with gr.Row():\n",
368
+ " commented_code = gr.Textbox(label= \"Formatted Code\", lines=10,elem_classes=[\"comments\"])\n",
369
+ " code_summary = gr.Textbox(label = \"Code Summary\", lines=10,elem_classes=[\"summary\"])\n",
370
+ " docs.click(create_docs,inputs=[code,model],outputs=[commented_code,code_summary]),"
371
+ ]
372
+ },
373
+ {
374
+ "cell_type": "code",
375
+ "execution_count": null,
376
+ "id": "1a9e3b1c-bfe6-4b71-aac8-fa36a491c157",
377
+ "metadata": {
378
+ "scrolled": true
379
+ },
380
+ "outputs": [],
381
+ "source": [
382
+ "ui.launch(inbrowser=True)"
383
+ ]
384
+ },
385
+ {
386
+ "cell_type": "code",
387
+ "execution_count": null,
388
+ "id": "ac895aa9-e044-4598-b715-d96d1c158656",
389
+ "metadata": {},
390
+ "outputs": [],
391
+ "source": []
392
+ },
393
+ {
394
+ "cell_type": "code",
395
+ "execution_count": null,
396
+ "id": "5a96877c-22b7-4ad5-b235-1cf8f8b200a1",
397
+ "metadata": {},
398
+ "outputs": [],
399
+ "source": [
400
+ "print(call_llama(func))"
401
+ ]
402
+ },
403
+ {
404
+ "cell_type": "code",
405
+ "execution_count": null,
406
+ "id": "f11de1a2-52c0-41c7-ad88-01ef5f8bc628",
407
+ "metadata": {},
408
+ "outputs": [],
409
+ "source": []
410
+ }
411
+ ],
412
+ "metadata": {
413
+ "kernelspec": {
414
+ "display_name": "Python 3 (ipykernel)",
415
+ "language": "python",
416
+ "name": "python3"
417
+ },
418
+ "language_info": {
419
+ "codemirror_mode": {
420
+ "name": "ipython",
421
+ "version": 3
422
+ },
423
+ "file_extension": ".py",
424
+ "mimetype": "text/x-python",
425
+ "name": "python",
426
+ "nbconvert_exporter": "python",
427
+ "pygments_lexer": "ipython3",
428
+ "version": "3.11.11"
429
+ }
430
+ },
431
+ "nbformat": 4,
432
+ "nbformat_minor": 5
433
+ }
community-contributions/day4-gemini-included.ipynb ADDED
@@ -0,0 +1,908 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Code Generator\n",
9
+ "\n",
10
+ "The requirement: use an Open Source model to generate high performance C++ code from Python code\n",
11
+ "\n",
12
+ "To replicate this, you'll need to set up a HuggingFace endpoint as I do in the video. It's simple to do, and it's quite satisfying to see the results!\n",
13
+ "\n",
14
+ "It's also an important part of your learning; this is the first example of deploying an open source model to be behind an API. We'll return to this in Week 8, but this should plant a seed in your mind for what's involved in moving open source models into production."
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "markdown",
19
+ "id": "22e1567b-33fd-49e7-866e-4b635d15715a",
20
+ "metadata": {},
21
+ "source": [
22
+ "<table style=\"margin: 0; text-align: left;\">\n",
23
+ " <tr>\n",
24
+ " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
25
+ " <img src=\"../../important.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
26
+ " </td>\n",
27
+ " <td>\n",
28
+ " <h1 style=\"color:#900;\">Important - Pause Endpoints when not in use</h1>\n",
29
+ " <span style=\"color:#900;\">\n",
30
+ " If you do decide to use HuggingFace endpoints for this project, you should stop or pause the endpoints when you are done to avoid accruing unnecessary running cost. The costs are very low as long as you only run the endpoint when you're using it. Navigate to the HuggingFace endpoint UI <a href=\"https://ui.endpoints.huggingface.co/\">here,</a> open your endpoint, and click Pause to put it on pause so you no longer pay for it. \n",
31
+ "Many thanks to student John L. for raising this.\n",
32
+ "<br/><br/>\n",
33
+ "In week 8 we will use Modal instead of HuggingFace endpoints; with Modal you only pay for the time that you use it and you should get free credits.\n",
34
+ " </span>\n",
35
+ " </td>\n",
36
+ " </tr>\n",
37
+ "</table>"
38
+ ]
39
+ },
40
+ {
41
+ "cell_type": "code",
42
+ "execution_count": null,
43
+ "id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
44
+ "metadata": {},
45
+ "outputs": [],
46
+ "source": [
47
+ "# imports\n",
48
+ "\n",
49
+ "import os\n",
50
+ "import io\n",
51
+ "import sys\n",
52
+ "import json\n",
53
+ "import requests\n",
54
+ "from dotenv import load_dotenv\n",
55
+ "from openai import OpenAI\n",
56
+ "import google.generativeai as genai\n",
57
+ "import anthropic\n",
58
+ "from IPython.display import Markdown, display, update_display\n",
59
+ "import gradio as gr\n",
60
+ "import subprocess"
61
+ ]
62
+ },
63
+ {
64
+ "cell_type": "code",
65
+ "execution_count": null,
66
+ "id": "4f672e1c-87e9-4865-b760-370fa605e614",
67
+ "metadata": {},
68
+ "outputs": [],
69
+ "source": [
70
+ "# environment\n",
71
+ "\n",
72
+ "load_dotenv()\n",
73
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
74
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
75
+ "os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')\n",
76
+ "os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY', 'your-key-if-not-using-env')"
77
+ ]
78
+ },
79
+ {
80
+ "cell_type": "code",
81
+ "execution_count": null,
82
+ "id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
83
+ "metadata": {},
84
+ "outputs": [],
85
+ "source": [
86
+ "# initialize\n",
87
+ "\n",
88
+ "openai = OpenAI()\n",
89
+ "claude = anthropic.Anthropic()\n",
90
+ "OPENAI_MODEL = \"gpt-4o\"\n",
91
+ "CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\"\n",
92
+ "GEMINI_MODEL = 'gemini-1.5-pro'"
93
+ ]
94
+ },
95
+ {
96
+ "cell_type": "code",
97
+ "execution_count": null,
98
+ "id": "6896636f-923e-4a2c-9d6c-fac07828a201",
99
+ "metadata": {},
100
+ "outputs": [],
101
+ "source": [
102
+ "system_message = \"You are an assistant that reimplements Python code in high performance C++ for an M1 Mac. \"\n",
103
+ "system_message += \"Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments. \"\n",
104
+ "system_message += \"The C++ response needs to produce an identical output in the fastest possible time. Keep implementations of random number generators identical so that results match exactly.\""
105
+ ]
106
+ },
107
+ {
108
+ "cell_type": "code",
109
+ "execution_count": null,
110
+ "id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
111
+ "metadata": {},
112
+ "outputs": [],
113
+ "source": [
114
+ "def user_prompt_for(python):\n",
115
+ " user_prompt = \"Rewrite this Python code in C++ with the fastest possible implementation that produces identical output in the least time. \"\n",
116
+ " user_prompt += \"Respond only with C++ code; do not explain your work other than a few comments. \"\n",
117
+ " user_prompt += \"Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\\n\\n\"\n",
118
+ " user_prompt += python\n",
119
+ " return user_prompt"
120
+ ]
121
+ },
122
+ {
123
+ "cell_type": "code",
124
+ "execution_count": null,
125
+ "id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
126
+ "metadata": {},
127
+ "outputs": [],
128
+ "source": [
129
+ "def messages_for(python):\n",
130
+ " return [\n",
131
+ " {\"role\": \"system\", \"content\": system_message},\n",
132
+ " {\"role\": \"user\", \"content\": user_prompt_for(python)}\n",
133
+ " ]"
134
+ ]
135
+ },
136
+ {
137
+ "cell_type": "code",
138
+ "execution_count": null,
139
+ "id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
140
+ "metadata": {},
141
+ "outputs": [],
142
+ "source": [
143
+ "# write to a file called optimized.cpp\n",
144
+ "\n",
145
+ "def write_output(cpp):\n",
146
+ " code = cpp.replace(\"```cpp\",\"\").replace(\"```\",\"\")\n",
147
+ " with open(\"optimized.cpp\", \"w\") as f:\n",
148
+ " f.write(code)"
149
+ ]
150
+ },
151
+ {
152
+ "cell_type": "code",
153
+ "execution_count": null,
154
+ "id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
155
+ "metadata": {},
156
+ "outputs": [],
157
+ "source": [
158
+ "def optimize_gpt(python): \n",
159
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n",
160
+ " reply = \"\"\n",
161
+ " for chunk in stream:\n",
162
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
163
+ " reply += fragment\n",
164
+ " print(fragment, end='', flush=True)\n",
165
+ " write_output(reply)"
166
+ ]
167
+ },
168
+ {
169
+ "cell_type": "code",
170
+ "execution_count": null,
171
+ "id": "7cd84ad8-d55c-4fe0-9eeb-1895c95c4a9d",
172
+ "metadata": {},
173
+ "outputs": [],
174
+ "source": [
175
+ "def optimize_claude(python):\n",
176
+ " result = claude.messages.stream(\n",
177
+ " model=CLAUDE_MODEL,\n",
178
+ " max_tokens=2000,\n",
179
+ " system=system_message,\n",
180
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
181
+ " )\n",
182
+ " reply = \"\"\n",
183
+ " with result as stream:\n",
184
+ " for text in stream.text_stream:\n",
185
+ " reply += text\n",
186
+ " print(text, end=\"\", flush=True)\n",
187
+ " write_output(reply)"
188
+ ]
189
+ },
190
+ {
191
+ "cell_type": "code",
192
+ "execution_count": null,
193
+ "id": "3625fcd6-209f-481c-a745-dcbcf5e44bc1",
194
+ "metadata": {},
195
+ "outputs": [],
196
+ "source": [
197
+ "def optimize_gemini(python):\n",
198
+ " gemini = genai.GenerativeModel(\n",
199
+ " model_name = GEMINI_MODEL,\n",
200
+ " system_instruction=system_message\n",
201
+ " )\n",
202
+ " response = gemini.generate_content(\n",
203
+ " user_prompt_for(python),\n",
204
+ " stream=True\n",
205
+ " )\n",
206
+ " reply = \"\"\n",
207
+ " for chunk in response:\n",
208
+ " reply += chunk.text\n",
209
+ " print(chunk.text, end=\"\", flush=True)\n",
210
+ " write_output(reply)"
211
+ ]
212
+ },
213
+ {
214
+ "cell_type": "code",
215
+ "execution_count": null,
216
+ "id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
217
+ "metadata": {},
218
+ "outputs": [],
219
+ "source": [
220
+ "pi = \"\"\"\n",
221
+ "import time\n",
222
+ "\n",
223
+ "def calculate(iterations, param1, param2):\n",
224
+ " result = 1.0\n",
225
+ " for i in range(1, iterations+1):\n",
226
+ " j = i * param1 - param2\n",
227
+ " result -= (1/j)\n",
228
+ " j = i * param1 + param2\n",
229
+ " result += (1/j)\n",
230
+ " return result\n",
231
+ "\n",
232
+ "start_time = time.time()\n",
233
+ "result = calculate(100_000_000, 4, 1) * 4\n",
234
+ "end_time = time.time()\n",
235
+ "\n",
236
+ "print(f\"Result: {result:.12f}\")\n",
237
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
238
+ "\"\"\""
239
+ ]
240
+ },
241
+ {
242
+ "cell_type": "code",
243
+ "execution_count": null,
244
+ "id": "fe891e3a-d1c4-4ee5-a361-34d0982fcff4",
245
+ "metadata": {},
246
+ "outputs": [],
247
+ "source": [
248
+ "optimize_gemini(pi)"
249
+ ]
250
+ },
251
+ {
252
+ "cell_type": "code",
253
+ "execution_count": null,
254
+ "id": "7fe1cd4b-d2c5-4303-afed-2115a3fef200",
255
+ "metadata": {},
256
+ "outputs": [],
257
+ "source": [
258
+ "exec(pi)"
259
+ ]
260
+ },
261
+ {
262
+ "cell_type": "code",
263
+ "execution_count": null,
264
+ "id": "105db6f9-343c-491d-8e44-3a5328b81719",
265
+ "metadata": {},
266
+ "outputs": [],
267
+ "source": [
268
+ "optimize_gpt(pi)"
269
+ ]
270
+ },
271
+ {
272
+ "cell_type": "code",
273
+ "execution_count": null,
274
+ "id": "bf26ee95-0c77-491d-9a91-579a1e96a8a3",
275
+ "metadata": {},
276
+ "outputs": [],
277
+ "source": [
278
+ "exec(pi)"
279
+ ]
280
+ },
281
+ {
282
+ "cell_type": "code",
283
+ "execution_count": null,
284
+ "id": "4194e40c-04ab-4940-9d64-b4ad37c5bb40",
285
+ "metadata": {},
286
+ "outputs": [],
287
+ "source": [
288
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
289
+ "!./optimized"
290
+ ]
291
+ },
292
+ {
293
+ "cell_type": "code",
294
+ "execution_count": null,
295
+ "id": "983a11fe-e24d-4c65-8269-9802c5ef3ae6",
296
+ "metadata": {},
297
+ "outputs": [],
298
+ "source": [
299
+ "optimize_claude(pi)"
300
+ ]
301
+ },
302
+ {
303
+ "cell_type": "code",
304
+ "execution_count": null,
305
+ "id": "d5a766f9-3d23-4bb4-a1d4-88ec44b61ddf",
306
+ "metadata": {},
307
+ "outputs": [],
308
+ "source": [
309
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
310
+ "!./optimized"
311
+ ]
312
+ },
313
+ {
314
+ "cell_type": "code",
315
+ "execution_count": null,
316
+ "id": "c3b497b3-f569-420e-b92e-fb0f49957ce0",
317
+ "metadata": {},
318
+ "outputs": [],
319
+ "source": [
320
+ "python_hard = \"\"\"# Be careful to support large number sizes\n",
321
+ "\n",
322
+ "def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
323
+ " value = seed\n",
324
+ " while True:\n",
325
+ " value = (a * value + c) % m\n",
326
+ " yield value\n",
327
+ " \n",
328
+ "def max_subarray_sum(n, seed, min_val, max_val):\n",
329
+ " lcg_gen = lcg(seed)\n",
330
+ " random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
331
+ " max_sum = float('-inf')\n",
332
+ " for i in range(n):\n",
333
+ " current_sum = 0\n",
334
+ " for j in range(i, n):\n",
335
+ " current_sum += random_numbers[j]\n",
336
+ " if current_sum > max_sum:\n",
337
+ " max_sum = current_sum\n",
338
+ " return max_sum\n",
339
+ "\n",
340
+ "def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
341
+ " total_sum = 0\n",
342
+ " lcg_gen = lcg(initial_seed)\n",
343
+ " for _ in range(20):\n",
344
+ " seed = next(lcg_gen)\n",
345
+ " total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
346
+ " return total_sum\n",
347
+ "\n",
348
+ "# Parameters\n",
349
+ "n = 10000 # Number of random numbers\n",
350
+ "initial_seed = 42 # Initial seed for the LCG\n",
351
+ "min_val = -10 # Minimum value of random numbers\n",
352
+ "max_val = 10 # Maximum value of random numbers\n",
353
+ "\n",
354
+ "# Timing the function\n",
355
+ "import time\n",
356
+ "start_time = time.time()\n",
357
+ "result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
358
+ "end_time = time.time()\n",
359
+ "\n",
360
+ "print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
361
+ "print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
362
+ "\"\"\""
363
+ ]
364
+ },
365
+ {
366
+ "cell_type": "code",
367
+ "execution_count": null,
368
+ "id": "dab5e4bc-276c-4555-bd4c-12c699d5e899",
369
+ "metadata": {},
370
+ "outputs": [],
371
+ "source": [
372
+ "exec(python_hard)"
373
+ ]
374
+ },
375
+ {
376
+ "cell_type": "code",
377
+ "execution_count": null,
378
+ "id": "e8d24ed5-2c15-4f55-80e7-13a3952b3cb8",
379
+ "metadata": {},
380
+ "outputs": [],
381
+ "source": [
382
+ "optimize_gpt(python_hard)"
383
+ ]
384
+ },
385
+ {
386
+ "cell_type": "code",
387
+ "execution_count": null,
388
+ "id": "e0b3d073-88a2-40b2-831c-6f0c345c256f",
389
+ "metadata": {},
390
+ "outputs": [],
391
+ "source": [
392
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
393
+ "!./optimized"
394
+ ]
395
+ },
396
+ {
397
+ "cell_type": "code",
398
+ "execution_count": null,
399
+ "id": "e9305446-1d0c-4b51-866a-b8c1e299bf5c",
400
+ "metadata": {},
401
+ "outputs": [],
402
+ "source": [
403
+ "optimize_claude(python_hard)"
404
+ ]
405
+ },
406
+ {
407
+ "cell_type": "code",
408
+ "execution_count": null,
409
+ "id": "0c181036-8193-4fdd-aef3-fc513b218d43",
410
+ "metadata": {},
411
+ "outputs": [],
412
+ "source": [
413
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
414
+ "!./optimized"
415
+ ]
416
+ },
417
+ {
418
+ "cell_type": "code",
419
+ "execution_count": null,
420
+ "id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
421
+ "metadata": {},
422
+ "outputs": [],
423
+ "source": [
424
+ "def stream_gpt(python): \n",
425
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n",
426
+ " reply = \"\"\n",
427
+ " for chunk in stream:\n",
428
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
429
+ " reply += fragment\n",
430
+ " yield reply.replace('```cpp\\n','').replace('```','')"
431
+ ]
432
+ },
433
+ {
434
+ "cell_type": "code",
435
+ "execution_count": null,
436
+ "id": "8669f56b-8314-4582-a167-78842caea131",
437
+ "metadata": {},
438
+ "outputs": [],
439
+ "source": [
440
+ "def stream_claude(python):\n",
441
+ " result = claude.messages.stream(\n",
442
+ " model=CLAUDE_MODEL,\n",
443
+ " max_tokens=2000,\n",
444
+ " system=system_message,\n",
445
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
446
+ " )\n",
447
+ " reply = \"\"\n",
448
+ " with result as stream:\n",
449
+ " for text in stream.text_stream:\n",
450
+ " reply += text\n",
451
+ " yield reply.replace('```cpp\\n','').replace('```','')"
452
+ ]
453
+ },
454
+ {
455
+ "cell_type": "code",
456
+ "execution_count": null,
457
+ "id": "a9b6938f-89ef-4998-a334-2f6c042a2da4",
458
+ "metadata": {},
459
+ "outputs": [],
460
+ "source": [
461
+ "def stream_gemini(python):\n",
462
+ " gemini = genai.GenerativeModel(\n",
463
+ " model_name = GEMINI_MODEL,\n",
464
+ " system_instruction=system_message\n",
465
+ " )\n",
466
+ " response = gemini.generate_content(\n",
467
+ " user_prompt_for(python),\n",
468
+ " stream=True\n",
469
+ " )\n",
470
+ " reply = \"\"\n",
471
+ " for chunk in response:\n",
472
+ " reply += chunk.text\n",
473
+ " yield reply.replace('```cpp\\n','').replace('```','')"
474
+ ]
475
+ },
476
+ {
477
+ "cell_type": "code",
478
+ "execution_count": null,
479
+ "id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
480
+ "metadata": {},
481
+ "outputs": [],
482
+ "source": [
483
+ "def optimize(python, model):\n",
484
+ " if model==\"GPT\":\n",
485
+ " result = stream_gpt(python)\n",
486
+ " elif model==\"Claude\":\n",
487
+ " result = stream_claude(python)\n",
488
+ " elif model==\"Gemini\":\n",
489
+ " result= stream_gemini(python)\n",
490
+ " else:\n",
491
+ " raise ValueError(\"Unknown model\")\n",
492
+ " for stream_so_far in result:\n",
493
+ " yield stream_so_far "
494
+ ]
495
+ },
496
+ {
497
+ "cell_type": "code",
498
+ "execution_count": null,
499
+ "id": "f1ddb38e-6b0a-4c37-baa4-ace0b7de887a",
500
+ "metadata": {},
501
+ "outputs": [],
502
+ "source": [
503
+ "with gr.Blocks() as ui:\n",
504
+ " with gr.Row():\n",
505
+ " python = gr.Textbox(label=\"Python code:\", lines=10, value=python_hard)\n",
506
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
507
+ " with gr.Row():\n",
508
+ " model = gr.Dropdown([\"GPT\", \"Claude\",\"Gemini\"], label=\"Select model\", value=\"GPT\")\n",
509
+ " convert = gr.Button(\"Convert code\")\n",
510
+ "\n",
511
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
512
+ "\n",
513
+ "ui.launch(inbrowser=True)"
514
+ ]
515
+ },
516
+ {
517
+ "cell_type": "code",
518
+ "execution_count": null,
519
+ "id": "19bf2bff-a822-4009-a539-f003b1651383",
520
+ "metadata": {},
521
+ "outputs": [],
522
+ "source": [
523
+ "def execute_python(code):\n",
524
+ " try:\n",
525
+ " output = io.StringIO()\n",
526
+ " sys.stdout = output\n",
527
+ " exec(code)\n",
528
+ " finally:\n",
529
+ " sys.stdout = sys.__stdout__\n",
530
+ " return output.getvalue()"
531
+ ]
532
+ },
533
+ {
534
+ "cell_type": "code",
535
+ "execution_count": null,
536
+ "id": "77f3ab5d-fcfb-4d3f-8728-9cacbf833ea6",
537
+ "metadata": {},
538
+ "outputs": [],
539
+ "source": [
540
+ "def execute_cpp(code):\n",
541
+ " write_output(code)\n",
542
+ " try:\n",
543
+ " compile_result = subprocess.run(compiler_cmd[2], check=True, text=True, capture_output=True)\n",
544
+ " run_cmd = [\"./optimized\"]\n",
545
+ " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
546
+ " return run_result.stdout\n",
547
+ " except subprocess.CalledProcessError as e:\n",
548
+ " return f\"An error occurred:\\n{e.stderr}\""
549
+ ]
550
+ },
551
+ {
552
+ "cell_type": "code",
553
+ "execution_count": null,
554
+ "id": "9a2274f1-d03b-42c0-8dcc-4ce159b18442",
555
+ "metadata": {},
556
+ "outputs": [],
557
+ "source": [
558
+ "css = \"\"\"\n",
559
+ ".python {background-color: #306998;}\n",
560
+ ".cpp {background-color: #050;}\n",
561
+ "\"\"\""
562
+ ]
563
+ },
564
+ {
565
+ "cell_type": "code",
566
+ "execution_count": null,
567
+ "id": "f1303932-160c-424b-97a8-d28c816721b2",
568
+ "metadata": {},
569
+ "outputs": [],
570
+ "source": [
571
+ "with gr.Blocks(css=css) as ui:\n",
572
+ " gr.Markdown(\"## Convert code from Python to C++\")\n",
573
+ " with gr.Row():\n",
574
+ " python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
575
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
576
+ " with gr.Row():\n",
577
+ " model = gr.Dropdown([\"GPT\", \"Claude\",\"Gemini\"], label=\"Select model\", value=\"GPT\")\n",
578
+ " with gr.Row():\n",
579
+ " convert = gr.Button(\"Convert code\")\n",
580
+ " with gr.Row():\n",
581
+ " python_run = gr.Button(\"Run Python\")\n",
582
+ " cpp_run = gr.Button(\"Run C++\")\n",
583
+ " with gr.Row():\n",
584
+ " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
585
+ " cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
586
+ "\n",
587
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
588
+ " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
589
+ " cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
590
+ "\n",
591
+ "ui.launch(inbrowser=True)"
592
+ ]
593
+ },
594
+ {
595
+ "cell_type": "code",
596
+ "execution_count": null,
597
+ "id": "bb8c5b4e-ec51-4f21-b3f8-6aa94fede86d",
598
+ "metadata": {},
599
+ "outputs": [],
600
+ "source": [
601
+ "from huggingface_hub import login, InferenceClient\n",
602
+ "from transformers import AutoTokenizer"
603
+ ]
604
+ },
605
+ {
606
+ "cell_type": "code",
607
+ "execution_count": null,
608
+ "id": "13347633-4606-4e38-9927-80c39e65c1f1",
609
+ "metadata": {},
610
+ "outputs": [],
611
+ "source": [
612
+ "hf_token = os.environ['HF_TOKEN']\n",
613
+ "login(hf_token, add_to_git_credential=True)"
614
+ ]
615
+ },
616
+ {
617
+ "cell_type": "code",
618
+ "execution_count": null,
619
+ "id": "ef60a4df-6267-4ebd-8eed-dcb917af0a5e",
620
+ "metadata": {},
621
+ "outputs": [],
622
+ "source": [
623
+ "code_qwen = \"Qwen/CodeQwen1.5-7B-Chat\"\n",
624
+ "code_gemma = \"google/codegemma-7b-it\"\n",
625
+ "CODE_QWEN_URL = \"https://h1vdol7jxhje3mpn.us-east-1.aws.endpoints.huggingface.cloud\"\n",
626
+ "CODE_GEMMA_URL = \"https://c5hggiyqachmgnqg.us-east-1.aws.endpoints.huggingface.cloud\""
627
+ ]
628
+ },
629
+ {
630
+ "cell_type": "code",
631
+ "execution_count": null,
632
+ "id": "695ce389-a903-4533-a2f1-cd9e2a6af8f2",
633
+ "metadata": {},
634
+ "outputs": [],
635
+ "source": [
636
+ "tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
637
+ "messages = messages_for(pi)\n",
638
+ "text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)"
639
+ ]
640
+ },
641
+ {
642
+ "cell_type": "code",
643
+ "execution_count": null,
644
+ "id": "d4548e96-0b32-4793-bdd6-1b072c2f26ab",
645
+ "metadata": {},
646
+ "outputs": [],
647
+ "source": [
648
+ "print(text)"
649
+ ]
650
+ },
651
+ {
652
+ "cell_type": "code",
653
+ "execution_count": null,
654
+ "id": "bb2a126b-09e7-4966-bc97-0ef5c2cc7896",
655
+ "metadata": {},
656
+ "outputs": [],
657
+ "source": [
658
+ "client = InferenceClient(CODE_QWEN_URL, token=hf_token)\n",
659
+ "stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)\n",
660
+ "for r in stream:\n",
661
+ " print(r.token.text, end = \"\")"
662
+ ]
663
+ },
664
+ {
665
+ "cell_type": "code",
666
+ "execution_count": null,
667
+ "id": "127a52e5-ad85-42b7-a0f5-9afda5efe090",
668
+ "metadata": {},
669
+ "outputs": [],
670
+ "source": [
671
+ "def stream_code_qwen(python):\n",
672
+ " tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
673
+ " messages = messages_for(python)\n",
674
+ " text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
675
+ " client = InferenceClient(CODE_QWEN_URL, token=hf_token)\n",
676
+ " stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)\n",
677
+ " result = \"\"\n",
678
+ " for r in stream:\n",
679
+ " result += r.token.text\n",
680
+ " yield result "
681
+ ]
682
+ },
683
+ {
684
+ "cell_type": "code",
685
+ "execution_count": null,
686
+ "id": "a82387d1-7651-4923-995b-fe18356fcaa6",
687
+ "metadata": {},
688
+ "outputs": [],
689
+ "source": [
690
+ "def optimize(python, model):\n",
691
+ " if model==\"GPT\":\n",
692
+ " result = stream_gpt(python)\n",
693
+ " elif model==\"Claude\":\n",
694
+ " result = stream_claude(python)\n",
695
+ " elif model==\"Gemini\":\n",
696
+ " result= stream_gemini(python)\n",
697
+ " elif model==\"CodeQwen\":\n",
698
+ " result = stream_code_qwen(python)\n",
699
+ " else:\n",
700
+ " raise ValueError(\"Unknown model\")\n",
701
+ " for stream_so_far in result:\n",
702
+ " yield stream_so_far "
703
+ ]
704
+ },
705
+ {
706
+ "cell_type": "markdown",
707
+ "id": "4b0a6a97-5b8a-4a9b-8ee0-7561e0ced673",
708
+ "metadata": {},
709
+ "source": [
710
+ "<table style=\"margin: 0; text-align: left;\">\n",
711
+ " <tr>\n",
712
+ " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
713
+ " <img src=\"../../thankyou.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
714
+ " </td>\n",
715
+ " <td>\n",
716
+ " <h2 style=\"color:#090;\">Thank you to @CloudLlama for an amazing contribution</h2>\n",
717
+ " <span style=\"color:#090;\">\n",
718
+ " A student has contributed a chunk of code to improve this, in the next 2 cells. You can now select which Python porgram to run,\n",
719
+ " and a compiler is automatically selected that will work on PC, Windows and Mac. Massive thank you @CloudLlama!\n",
720
+ " </span>\n",
721
+ " </td>\n",
722
+ " </tr>\n",
723
+ "</table>"
724
+ ]
725
+ },
726
+ {
727
+ "cell_type": "code",
728
+ "execution_count": null,
729
+ "id": "4ba311ec-c16a-4fe0-946b-4b940704cf65",
730
+ "metadata": {},
731
+ "outputs": [],
732
+ "source": [
733
+ "def select_sample_program(sample_program):\n",
734
+ " if sample_program==\"pi\":\n",
735
+ " return pi\n",
736
+ " elif sample_program==\"python_hard\":\n",
737
+ " return python_hard\n",
738
+ " else:\n",
739
+ " return \"Type your Python program here\""
740
+ ]
741
+ },
742
+ {
743
+ "cell_type": "code",
744
+ "execution_count": null,
745
+ "id": "e42286bc-085c-45dc-b101-234308e58269",
746
+ "metadata": {},
747
+ "outputs": [],
748
+ "source": [
749
+ "import platform\n",
750
+ "\n",
751
+ "VISUAL_STUDIO_2022_TOOLS = \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\Common7\\Tools\\\\VsDevCmd.bat\"\n",
752
+ "VISUAL_STUDIO_2019_TOOLS = \"C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2019\\\\BuildTools\\\\Common7\\\\Tools\\\\VsDevCmd.bat\"\n",
753
+ "\n",
754
+ "simple_cpp = \"\"\"\n",
755
+ "#include <iostream>\n",
756
+ "\n",
757
+ "int main() {\n",
758
+ " std::cout << \"Hello\";\n",
759
+ " return 0;\n",
760
+ "}\n",
761
+ "\"\"\"\n",
762
+ "\n",
763
+ "def run_cmd(command_to_run):\n",
764
+ " try:\n",
765
+ " run_result = subprocess.run(command_to_run, check=True, text=True, capture_output=True)\n",
766
+ " return run_result.stdout if run_result.stdout else \"SUCCESS\"\n",
767
+ " except:\n",
768
+ " return \"\"\n",
769
+ "\n",
770
+ "def c_compiler_cmd(filename_base):\n",
771
+ " my_platform = platform.system()\n",
772
+ " my_compiler = []\n",
773
+ "\n",
774
+ " try:\n",
775
+ " with open(\"simple.cpp\", \"w\") as f:\n",
776
+ " f.write(simple_cpp)\n",
777
+ " \n",
778
+ " if my_platform == \"Windows\":\n",
779
+ " if os.path.isfile(VISUAL_STUDIO_2022_TOOLS):\n",
780
+ " if os.path.isfile(\"./simple.exe\"):\n",
781
+ " os.remove(\"./simple.exe\")\n",
782
+ " compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
783
+ " if run_cmd(compile_cmd):\n",
784
+ " if run_cmd([\"./simple.exe\"]) == \"Hello\":\n",
785
+ " my_compiler = [\"Windows\", \"Visual Studio 2022\", [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
786
+ " \n",
787
+ " if not my_compiler:\n",
788
+ " if os.path.isfile(VISUAL_STUDIO_2019_TOOLS):\n",
789
+ " if os.path.isfile(\"./simple.exe\"):\n",
790
+ " os.remove(\"./simple.exe\")\n",
791
+ " compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
792
+ " if run_cmd(compile_cmd):\n",
793
+ " if run_cmd([\"./simple.exe\"]) == \"Hello\":\n",
794
+ " my_compiler = [\"Windows\", \"Visual Studio 2019\", [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
795
+ " \n",
796
+ " if not my_compiler:\n",
797
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
798
+ " \n",
799
+ " elif my_platform == \"Linux\":\n",
800
+ " if os.path.isfile(\"./simple\"):\n",
801
+ " os.remove(\"./simple\")\n",
802
+ " compile_cmd = [\"g++\", \"simple.cpp\", \"-o\", \"simple\"]\n",
803
+ " if run_cmd(compile_cmd):\n",
804
+ " if run_cmd([\"./simple\"]) == \"Hello\":\n",
805
+ " my_compiler = [\"Linux\", \"GCC (g++)\", [\"g++\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\" ]]\n",
806
+ " \n",
807
+ " if not my_compiler:\n",
808
+ " if os.path.isfile(\"./simple\"):\n",
809
+ " os.remove(\"./simple\")\n",
810
+ " compile_cmd = [\"clang++\", \"simple.cpp\", \"-o\", \"simple\"]\n",
811
+ " if run_cmd(compile_cmd):\n",
812
+ " if run_cmd([\"./simple\"]) == \"Hello\":\n",
813
+ " my_compiler = [\"Linux\", \"Clang++\", [\"clang++\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\"]]\n",
814
+ " \n",
815
+ " if not my_compiler:\n",
816
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
817
+ " \n",
818
+ " elif my_platform == \"Darwin\":\n",
819
+ " if os.path.isfile(\"./simple\"):\n",
820
+ " os.remove(\"./simple\")\n",
821
+ " compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", \"simple\", \"simple.cpp\"]\n",
822
+ " if run_cmd(compile_cmd):\n",
823
+ " if run_cmd([\"./simple\"]) == \"Hello\":\n",
824
+ " my_compiler = [\"Macintosh\", \"Clang++\", [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", f\"{filename_base}\", f\"{filename_base}.cpp\"]]\n",
825
+ " \n",
826
+ " if not my_compiler:\n",
827
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
828
+ " except:\n",
829
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
830
+ " \n",
831
+ " if my_compiler:\n",
832
+ " return my_compiler\n",
833
+ " else:\n",
834
+ " return [\"Unknown\", \"Unavailable\", []]\n"
835
+ ]
836
+ },
837
+ {
838
+ "cell_type": "code",
839
+ "execution_count": null,
840
+ "id": "f9ca2e6f-60c1-4e5f-b570-63c75b2d189b",
841
+ "metadata": {},
842
+ "outputs": [],
843
+ "source": [
844
+ "compiler_cmd = c_compiler_cmd(\"optimized\")\n",
845
+ "\n",
846
+ "with gr.Blocks(css=css) as ui:\n",
847
+ " gr.Markdown(\"## Convert code from Python to C++\")\n",
848
+ " with gr.Row():\n",
849
+ " python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
850
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
851
+ " with gr.Row():\n",
852
+ " with gr.Column():\n",
853
+ " sample_program = gr.Radio([\"pi\", \"python_hard\"], label=\"Sample program\", value=\"python_hard\")\n",
854
+ " model = gr.Dropdown([\"GPT\", \"Claude\", \"Gemini\", \"CodeQwen\"], label=\"Select model\", value=\"GPT\")\n",
855
+ " with gr.Column():\n",
856
+ " architecture = gr.Radio([compiler_cmd[0]], label=\"Architecture\", interactive=False, value=compiler_cmd[0])\n",
857
+ " compiler = gr.Radio([compiler_cmd[1]], label=\"Compiler\", interactive=False, value=compiler_cmd[1])\n",
858
+ " with gr.Row():\n",
859
+ " convert = gr.Button(\"Convert code\")\n",
860
+ " with gr.Row():\n",
861
+ " python_run = gr.Button(\"Run Python\")\n",
862
+ " if not compiler_cmd[1] == \"Unavailable\":\n",
863
+ " cpp_run = gr.Button(\"Run C++\")\n",
864
+ " else:\n",
865
+ " cpp_run = gr.Button(\"No compiler to run C++\", interactive=False)\n",
866
+ " with gr.Row():\n",
867
+ " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
868
+ " cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
869
+ "\n",
870
+ " sample_program.change(select_sample_program, inputs=[sample_program], outputs=[python])\n",
871
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
872
+ " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
873
+ " cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
874
+ "\n",
875
+ "ui.launch(inbrowser=True)"
876
+ ]
877
+ },
878
+ {
879
+ "cell_type": "code",
880
+ "execution_count": null,
881
+ "id": "9d0ad093-425b-488e-8c3f-67f729dd9c06",
882
+ "metadata": {},
883
+ "outputs": [],
884
+ "source": []
885
+ }
886
+ ],
887
+ "metadata": {
888
+ "kernelspec": {
889
+ "display_name": "Python 3 (ipykernel)",
890
+ "language": "python",
891
+ "name": "python3"
892
+ },
893
+ "language_info": {
894
+ "codemirror_mode": {
895
+ "name": "ipython",
896
+ "version": 3
897
+ },
898
+ "file_extension": ".py",
899
+ "mimetype": "text/x-python",
900
+ "name": "python",
901
+ "nbconvert_exporter": "python",
902
+ "pygments_lexer": "ipython3",
903
+ "version": "3.11.11"
904
+ }
905
+ },
906
+ "nbformat": 4,
907
+ "nbformat_minor": 5
908
+ }
community-contributions/day4_extra_deepseek_and_hf_inference_provider_added.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
community-contributions/day4_with_inference_provider.ipynb ADDED
@@ -0,0 +1,925 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Code Generator\n",
9
+ "\n",
10
+ "The requirement: use an Open Source model to generate high performance C++ code from Python code\n",
11
+ "\n",
12
+ "To replicate this, you'll need to set up a HuggingFace endpoint as I do in the video. It's simple to do, and it's quite satisfying to see the results!\n",
13
+ "\n",
14
+ "It's also an important part of your learning; this is the first example of deploying an open source model to be behind an API. We'll return to this in Week 8, but this should plant a seed in your mind for what's involved in moving open source models into production.\n",
15
+ "\n",
16
+ "Added the use of inference providers that was introduced recently by Hugging Face to convert the code.\n",
17
+ "Improved the user prompt to include algorithic efficeiny and performance optimization.\n",
18
+ "\n",
19
+ "Added Java as a conversion option.\n",
20
+ "\n",
21
+ "Note: C++ commands work on windows environment.\n"
22
+ ]
23
+ },
24
+ {
25
+ "cell_type": "markdown",
26
+ "id": "22e1567b-33fd-49e7-866e-4b635d15715a",
27
+ "metadata": {},
28
+ "source": [
29
+ "<table style=\"margin: 0; text-align: left;\">\n",
30
+ " <tr>\n",
31
+ " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
32
+ " <img src=\"../important.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
33
+ " </td>\n",
34
+ " <td>\n",
35
+ " <h1 style=\"color:#900;\">Important - Pause Endpoints when not in use</h1>\n",
36
+ " <span style=\"color:#900;\">\n",
37
+ " If you do decide to use HuggingFace endpoints for this project, you should stop or pause the endpoints when you are done to avoid accruing unnecessary running cost. The costs are very low as long as you only run the endpoint when you're using it. Navigate to the HuggingFace endpoint UI <a href=\"https://ui.endpoints.huggingface.co/\">here,</a> open your endpoint, and click Pause to put it on pause so you no longer pay for it. \n",
38
+ "Many thanks to student John L. for raising this.\n",
39
+ "<br/><br/>\n",
40
+ "In week 8 we will use Modal instead of HuggingFace endpoints; with Modal you only pay for the time that you use it and you should get free credits.\n",
41
+ " </span>\n",
42
+ " </td>\n",
43
+ " </tr>\n",
44
+ "</table>"
45
+ ]
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": 231,
50
+ "id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
51
+ "metadata": {},
52
+ "outputs": [],
53
+ "source": [
54
+ "# imports\n",
55
+ "\n",
56
+ "import os\n",
57
+ "import io\n",
58
+ "import sys\n",
59
+ "import json\n",
60
+ "import requests\n",
61
+ "from dotenv import load_dotenv\n",
62
+ "from openai import OpenAI\n",
63
+ "import google.generativeai\n",
64
+ "import anthropic\n",
65
+ "from IPython.display import Markdown, display, update_display\n",
66
+ "import gradio as gr\n",
67
+ "import subprocess, re"
68
+ ]
69
+ },
70
+ {
71
+ "cell_type": "code",
72
+ "execution_count": 198,
73
+ "id": "4f672e1c-87e9-4865-b760-370fa605e614",
74
+ "metadata": {},
75
+ "outputs": [],
76
+ "source": [
77
+ "# environment\n",
78
+ "\n",
79
+ "load_dotenv()\n",
80
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
81
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
82
+ "os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
83
+ ]
84
+ },
85
+ {
86
+ "cell_type": "code",
87
+ "execution_count": 199,
88
+ "id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
89
+ "metadata": {},
90
+ "outputs": [],
91
+ "source": [
92
+ "# initialize\n",
93
+ "\n",
94
+ "openai = OpenAI()\n",
95
+ "claude = anthropic.Anthropic()\n",
96
+ "OPENAI_MODEL = \"gpt-4o\"\n",
97
+ "CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\""
98
+ ]
99
+ },
100
+ {
101
+ "cell_type": "code",
102
+ "execution_count": 200,
103
+ "id": "2db60a72-d098-42ca-8ce2-1e037c86b718",
104
+ "metadata": {},
105
+ "outputs": [],
106
+ "source": [
107
+ "def system_prompt_for(language: str) -> str:\n",
108
+ " system_prompt = (\n",
109
+ " f\"You are an assistant that reimplements Python code in high performance {language.upper()} for an Windows intel i7.\"\n",
110
+ " f\"Respond only with {language.upper()} code; use comments sparingly and do not provide any explanation other than occasional comments.\"\n",
111
+ " f\"The {language.upper()} response needs to produce an identical output in the fastest possible time. Keep implementations of random number generators identical so that results match exactly.\"\n",
112
+ " )\n",
113
+ " return system_prompt"
114
+ ]
115
+ },
116
+ {
117
+ "cell_type": "code",
118
+ "execution_count": 243,
119
+ "id": "70583432-e851-40d1-a219-2fb32b830dc8",
120
+ "metadata": {},
121
+ "outputs": [],
122
+ "source": [
123
+ "#updated the original prompt to include algorithic efficeiny and performance optimization\n",
124
+ "def user_prompt_for(python: str, language: str) -> str:\n",
125
+ " if language.lower() not in {\"cpp\", \"java\"}:\n",
126
+ " raise ValueError(\"Unsupported language. Please choose 'C++' or 'Java'.\")\n",
127
+ " \n",
128
+ " optimization_notes = {\n",
129
+ " \"cpp\": (\n",
130
+ " \"- Use `int64_t` instead of `int` where necessary to prevent overflows.\\n\"\n",
131
+ " \"- Ensure random number generation in C++ matches Python's output as closely as possible.\\n\"\n",
132
+ " \"- Avoid undefined behavior, such as bit shifts that exceed type width (`1UL << 32` is incorrect for `uint32_t`).\\n\"\n",
133
+ " \"- Utilize `std::vector` for dynamic arrays and prefer preallocation for efficiency.\\n\"\n",
134
+ " \"- Consider `std::array` or `std::span` when fixed-size arrays are sufficient.\\n\"\n",
135
+ " \"- Optimize with **SIMD**, cache-friendly structures, and memory alignment where necessary.\\n\"\n",
136
+ " ),\n",
137
+ " \"java\": (\n",
138
+ " \"- Use `long` instead of `int` where necessary to prevent overflows.\\n\"\n",
139
+ " \"- Ensure random number generation in Java matches Python's output as closely as possible.\\n\"\n",
140
+ " \"- Use `ArrayList` instead of primitive arrays if dynamic resizing is needed.\\n\"\n",
141
+ " \"- Utilize `BigInteger` if handling large numbers that could exceed `long`.\\n\"\n",
142
+ " \"- Optimize with **parallel streams** (`IntStream.parallel()`) and **efficient data structures** (`HashMap`, `LinkedList`, etc.).\\n\"\n",
143
+ " )\n",
144
+ " }\n",
145
+ "\n",
146
+ " user_prompt = (\n",
147
+ " f\"First, analyze the given Python code to understand its core purpose and algorithmic approach. \"\n",
148
+ " f\"Then, implement a {language} solution that achieves the same output while prioritizing:\\n\"\n",
149
+ " \"1. **Algorithmic Efficiency** - Optimize time and space complexity, even if it means using a different approach.\\n\"\n",
150
+ " \"2. **Numerical Correctness** - Prevent integer overflows, use appropriate data types (`long`, `BigInteger`, etc.), \"\n",
151
+ " \"and ensure correct handling of edge cases.\\n\"\n",
152
+ " \"3. **Performance Optimization** - Utilize language-specific features for efficiency.\\n\\n\"\n",
153
+ " \n",
154
+ " \"### **Important Notes:**\\n\"\n",
155
+ " + optimization_notes[language.lower()] +\n",
156
+ " \"\\n### **Expected Response:**\\n\"\n",
157
+ " f\"Respond **only with {language} code**, including all necessary imports and ensuring the output matches the Python version exactly.\\n\\n\"\n",
158
+ " \n",
159
+ " \"Here's the Python code to analyze and optimize:\\n\\n\"\n",
160
+ " + python\n",
161
+ " )\n",
162
+ " \n",
163
+ " return user_prompt\n"
164
+ ]
165
+ },
166
+ {
167
+ "cell_type": "code",
168
+ "execution_count": 202,
169
+ "id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
170
+ "metadata": {},
171
+ "outputs": [],
172
+ "source": [
173
+ "def messages_for(python, language=\"cpp\"):\n",
174
+ " return [\n",
175
+ " {\"role\": \"system\", \"content\": system_prompt_for(language)},\n",
176
+ " {\"role\": \"user\", \"content\": user_prompt_for(python, language)}\n",
177
+ " ]"
178
+ ]
179
+ },
180
+ {
181
+ "cell_type": "code",
182
+ "execution_count": 241,
183
+ "id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
184
+ "metadata": {},
185
+ "outputs": [],
186
+ "source": [
187
+ "# write to a file called optimized.cpp\n",
188
+ "\n",
189
+ "def write_output(code, file_name):\n",
190
+ " with open(file_name, \"w\") as f:\n",
191
+ " f.write(code)"
192
+ ]
193
+ },
194
+ {
195
+ "cell_type": "code",
196
+ "execution_count": 226,
197
+ "id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
198
+ "metadata": {},
199
+ "outputs": [],
200
+ "source": [
201
+ "def optimize_gpt(python, language=\"cpp\"): \n",
202
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python, language), stream=True)\n",
203
+ " reply = \"\"\n",
204
+ " for chunk in stream:\n",
205
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
206
+ " reply += fragment\n",
207
+ " print(fragment, end='', flush=True)\n",
208
+ " file_name= f\"optimized.{language}\"\n",
209
+ " if language == \"java\":\n",
210
+ " # Extract class name from Java code\n",
211
+ " match = re.search(r\"\\b(public\\s+)?class\\s+(\\w+)\", reply)\n",
212
+ " class_name = match.group(2) if match else \"OptimizedJava\"\n",
213
+ " file_name = f\"{class_name}.java\"\n",
214
+ " else:\n",
215
+ " file_name = f\"optimized.{language}\"\n",
216
+ " write_output(reply, file_name)"
217
+ ]
218
+ },
219
+ {
220
+ "cell_type": "code",
221
+ "execution_count": 227,
222
+ "id": "7cd84ad8-d55c-4fe0-9eeb-1895c95c4a9d",
223
+ "metadata": {},
224
+ "outputs": [],
225
+ "source": [
226
+ "def optimize_claude(python, language=\"cpp\"):\n",
227
+ " result = claude.messages.stream(\n",
228
+ " model=CLAUDE_MODEL,\n",
229
+ " max_tokens=2000,\n",
230
+ " system=system_message,\n",
231
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python, language)}],\n",
232
+ " )\n",
233
+ " reply = \"\"\n",
234
+ " with result as stream:\n",
235
+ " for text in stream.text_stream:\n",
236
+ " reply += text\n",
237
+ " print(text, end=\"\", flush=True)\n",
238
+ " if language == \"java\":\n",
239
+ " # Extract class name from Java code\n",
240
+ " match = re.search(r\"\\b(public\\s+)?class\\s+(\\w+)\", reply)\n",
241
+ " class_name = match.group(2) if match else \"OptimizedJava\"\n",
242
+ " file_name = f\"{class_name}.java\"\n",
243
+ " else:\n",
244
+ " file_name = f\"optimized.{language}\"\n",
245
+ " write_output(reply, file_name)"
246
+ ]
247
+ },
248
+ {
249
+ "cell_type": "code",
250
+ "execution_count": 206,
251
+ "id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
252
+ "metadata": {},
253
+ "outputs": [],
254
+ "source": [
255
+ "pi = \"\"\"\n",
256
+ "import time\n",
257
+ "\n",
258
+ "def calculate(iterations, param1, param2):\n",
259
+ " result = 1.0\n",
260
+ " for i in range(1, iterations+1):\n",
261
+ " j = i * param1 - param2\n",
262
+ " result -= (1/j)\n",
263
+ " j = i * param1 + param2\n",
264
+ " result += (1/j)\n",
265
+ " return result\n",
266
+ "\n",
267
+ "start_time = time.time()\n",
268
+ "result = calculate(100_000_000, 4, 1) * 4\n",
269
+ "end_time = time.time()\n",
270
+ "\n",
271
+ "print(f\"Result: {result:.12f}\")\n",
272
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
273
+ "\"\"\""
274
+ ]
275
+ },
276
+ {
277
+ "cell_type": "code",
278
+ "execution_count": 59,
279
+ "id": "7fe1cd4b-d2c5-4303-afed-2115a3fef200",
280
+ "metadata": {},
281
+ "outputs": [],
282
+ "source": [
283
+ "exec(pi)"
284
+ ]
285
+ },
286
+ {
287
+ "cell_type": "code",
288
+ "execution_count": 91,
289
+ "id": "105db6f9-343c-491d-8e44-3a5328b81719",
290
+ "metadata": {},
291
+ "outputs": [],
292
+ "source": [
293
+ "optimize_gpt(pi, \"java\")"
294
+ ]
295
+ },
296
+ {
297
+ "cell_type": "code",
298
+ "execution_count": null,
299
+ "id": "bf26ee95-0c77-491d-9a91-579a1e96a8a3",
300
+ "metadata": {},
301
+ "outputs": [],
302
+ "source": [
303
+ "exec(pi)"
304
+ ]
305
+ },
306
+ {
307
+ "cell_type": "code",
308
+ "execution_count": null,
309
+ "id": "4194e40c-04ab-4940-9d64-b4ad37c5bb40",
310
+ "metadata": {},
311
+ "outputs": [],
312
+ "source": [
313
+ "!g++ -O3 -std=c++17 -march=native -o optimized optimized.cpp\n",
314
+ "!optimized.exe"
315
+ ]
316
+ },
317
+ {
318
+ "cell_type": "code",
319
+ "execution_count": null,
320
+ "id": "983a11fe-e24d-4c65-8269-9802c5ef3ae6",
321
+ "metadata": {},
322
+ "outputs": [],
323
+ "source": [
324
+ "optimize_claude(pi)"
325
+ ]
326
+ },
327
+ {
328
+ "cell_type": "code",
329
+ "execution_count": null,
330
+ "id": "d5a766f9-3d23-4bb4-a1d4-88ec44b61ddf",
331
+ "metadata": {},
332
+ "outputs": [],
333
+ "source": [
334
+ "!g++ -O3 -std=c++17 -march=native -o optimized optimized.cpp\n",
335
+ "!optimized.exe"
336
+ ]
337
+ },
338
+ {
339
+ "cell_type": "code",
340
+ "execution_count": 207,
341
+ "id": "c3b497b3-f569-420e-b92e-fb0f49957ce0",
342
+ "metadata": {},
343
+ "outputs": [],
344
+ "source": [
345
+ "python_hard = \"\"\"# Be careful to support large number sizes\n",
346
+ "\n",
347
+ "def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
348
+ " value = seed\n",
349
+ " while True:\n",
350
+ " value = (a * value + c) % m\n",
351
+ " yield value\n",
352
+ " \n",
353
+ "def max_subarray_sum(n, seed, min_val, max_val):\n",
354
+ " lcg_gen = lcg(seed)\n",
355
+ " random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
356
+ " max_sum = float('-inf')\n",
357
+ " for i in range(n):\n",
358
+ " current_sum = 0\n",
359
+ " for j in range(i, n):\n",
360
+ " current_sum += random_numbers[j]\n",
361
+ " if current_sum > max_sum:\n",
362
+ " max_sum = current_sum\n",
363
+ " return max_sum\n",
364
+ "\n",
365
+ "def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
366
+ " total_sum = 0\n",
367
+ " lcg_gen = lcg(initial_seed)\n",
368
+ " for _ in range(20):\n",
369
+ " seed = next(lcg_gen)\n",
370
+ " total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
371
+ " return total_sum\n",
372
+ "\n",
373
+ "# Parameters\n",
374
+ "n = 10000 # Number of random numbers\n",
375
+ "initial_seed = 42 # Initial seed for the LCG\n",
376
+ "min_val = -10 # Minimum value of random numbers\n",
377
+ "max_val = 10 # Maximum value of random numbers\n",
378
+ "\n",
379
+ "# Timing the function\n",
380
+ "import time\n",
381
+ "start_time = time.time()\n",
382
+ "result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
383
+ "end_time = time.time()\n",
384
+ "\n",
385
+ "print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
386
+ "print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
387
+ "\"\"\""
388
+ ]
389
+ },
390
+ {
391
+ "cell_type": "code",
392
+ "execution_count": 23,
393
+ "id": "dab5e4bc-276c-4555-bd4c-12c699d5e899",
394
+ "metadata": {},
395
+ "outputs": [],
396
+ "source": [
397
+ "exec(python_hard)"
398
+ ]
399
+ },
400
+ {
401
+ "cell_type": "code",
402
+ "execution_count": null,
403
+ "id": "e8d24ed5-2c15-4f55-80e7-13a3952b3cb8",
404
+ "metadata": {},
405
+ "outputs": [],
406
+ "source": [
407
+ "optimize_gpt(python_hard)"
408
+ ]
409
+ },
410
+ {
411
+ "cell_type": "code",
412
+ "execution_count": null,
413
+ "id": "e0b3d073-88a2-40b2-831c-6f0c345c256f",
414
+ "metadata": {},
415
+ "outputs": [],
416
+ "source": [
417
+ "!g++ -O3 -std=c++17 -march=native -o optimized optimized.cpp\n",
418
+ "!optimized.exe"
419
+ ]
420
+ },
421
+ {
422
+ "cell_type": "code",
423
+ "execution_count": null,
424
+ "id": "e9305446-1d0c-4b51-866a-b8c1e299bf5c",
425
+ "metadata": {},
426
+ "outputs": [],
427
+ "source": [
428
+ "optimize_claude(python_hard)"
429
+ ]
430
+ },
431
+ {
432
+ "cell_type": "code",
433
+ "execution_count": null,
434
+ "id": "0c181036-8193-4fdd-aef3-fc513b218d43",
435
+ "metadata": {},
436
+ "outputs": [],
437
+ "source": [
438
+ "!g++ -O3 -std=c++17 -march=native -o optimized optimized.cpp\n",
439
+ "!optimized.exe"
440
+ ]
441
+ },
442
+ {
443
+ "cell_type": "code",
444
+ "execution_count": 240,
445
+ "id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
446
+ "metadata": {},
447
+ "outputs": [],
448
+ "source": [
449
+ "def stream_gpt(python, language=\"cpp\"): \n",
450
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python, language), stream=True)\n",
451
+ " reply = \"\"\n",
452
+ " code_block = f\"```{language}\\n\"\n",
453
+ " for chunk in stream:\n",
454
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
455
+ " reply += fragment\n",
456
+ " cleaned_reply = reply.replace(code_block,'').replace('```','')\n",
457
+ " yield cleaned_reply"
458
+ ]
459
+ },
460
+ {
461
+ "cell_type": "code",
462
+ "execution_count": 239,
463
+ "id": "8669f56b-8314-4582-a167-78842caea131",
464
+ "metadata": {},
465
+ "outputs": [],
466
+ "source": [
467
+ "def stream_claude(python, language=\"cpp\"):\n",
468
+ " result = claude.messages.stream(\n",
469
+ " model=CLAUDE_MODEL,\n",
470
+ " max_tokens=2000,\n",
471
+ " system=system_message,\n",
472
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python, language)}],\n",
473
+ " )\n",
474
+ " reply = \"\"\n",
475
+ " code_block = f\"```{language}\\n\"\n",
476
+ " with result as stream:\n",
477
+ " for text in stream.text_stream:\n",
478
+ " reply += text\n",
479
+ " cleaned_reply = reply.replace(code_block,'').replace('```','')\n",
480
+ " yield cleaned_reply"
481
+ ]
482
+ },
483
+ {
484
+ "cell_type": "code",
485
+ "execution_count": 186,
486
+ "id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
487
+ "metadata": {},
488
+ "outputs": [],
489
+ "source": [
490
+ "def optimize(python, model):\n",
491
+ " if model==\"GPT\":\n",
492
+ " result = stream_gpt(python)\n",
493
+ " elif model==\"Claude\":\n",
494
+ " result = stream_claude(python)\n",
495
+ " else:\n",
496
+ " raise ValueError(\"Unknown model\")\n",
497
+ " for stream_so_far in result:\n",
498
+ " yield stream_so_far "
499
+ ]
500
+ },
501
+ {
502
+ "cell_type": "code",
503
+ "execution_count": 189,
504
+ "id": "f1ddb38e-6b0a-4c37-baa4-ace0b7de887a",
505
+ "metadata": {},
506
+ "outputs": [
507
+ {
508
+ "data": {
509
+ "text/html": [
510
+ "<div><iframe src=\"http://127.0.0.1:7888/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
511
+ ],
512
+ "text/plain": [
513
+ "<IPython.core.display.HTML object>"
514
+ ]
515
+ },
516
+ "metadata": {},
517
+ "output_type": "display_data"
518
+ },
519
+ {
520
+ "data": {
521
+ "text/plain": []
522
+ },
523
+ "execution_count": 189,
524
+ "metadata": {},
525
+ "output_type": "execute_result"
526
+ }
527
+ ],
528
+ "source": [
529
+ "with gr.Blocks() as ui:\n",
530
+ " with gr.Row():\n",
531
+ " python = gr.Textbox(label=\"Python code:\", lines=10, value=python_hard)\n",
532
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
533
+ " with gr.Row():\n",
534
+ " model = gr.Dropdown([\"GPT\", \"Claude\"], label=\"Select model\", value=\"GPT\")\n",
535
+ " convert = gr.Button(\"Convert code\")\n",
536
+ "\n",
537
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
538
+ "\n",
539
+ "ui.launch(inbrowser=True)"
540
+ ]
541
+ },
542
+ {
543
+ "cell_type": "code",
544
+ "execution_count": 210,
545
+ "id": "19bf2bff-a822-4009-a539-f003b1651383",
546
+ "metadata": {},
547
+ "outputs": [],
548
+ "source": [
549
+ "def execute_python(code):\n",
550
+ " try:\n",
551
+ " output = io.StringIO()\n",
552
+ " sys.stdout = output\n",
553
+ " exec(code)\n",
554
+ " finally:\n",
555
+ " sys.stdout = sys.__stdout__\n",
556
+ " return output.getvalue()"
557
+ ]
558
+ },
559
+ {
560
+ "cell_type": "code",
561
+ "execution_count": 211,
562
+ "id": "9a2274f1-d03b-42c0-8dcc-4ce159b18442",
563
+ "metadata": {},
564
+ "outputs": [],
565
+ "source": [
566
+ "css = \"\"\"\n",
567
+ ".python {background-color: #306998;}\n",
568
+ ".cpp {background-color: #050;}\n",
569
+ ".java {background-color: #306775;}\n",
570
+ "\"\"\""
571
+ ]
572
+ },
573
+ {
574
+ "cell_type": "code",
575
+ "execution_count": 97,
576
+ "id": "f1303932-160c-424b-97a8-d28c816721b2",
577
+ "metadata": {},
578
+ "outputs": [
579
+ {
580
+ "data": {
581
+ "text/html": [
582
+ "<div><iframe src=\"http://127.0.0.1:7868/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
583
+ ],
584
+ "text/plain": [
585
+ "<IPython.core.display.HTML object>"
586
+ ]
587
+ },
588
+ "metadata": {},
589
+ "output_type": "display_data"
590
+ },
591
+ {
592
+ "data": {
593
+ "text/plain": []
594
+ },
595
+ "execution_count": 97,
596
+ "metadata": {},
597
+ "output_type": "execute_result"
598
+ }
599
+ ],
600
+ "source": [
601
+ "with gr.Blocks(css=css) as ui:\n",
602
+ " gr.Markdown(\"## Convert code from Python to C++\")\n",
603
+ " with gr.Row():\n",
604
+ " python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
605
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
606
+ " with gr.Row():\n",
607
+ " model = gr.Dropdown([\"GPT\", \"Claude\"], label=\"Select model\", value=\"GPT\")\n",
608
+ " with gr.Row():\n",
609
+ " convert = gr.Button(\"Convert code\")\n",
610
+ " with gr.Row():\n",
611
+ " python_run = gr.Button(\"Run Python\")\n",
612
+ " cpp_run = gr.Button(\"Run C++\")\n",
613
+ " with gr.Row():\n",
614
+ " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
615
+ " cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
616
+ "\n",
617
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
618
+ " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
619
+ " cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
620
+ "\n",
621
+ "ui.launch(inbrowser=True)"
622
+ ]
623
+ },
624
+ {
625
+ "cell_type": "code",
626
+ "execution_count": 191,
627
+ "id": "bb8c5b4e-ec51-4f21-b3f8-6aa94fede86d",
628
+ "metadata": {},
629
+ "outputs": [],
630
+ "source": [
631
+ "from huggingface_hub import login, InferenceClient"
632
+ ]
633
+ },
634
+ {
635
+ "cell_type": "code",
636
+ "execution_count": 117,
637
+ "id": "13347633-4606-4e38-9927-80c39e65c1f1",
638
+ "metadata": {},
639
+ "outputs": [
640
+ {
641
+ "name": "stderr",
642
+ "output_type": "stream",
643
+ "text": [
644
+ "Note: Environment variable`HF_TOKEN` is set and is the current active token independently from the token you've just configured.\n"
645
+ ]
646
+ }
647
+ ],
648
+ "source": [
649
+ "hf_token = os.environ['HF_TOKEN']\n",
650
+ "login(hf_token)"
651
+ ]
652
+ },
653
+ {
654
+ "cell_type": "code",
655
+ "execution_count": 118,
656
+ "id": "ef60a4df-6267-4ebd-8eed-dcb917af0a5e",
657
+ "metadata": {},
658
+ "outputs": [],
659
+ "source": [
660
+ "code_qwen = \"Qwen/CodeQwen1.5-7B-Chat\"\n",
661
+ "code_gemma = \"google/codegemma-7b-it\"\n",
662
+ "messages=messages_for(pi)"
663
+ ]
664
+ },
665
+ {
666
+ "cell_type": "code",
667
+ "execution_count": 119,
668
+ "id": "3825d77a-03c6-42b2-89bc-ccbcb1585740",
669
+ "metadata": {},
670
+ "outputs": [
671
+ {
672
+ "ename": "HfHubHTTPError",
673
+ "evalue": "402 Client Error: Payment Required for url: https://huggingface.co/api/inference-proxy/sambanova/v1/chat/completions (Request ID: Root=1-67afb729-1eb9aff1704314144ef14e59;2df843ad-b7d2-4145-bb7b-1cfd94ae19ef)\n\nYou have exceeded your monthly included credits for Inference Endpoints. Subscribe to PRO to get 20x more monthly allowance.",
674
+ "output_type": "error",
675
+ "traceback": [
676
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
677
+ "\u001b[1;31mHTTPError\u001b[0m Traceback (most recent call last)",
678
+ "File \u001b[1;32m~\\anaconda3\\envs\\llms\\Lib\\site-packages\\huggingface_hub\\utils\\_http.py:406\u001b[0m, in \u001b[0;36mhf_raise_for_status\u001b[1;34m(response, endpoint_name)\u001b[0m\n\u001b[0;32m 405\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 406\u001b[0m \u001b[43mresponse\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraise_for_status\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 407\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m HTTPError \u001b[38;5;28;01mas\u001b[39;00m e:\n",
679
+ "File \u001b[1;32m~\\anaconda3\\envs\\llms\\Lib\\site-packages\\requests\\models.py:1024\u001b[0m, in \u001b[0;36mResponse.raise_for_status\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 1023\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m http_error_msg:\n\u001b[1;32m-> 1024\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HTTPError(http_error_msg, response\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m)\n",
680
+ "\u001b[1;31mHTTPError\u001b[0m: 402 Client Error: Payment Required for url: https://huggingface.co/api/inference-proxy/sambanova/v1/chat/completions",
681
+ "\nThe above exception was the direct cause of the following exception:\n",
682
+ "\u001b[1;31mHfHubHTTPError\u001b[0m Traceback (most recent call last)",
683
+ "Cell \u001b[1;32mIn[119], line 5\u001b[0m\n\u001b[0;32m 1\u001b[0m client \u001b[38;5;241m=\u001b[39m InferenceClient(\n\u001b[0;32m 2\u001b[0m \tprovider\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msambanova\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m 3\u001b[0m \tapi_key\u001b[38;5;241m=\u001b[39mhf_token\n\u001b[0;32m 4\u001b[0m )\n\u001b[1;32m----> 5\u001b[0m stream \u001b[38;5;241m=\u001b[39m \u001b[43mclient\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mchat\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcompletions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreate\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 6\u001b[0m \u001b[43m\t\u001b[49m\u001b[43mmodel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mQwen/Qwen2.5-Coder-32B-Instruct\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\n\u001b[0;32m 7\u001b[0m \u001b[43m\t\u001b[49m\u001b[43mmessages\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmessages\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\n\u001b[0;32m 8\u001b[0m \u001b[43m\t\u001b[49m\u001b[43mmax_tokens\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m500\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m 9\u001b[0m \u001b[43m\t\u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\n\u001b[0;32m 10\u001b[0m \u001b[43m)\u001b[49m\n\u001b[0;32m 12\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m chunk \u001b[38;5;129;01min\u001b[39;00m stream:\n\u001b[0;32m 13\u001b[0m \u001b[38;5;28mprint\u001b[39m(chunk\u001b[38;5;241m.\u001b[39mchoices[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m.\u001b[39mdelta\u001b[38;5;241m.\u001b[39mcontent, end\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
684
+ "File \u001b[1;32m~\\anaconda3\\envs\\llms\\Lib\\site-packages\\huggingface_hub\\inference\\_client.py:970\u001b[0m, in \u001b[0;36mInferenceClient.chat_completion\u001b[1;34m(self, messages, model, stream, frequency_penalty, logit_bias, logprobs, max_tokens, n, presence_penalty, response_format, seed, stop, stream_options, temperature, tool_choice, tool_prompt, tools, top_logprobs, top_p)\u001b[0m\n\u001b[0;32m 943\u001b[0m parameters \u001b[38;5;241m=\u001b[39m {\n\u001b[0;32m 944\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m\"\u001b[39m: payload_model,\n\u001b[0;32m 945\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrequency_penalty\u001b[39m\u001b[38;5;124m\"\u001b[39m: frequency_penalty,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 961\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstream_options\u001b[39m\u001b[38;5;124m\"\u001b[39m: stream_options,\n\u001b[0;32m 962\u001b[0m }\n\u001b[0;32m 963\u001b[0m request_parameters \u001b[38;5;241m=\u001b[39m provider_helper\u001b[38;5;241m.\u001b[39mprepare_request(\n\u001b[0;32m 964\u001b[0m inputs\u001b[38;5;241m=\u001b[39mmessages,\n\u001b[0;32m 965\u001b[0m parameters\u001b[38;5;241m=\u001b[39mparameters,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 968\u001b[0m api_key\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtoken,\n\u001b[0;32m 969\u001b[0m )\n\u001b[1;32m--> 970\u001b[0m data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_inner_post\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequest_parameters\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 972\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m stream:\n\u001b[0;32m 973\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _stream_chat_completion_response(data) \u001b[38;5;66;03m# type: ignore[arg-type]\u001b[39;00m\n",
685
+ "File \u001b[1;32m~\\anaconda3\\envs\\llms\\Lib\\site-packages\\huggingface_hub\\inference\\_client.py:327\u001b[0m, in \u001b[0;36mInferenceClient._inner_post\u001b[1;34m(self, request_parameters, stream)\u001b[0m\n\u001b[0;32m 324\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m InferenceTimeoutError(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInference call timed out: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrequest_parameters\u001b[38;5;241m.\u001b[39murl\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merror\u001b[39;00m \u001b[38;5;66;03m# type: ignore\u001b[39;00m\n\u001b[0;32m 326\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 327\u001b[0m \u001b[43mhf_raise_for_status\u001b[49m\u001b[43m(\u001b[49m\u001b[43mresponse\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 328\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m response\u001b[38;5;241m.\u001b[39miter_lines() \u001b[38;5;28;01mif\u001b[39;00m stream \u001b[38;5;28;01melse\u001b[39;00m response\u001b[38;5;241m.\u001b[39mcontent\n\u001b[0;32m 329\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m HTTPError \u001b[38;5;28;01mas\u001b[39;00m error:\n",
686
+ "File \u001b[1;32m~\\anaconda3\\envs\\llms\\Lib\\site-packages\\huggingface_hub\\utils\\_http.py:477\u001b[0m, in \u001b[0;36mhf_raise_for_status\u001b[1;34m(response, endpoint_name)\u001b[0m\n\u001b[0;32m 473\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m _format(HfHubHTTPError, message, response) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n\u001b[0;32m 475\u001b[0m \u001b[38;5;66;03m# Convert `HTTPError` into a `HfHubHTTPError` to display request information\u001b[39;00m\n\u001b[0;32m 476\u001b[0m \u001b[38;5;66;03m# as well (request id and/or server error message)\u001b[39;00m\n\u001b[1;32m--> 477\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m _format(HfHubHTTPError, \u001b[38;5;28mstr\u001b[39m(e), response) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n",
687
+ "\u001b[1;31mHfHubHTTPError\u001b[0m: 402 Client Error: Payment Required for url: https://huggingface.co/api/inference-proxy/sambanova/v1/chat/completions (Request ID: Root=1-67afb729-1eb9aff1704314144ef14e59;2df843ad-b7d2-4145-bb7b-1cfd94ae19ef)\n\nYou have exceeded your monthly included credits for Inference Endpoints. Subscribe to PRO to get 20x more monthly allowance."
688
+ ]
689
+ }
690
+ ],
691
+ "source": [
692
+ "client = InferenceClient(\n",
693
+ "\tprovider=\"sambanova\",\n",
694
+ "\tapi_key=hf_token\n",
695
+ ")\n",
696
+ "stream = client.chat.completions.create(\n",
697
+ "\tmodel=\"Qwen/Qwen2.5-Coder-32B-Instruct\", \n",
698
+ "\tmessages=messages, \n",
699
+ "\tmax_tokens=500,\n",
700
+ "\tstream=True\n",
701
+ ")\n",
702
+ "\n",
703
+ "for chunk in stream:\n",
704
+ " print(chunk.choices[0].delta.content, end=\"\")"
705
+ ]
706
+ },
707
+ {
708
+ "cell_type": "code",
709
+ "execution_count": 65,
710
+ "id": "cc0c3e9c-2572-41d1-a476-6eae96b20695",
711
+ "metadata": {},
712
+ "outputs": [],
713
+ "source": [
714
+ "# using inference providers\n",
715
+ "def stream_code_qwen(python):\n",
716
+ " messages = messages_for(python)\n",
717
+ " client = InferenceClient(\n",
718
+ " \tprovider=\"sambanova\",\n",
719
+ " \tapi_key=hf_token\n",
720
+ " )\n",
721
+ " stream = client.chat.completions.create(\n",
722
+ " \tmodel=\"Qwen/Qwen2.5-Coder-32B-Instruct\", \n",
723
+ " \tmessages=messages, \n",
724
+ " \tmax_tokens=500,\n",
725
+ " \tstream=True\n",
726
+ " )\n",
727
+ " result = \"\"\n",
728
+ " for chunk in stream:\n",
729
+ " if chunk.choices and chunk.choices[0].delta.content:\n",
730
+ " result += chunk.choices[0].delta.content\n",
731
+ " yield result"
732
+ ]
733
+ },
734
+ {
735
+ "cell_type": "code",
736
+ "execution_count": 212,
737
+ "id": "a82387d1-7651-4923-995b-fe18356fcaa6",
738
+ "metadata": {},
739
+ "outputs": [],
740
+ "source": [
741
+ "def optimize(python, model, language):\n",
742
+ " if model==\"GPT\":\n",
743
+ " result = stream_gpt(python, language)\n",
744
+ " elif model==\"Claude\":\n",
745
+ " result = stream_claude(python, language)\n",
746
+ " elif model==\"CodeQwen\":\n",
747
+ " result = stream_code_qwen(python, language)\n",
748
+ " else:\n",
749
+ " raise ValueError(\"Unknown model\")\n",
750
+ " for stream_so_far in result:\n",
751
+ " yield stream_so_far "
752
+ ]
753
+ },
754
+ {
755
+ "cell_type": "code",
756
+ "execution_count": 213,
757
+ "id": "4ba311ec-c16a-4fe0-946b-4b940704cf65",
758
+ "metadata": {},
759
+ "outputs": [],
760
+ "source": [
761
+ "def select_sample_program(sample_program):\n",
762
+ " if sample_program==\"pi\":\n",
763
+ " return pi\n",
764
+ " elif sample_program==\"python_hard\":\n",
765
+ " return python_hard\n",
766
+ " else:\n",
767
+ " return \"Type your Python program here\""
768
+ ]
769
+ },
770
+ {
771
+ "cell_type": "code",
772
+ "execution_count": 214,
773
+ "id": "06148e88-501b-4686-a41d-c3be528d8e6f",
774
+ "metadata": {},
775
+ "outputs": [],
776
+ "source": [
777
+ "def execute_cpp(code):\n",
778
+ " write_output(code, \"optimized.exe\")\n",
779
+ " try:\n",
780
+ " compile_cmd = [\"g++\", \"-Ofast\", \"-std=c++17\", \"-march=native\", \"-mtune=intel\", \"-o\", \"optimized\", \"optimized.cpp\"]\n",
781
+ " compile_result = subprocess.run(compile_cmd, check=True, text=True, capture_output=True)\n",
782
+ " run_cmd = [\"optimized.exe\"]\n",
783
+ " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
784
+ " return run_result.stdout\n",
785
+ " except subprocess.CalledProcessError as e:\n",
786
+ " return f\"An error occurred:\\n{e.stderr}\""
787
+ ]
788
+ },
789
+ {
790
+ "cell_type": "code",
791
+ "execution_count": 236,
792
+ "id": "a42e3871-f3a5-4f14-836c-1e8ecacb56b5",
793
+ "metadata": {},
794
+ "outputs": [],
795
+ "source": [
796
+ "def execute_java(code):\n",
797
+ " # Extract the class name from the Java code\n",
798
+ " match = re.search(r\"\\b(public\\s+)?class\\s+(\\w+)\", code)\n",
799
+ " class_name = match.group(2) if match else \"OptimizedJava\"\n",
800
+ "\n",
801
+ " file_name = f\"{class_name}.java\"\n",
802
+ " write_output(code, file_name)\n",
803
+ " try:\n",
804
+ " compile_cmd =[\"javac\", file_name]\n",
805
+ " subprocess.run(compile_cmd, check=True, text=True, capture_output=True)\n",
806
+ " run_cmd = [\"java\", class_name]\n",
807
+ " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
808
+ " return run_result.stdout\n",
809
+ " except subprocess.CalledProcessError as e:\n",
810
+ " return f\"Error during compilation or execution:\\n{e.stderr}\""
811
+ ]
812
+ },
813
+ {
814
+ "cell_type": "code",
815
+ "execution_count": 238,
816
+ "id": "f9ca2e6f-60c1-4e5f-b570-63c75b2d189b",
817
+ "metadata": {
818
+ "scrolled": true
819
+ },
820
+ "outputs": [
821
+ {
822
+ "data": {
823
+ "text/html": [
824
+ "<div><iframe src=\"http://127.0.0.1:7901/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
825
+ ],
826
+ "text/plain": [
827
+ "<IPython.core.display.HTML object>"
828
+ ]
829
+ },
830
+ "metadata": {},
831
+ "output_type": "display_data"
832
+ },
833
+ {
834
+ "data": {
835
+ "text/plain": []
836
+ },
837
+ "execution_count": 238,
838
+ "metadata": {},
839
+ "output_type": "execute_result"
840
+ }
841
+ ],
842
+ "source": [
843
+ "with gr.Blocks(css=css) as ui:\n",
844
+ " gr.Markdown(\"## Convert code from Python to C++ or Java\")\n",
845
+ " #input and output\n",
846
+ " with gr.Row():\n",
847
+ " python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
848
+ " converted_code = gr.Textbox(label=\"Converted code:\", lines=10)\n",
849
+ " # java = gr.Textbox(label=\"Java code:\", lines=10)\n",
850
+ " #sample programs\n",
851
+ " with gr.Row():\n",
852
+ " with gr.Column():\n",
853
+ " sample_program = gr.Radio([\"pi\", \"python_hard\"], label=\"Sample program\", value=\"python_hard\")\n",
854
+ " #select model and language\n",
855
+ " with gr.Row():\n",
856
+ " with gr.Column():\n",
857
+ " model = gr.Dropdown([\"GPT\", \"Claude\", \"CodeQwen\"], label=\"Select model\", value=\"GPT\")\n",
858
+ " language = gr.Dropdown([\"C++\",\"Java\"], label=\"Select language\", value=\"C++\")\n",
859
+ " with gr.Row():\n",
860
+ " convert = gr.Button(\"Convert\")\n",
861
+ " #Code execution\n",
862
+ " with gr.Row():\n",
863
+ " python_run = gr.Button(\"Run Python\")\n",
864
+ " converted_run = gr.Button(\"Run converted code\")\n",
865
+ " with gr.Row():\n",
866
+ " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
867
+ " output = gr.TextArea(label=\"Converted code result:\", elem_classes=[\"cpp\"])\n",
868
+ " \n",
869
+ " # Function to convert Python code based on language\n",
870
+ " def convert_code(python_code, model, selected_language):\n",
871
+ " if selected_language == \"C++\":\n",
872
+ " for chunk in optimize(python_code, model, \"cpp\"):\n",
873
+ " yield chunk # Stream each chunk\n",
874
+ " elif selected_language == \"Java\":\n",
875
+ " for chunk in optimize(python_code, model, \"java\"):\n",
876
+ " yield chunk\n",
877
+ " return \"\"\n",
878
+ "\n",
879
+ " # Function to execute converted code\n",
880
+ " def run_code(converted_code, selected_language):\n",
881
+ " if selected_language == \"C++\":\n",
882
+ " return execute_cpp(converted_code)\n",
883
+ " elif selected_language == \"Java\":\n",
884
+ " return execute_java(converted_code)\n",
885
+ " return \"Invalid language selection\"\n",
886
+ "\n",
887
+ " sample_program.change(select_sample_program, inputs=[sample_program], outputs=[python])\n",
888
+ " convert.click(convert_code, inputs=[python, model, language], outputs=[converted_code])\n",
889
+ " converted_run.click(run_code, inputs=[converted_code, language], outputs=[output]) \n",
890
+ " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
891
+ "\n",
892
+ "ui.launch(inbrowser=True)"
893
+ ]
894
+ },
895
+ {
896
+ "cell_type": "code",
897
+ "execution_count": null,
898
+ "id": "9d0ad093-425b-488e-8c3f-67f729dd9c06",
899
+ "metadata": {},
900
+ "outputs": [],
901
+ "source": []
902
+ }
903
+ ],
904
+ "metadata": {
905
+ "kernelspec": {
906
+ "display_name": "Python 3 (ipykernel)",
907
+ "language": "python",
908
+ "name": "python3"
909
+ },
910
+ "language_info": {
911
+ "codemirror_mode": {
912
+ "name": "ipython",
913
+ "version": 3
914
+ },
915
+ "file_extension": ".py",
916
+ "mimetype": "text/x-python",
917
+ "name": "python",
918
+ "nbconvert_exporter": "python",
919
+ "pygments_lexer": "ipython3",
920
+ "version": "3.11.11"
921
+ }
922
+ },
923
+ "nbformat": 4,
924
+ "nbformat_minor": 5
925
+ }
community-contributions/day5-homework.ipynb ADDED
@@ -0,0 +1,565 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "ff022957-2e81-4ea9-84d3-e52d5753e133",
6
+ "metadata": {},
7
+ "source": [
8
+ "### Comment and Unit Test Generater \n",
9
+ "\n",
10
+ "The requirement: \n",
11
+ "* use an LLM to generate docstring and comments for Python code\n",
12
+ "* use an LLM to generate unit test\n",
13
+ "\n",
14
+ "This is my week 4 day 5 project."
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": null,
20
+ "id": "ea1841f6-4afc-4d29-ace8-5ca5a3915c8c",
21
+ "metadata": {},
22
+ "outputs": [],
23
+ "source": [
24
+ "# imports\n",
25
+ "\n",
26
+ "import os\n",
27
+ "import io\n",
28
+ "import sys\n",
29
+ "import json\n",
30
+ "import requests\n",
31
+ "from dotenv import load_dotenv\n",
32
+ "from openai import OpenAI\n",
33
+ "import google.generativeai\n",
34
+ "import anthropic\n",
35
+ "from IPython.display import Markdown, display, update_display\n",
36
+ "import gradio as gr\n",
37
+ "import subprocess\n",
38
+ "from huggingface_hub import login, InferenceClient\n",
39
+ "from transformers import AutoTokenizer"
40
+ ]
41
+ },
42
+ {
43
+ "cell_type": "code",
44
+ "execution_count": null,
45
+ "id": "11957fd3-6c61-4496-aef1-8223cb9ec4ce",
46
+ "metadata": {},
47
+ "outputs": [],
48
+ "source": [
49
+ "# environment\n",
50
+ "\n",
51
+ "load_dotenv()\n",
52
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
53
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
54
+ "os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
55
+ ]
56
+ },
57
+ {
58
+ "cell_type": "code",
59
+ "execution_count": null,
60
+ "id": "ee7b08fd-e678-4234-895e-4e3a925e60f0",
61
+ "metadata": {},
62
+ "outputs": [],
63
+ "source": [
64
+ "# initialize\n",
65
+ "\n",
66
+ "openai = OpenAI()\n",
67
+ "claude = anthropic.Anthropic()\n",
68
+ "OPENAI_MODEL = \"gpt-4o\"\n",
69
+ "CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\""
70
+ ]
71
+ },
72
+ {
73
+ "cell_type": "code",
74
+ "execution_count": null,
75
+ "id": "c8023255-9c98-4fbc-92e4-c553bed3b605",
76
+ "metadata": {},
77
+ "outputs": [],
78
+ "source": [
79
+ "hf_token = os.environ['HF_TOKEN']\n",
80
+ "login(hf_token, add_to_git_credential=True)"
81
+ ]
82
+ },
83
+ {
84
+ "cell_type": "code",
85
+ "execution_count": null,
86
+ "id": "f8ce3f5e-74c4-4d35-bfbc-91c5be85e094",
87
+ "metadata": {},
88
+ "outputs": [],
89
+ "source": [
90
+ "code_qwen = \"Qwen/CodeQwen1.5-7B-Chat\"\n",
91
+ "CODE_QWEN_URL = \"https://g39mbjooiiwkbgyz.us-east-1.aws.endpoints.huggingface.cloud\""
92
+ ]
93
+ },
94
+ {
95
+ "cell_type": "code",
96
+ "execution_count": null,
97
+ "id": "1bbc66b6-52ae-465e-a368-edc8f097fe9d",
98
+ "metadata": {},
99
+ "outputs": [],
100
+ "source": [
101
+ "def system_prompt_for_comment():\n",
102
+ " system=\"\"\"\n",
103
+ " You are a Python documentation expert. When writing documentation:\n",
104
+ " - Follow PEP 257 and Google docstring style guidelines\n",
105
+ " - Write clear, concise explanations\n",
106
+ " - Include practical examples\n",
107
+ " - Highlight edge cases and limitations\n",
108
+ " - Use type hints in docstrings\n",
109
+ " - Add inline comments only for complex logic\n",
110
+ " - Never skip documenting parameters or return values\n",
111
+ " - Validate that all documentation is accurate and complete\n",
112
+ " \"\"\"\n",
113
+ " return system"
114
+ ]
115
+ },
116
+ {
117
+ "cell_type": "code",
118
+ "execution_count": null,
119
+ "id": "b089f87b-53ae-40ad-8d06-b9924bb998a0",
120
+ "metadata": {},
121
+ "outputs": [],
122
+ "source": [
123
+ "def system_prompt_for_unit_test():\n",
124
+ " system=\"\"\"\n",
125
+ " You are an expert Python testing engineer who specializes in creating comprehensive unit tests. Follow these principles:\n",
126
+ " - Use pytest as the testing framework\n",
127
+ " - Follow the Arrange-Act-Assert pattern\n",
128
+ " - Test both valid and invalid inputs\n",
129
+ " - Include edge cases and boundary conditions\n",
130
+ " - Write descriptive test names that explain the scenario being tested\n",
131
+ " - Create independent tests that don't rely on each other\n",
132
+ " - Use appropriate fixtures and parametrize when needed\n",
133
+ " - Add clear comments explaining complex test logic\n",
134
+ " - Cover error cases and exceptions\n",
135
+ " - Achieve high code coverage while maintaining meaningful tests\n",
136
+ " \"\"\"\n",
137
+ " return system"
138
+ ]
139
+ },
140
+ {
141
+ "cell_type": "code",
142
+ "execution_count": null,
143
+ "id": "22193622-f3a0-4894-a6c4-eb6d88097861",
144
+ "metadata": {},
145
+ "outputs": [],
146
+ "source": [
147
+ "def user_prompt_for_comment(code):\n",
148
+ " user = f\"\"\"\n",
149
+ " Please document this Python code with:\n",
150
+ " \n",
151
+ " 1. A docstring containing:\n",
152
+ " - A clear description of purpose and functionality\n",
153
+ " - All parameters with types and descriptions\n",
154
+ " - Return values with types\n",
155
+ " - Exceptions that may be raised\n",
156
+ " - Any important notes or limitations\n",
157
+ " \n",
158
+ " 2. Strategic inline comments for:\n",
159
+ " - Complex algorithms or business logic\n",
160
+ " - Non-obvious implementation choices\n",
161
+ " - Performance considerations\n",
162
+ " - Edge cases\n",
163
+ " \n",
164
+ " Here's the code to document:\n",
165
+ " \\n{code}\n",
166
+ " \"\"\"\n",
167
+ " return user;"
168
+ ]
169
+ },
170
+ {
171
+ "cell_type": "code",
172
+ "execution_count": null,
173
+ "id": "81e61752-ec2f-44c1-86a2-ff3234a0358c",
174
+ "metadata": {},
175
+ "outputs": [],
176
+ "source": [
177
+ "def user_prompt_for_unit_test(code):\n",
178
+ " user = f\"\"\"\n",
179
+ " Please generate unit tests for the following Python code. Include:\n",
180
+ " \n",
181
+ " 1. Test cases for:\n",
182
+ " - Normal/expected inputs\n",
183
+ " - Edge cases and boundary values\n",
184
+ " - Invalid inputs and error conditions\n",
185
+ " - Different combinations of parameters\n",
186
+ " - All public methods and functions\n",
187
+ " \n",
188
+ " 2. For each test:\n",
189
+ " - Clear test function names describing the scenario\n",
190
+ " - Setup code (fixtures if needed)\n",
191
+ " - Test data preparation\n",
192
+ " - Expected outcomes\n",
193
+ " - Assertions checking results\n",
194
+ " - Comments explaining complex test logic\n",
195
+ " \n",
196
+ " 3. Include any necessary:\n",
197
+ " - Imports\n",
198
+ " - Fixtures\n",
199
+ " - Mock objects\n",
200
+ " - Helper functions\n",
201
+ " - Test data generators\n",
202
+ " \n",
203
+ " Here's the code to test:\n",
204
+ " \\n{code}\n",
205
+ " \"\"\"\n",
206
+ " return user"
207
+ ]
208
+ },
209
+ {
210
+ "cell_type": "code",
211
+ "execution_count": null,
212
+ "id": "f31ceed3-0eb2-4962-ab86-2d0302185560",
213
+ "metadata": {},
214
+ "outputs": [],
215
+ "source": [
216
+ "pi = \"\"\"\n",
217
+ "import time\n",
218
+ "\n",
219
+ "def calculate(iterations, param1, param2):\n",
220
+ " result = 1.0\n",
221
+ " for i in range(1, iterations+1):\n",
222
+ " j = i * param1 - param2\n",
223
+ " result -= (1/j)\n",
224
+ " j = i * param1 + param2\n",
225
+ " result += (1/j)\n",
226
+ " return result\n",
227
+ "\n",
228
+ "start_time = time.time()\n",
229
+ "result = calculate(100_000_000, 4, 1) * 4\n",
230
+ "end_time = time.time()\n",
231
+ "\n",
232
+ "print(f\"Result: {result:.12f}\")\n",
233
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
234
+ "\"\"\""
235
+ ]
236
+ },
237
+ {
238
+ "cell_type": "code",
239
+ "execution_count": null,
240
+ "id": "192c30f5-4be6-49b7-a054-11bfcffa91e0",
241
+ "metadata": {},
242
+ "outputs": [],
243
+ "source": [
244
+ "exec(pi)"
245
+ ]
246
+ },
247
+ {
248
+ "cell_type": "code",
249
+ "execution_count": null,
250
+ "id": "d4e920dc-4094-42d8-9255-18f2919df2d4",
251
+ "metadata": {},
252
+ "outputs": [],
253
+ "source": [
254
+ "def messages_for_comment(python):\n",
255
+ " return [\n",
256
+ " {\"role\": \"system\", \"content\": system_prompt_for_comment()},\n",
257
+ " {\"role\": \"user\", \"content\": user_prompt_for_comment(python)}\n",
258
+ " ]"
259
+ ]
260
+ },
261
+ {
262
+ "cell_type": "code",
263
+ "execution_count": null,
264
+ "id": "77500cae-bf84-405c-8b03-2f984108951b",
265
+ "metadata": {},
266
+ "outputs": [],
267
+ "source": [
268
+ "def messages_for_unit_test(python):\n",
269
+ " return [\n",
270
+ " {\"role\": \"system\", \"content\": system_prompt_for_unit_test()},\n",
271
+ " {\"role\": \"user\", \"content\": user_prompt_for_unit_test(python)}\n",
272
+ " ]"
273
+ ]
274
+ },
275
+ {
276
+ "cell_type": "code",
277
+ "execution_count": null,
278
+ "id": "5ec58bf1-4a44-4c21-a71a-2cac359884e5",
279
+ "metadata": {},
280
+ "outputs": [],
281
+ "source": [
282
+ "def stream_comment_gpt(code):\n",
283
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for_comment(code), stream=True)\n",
284
+ " reply = \"\"\n",
285
+ " for chunk in stream:\n",
286
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
287
+ " reply += fragment\n",
288
+ " #print(fragment, end='', flush=True)\n",
289
+ " yield reply.replace('```','') \n",
290
+ " "
291
+ ]
292
+ },
293
+ {
294
+ "cell_type": "code",
295
+ "execution_count": null,
296
+ "id": "47c615e2-4eb6-4ce1-ad09-7f2e6dbc3934",
297
+ "metadata": {},
298
+ "outputs": [],
299
+ "source": [
300
+ "stream_comment_gpt(pi)"
301
+ ]
302
+ },
303
+ {
304
+ "cell_type": "code",
305
+ "execution_count": null,
306
+ "id": "0b990875-31fd-40e5-bc8c-f6099d362249",
307
+ "metadata": {},
308
+ "outputs": [],
309
+ "source": [
310
+ "def stream_unit_test_gpt(code):\n",
311
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for_unit_test(code), stream=True)\n",
312
+ " reply = \"\"\n",
313
+ " for chunk in stream:\n",
314
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
315
+ " reply += fragment\n",
316
+ " #print(fragment, end='', flush=True)\n",
317
+ " yield reply.replace('```','')"
318
+ ]
319
+ },
320
+ {
321
+ "cell_type": "code",
322
+ "execution_count": null,
323
+ "id": "3dc90578-4f5e-47f1-b30f-c21b5795e82f",
324
+ "metadata": {},
325
+ "outputs": [],
326
+ "source": [
327
+ "stream_unit_test_gpt(pi)"
328
+ ]
329
+ },
330
+ {
331
+ "cell_type": "code",
332
+ "execution_count": null,
333
+ "id": "17380c0f-b851-472b-a234-d86f5c219e50",
334
+ "metadata": {},
335
+ "outputs": [],
336
+ "source": [
337
+ "def stream_comment_claude(code):\n",
338
+ " result = claude.messages.stream(\n",
339
+ " model=CLAUDE_MODEL,\n",
340
+ " max_tokens=2000,\n",
341
+ " system=system_prompt_for_comment(),\n",
342
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for_comment(code)}],\n",
343
+ " )\n",
344
+ " reply = \"\"\n",
345
+ " with result as stream:\n",
346
+ " for text in stream.text_stream:\n",
347
+ " reply += text\n",
348
+ " #print(text, end=\"\", flush=True)\n",
349
+ " yield reply.replace('```','')"
350
+ ]
351
+ },
352
+ {
353
+ "cell_type": "code",
354
+ "execution_count": null,
355
+ "id": "0a2d016d-76a2-4752-bd4d-6f93ddec46be",
356
+ "metadata": {},
357
+ "outputs": [],
358
+ "source": [
359
+ "def stream_unit_test_claude(code):\n",
360
+ " result = claude.messages.stream(\n",
361
+ " model=CLAUDE_MODEL,\n",
362
+ " max_tokens=2000,\n",
363
+ " system=system_prompt_for_unit_test(),\n",
364
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for_unit_test(code)}],\n",
365
+ " )\n",
366
+ " reply = \"\"\n",
367
+ " with result as stream:\n",
368
+ " for text in stream.text_stream:\n",
369
+ " reply += text\n",
370
+ " #print(text, end=\"\", flush=True)\n",
371
+ " yield reply.replace('```','')"
372
+ ]
373
+ },
374
+ {
375
+ "cell_type": "code",
376
+ "execution_count": null,
377
+ "id": "ee43428e-b577-4e95-944d-399f2f3b89ff",
378
+ "metadata": {},
379
+ "outputs": [],
380
+ "source": [
381
+ "stream_comment_claude(pi)"
382
+ ]
383
+ },
384
+ {
385
+ "cell_type": "code",
386
+ "execution_count": null,
387
+ "id": "0565e33b-9f14-48b7-ae8d-d22dc03b93c9",
388
+ "metadata": {},
389
+ "outputs": [],
390
+ "source": [
391
+ "stream_unit_test_claude(pi)"
392
+ ]
393
+ },
394
+ {
395
+ "cell_type": "code",
396
+ "execution_count": null,
397
+ "id": "f13b3a5b-366d-4b28-adda-977a313e6b4d",
398
+ "metadata": {},
399
+ "outputs": [],
400
+ "source": [
401
+ "def stream_comment_model(model, model_url, code):\n",
402
+ " tokenizer = AutoTokenizer.from_pretrained(model)\n",
403
+ " messages = messages_for_comment(code)\n",
404
+ " text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
405
+ " client = InferenceClient(model_url, token=hf_token)\n",
406
+ " stream = client.text_generation(text, stream=True, details=True, max_new_tokens=5000)\n",
407
+ " result = \"\"\n",
408
+ " for r in stream:\n",
409
+ " #print(r.token.text, end = \"\")\n",
410
+ " result += r.token.text\n",
411
+ " yield result \n",
412
+ " "
413
+ ]
414
+ },
415
+ {
416
+ "cell_type": "code",
417
+ "execution_count": null,
418
+ "id": "e2efdb92-fc7a-4952-ab46-ae942cb996bf",
419
+ "metadata": {},
420
+ "outputs": [],
421
+ "source": [
422
+ "def stream_unit_test_model(model, model_url, code):\n",
423
+ " tokenizer = AutoTokenizer.from_pretrained(model)\n",
424
+ " messages = messages_for_unit_test(code)\n",
425
+ " text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
426
+ " client = InferenceClient(model_url, token=hf_token)\n",
427
+ " stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)\n",
428
+ " result = \"\"\n",
429
+ " for r in stream:\n",
430
+ " #print(r.token.text, end = \"\")\n",
431
+ " result += r.token.text\n",
432
+ " yield result \n",
433
+ " "
434
+ ]
435
+ },
436
+ {
437
+ "cell_type": "code",
438
+ "execution_count": null,
439
+ "id": "0a756193-fcba-43da-a981-203c10d36488",
440
+ "metadata": {},
441
+ "outputs": [],
442
+ "source": [
443
+ "stream_comment_model(code_qwen, CODE_QWEN_URL, pi)"
444
+ ]
445
+ },
446
+ {
447
+ "cell_type": "code",
448
+ "execution_count": null,
449
+ "id": "12ddcbf4-6286-47a8-847b-5be78e7aa995",
450
+ "metadata": {},
451
+ "outputs": [],
452
+ "source": [
453
+ "stream_unit_test_model(code_qwen, CODE_QWEN_URL, pi)"
454
+ ]
455
+ },
456
+ {
457
+ "cell_type": "code",
458
+ "execution_count": null,
459
+ "id": "321609ee-b64a-44fc-9090-39f87e1f8e0e",
460
+ "metadata": {},
461
+ "outputs": [],
462
+ "source": [
463
+ "def comment_code(python, model):\n",
464
+ " if model==\"GPT\":\n",
465
+ " result = stream_comment_gpt(python)\n",
466
+ " elif model==\"Claude\":\n",
467
+ " result = stream_comment_claude(python)\n",
468
+ " elif model==\"CodeQwen\":\n",
469
+ " result = stream_comment_model(code_qwen, CODE_QWEN_URL, python)\n",
470
+ " else:\n",
471
+ " raise ValueError(\"Unknown model\")\n",
472
+ " for stream_so_far in result:\n",
473
+ " yield stream_so_far "
474
+ ]
475
+ },
476
+ {
477
+ "cell_type": "code",
478
+ "execution_count": null,
479
+ "id": "d4c560c9-922d-4893-941f-42893373b1be",
480
+ "metadata": {},
481
+ "outputs": [],
482
+ "source": [
483
+ "def get_unit_test(python, model):\n",
484
+ " if model==\"GPT\":\n",
485
+ " result = stream_unit_test_gpt(python)\n",
486
+ " elif model==\"Claude\":\n",
487
+ " result = stream_unit_test_claude(python)\n",
488
+ " elif model==\"CodeQwen\":\n",
489
+ " result = stream_unit_test_model(code_qwen, CODE_QWEN_URL, python)\n",
490
+ " else:\n",
491
+ " raise ValueError(\"Unknown model\")\n",
492
+ " for stream_so_far in result:\n",
493
+ " yield stream_so_far "
494
+ ]
495
+ },
496
+ {
497
+ "cell_type": "code",
498
+ "execution_count": null,
499
+ "id": "f85bc777-bebe-436b-88cc-b9ecdb6306c0",
500
+ "metadata": {},
501
+ "outputs": [],
502
+ "source": [
503
+ "css = \"\"\"\n",
504
+ ".python {background-color: #306998;}\n",
505
+ ".cpp {background-color: #050;}\n",
506
+ "\"\"\""
507
+ ]
508
+ },
509
+ {
510
+ "cell_type": "code",
511
+ "execution_count": null,
512
+ "id": "ee27cc91-81e6-42c8-ae3c-c04161229d8c",
513
+ "metadata": {
514
+ "scrolled": true
515
+ },
516
+ "outputs": [],
517
+ "source": [
518
+ "with gr.Blocks(css=css) as ui:\n",
519
+ " gr.Markdown(\"## Convert code from Python to C++\")\n",
520
+ " with gr.Row():\n",
521
+ " python = gr.Textbox(label=\"Python code:\", value=pi, lines=10)\n",
522
+ " result = gr.Textbox(label=\"Result code:\", lines=10)\n",
523
+ " with gr.Row():\n",
524
+ " model = gr.Dropdown([\"GPT\", \"Claude\",\"CodeQwen\"], label=\"Select model\", value=\"GPT\")\n",
525
+ " with gr.Row():\n",
526
+ " comment_button = gr.Button(\"Comment code\")\n",
527
+ " with gr.Row():\n",
528
+ " unit_test_button = gr.Button(\"Unit Test code\")\n",
529
+ " \n",
530
+ " comment_button.click(comment_code, inputs=[python, model], outputs=[result])\n",
531
+ " unit_test_button.click(get_unit_test, inputs=[python, model], outputs=[result])\n",
532
+ "ui.launch(inbrowser=True)"
533
+ ]
534
+ },
535
+ {
536
+ "cell_type": "code",
537
+ "execution_count": null,
538
+ "id": "06e8279c-b488-4807-9bed-9d26be11c057",
539
+ "metadata": {},
540
+ "outputs": [],
541
+ "source": []
542
+ }
543
+ ],
544
+ "metadata": {
545
+ "kernelspec": {
546
+ "display_name": "Python 3 (ipykernel)",
547
+ "language": "python",
548
+ "name": "python3"
549
+ },
550
+ "language_info": {
551
+ "codemirror_mode": {
552
+ "name": "ipython",
553
+ "version": 3
554
+ },
555
+ "file_extension": ".py",
556
+ "mimetype": "text/x-python",
557
+ "name": "python",
558
+ "nbconvert_exporter": "python",
559
+ "pygments_lexer": "ipython3",
560
+ "version": "3.11.11"
561
+ }
562
+ },
563
+ "nbformat": 4,
564
+ "nbformat_minor": 5
565
+ }
community-contributions/doc_string_exercise/README.md ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Script Overview
2
+
3
+ The documentation will show you how to run the python script generate_doc_string.py. It is designed to take input
4
+ from an existing python file and create a new one with a suffix ('claude' or 'gpt'). If you do not specify and llm
5
+ model, it will default to claude.
6
+
7
+ # How to run
8
+
9
+ ```powershell
10
+ conda activate llms
11
+ cd <script_location>
12
+ python generate_doc_string -fp <full_file_path> -llm <name_of_model>
13
+ ```
14
+
15
+ # Show Help Instructions
16
+
17
+ ```shell
18
+ python generate_doc_string --help
19
+ ```
20
+
21
+ # Error Checking
22
+
23
+ 1) File Path Existence
24
+
25
+ If the file path doesn't exist, the script will stop running and print out an error.
26
+
27
+ 2) LLM Model Choice
28
+
29
+ If you choose something other than 'gpt' or 'claude', it will show and assertion error.
community-contributions/doc_string_exercise/data/original_file.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ def calculate(iterations, param1, param2):
3
+ result = 1.0
4
+ for i in range(1, iterations+1):
5
+ j = i * param1 - param2
6
+ result -= (1/j)
7
+ j = i * param1 + param2
8
+ result += (1/j)
9
+ return result
10
+
11
+
12
+ def calculate_2(iterations, param1, param2):
13
+ result = 1.0
14
+ for i in range(1, iterations+1):
15
+ j = i * param1 - param2
16
+ result -= (1/j)
17
+ j = i * param1 + param2
18
+ result += (1/j)
19
+ return result
community-contributions/doc_string_exercise/generate_doc_string.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from argparse import ArgumentParser
2
+ import os
3
+ from dotenv import load_dotenv
4
+ from openai import OpenAI
5
+ import anthropic
6
+ from utils import add_doc_string, Model, get_system_message
7
+ from pathlib import Path
8
+
9
+
10
+ def main():
11
+
12
+ # get run time arguments
13
+ parser = ArgumentParser(
14
+ prog='Generate Doc String for an existing functions',
15
+ description='Run Doc String for a given file and model',
16
+ )
17
+ parser.add_argument(
18
+ '-fp',
19
+ '--file_path',
20
+ help='Enter the file path to the script that will be updated with doc strings',
21
+ default=None
22
+ )
23
+ parser.add_argument(
24
+ '-llm',
25
+ '--llm_model',
26
+ help='Choose the LLM model that will create the doc strings',
27
+ default='claude'
28
+ )
29
+
30
+ # get run time arguments
31
+ args = parser.parse_args()
32
+ file_path = Path(args.file_path)
33
+ llm_model = args.llm_model
34
+
35
+ # check for file path
36
+ assert file_path.exists(), f"File Path {str(file_path.as_posix())} doesn't exist. Please try again."
37
+
38
+ # check for value llm values
39
+ assert llm_model in ['gpt', 'claude'], (f"Invalid model chosen '{llm_model}'. "
40
+ f"Please choose a valid model ('gpt' or 'claude')")
41
+
42
+ # load keys and environment variables
43
+ load_dotenv()
44
+ os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')
45
+ os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')
46
+ os.environ['HF_TOKEN'] = os.getenv('HF_INF_TOKEN', 'your-key-if-not-using-env')
47
+
48
+ # get system messages
49
+ system_message = get_system_message()
50
+
51
+ # get model info
52
+ model_info = {
53
+ 'gpt': {
54
+ 'client': OpenAI(),
55
+ 'model': Model.OPENAI_MODEL.value,
56
+ },
57
+ 'claude': {
58
+ 'client': anthropic.Anthropic(),
59
+ 'model': Model.CLAUDE_MODEL.value
60
+ }
61
+ }
62
+
63
+ # add standard argumens
64
+ model_info[llm_model].update(
65
+ {
66
+ 'file_path': file_path,
67
+ 'system_message': system_message
68
+ }
69
+ )
70
+
71
+ # convert python code to c++ code using open ai
72
+ print(f"\nSTARTED | Doc Strings Using {llm_model.upper()} for file {str(file_path)}\n\n")
73
+ add_doc_string(**model_info[llm_model])
74
+ print(f"\nFINISHED | Doc Strings Using {llm_model.upper()} for file {str(file_path)}\n\n")
75
+
76
+
77
+ if __name__ == '__main__':
78
+
79
+ main()
80
+
81
+
82
+
83
+
84
+
85
+
community-contributions/doc_string_exercise/utils.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from enum import Enum
2
+ from pathlib import Path
3
+
4
+
5
+ class Model(Enum):
6
+ """
7
+ Enumeration of supported AI models.
8
+ """
9
+ OPENAI_MODEL = "gpt-4o"
10
+ CLAUDE_MODEL = "claude-3-5-sonnet-20240620"
11
+
12
+
13
+ def get_system_message() -> str:
14
+ """
15
+ Generate a system message for AI assistants creating docstrings.
16
+
17
+ :return: A string containing instructions for the AI assistant.
18
+ :rtype: str
19
+ """
20
+ system_message = "You are an assistant that creates doc strings in reStructure Text format for an existing python function. "
21
+ system_message += "Respond only with an updated python function; use comments sparingly and do not provide any explanation other than occasional comments. "
22
+ system_message += "Be sure to include typing annotation for each function argument or key word argument and return object types."
23
+
24
+ return system_message
25
+
26
+
27
+ def user_prompt_for(python: str) -> str:
28
+ """
29
+ Generate a user prompt for rewriting Python functions with docstrings.
30
+
31
+ :param python: The Python code to be rewritten.
32
+ :type python: str
33
+ :return: A string containing the user prompt and the Python code.
34
+ :rtype: str
35
+ """
36
+ user_prompt = "Rewrite this Python function with doc strings in the reStructuredText style."
37
+ user_prompt += "Respond only with python code; do not explain your work other than a few comments. "
38
+ user_prompt += "Be sure to write a description of the function purpose with typing for each argument and return\n\n"
39
+ user_prompt += python
40
+ return user_prompt
41
+
42
+
43
+ def messages_for(python: str, system_message: str) -> list:
44
+ """
45
+ Create a list of messages for the AI model.
46
+
47
+ :param python: The Python code to be processed.
48
+ :type python: str
49
+ :param system_message: The system message for the AI assistant.
50
+ :type system_message: str
51
+ :return: A list of dictionaries containing role and content for each message.
52
+ :rtype: list
53
+ """
54
+ return [
55
+ {"role": "system", "content": system_message},
56
+ {"role": "user", "content": user_prompt_for(python)}
57
+ ]
58
+
59
+
60
+ def write_output(output: str, file_suffix: str, file_path: Path) -> None:
61
+ """
62
+ Write the processed output to a file.
63
+
64
+ :param output: The processed Python code with docstrings.
65
+ :type output: str
66
+ :param file_suffix: The suffix to be added to the output file name.
67
+ :type file_suffix: str
68
+ :param file_path: The path of the input file.
69
+ :type file_path: Path
70
+ :return: None
71
+ """
72
+ code = output.replace("", "").replace("", "")
73
+ out_file = file_path.with_name(f"{file_path.stem}{file_suffix if file_suffix else ''}.py")
74
+ out_file.write_text(code)
75
+
76
+
77
+ def add_doc_string(client: object, system_message: str, file_path: Path, model: str) -> None:
78
+ """
79
+ Add docstrings to a Python file using the specified AI model.
80
+
81
+ :param client: The AI client object.
82
+ :type client: object
83
+ :param system_message: The system message for the AI assistant.
84
+ :type system_message: str
85
+ :param file_path: The path of the input Python file.
86
+ :type file_path: Path
87
+ :param model: The AI model to be used.
88
+ :type model: str
89
+ :return: None
90
+ """
91
+ if 'gpt' in model:
92
+ add_doc_string_gpt(client=client, system_message=system_message, file_path=file_path, model=model)
93
+ else:
94
+ add_doc_string_claude(client=client, system_message=system_message, file_path=file_path, model=model)
95
+
96
+
97
+ def add_doc_string_gpt(client: object, system_message: str, file_path: Path, model: str = 'gpt-4o') -> None:
98
+ """
99
+ Add docstrings to a Python file using GPT model.
100
+
101
+ :param client: The OpenAI client object.
102
+ :type client: object
103
+ :param system_message: The system message for the AI assistant.
104
+ :type system_message: str
105
+ :param file_path: The path of the input Python file.
106
+ :type file_path: Path
107
+ :param model: The GPT model to be used, defaults to 'gpt-4o'.
108
+ :type model: str
109
+ :return: None
110
+ """
111
+ code_text = file_path.read_text(encoding='utf-8')
112
+ stream = client.chat.completions.create(model=model, messages=messages_for(code_text, system_message), stream=True)
113
+ reply = ""
114
+ for chunk in stream:
115
+ fragment = chunk.choices[0].delta.content or ""
116
+ reply += fragment
117
+ print(fragment, end='', flush=True)
118
+ write_output(reply, file_suffix='_gpt', file_path=file_path)
119
+
120
+
121
+ def add_doc_string_claude(client: object, system_message: str, file_path: Path, model: str = 'claude-3-5-sonnet-20240620') -> None:
122
+ """
123
+ Add docstrings to a Python file using Claude model.
124
+
125
+ :param client: The Anthropic client object.
126
+ :type client: object
127
+ :param system_message: The system message for the AI assistant.
128
+ :type system_message: str
129
+ :param file_path: The path of the input Python file.
130
+ :type file_path: Path
131
+ :param model: The Claude model to be used, defaults to 'claude-3-5-sonnet-20240620'.
132
+ :type model: str
133
+ :return: None
134
+ """
135
+ code_text = file_path.read_text(encoding='utf-8')
136
+ result = client.messages.stream(
137
+ model=model,
138
+ max_tokens=2000,
139
+ system=system_message,
140
+ messages=[{"role": "user", "content": user_prompt_for(code_text)}],
141
+ )
142
+ reply = ""
143
+ with result as stream:
144
+ for text in stream.text_stream:
145
+ reply += text
146
+ print(text, end="", flush=True)
147
+ write_output(reply, file_suffix='_claude', file_path=file_path)
community-contributions/ems_week4_docupy.ipynb ADDED
@@ -0,0 +1,869 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {
6
+ "id": "ykDDGx1cjYlh"
7
+ },
8
+ "source": [
9
+ "# **DocuPy** \n",
10
+ "### _\"Automate Documentation, Comments, and Unit Tests for Python Code\"_ \n",
11
+ "\n",
12
+ "## Overview \n",
13
+ "DocuPy is a Gradio-powered tool designed to automate essential but time-consuming Python development tasks. It streamlines documentation, unit testing, and Python-to-C++ code conversion with AI-driven assistance. \n",
14
+ "\n",
15
+ "### Key Features \n",
16
+ "✅ **Auto-Generate Docstrings & Comments** – Instantly improve code clarity and maintainability. \n",
17
+ "✅ **Unit Test Generation** – Ensure reliability with AI-generated test cases. \n",
18
+ "✅ **Python to C++ Conversion** – Seamlessly translate Python code to C++ with execution support. \n",
19
+ "\n",
20
+ "With an intuitive tab-based UI, DocuPy enhances productivity for developers of all levels. Whether you're documenting functions, validating code with tests, or exploring C++ conversions, this tool lets you focus on coding while it handles the rest. \n",
21
+ "\n",
22
+ "🔗 **Check out the repo**: [GitHub Repo](https://github.com/emads22/DocuPy) \n",
23
+ "\n",
24
+ "💡 **Have insights, feedback, or ideas?** Feel free to reach out. \n",
25
+ "\n",
26
+ "[<img src=\"https://img.shields.io/badge/GitHub-Emad-blue?logo=github\" width=\"150\">](https://github.com/emads22)\n"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "markdown",
31
+ "metadata": {},
32
+ "source": [
33
+ "If you're running this notebook on **`Google Colab`**, ensure you install the required libraries by running the following command:\n",
34
+ "\n",
35
+ "```bash\n",
36
+ "!pip install -q openai anthropic python-dotenv gradio huggingface_hub transformers\n",
37
+ "```\n",
38
+ "Otherwise, make sure to activate the Conda environment `docupy` that already includes these modules:\n",
39
+ "\n",
40
+ "```bash\n",
41
+ "conda activate docupy\n",
42
+ "```"
43
+ ]
44
+ },
45
+ {
46
+ "cell_type": "code",
47
+ "execution_count": null,
48
+ "metadata": {
49
+ "id": "6wIpBtNPjXc8"
50
+ },
51
+ "outputs": [],
52
+ "source": [
53
+ "# Uncomment the following command when running on Google Colab\n",
54
+ "# !pip install -q openai anthropic python-dotenv gradio huggingface_hub transformers "
55
+ ]
56
+ },
57
+ {
58
+ "cell_type": "markdown",
59
+ "metadata": {
60
+ "id": "T-cTBf9amBxf"
61
+ },
62
+ "source": [
63
+ "## Setup and Install Dependencies\n",
64
+ "\n",
65
+ "- Start by installing all necessary libraries."
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "execution_count": null,
71
+ "metadata": {
72
+ "id": "aIHWC7xpk87X"
73
+ },
74
+ "outputs": [],
75
+ "source": [
76
+ "# imports\n",
77
+ "import os\n",
78
+ "import io\n",
79
+ "import sys\n",
80
+ "import subprocess\n",
81
+ "import openai\n",
82
+ "import anthropic\n",
83
+ "import google.generativeai as google_genai\n",
84
+ "import gradio as gr\n",
85
+ "from openai import OpenAI\n",
86
+ "# from google.colab import userdata\n",
87
+ "from dotenv import load_dotenv\n",
88
+ "from pathlib import Path\n",
89
+ "from huggingface_hub import login, InferenceClient\n",
90
+ "from transformers import AutoTokenizer"
91
+ ]
92
+ },
93
+ {
94
+ "cell_type": "markdown",
95
+ "metadata": {
96
+ "id": "LZQbXR3dmZy4"
97
+ },
98
+ "source": [
99
+ "## Add Secrets to the Colab Notebook\n",
100
+ "\n",
101
+ "- Add the API keys for OpenAI, Claude, and Gemini to authenticate and access their respective models and services.\n"
102
+ ]
103
+ },
104
+ {
105
+ "cell_type": "code",
106
+ "execution_count": null,
107
+ "metadata": {
108
+ "id": "AadABekBm4fV"
109
+ },
110
+ "outputs": [],
111
+ "source": [
112
+ "# # Log in to Hugging Face using the token and add it to git credentials\n",
113
+ "# hf_token = userdata.get('HF_TOKEN')\n",
114
+ "# login(token=hf_token, add_to_git_credential=True)\n",
115
+ "\n",
116
+ "# # Endpoint URL for accessing the Code Qwen model through Hugging Face\n",
117
+ "# CODE_QWEN_URL = userdata.get('CODE_QWEN_URL')\n",
118
+ "\n",
119
+ "# # Initialize inference clients with every model using API keys\n",
120
+ "# gpt = openai.OpenAI(api_key=userdata.get('OPENAI_API_KEY'))\n",
121
+ "# claude = anthropic.Anthropic(api_key=userdata.get('ANTHROPIC_API_KEY'))\n",
122
+ "# google_genai.configure(api_key=userdata.get('GOOGLE_API_KEY'))\n",
123
+ "# code_qwen = InferenceClient(CODE_QWEN_URL, token=hf_token)"
124
+ ]
125
+ },
126
+ {
127
+ "cell_type": "markdown",
128
+ "metadata": {
129
+ "id": "Ej3JNfh_wc0m"
130
+ },
131
+ "source": [
132
+ "## Alternatively, if not running on Google Colab, Load Environment Variables for API Keys\n",
133
+ "\n",
134
+ "- Use the `load_dotenv()` function to securely load API keys from a `.env` file.\n",
135
+ "- Ensure that the `.env` file is located in the same directory as your script or Jupyter Notebook.\n",
136
+ "- The `.env` file should include the required API keys for OpenAI, Claude, and Gemini."
137
+ ]
138
+ },
139
+ {
140
+ "cell_type": "code",
141
+ "execution_count": null,
142
+ "metadata": {
143
+ "id": "av9X9XpQw0Vd"
144
+ },
145
+ "outputs": [],
146
+ "source": [
147
+ "load_dotenv()\n",
148
+ "\n",
149
+ "# Log in to Hugging Face using the token and add it to git credentials\n",
150
+ "hf_token = os.getenv('HF_TOKEN')\n",
151
+ "login(token=hf_token, add_to_git_credential=True)\n",
152
+ "\n",
153
+ "# Endpoint URL for accessing the Code Qwen model through Hugging Face\n",
154
+ "CODE_QWEN_URL = os.getenv('CODE_QWEN_URL')\n",
155
+ "\n",
156
+ "# Initialize inference clients with every model using API keys\n",
157
+ "gpt = openai.OpenAI(api_key=os.getenv('OPENAI_API_KEY'))\n",
158
+ "claude = anthropic.Anthropic(api_key=os.getenv('ANTHROPIC_API_KEY'))\n",
159
+ "google_genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))\n",
160
+ "code_qwen = InferenceClient(CODE_QWEN_URL, token=hf_token)"
161
+ ]
162
+ },
163
+ {
164
+ "cell_type": "markdown",
165
+ "metadata": {
166
+ "id": "lvEhCuQjrTYu"
167
+ },
168
+ "source": [
169
+ "## Define Required Constants\n",
170
+ "\n",
171
+ "- Initialize the essential constants required for the application's functionality.\n",
172
+ "- Configure the system and user prompts specific to each task or feature.\n"
173
+ ]
174
+ },
175
+ {
176
+ "cell_type": "code",
177
+ "execution_count": null,
178
+ "metadata": {
179
+ "id": "AKEBKKmAowt2"
180
+ },
181
+ "outputs": [],
182
+ "source": [
183
+ "# Models\n",
184
+ "OPENAI_MODEL = \"gpt-4o\"\n",
185
+ "CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\"\n",
186
+ "GEMINI_MODEL = \"gemini-1.5-pro\"\n",
187
+ "CODE_QWEN_MODEL = \"Qwen/CodeQwen1.5-7B-Chat\"\n",
188
+ "\n",
189
+ "MODELS_IN_USE = [\"GPT\", \"Claude\", \"Gemini\", \"CodeQwen\"]\n",
190
+ "\n",
191
+ "MAX_TOKENS = 2000\n",
192
+ "\n",
193
+ "ACTION_A = \"commenting\"\n",
194
+ "ACTION_B = \"testing\"\n",
195
+ "ACTION_C = \"converting\"\n",
196
+ "\n",
197
+ "# Define and create the path for the \"temp_files\" directory within the current script's directory\n",
198
+ "TEMP_DIR = Path.cwd() / \"temp_files\"\n",
199
+ "TEMP_DIR.mkdir(parents=True, exist_ok=True)\n",
200
+ "\n",
201
+ "PYTHON_SCRIPT_EASY = \"\"\"\n",
202
+ "import time\n",
203
+ "\n",
204
+ "def reverse_string(s):\n",
205
+ " return s[::-1]\n",
206
+ "\n",
207
+ "if __name__ == \"__main__\":\n",
208
+ " start_time = time.time()\n",
209
+ " text = \"Hello, World!\"\n",
210
+ " print(f\"- Original string: {text}\")\n",
211
+ " print(\"- Reversed string:\", reverse_string(text))\n",
212
+ " execution_time = time.time() - start_time \n",
213
+ " print(f\"\\\\n=> Execution Time: {execution_time:.6f} seconds\")\n",
214
+ "\"\"\"\n",
215
+ "\n",
216
+ "PYTHON_SCRIPT_INTERMEDIATE = \"\"\"\n",
217
+ "import time\n",
218
+ "\n",
219
+ "def is_palindrome(s):\n",
220
+ " s = s.lower().replace(\" \", \"\") \n",
221
+ " return s == s[::-1]\n",
222
+ "\n",
223
+ "if __name__ == \"__main__\":\n",
224
+ " start_time = time.time()\n",
225
+ " text = \"Racecar\"\n",
226
+ " if is_palindrome(text):\n",
227
+ " print(f\"- '{text}' is a palindrome!\")\n",
228
+ " else:\n",
229
+ " print(f\"- '{text}' is Not a palindrome.\")\n",
230
+ " execution_time = time.time() - start_time \n",
231
+ " print(f\"\\\\n=> Execution Time: {execution_time:.6f} seconds\")\n",
232
+ "\"\"\"\n",
233
+ "\n",
234
+ "PYTHON_SCRIPT_HARD = \"\"\"\n",
235
+ "import time\n",
236
+ "\n",
237
+ "def generate_primes(limit):\n",
238
+ " primes = []\n",
239
+ " for num in range(2, limit + 1):\n",
240
+ " if all(num % p != 0 for p in primes):\n",
241
+ " primes.append(num)\n",
242
+ " return primes\n",
243
+ "\n",
244
+ "if __name__ == \"__main__\":\n",
245
+ " start_time = time.time()\n",
246
+ " n = 20\n",
247
+ " print(f\"- Generating primes up to: {n}\")\n",
248
+ " print(\"- Prime numbers:\", generate_primes(n))\n",
249
+ " execution_time = time.time() - start_time \n",
250
+ " print(f\"\\\\n=> Execution Time: {execution_time:.6f} seconds\")\n",
251
+ "\"\"\"\n",
252
+ "\n",
253
+ "PYTHON_SCRIPTS = {\n",
254
+ " \"reverse_string\" : PYTHON_SCRIPT_EASY,\n",
255
+ " \"is_palindrome\" : PYTHON_SCRIPT_INTERMEDIATE,\n",
256
+ " \"generate_primes\" : PYTHON_SCRIPT_HARD,\n",
257
+ " \"custom\" : \"\"\"\n",
258
+ "# Write your custom Python script here\n",
259
+ "if __name__ == \"__main__\":\n",
260
+ " print(\"Hello, World!\")\n",
261
+ "\"\"\"\n",
262
+ "}\n",
263
+ "\n",
264
+ "# Relative system prompts\n",
265
+ "SYSTEM_PROMPT_COMMENTS = \"\"\"\n",
266
+ "You are an AI model specializing in enhancing Python code documentation.\n",
267
+ "Generate detailed and precise docstrings and inline comments for the provided Python code.\n",
268
+ "Ensure the docstrings clearly describe the purpose, parameters, and return values of each function.\n",
269
+ "Inline comments should explain complex or non-obvious code segments.\n",
270
+ "Do not include any introductions, explanations, conclusions, or additional context.\n",
271
+ "Return only the updated Python code enclosed within ```python ... ``` for proper formatting and syntax highlighting.\n",
272
+ "\"\"\"\n",
273
+ "\n",
274
+ "SYSTEM_PROMPT_TESTS = \"\"\"\n",
275
+ "You are an AI model specializing in generating comprehensive unit tests for Python code.\n",
276
+ "Create Python unit tests that thoroughly validate the functionality of the given code.\n",
277
+ "Use the `unittest` framework and ensure edge cases and error conditions are tested.\n",
278
+ "Do not include any comments, introductions, explanations, conclusions, or additional context.\n",
279
+ "Return only the unit test code enclosed within ```python ... ``` for proper formatting and syntax highlighting.\n",
280
+ "\"\"\"\n",
281
+ "\n",
282
+ "SYSTEM_PROMPT_CONVERT = \"\"\"\n",
283
+ "You are an AI model specializing in high-performance code translation.\n",
284
+ "Translate the given Python code into equivalent, optimized C++ code.\n",
285
+ "Focus on:\n",
286
+ "- Using efficient data structures and algorithms.\n",
287
+ "- Avoiding unnecessary memory allocations and computational overhead.\n",
288
+ "- Ensuring minimal risk of integer overflow by using appropriate data types.\n",
289
+ "- Leveraging the C++ Standard Library (e.g., `<vector>`, `<algorithm>`) for performance and readability.\n",
290
+ "Produce concise and efficient C++ code that matches the functionality of the original Python code.\n",
291
+ "Do not include any comments, introductions, explanations, conclusions, or additional context..\n",
292
+ "Return only the C++ code enclosed within ```cpp ... ``` for proper formatting and syntax highlighting.\n",
293
+ "\"\"\""
294
+ ]
295
+ },
296
+ {
297
+ "cell_type": "code",
298
+ "execution_count": null,
299
+ "metadata": {
300
+ "id": "JJ1zttf7ANqD"
301
+ },
302
+ "outputs": [],
303
+ "source": [
304
+ "# Relative user prompts\n",
305
+ "def user_prompt_comments(python_code):\n",
306
+ " user_prompt = f\"\"\"\n",
307
+ "Add detailed docstrings and inline comments to the following Python code:\n",
308
+ "\n",
309
+ "```python\n",
310
+ "{python_code}\n",
311
+ "```\n",
312
+ "\"\"\"\n",
313
+ " return user_prompt\n",
314
+ "\n",
315
+ "def user_prompt_tests(python_code):\n",
316
+ " user_prompt = f\"\"\"\n",
317
+ "Generate unit tests for the following Python code using the `unittest` framework:\n",
318
+ "\n",
319
+ "```python\n",
320
+ "{python_code}\n",
321
+ "```\n",
322
+ "\"\"\"\n",
323
+ " return user_prompt\n",
324
+ "\n",
325
+ "def user_prompt_convert(python_code):\n",
326
+ " user_prompt = f\"\"\"\n",
327
+ "Convert the following Python code into C++:\n",
328
+ "\n",
329
+ "```python\n",
330
+ "{python_code}\n",
331
+ "``` \n",
332
+ "\"\"\"\n",
333
+ " return user_prompt"
334
+ ]
335
+ },
336
+ {
337
+ "cell_type": "markdown",
338
+ "metadata": {
339
+ "id": "tqrOO_qsCRkd"
340
+ },
341
+ "source": [
342
+ "### Define the Tab Functions\n",
343
+ "\n",
344
+ "- Develop dedicated functions for each service: documenting Python code, generating unit tests, and converting Python to C++.\n",
345
+ "- Structure each function to handle user input, process it using the selected AI model, and display the generated output seamlessly.\n",
346
+ "- Ensure the functionality of each tab aligns with its specific purpose, providing an intuitive and efficient user experience.\n"
347
+ ]
348
+ },
349
+ {
350
+ "cell_type": "code",
351
+ "execution_count": null,
352
+ "metadata": {
353
+ "id": "HBsBrq3G94ul"
354
+ },
355
+ "outputs": [],
356
+ "source": [
357
+ "def stream_gpt(system_prompt, user_prompt):\n",
358
+ " stream = gpt.chat.completions.create(\n",
359
+ " model=OPENAI_MODEL,\n",
360
+ " messages=[\n",
361
+ " {\"role\": \"system\", \"content\": system_prompt},\n",
362
+ " {\"role\": \"user\", \"content\": user_prompt}\n",
363
+ " ],\n",
364
+ " stream=True)\n",
365
+ " reply = \"\"\n",
366
+ " for chunk in stream:\n",
367
+ " reply += chunk.choices[0].delta.content or \"\"\n",
368
+ " yield reply.replace(\"```python\\n\", \"\").replace(\"```cpp\\n\", \"\").replace(\"```\", \"\")\n",
369
+ "\n",
370
+ "def stream_claude(system_prompt, user_prompt):\n",
371
+ " response = claude.messages.stream(\n",
372
+ " model=CLAUDE_MODEL,\n",
373
+ " max_tokens=MAX_TOKENS,\n",
374
+ " system=system_prompt,\n",
375
+ " messages=[{\"role\": \"user\", \"content\": user_prompt}],\n",
376
+ " )\n",
377
+ " reply = \"\"\n",
378
+ " with response as stream:\n",
379
+ " for text in stream.text_stream:\n",
380
+ " reply += text\n",
381
+ " yield reply.replace(\"```python\\n\", \"\").replace(\"```cpp\\n\", \"\").replace(\"```\", \"\")\n",
382
+ "\n",
383
+ "def stream_gemini(system_prompt, user_prompt):\n",
384
+ " gemini = google_genai.GenerativeModel(\n",
385
+ " model_name=GEMINI_MODEL,\n",
386
+ " system_instruction=system_prompt\n",
387
+ " )\n",
388
+ " stream = gemini.generate_content(\n",
389
+ " contents=user_prompt,\n",
390
+ " stream=True\n",
391
+ " )\n",
392
+ " reply = \"\"\n",
393
+ " for chunk in stream:\n",
394
+ " reply += chunk.text or \"\"\n",
395
+ " yield reply.replace(\"```python\\n\", \"\").replace(\"```cpp\\n\", \"\").replace(\"```\", \"\")\n",
396
+ "\n",
397
+ "def stream_code_qwen(system_prompt, user_prompt):\n",
398
+ " tokenizer = AutoTokenizer.from_pretrained(CODE_QWEN_MODEL)\n",
399
+ " model_input = tokenizer.apply_chat_template(\n",
400
+ " conversation=[\n",
401
+ " {\"role\": \"system\", \"content\": system_prompt},\n",
402
+ " {\"role\": \"user\", \"content\": user_prompt}\n",
403
+ " ],\n",
404
+ " tokenize=False,\n",
405
+ " add_generation_prompt=True\n",
406
+ " )\n",
407
+ " stream = code_qwen.text_generation(\n",
408
+ " prompt=model_input,\n",
409
+ " stream=True,\n",
410
+ " details=True,\n",
411
+ " max_new_tokens=MAX_TOKENS\n",
412
+ " )\n",
413
+ " reply = \"\"\n",
414
+ " for chunk in stream:\n",
415
+ " reply += chunk.token.text or \"\"\n",
416
+ " yield reply.replace(\"```python\\n\", \"\").replace(\"```cpp\\n\", \"\").replace(\"```\", \"\")\n",
417
+ "\n",
418
+ "def set_prompts(user_input, action):\n",
419
+ " action = action.lower()\n",
420
+ "\n",
421
+ " if action == ACTION_A.lower():\n",
422
+ " system_prompt = SYSTEM_PROMPT_COMMENTS\n",
423
+ " user_prompt = user_prompt_comments(user_input)\n",
424
+ " elif action == ACTION_B.lower():\n",
425
+ " system_prompt = SYSTEM_PROMPT_TESTS\n",
426
+ " user_prompt = user_prompt_tests(user_input)\n",
427
+ " elif action == ACTION_C.lower():\n",
428
+ " system_prompt = SYSTEM_PROMPT_CONVERT\n",
429
+ " user_prompt = user_prompt_convert(user_input)\n",
430
+ " else:\n",
431
+ " return None, None\n",
432
+ " \n",
433
+ " return system_prompt, user_prompt\n",
434
+ "\n",
435
+ "def stream_response(user_input, model, action):\n",
436
+ " system_prompt, user_prompt = set_prompts(user_input, action)\n",
437
+ " if not all((system_prompt, user_prompt)):\n",
438
+ " raise ValueError(\"Unknown Action\")\n",
439
+ "\n",
440
+ " match model:\n",
441
+ " case \"GPT\":\n",
442
+ " yield from stream_gpt(system_prompt, user_prompt)\n",
443
+ "\n",
444
+ " case \"Claude\":\n",
445
+ " yield from stream_claude(system_prompt, user_prompt)\n",
446
+ "\n",
447
+ " case \"Gemini\":\n",
448
+ " yield from stream_gemini(system_prompt, user_prompt)\n",
449
+ "\n",
450
+ " case \"CodeQwen\":\n",
451
+ " yield from stream_code_qwen(system_prompt, user_prompt)\n",
452
+ " \n",
453
+ "def generate_comments(python_code, selected_model):\n",
454
+ " for model in MODELS_IN_USE:\n",
455
+ " if model == selected_model:\n",
456
+ " yield from stream_response(python_code, model, action=ACTION_A)\n",
457
+ " return # Exit the function immediately after exhausting the generator\n",
458
+ " raise ValueError(\"Unknown Model\")\n",
459
+ "\n",
460
+ "def generate_tests(python_code, selected_model):\n",
461
+ " for model in MODELS_IN_USE:\n",
462
+ " if model == selected_model:\n",
463
+ " yield from stream_response(python_code, model, action=ACTION_B)\n",
464
+ " return # Exit the function immediately after exhausting the generator\n",
465
+ " raise ValueError(\"Unknown Model\")\n",
466
+ "\n",
467
+ "def convert_code(python_code, selected_model):\n",
468
+ " for model in MODELS_IN_USE:\n",
469
+ " if model == selected_model:\n",
470
+ " yield from stream_response(python_code, model, action=ACTION_C)\n",
471
+ " return # Exit the function immediately after exhausting the generator\n",
472
+ " raise ValueError(\"Unknown Model\")"
473
+ ]
474
+ },
475
+ {
476
+ "cell_type": "markdown",
477
+ "metadata": {},
478
+ "source": [
479
+ "## Running Code Functions\n",
480
+ "\n",
481
+ "- Functions that dynamically execute Python or C++ code provided as a string and captures its output.\n",
482
+ "- This is useful for evaluating Python or C++ code snippets and returning their results programmatically.\n",
483
+ "\n",
484
+ "### IMPORTANT WARNING:\n",
485
+ "The functions that dynamically execute Python or C++ code provided as input.\n",
486
+ "While powerful, this is extremely dangerous if the input code is not trusted.\n",
487
+ "Any malicious code can be executed, including:\n",
488
+ " - Deleting files or directories\n",
489
+ " - Stealing sensitive data (e.g., accessing environment variables or credentials)\n",
490
+ " - Running arbitrary commands that compromise the system\n",
491
+ "\n",
492
+ "Sharing this notebook with this code snippet can allow attackers to exploit this functionality \n",
493
+ "by passing harmful code as input. \n",
494
+ "\n",
495
+ "If you share this notebook or use this function:\n",
496
+ " 1. Only accept input from trusted sources.\n",
497
+ " 2. Consider running the code in a sandboxed environment (e.g., virtual machine or container).\n",
498
+ " 3. Avoid using this function in publicly accessible applications or notebooks without strict validation."
499
+ ]
500
+ },
501
+ {
502
+ "cell_type": "code",
503
+ "execution_count": null,
504
+ "metadata": {},
505
+ "outputs": [],
506
+ "source": [
507
+ "def run_python_exec(code):\n",
508
+ " try:\n",
509
+ " # Capture stdout using StringIO\n",
510
+ " output = io.StringIO()\n",
511
+ "\n",
512
+ " # Redirect stdout to StringIO\n",
513
+ " sys.stdout = output\n",
514
+ "\n",
515
+ " # Execute the provided Python code\n",
516
+ " exec(code)\n",
517
+ " finally:\n",
518
+ " # Restore original stdout\n",
519
+ " sys.stdout = sys.__stdout__\n",
520
+ "\n",
521
+ " # Return the captured output\n",
522
+ " return output.getvalue()\n",
523
+ "\n",
524
+ "# Improved running python function\n",
525
+ "def run_python(code):\n",
526
+ " # Save the Python code to a file\n",
527
+ " with open(TEMP_DIR / \"python_code.py\", \"w\") as python_file:\n",
528
+ " python_file.write(code)\n",
529
+ "\n",
530
+ " try:\n",
531
+ " # Execute the Python code\n",
532
+ " result = subprocess.run(\n",
533
+ " [\"python\", str(TEMP_DIR / \"python_code.py\")],\n",
534
+ " check=True, text=True, capture_output=True\n",
535
+ " )\n",
536
+ "\n",
537
+ " # Return the program's output\n",
538
+ " return result.stdout\n",
539
+ "\n",
540
+ " except subprocess.CalledProcessError as e:\n",
541
+ " # Handle compilation or execution errors\n",
542
+ " return f\"An error occurred during execution:\\n{e.stderr}\"\n",
543
+ "\n",
544
+ " finally:\n",
545
+ " # Clean up: Delete the Python code file and executable\n",
546
+ " file_path = TEMP_DIR / \"python_code.py\"\n",
547
+ " if file_path.exists():\n",
548
+ " file_path.unlink()\n",
549
+ "\n",
550
+ "def run_cpp(code):\n",
551
+ " # Save the C++ code to a file\n",
552
+ " with open(TEMP_DIR / \"cpp_code.cpp\", \"w\") as cpp_file:\n",
553
+ " cpp_file.write(code)\n",
554
+ "\n",
555
+ " try:\n",
556
+ " # Compile the C++ code\n",
557
+ " subprocess.run(\n",
558
+ " [\"g++\", \"-o\", str(TEMP_DIR / \"cpp_code\"), str(TEMP_DIR / \"cpp_code.cpp\")],\n",
559
+ " check=True, text=True, capture_output=True\n",
560
+ " )\n",
561
+ "\n",
562
+ " # Execute the compiled program\n",
563
+ " result = subprocess.run(\n",
564
+ " [str(TEMP_DIR / \"cpp_code\")],\n",
565
+ " check=True, text=True, capture_output=True\n",
566
+ " )\n",
567
+ "\n",
568
+ " # Return the program's output\n",
569
+ " return result.stdout\n",
570
+ "\n",
571
+ " except subprocess.CalledProcessError as e:\n",
572
+ " # Handle compilation or execution errors\n",
573
+ " error_context = \"during compilation\" if \"cpp_code.cpp\" in e.stderr else \"during execution\"\n",
574
+ " return f\"An error occurred {error_context}:\\n{e.stderr}\"\n",
575
+ "\n",
576
+ " finally:\n",
577
+ " # Clean up: Delete the C++ source file and executable\n",
578
+ " for filename in [\"cpp_code.cpp\", \"cpp_code\", \"cpp_code.exe\"]:\n",
579
+ " file_path = TEMP_DIR / filename\n",
580
+ " if file_path.exists():\n",
581
+ " file_path.unlink()"
582
+ ]
583
+ },
584
+ {
585
+ "cell_type": "markdown",
586
+ "metadata": {
587
+ "id": "Vude1jzPrgT2"
588
+ },
589
+ "source": [
590
+ "## Develop a User-Friendly Interface with Gradio\n",
591
+ "\n",
592
+ "- Design a clean, intuitive, and user-centric interface using Gradio.\n",
593
+ "- Ensure responsiveness and accessibility to provide a seamless and efficient user experience.\n",
594
+ "- Focus on simplicity while maintaining functionality to cater to diverse user needs.\n",
595
+ "\n"
596
+ ]
597
+ },
598
+ {
599
+ "cell_type": "code",
600
+ "execution_count": null,
601
+ "metadata": {
602
+ "id": "Eh-sWFZVBb_y"
603
+ },
604
+ "outputs": [],
605
+ "source": [
606
+ "# CSS styles for customizing the appearance of the Gradio UI elements.\n",
607
+ "css = \"\"\"\n",
608
+ ".python { \n",
609
+ " background-color: #377ef0; \n",
610
+ " color: #ffffff; \n",
611
+ " padding: 0.5em; \n",
612
+ " border-radius: 5px; /* Slightly rounded corners */\n",
613
+ "}\n",
614
+ ".cpp { \n",
615
+ " background-color: #00549e; \n",
616
+ " color: #ffffff; \n",
617
+ " padding: 0.5em; \n",
618
+ " border-radius: 5px; \n",
619
+ "}\n",
620
+ ".model { \n",
621
+ " background-color: #17a2b8; /* Vibrant cyan color */\n",
622
+ " color: white; \n",
623
+ " font-size: 1.2em; \n",
624
+ " padding: 0.5em; \n",
625
+ " border: none; \n",
626
+ " border-radius: 5px; \n",
627
+ " cursor: pointer; \n",
628
+ "}\n",
629
+ ".button { \n",
630
+ " height: 4em; \n",
631
+ " font-size: 1.5em; \n",
632
+ " padding: 0.5em 1em; \n",
633
+ " background-color: #e67e22; /* Vibrant orange */\n",
634
+ " color: white; \n",
635
+ " border: none; \n",
636
+ " border-radius: 5px; \n",
637
+ " cursor: pointer; \n",
638
+ "}\n",
639
+ ".run-button { \n",
640
+ " height: 3em; \n",
641
+ " font-size: 1.5em; \n",
642
+ " padding: 0.5em 1em; \n",
643
+ " background-color: #16a085; /* Rich teal color */\n",
644
+ " color: white; \n",
645
+ " border: none; \n",
646
+ " border-radius: 5px; \n",
647
+ " cursor: pointer; \n",
648
+ "}\n",
649
+ ".button:hover, .run-button:hover {\n",
650
+ " background-color: #2c3e50; /* Dark navy for hover effect */\n",
651
+ " color: #fff; \n",
652
+ "}\n",
653
+ "\"\"\""
654
+ ]
655
+ },
656
+ {
657
+ "cell_type": "code",
658
+ "execution_count": null,
659
+ "metadata": {
660
+ "id": "M_v-j-B_sQHe"
661
+ },
662
+ "outputs": [],
663
+ "source": [
664
+ "# Tab to Document Code with Docstrings and Comments\n",
665
+ "def docs_comments_ui():\n",
666
+ " with gr.Tab(\"Docstrings & Comments\"):\n",
667
+ " gr.Markdown(\"\"\"\n",
668
+ " ## Document Code with Docstrings and Comments\n",
669
+ " This tab allows you to automatically generate docstrings and inline comments for your Python code.\n",
670
+ " - Paste your Python code into the **`Python Code`** textbox.\n",
671
+ " - Select your preferred model (GPT, Claude, Gemini, or CodeQwen) to process the code.\n",
672
+ " - Click the **`Add Docstrings & Comments`** button to generate well-documented Python code.\n",
673
+ " The generated code will appear in the **`Python Code with Docstrings and Comments`** textarea.\n",
674
+ " \"\"\")\n",
675
+ " with gr.Row():\n",
676
+ " python = gr.Textbox(label=\"Python Code:\", lines=20, value=PYTHON_SCRIPTS[\"custom\"], elem_classes=[\"python\"])\n",
677
+ " python_with_comments = gr.TextArea(label=\"Python Code with Docstrings and Comments:\", interactive=True, lines=20, elem_classes=[\"python\"])\n",
678
+ " with gr.Row():\n",
679
+ " python_script = gr.Dropdown(choices=list(PYTHON_SCRIPTS.keys()), label=\"Select a Python script\", value=\"custom\", elem_classes=[\"model\"])\n",
680
+ " comments_btn = gr.Button(\"Add Docstrings & Comments\", elem_classes=[\"button\"])\n",
681
+ " model = gr.Dropdown([\"GPT\", \"Claude\", \"Gemini\", \"CodeQwen\"], label=\"Select Model\", value=\"GPT\", elem_classes=[\"model\"])\n",
682
+ " \n",
683
+ " python_script.change(\n",
684
+ " fn=lambda script: PYTHON_SCRIPTS[script],\n",
685
+ " inputs=[python_script],\n",
686
+ " outputs=[python]\n",
687
+ " )\n",
688
+ " \n",
689
+ " comments_btn.click(\n",
690
+ " fn=lambda: \"\",\n",
691
+ " inputs=None,\n",
692
+ " outputs=[python_with_comments]\n",
693
+ " ).then(\n",
694
+ " fn=generate_comments,\n",
695
+ " inputs=[python, model],\n",
696
+ " outputs=[python_with_comments]\n",
697
+ " )\n",
698
+ "\n",
699
+ " return python_with_comments"
700
+ ]
701
+ },
702
+ {
703
+ "cell_type": "code",
704
+ "execution_count": null,
705
+ "metadata": {
706
+ "id": "WDjJp1eXtQzY"
707
+ },
708
+ "outputs": [],
709
+ "source": [
710
+ "# Tab to Generate Comprehensive Unit Tests\n",
711
+ "def unit_tests_ui():\n",
712
+ " with gr.Tab(\"Unit Tests\"):\n",
713
+ " gr.Markdown(\"\"\"\n",
714
+ " ## Generate Comprehensive Unit Tests\n",
715
+ " This tab helps you create unit tests for your Python code automatically.\n",
716
+ " - Paste your Python code into the **`Python Code`** textbox.\n",
717
+ " - Choose a model (GPT, Claude, Gemini, or CodeQwen) to generate the unit tests.\n",
718
+ " - Click the **`Generate Unit Tests`** button, and the generated unit tests will appear in the **`Python Code with Unit Tests`** textarea.\n",
719
+ " Use these unit tests to ensure your code behaves as expected.\n",
720
+ " \"\"\")\n",
721
+ " with gr.Row():\n",
722
+ " python = gr.Textbox(label=\"Python Code:\", lines=20, value=PYTHON_SCRIPTS[\"custom\"], elem_classes=[\"python\"])\n",
723
+ " python_unit_tests = gr.TextArea(label=\"Python Code with Unit Tests:\", interactive=True, lines=20, elem_classes=[\"python\"])\n",
724
+ " with gr.Row():\n",
725
+ " python_script = gr.Dropdown(choices=list(PYTHON_SCRIPTS.keys()), label=\"Select a Python script\", value=\"custom\", elem_classes=[\"model\"])\n",
726
+ " unit_tests_btn = gr.Button(\"Generate Unit Tests\", elem_classes=[\"button\"])\n",
727
+ " model = gr.Dropdown([\"GPT\", \"Claude\", \"Gemini\", \"CodeQwen\"], label=\"Select Model\", value=\"GPT\", elem_classes=[\"model\"])\n",
728
+ " \n",
729
+ " python_script.change(\n",
730
+ " fn=lambda script: PYTHON_SCRIPTS[script],\n",
731
+ " inputs=[python_script],\n",
732
+ " outputs=[python]\n",
733
+ " )\n",
734
+ " \n",
735
+ " unit_tests_btn.click(\n",
736
+ " fn=lambda: \"\",\n",
737
+ " inputs=None,\n",
738
+ " outputs=[python_unit_tests]\n",
739
+ " ).then(\n",
740
+ " fn=generate_tests,\n",
741
+ " inputs=[python, model],\n",
742
+ " outputs=[python_unit_tests]\n",
743
+ " )\n",
744
+ "\n",
745
+ " return python_unit_tests"
746
+ ]
747
+ },
748
+ {
749
+ "cell_type": "code",
750
+ "execution_count": null,
751
+ "metadata": {
752
+ "id": "x57SZeLi9NyV"
753
+ },
754
+ "outputs": [],
755
+ "source": [
756
+ "# Tab to Convert Python Code to C++\n",
757
+ "def python_to_cpp_ui():\n",
758
+ " with gr.Tab(\"Python to C++\"):\n",
759
+ " gr.Markdown(\"\"\"\n",
760
+ " ## Convert Python Code to C++\n",
761
+ " This tab facilitates the conversion of Python code into C++.\n",
762
+ " - Paste your Python code into the **`Python Code`** textbox.\n",
763
+ " - Select your preferred model (GPT, Claude, Gemini, or CodeQwen) to perform the conversion.\n",
764
+ " - Click **`Convert to C++`** to see the equivalent C++ code in the **`C++ Code`** textbox.\n",
765
+ " Additional Features:\n",
766
+ " - You can execute the Python or C++ code directly using the respective **`Run Python`** or **`Run C++`** buttons.\n",
767
+ " - The output will appear in the respective result text areas below.\n",
768
+ " \"\"\")\n",
769
+ " with gr.Row():\n",
770
+ " python = gr.Textbox(label=\"Python Code:\", lines=20, value=PYTHON_SCRIPTS[\"custom\"], elem_classes=[\"python\"])\n",
771
+ " cpp = gr.Textbox(label=\"C++ Code:\", interactive=True, lines=20, elem_classes=[\"cpp\"])\n",
772
+ " with gr.Row():\n",
773
+ " python_script = gr.Dropdown(choices=list(PYTHON_SCRIPTS.keys()), label=\"Select a Python script\", value=\"custom\", elem_classes=[\"model\"])\n",
774
+ " convert_btn = gr.Button(\"Convert to C++\", elem_classes=[\"button\"])\n",
775
+ " model = gr.Dropdown([\"GPT\", \"Claude\", \"Gemini\", \"CodeQwen\"], label=\"Select Model\", value=\"GPT\", elem_classes=[\"model\"])\n",
776
+ " with gr.Row():\n",
777
+ " run_python_btn = gr.Button(\"Run Python\", elem_classes=[\"run-button\"])\n",
778
+ " run_cpp_btn = gr.Button(\"Run C++\", elem_classes=[\"run-button\"])\n",
779
+ " with gr.Row():\n",
780
+ " python_out = gr.TextArea(label=\"Python Result:\", lines=10, elem_classes=[\"python\"])\n",
781
+ " cpp_out = gr.TextArea(label=\"C++ Result:\", lines=10, elem_classes=[\"cpp\"])\n",
782
+ "\n",
783
+ " python_script.change(\n",
784
+ " fn=lambda script: PYTHON_SCRIPTS[script],\n",
785
+ " inputs=[python_script],\n",
786
+ " outputs=[python]\n",
787
+ " )\n",
788
+ " \n",
789
+ " convert_btn.click(\n",
790
+ " fn=lambda: \"\",\n",
791
+ " inputs=None,\n",
792
+ " outputs=[cpp]\n",
793
+ " ).then(\n",
794
+ " fn=convert_code,\n",
795
+ " inputs=[python, model],\n",
796
+ " outputs=[cpp]\n",
797
+ " )\n",
798
+ " run_python_btn.click(run_python, inputs=[python], outputs=[python_out])\n",
799
+ " run_cpp_btn.click(run_cpp, inputs=[cpp], outputs=[cpp_out])\n",
800
+ "\n",
801
+ " return cpp, python_out, cpp_out"
802
+ ]
803
+ },
804
+ {
805
+ "cell_type": "code",
806
+ "execution_count": null,
807
+ "metadata": {
808
+ "colab": {
809
+ "base_uri": "https://localhost:8080/",
810
+ "height": 645
811
+ },
812
+ "id": "n8ZdDrOrrbl-",
813
+ "outputId": "08350d69-569e-4947-8da1-d755e9a2678f"
814
+ },
815
+ "outputs": [],
816
+ "source": [
817
+ "# Combine the tabs into the main UI and handle tab switching\n",
818
+ "with gr.Blocks(css=css) as main_ui:\n",
819
+ " with gr.Tabs() as tabs:\n",
820
+ " comments_output = docs_comments_ui()\n",
821
+ " tests_output = unit_tests_ui()\n",
822
+ " cpp_output, python_out, cpp_out = python_to_cpp_ui()\n",
823
+ "\n",
824
+ " # Reset outputs on tab switch\n",
825
+ " tabs.select(\n",
826
+ " fn=lambda: [\"\", \"\", \"\", \"\", \"\"],\n",
827
+ " inputs=None,\n",
828
+ " outputs=[comments_output, \n",
829
+ " tests_output, \n",
830
+ " cpp_output, python_out, cpp_out]\n",
831
+ " )\n",
832
+ "\n",
833
+ "# Launch the app\n",
834
+ "main_ui.launch(inbrowser=True)"
835
+ ]
836
+ },
837
+ {
838
+ "cell_type": "code",
839
+ "execution_count": null,
840
+ "metadata": {},
841
+ "outputs": [],
842
+ "source": []
843
+ }
844
+ ],
845
+ "metadata": {
846
+ "colab": {
847
+ "provenance": []
848
+ },
849
+ "kernelspec": {
850
+ "display_name": "Python 3 (ipykernel)",
851
+ "language": "python",
852
+ "name": "python3"
853
+ },
854
+ "language_info": {
855
+ "codemirror_mode": {
856
+ "name": "ipython",
857
+ "version": 3
858
+ },
859
+ "file_extension": ".py",
860
+ "mimetype": "text/x-python",
861
+ "name": "python",
862
+ "nbconvert_exporter": "python",
863
+ "pygments_lexer": "ipython3",
864
+ "version": "3.11.11"
865
+ }
866
+ },
867
+ "nbformat": 4,
868
+ "nbformat_minor": 4
869
+ }
community-contributions/ems_week4_trading.ipynb ADDED
@@ -0,0 +1,528 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Trading Decision Simulator\n",
9
+ "\n",
10
+ "## Description\n",
11
+ "This document provides Python functions to simulate trading decisions using a predefined API. The API includes stock tickers, historical prices, and a `Trade` class to represent buy or sell actions. Each function demonstrates a unique trading strategy, such as momentum-based trading, mean reversion, portfolio diversification, and more. These examples can serve as a foundation for developing or testing algorithmic trading systems.\n"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "execution_count": null,
17
+ "id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
18
+ "metadata": {},
19
+ "outputs": [],
20
+ "source": [
21
+ "import os\n",
22
+ "from dotenv import load_dotenv\n",
23
+ "from openai import OpenAI\n",
24
+ "from huggingface_hub import login, InferenceClient\n",
25
+ "from transformers import AutoTokenizer\n",
26
+ "import google.generativeai as google_genai\n",
27
+ "import anthropic\n",
28
+ "import gradio as gr"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "code",
33
+ "execution_count": null,
34
+ "id": "4f672e1c-87e9-4865-b760-370fa605e614",
35
+ "metadata": {},
36
+ "outputs": [],
37
+ "source": [
38
+ "# Setting up environment\n",
39
+ "load_dotenv()\n",
40
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
41
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
42
+ "os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY', 'your-key-if-not-using-env')\n",
43
+ "os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')\n",
44
+ "os.environ['CODE_QWEN_URL'] = os.getenv('CODE_QWEN_URL', 'your-url-if-not-using-env')"
45
+ ]
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": null,
50
+ "id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
51
+ "metadata": {},
52
+ "outputs": [],
53
+ "source": [
54
+ "# Initialize\n",
55
+ "openai = OpenAI()\n",
56
+ "claude = anthropic.Anthropic()\n",
57
+ "google_genai.configure()\n",
58
+ "code_qwen = InferenceClient(CODE_QWEN_URL)"
59
+ ]
60
+ },
61
+ {
62
+ "cell_type": "code",
63
+ "execution_count": null,
64
+ "id": "cbb4319c-870f-4c04-99e2-6f54c650537a",
65
+ "metadata": {},
66
+ "outputs": [],
67
+ "source": [
68
+ "# Constants \n",
69
+ "MODELS = {\n",
70
+ " \"GPT\": \"gpt-4o\", \n",
71
+ " \"Claude\": \"claude-3-5-sonnet-20240620\", \n",
72
+ " \"Gemini\": \"gemini-1.5-pro\", \n",
73
+ " \"CodeQwen\": \"Qwen/CodeQwen1.5-7B-Chat\"\n",
74
+ "}\n",
75
+ "\n",
76
+ "MAX_TOKENS = 2000\n",
77
+ "\n",
78
+ "SYSTEM_PROMPT = \"\"\"\n",
79
+ "You are an advanced code generation assistant capable of creating high-quality Python code for financial trading systems. \n",
80
+ "Your task is to generate Python functions that simulate trading decisions based on the following API:\n",
81
+ "\n",
82
+ "API DETAILS:\n",
83
+ "1. tickers: A list of stock tickers (strings) representing available stocks.\n",
84
+ "2. prices: A dictionary where the key is a stock ticker (string) and the value is a list of historical prices (floats). The list is ordered with the most recent price first.\n",
85
+ "3. Trade: A class used to represent trading actions.\n",
86
+ " - `Trade(ticker, quantity)` creates a trade object:\n",
87
+ " - Positive `quantity` (e.g., `100`) represents buying shares.\n",
88
+ " - Negative `quantity` (e.g., `-50`) represents selling/shorting shares.\n",
89
+ "\n",
90
+ "INSTRUCTIONS:\n",
91
+ "- You will be provided with an example Python function to demonstrate the API.\n",
92
+ "- Your job is to generate 5 additional Python functions, each implementing a unique trading strategy.\n",
93
+ "- Ensure the functions are named sequentially (e.g., `trade2()`, `trade3()`, etc.).\n",
94
+ "- Include clear comments explaining the logic behind each function.\n",
95
+ "- Return a list of `Trade` objects from each function.\n",
96
+ "- The output should only include the Python code. Do not include any introductions, conclusions, summaries, or additional context.\n",
97
+ "\n",
98
+ "CONSIDERATIONS FOR TRADING STRATEGIES:\n",
99
+ "- Momentum-based strategies (e.g., trading based on price trends).\n",
100
+ "- Mean reversion strategies (e.g., identifying overbought or oversold stocks).\n",
101
+ "- Randomized strategies (e.g., simulating stochastic decision-making).\n",
102
+ "- Portfolio diversification (e.g., distributing trades across multiple tickers).\n",
103
+ "- Risk management strategies (e.g., limiting losses or locking in profits).\n",
104
+ "\n",
105
+ "EXAMPLE FUNCTION:\n",
106
+ "\"\"\""
107
+ ]
108
+ },
109
+ {
110
+ "cell_type": "code",
111
+ "execution_count": null,
112
+ "id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
113
+ "metadata": {},
114
+ "outputs": [],
115
+ "source": [
116
+ "def user_prompt(example_function):\n",
117
+ " \"\"\"\n",
118
+ " Returns a user prompt for the model by appending the provided example function.\n",
119
+ " \"\"\"\n",
120
+ " return f\"\"\"\n",
121
+ "{example_function}\n",
122
+ "\n",
123
+ "TASK:\n",
124
+ "Based on the provided example function and API, please write 5 additional trading functions named `trade2()`, `trade3()`, and so on. Each function should implement a unique trading strategy as outlined in the system prompt. Make sure each function has clear comments explaining the logic and returns a list of `Trade` objects.\n",
125
+ "\"\"\""
126
+ ]
127
+ },
128
+ {
129
+ "cell_type": "markdown",
130
+ "id": "455728fd-be9b-4d7a-88f2-3afcf026303e",
131
+ "metadata": {},
132
+ "source": [
133
+ "# Trade Function Example: `trade1`\n",
134
+ "\n",
135
+ "This Python script demonstrates a simple trading strategy implemented using a provided API. The `trade1` function identifies the top-performing stock over the last 5 days based on its average price and creates a trade object to buy 100 shares of the selected stock. The function leverages the following components:\n",
136
+ "\n",
137
+ "- **`tickers`**: A list of available stock tickers.\n",
138
+ "- **`prices`**: A dictionary containing historical prices for each stock.\n",
139
+ "- **`Trade`**: A class used to represent trading actions (buy or sell).\n",
140
+ "- **`numpy`**: Used to calculate average prices efficiently.\n",
141
+ "\n",
142
+ "The example highlights a momentum-based strategy where the stock with the best recent performance is selected for trading.\n",
143
+ "\n",
144
+ "example:\n",
145
+ "```python\n",
146
+ "# Importing the required modules and classes for the trading simulation\n",
147
+ "\n",
148
+ "# `tickers` is a list of stock tickers (strings), representing available stocks to trade.\n",
149
+ "import tickers\n",
150
+ "\n",
151
+ "# `prices` is a dictionary where:\n",
152
+ "# - The key is a stock ticker (string).\n",
153
+ "# - The value is a list of historical prices (floats), ordered with the most recent price first.\n",
154
+ "import prices\n",
155
+ "\n",
156
+ "# `Trade` is a class that represents a trading decision. It takes two arguments:\n",
157
+ "# - `ticker`: A string representing the stock ticker.\n",
158
+ "# - `quantity`: An integer representing the number of shares to buy (positive) or sell/short (negative).\n",
159
+ "# Example usage:\n",
160
+ "# Trade(\"IBM\", 100) -> Buys 100 shares of IBM stock.\n",
161
+ "# Trade(\"IBM\", -50) -> Sells or shorts 50 shares of IBM stock.\n",
162
+ "import Trade\n",
163
+ "\n",
164
+ "# Additional modules for random number generation and numerical operations\n",
165
+ "import random\n",
166
+ "import numpy as np\n",
167
+ "\n",
168
+ "def trade1():\n",
169
+ " \"\"\"\n",
170
+ " Buys the top-performing stock based on its average price over the last 5 days.\n",
171
+ "\n",
172
+ " Strategy:\n",
173
+ " - Calculate the average price of the last 5 days for each stock in `tickers`.\n",
174
+ " - Identify the stock with the highest average price.\n",
175
+ " - Create a trade object to buy 100 shares of the identified stock.\n",
176
+ " \n",
177
+ " Returns:\n",
178
+ " list[Trade]: A list containing a single trade object for the chosen stock.\n",
179
+ " \"\"\"\n",
180
+ " # Calculate the 5-day average price for each stock\n",
181
+ " avg_prices = {ticker: np.mean(prices[ticker][:5]) for ticker in tickers}\n",
182
+ "\n",
183
+ " # Find the stock ticker with the highest 5-day average price\n",
184
+ " best_ticker = max(avg_prices, key=avg_prices.get)\n",
185
+ "\n",
186
+ " # Create a trade object to buy 100 shares of the top-performing stock\n",
187
+ " trade = Trade(best_ticker, 100)\n",
188
+ "\n",
189
+ " # Return the trade as a list\n",
190
+ " return [trade]\n",
191
+ "\n",
192
+ "```"
193
+ ]
194
+ },
195
+ {
196
+ "cell_type": "code",
197
+ "execution_count": null,
198
+ "id": "ce0fa282-4e07-4bf8-8b49-7cf7ef4a7572",
199
+ "metadata": {},
200
+ "outputs": [],
201
+ "source": [
202
+ "# A trading function example\n",
203
+ "TRADING_FUNCTION_EXAMPLE = \"\"\"\n",
204
+ "# tickers is a list of stock tickers (strings)\n",
205
+ "import tickers\n",
206
+ "\n",
207
+ "# prices is a dict; the key is a ticker and the value is a list of historic prices, today first\n",
208
+ "import prices\n",
209
+ "\n",
210
+ "# Trade represents a decision to buy or sell a quantity of a ticker\n",
211
+ "# Trade(\"IBM\", 100) for a trade object representing purchasing 100 shares of IBM stock\n",
212
+ "# Trade(\"IBM\", -50) for a trade object representing selling or shorting 50 shares of IBM stock\n",
213
+ "import Trade\n",
214
+ "\n",
215
+ "import random\n",
216
+ "import numpy as np\n",
217
+ "\n",
218
+ "def trade1():\n",
219
+ " # Buy top performing stock in the last 5 days\n",
220
+ " avg_prices = {ticker: np.mean(prices[ticker][:5]) for ticker in tickers}\n",
221
+ " best_ticker = max(avg_prices, key=avg_prices.get)\n",
222
+ " trade = Trade(best_ticker, 100)\n",
223
+ " return [trade]\n",
224
+ "\"\"\""
225
+ ]
226
+ },
227
+ {
228
+ "cell_type": "code",
229
+ "execution_count": null,
230
+ "id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
231
+ "metadata": {},
232
+ "outputs": [],
233
+ "source": [
234
+ "# UI function to trade using GPT\n",
235
+ "def trade_gpt(function_example): \n",
236
+ " stream = openai.chat.completions.create(\n",
237
+ " model=MODELS[\"GPT\"], \n",
238
+ " messages=[\n",
239
+ " {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n",
240
+ " {\"role\": \"user\", \"content\": user_prompt(function_example)}\n",
241
+ " ], \n",
242
+ " stream=True\n",
243
+ " )\n",
244
+ " reply = \"\"\n",
245
+ " for chunk in stream:\n",
246
+ " reply += chunk.choices[0].delta.content or \"\"\n",
247
+ " yield reply.replace(\"```python\\n\", \"\").replace(\"```\", \"\")"
248
+ ]
249
+ },
250
+ {
251
+ "cell_type": "code",
252
+ "execution_count": null,
253
+ "id": "8669f56b-8314-4582-a167-78842caea131",
254
+ "metadata": {},
255
+ "outputs": [],
256
+ "source": [
257
+ "# UI function to trade using Claude\n",
258
+ "def trade_claude(function_example):\n",
259
+ " result = claude.messages.stream(\n",
260
+ " model=MODELS[\"Claude\"],\n",
261
+ " max_tokens=MAX_TOKENS,\n",
262
+ " system=SYSTEM_PROMPT,\n",
263
+ " messages=[{\"role\": \"user\", \"content\": user_prompt(function_example)}],\n",
264
+ " )\n",
265
+ " reply = \"\"\n",
266
+ " with result as stream:\n",
267
+ " for text in stream.text_stream:\n",
268
+ " reply += text\n",
269
+ " yield reply.replace(\"```python\\n\", \"\").replace(\"```\", \"\")"
270
+ ]
271
+ },
272
+ {
273
+ "cell_type": "code",
274
+ "execution_count": null,
275
+ "id": "d27456d0-5cd3-4c2c-a12a-176d53142752",
276
+ "metadata": {},
277
+ "outputs": [],
278
+ "source": [
279
+ "# UI function to trade using Gemini\n",
280
+ "def trade_gemini(function_example):\n",
281
+ " gemini = google_genai.GenerativeModel(\n",
282
+ " model_name=MODELS[\"Gemini\"],\n",
283
+ " system_instruction=SYSTEM_PROMPT\n",
284
+ " )\n",
285
+ " stream = gemini.generate_content(\n",
286
+ " contents=user_prompt(function_example),\n",
287
+ " stream=True\n",
288
+ " )\n",
289
+ " reply = \"\"\n",
290
+ " for chunk in stream:\n",
291
+ " reply += chunk.text or \"\"\n",
292
+ " yield reply.replace(\"```python\\n\", \"\").replace(\"```\", \"\")"
293
+ ]
294
+ },
295
+ {
296
+ "cell_type": "code",
297
+ "execution_count": null,
298
+ "id": "0a9fb676-83c3-452e-abeb-8712ebdee1d1",
299
+ "metadata": {},
300
+ "outputs": [],
301
+ "source": [
302
+ "# UI function to trade using CodeQwen\n",
303
+ "def trade_code_qwen(function_example):\n",
304
+ " tokenizer = AutoTokenizer.from_pretrained(MODELS[\"CodeQwen\"])\n",
305
+ " model_input = tokenizer.apply_chat_template(\n",
306
+ " conversation=[\n",
307
+ " {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n",
308
+ " {\"role\": \"user\", \"content\": user_prompt(function_example)}\n",
309
+ " ],\n",
310
+ " tokenize=False,\n",
311
+ " add_generation_prompt=True\n",
312
+ " )\n",
313
+ " stream = code_qwen.text_generation(\n",
314
+ " prompt=model_input,\n",
315
+ " stream=True,\n",
316
+ " details=True,\n",
317
+ " max_new_tokens=MAX_TOKENS\n",
318
+ " )\n",
319
+ " reply = \"\"\n",
320
+ " for chunk in stream:\n",
321
+ " reply += chunk.token.text or \"\"\n",
322
+ " yield reply.replace(\"```python\\n\", \"\").replace(\"```\", \"\")"
323
+ ]
324
+ },
325
+ {
326
+ "cell_type": "code",
327
+ "execution_count": null,
328
+ "id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
329
+ "metadata": {},
330
+ "outputs": [],
331
+ "source": [
332
+ "# UI function to select model from dropdown \n",
333
+ "def trade(trading_function, model):\n",
334
+ " if model==\"GPT\":\n",
335
+ " yield from trade_gpt(trading_function)\n",
336
+ " elif model==\"Claude\":\n",
337
+ " yield from trade_claude(trading_function)\n",
338
+ " elif model==\"Gemini\":\n",
339
+ " yield from trade_gemini(trading_function)\n",
340
+ " elif model==\"CodeQwen\":\n",
341
+ " yield from trade_code_qwen(trading_function)\n",
342
+ " else:\n",
343
+ " raise ValueError(\"Unknown Model\")"
344
+ ]
345
+ },
346
+ {
347
+ "cell_type": "code",
348
+ "execution_count": null,
349
+ "id": "4e6af1cd-f3d9-43f0-91d9-9800d9681a77",
350
+ "metadata": {},
351
+ "outputs": [],
352
+ "source": [
353
+ "# CSS styling for the UI\n",
354
+ "UI_CSS = \"\"\"\n",
355
+ "#title {\n",
356
+ " text-align: center;\n",
357
+ " font-size: 2.5em;\n",
358
+ " font-weight: bold;\n",
359
+ " margin-bottom: 10px;\n",
360
+ "}\n",
361
+ "\n",
362
+ "#description {\n",
363
+ " text-align: left;\n",
364
+ " font-size: 1.2em;\n",
365
+ " font-weight: bold;\n",
366
+ " margin-bottom: 20px;\n",
367
+ " color: #BBB;\n",
368
+ "}\n",
369
+ "\n",
370
+ "#simulate-btn {\n",
371
+ " height: 3em;\n",
372
+ " font-size: 2em !important;\n",
373
+ " padding: 12px 25px !important;\n",
374
+ " border-radius: 10px !important;\n",
375
+ " border: none !important;\n",
376
+ " cursor: pointer;\n",
377
+ " transition: background-color 0.3s, transform 0.2s; /* Smooth effects */\n",
378
+ "}\n",
379
+ "\n",
380
+ "#simulate-btn:hover {\n",
381
+ " background-color: #FFC107 !important; /* Bright golden-yellow on hover */\n",
382
+ " transform: scale(1.05); /* Slight zoom effect */\n",
383
+ " box-shadow: 0 6px 8px rgba(0, 0, 0, 0.25); /* Enhance shadow on hover */\n",
384
+ "}\n",
385
+ "\n",
386
+ "#simulate-btn:active {\n",
387
+ " background-color: #B8860B !important; /* Darker goldenrod on click */\n",
388
+ " transform: scale(0.95); /* Slight press effect */\n",
389
+ " box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); /* Reduce shadow on click */\n",
390
+ "}\n",
391
+ "\n",
392
+ "#simulate-btn,\n",
393
+ "#trading-decisions {\n",
394
+ " background-color: #DAA520 !important; /* Goldenrod color same as #simulate-btn */\n",
395
+ " color: #FFFFFF !important; /* White text for contrast */\n",
396
+ " box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3); /* Subtle shadow for depth */\n",
397
+ "}\n",
398
+ "\n",
399
+ "#trading-decisions {\n",
400
+ " border: 2px solid #B8860B; /* Darker goldenrod border */\n",
401
+ "}\n",
402
+ "\n",
403
+ "#trading-decisions:focus {\n",
404
+ " outline: none;\n",
405
+ " box-shadow: 0 0 8px #FFC107; /* Bright golden-yellow glow on focus */\n",
406
+ "}\n",
407
+ "\n",
408
+ "#example-function, \n",
409
+ "#model-dropdown {\n",
410
+ " background-color: #007965 !important; /* Darker and sharper teal for better contrast */\n",
411
+ " color: #FFFFFF !important; /* Pure white for strong visibility */\n",
412
+ " cursor: pointer;\n",
413
+ " border: 2px solid #00594D; /* Deep teal border for emphasis */\n",
414
+ " box-shadow: 0 4px 8px rgba(0, 0, 0, 0.9); /* Strong shadow for depth */\n",
415
+ "}\n",
416
+ "\n",
417
+ "#example-function:focus,\n",
418
+ "#model-dropdown:focus {\n",
419
+ " outline: none;\n",
420
+ " box-shadow: 0 0 10px #00A389; /* Vibrant teal glow on focus */\n",
421
+ "}\n",
422
+ "\n",
423
+ "#model-dropdown:hover {\n",
424
+ " background-color: #005F4A !important; /* Darker teal for hover effect */\n",
425
+ " box-shadow: 0 6px 10px rgba(0, 0, 0, 0.4); /* Enhanced shadow on hover */\n",
426
+ " border-color: #00A389; /* Change border color for hover */\n",
427
+ "}\n",
428
+ "\"\"\""
429
+ ]
430
+ },
431
+ {
432
+ "cell_type": "code",
433
+ "execution_count": null,
434
+ "id": "f733330f-6945-4be4-a2ab-9e68c94f70f0",
435
+ "metadata": {},
436
+ "outputs": [],
437
+ "source": [
438
+ "# Gradio UI\n",
439
+ "with gr.Blocks(css=UI_CSS) as ui:\n",
440
+ " # Title for the application\n",
441
+ " gr.Markdown(\"<div id='title'>🛠️ Trading Strategy Simulator</div><br>\")\n",
442
+ " \n",
443
+ " # Input and output section\n",
444
+ " with gr.Row():\n",
445
+ " trading_f = gr.Textbox(\n",
446
+ " label=\"📄 Trading Function Input\",\n",
447
+ " placeholder=\"Paste your trading function here...\",\n",
448
+ " lines=15,\n",
449
+ " value=TRADING_FUNCTION_EXAMPLE,\n",
450
+ " elem_id=\"example-function\"\n",
451
+ " )\n",
452
+ " decisions = gr.Textbox(\n",
453
+ " label=\"📊 Generated Trading Decisions\",\n",
454
+ " placeholder=\"Trading decisions will appear here...\",\n",
455
+ " lines=20,\n",
456
+ " interactive=False,\n",
457
+ " elem_id=\"trading-decisions\"\n",
458
+ " )\n",
459
+ " \n",
460
+ " with gr.Row():\n",
461
+ " # Dropdown scaled to take 1 part of the row\n",
462
+ " model = gr.Dropdown(\n",
463
+ " choices=MODELS,\n",
464
+ " label=\"🤖 Select AI Model\",\n",
465
+ " value=\"GPT\",\n",
466
+ " scale=1,\n",
467
+ " elem_id=\"model-dropdown\"\n",
468
+ " )\n",
469
+ " # Markdown for the description scaled to 2 parts of the row\n",
470
+ " with gr.Column(scale=2):\n",
471
+ " gr.Markdown(\n",
472
+ " \"\"\"\n",
473
+ " <div id='description'>\n",
474
+ " This interface allows you to test and simulate trading strategies using a predefined example function.\n",
475
+ " Simply input a trading function, select your preferred AI model, and see the generated trading decisions in action.<br>\n",
476
+ " Experiment with different strategies to refine your approach and analyze outcomes effectively.\n",
477
+ " </div>\n",
478
+ " \"\"\"\n",
479
+ " )\n",
480
+ " # Button scaled to take 1 part of the row\n",
481
+ " trade_btn = gr.Button(\n",
482
+ " \"💼 Simulate Trading\",\n",
483
+ " elem_id=\"simulate-btn\",\n",
484
+ " scale=1\n",
485
+ " )\n",
486
+ "\n",
487
+ " # Action button behavior\n",
488
+ " trade_btn.click(\n",
489
+ " fn=trade, \n",
490
+ " inputs=[trading_f, model], \n",
491
+ " outputs=[decisions]\n",
492
+ " )\n",
493
+ "\n",
494
+ "# Launch the UI in a browser\n",
495
+ "ui.launch(inbrowser=True)"
496
+ ]
497
+ },
498
+ {
499
+ "cell_type": "code",
500
+ "execution_count": null,
501
+ "id": "9d0ad093-425b-488e-8c3f-67f729dd9c06",
502
+ "metadata": {},
503
+ "outputs": [],
504
+ "source": []
505
+ }
506
+ ],
507
+ "metadata": {
508
+ "kernelspec": {
509
+ "display_name": "Python 3 (ipykernel)",
510
+ "language": "python",
511
+ "name": "python3"
512
+ },
513
+ "language_info": {
514
+ "codemirror_mode": {
515
+ "name": "ipython",
516
+ "version": 3
517
+ },
518
+ "file_extension": ".py",
519
+ "mimetype": "text/x-python",
520
+ "name": "python",
521
+ "nbconvert_exporter": "python",
522
+ "pygments_lexer": "ipython3",
523
+ "version": "3.11.11"
524
+ }
525
+ },
526
+ "nbformat": 4,
527
+ "nbformat_minor": 5
528
+ }
community-contributions/unit-tests-generator.ipynb ADDED
@@ -0,0 +1,462 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "## Requirements\n",
8
+ "\n",
9
+ "1. Install pytest and pytest-cov library\n"
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "code",
14
+ "execution_count": 1,
15
+ "metadata": {},
16
+ "outputs": [],
17
+ "source": [
18
+ "#!pipenv install pytest pytest-cov"
19
+ ]
20
+ },
21
+ {
22
+ "cell_type": "markdown",
23
+ "metadata": {},
24
+ "source": [
25
+ "## Current flow:\n",
26
+ "\n",
27
+ "1. For a python code it generates the unit tests using `pytest` library. The dashboard supports tests execution along with a coverage report. If the unit tests are fine, there is an option to save them for future use. It can happen, especially with Ollama , the tests are having a typing error. In this case the code can be edited in the right window and executed afterwards. \n",
28
+ "\n",
29
+ "2. Supports 3 models: \n",
30
+ "\n",
31
+ "- gpt-4o-mini\n",
32
+ "- claude-3-5-sonnet-20240620\n",
33
+ "- llama3.2\n",
34
+ "\n",
35
+ "It is recommended though to use other models except Ollama, my tests showed the code returned from ollama required more supervision and editing. Some generated unit tests from ollama don't provide full coverage, but still it is a good starting point for building such a tool."
36
+ ]
37
+ },
38
+ {
39
+ "cell_type": "code",
40
+ "execution_count": 2,
41
+ "metadata": {},
42
+ "outputs": [],
43
+ "source": [
44
+ "# imports\n",
45
+ "import re\n",
46
+ "import os\n",
47
+ "import sys\n",
48
+ "import textwrap\n",
49
+ "from dotenv import load_dotenv\n",
50
+ "from openai import OpenAI\n",
51
+ "import anthropic\n",
52
+ "import gradio as gr\n",
53
+ "from pathlib import Path\n",
54
+ "import subprocess\n",
55
+ "from IPython.display import Markdown"
56
+ ]
57
+ },
58
+ {
59
+ "cell_type": "code",
60
+ "execution_count": null,
61
+ "metadata": {},
62
+ "outputs": [],
63
+ "source": [
64
+ "# Initialization\n",
65
+ "\n",
66
+ "load_dotenv()\n",
67
+ "\n",
68
+ "openai_api_key = os.getenv('OPENAI_API_KEY')\n",
69
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
70
+ "if openai_api_key:\n",
71
+ " print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
72
+ "else:\n",
73
+ " print(\"OpenAI API Key not set\")\n",
74
+ " \n",
75
+ "OPENAI_MODEL = \"gpt-4o-mini\"\n",
76
+ "CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\"\n",
77
+ "openai = OpenAI()\n",
78
+ "claude = anthropic.Anthropic()"
79
+ ]
80
+ },
81
+ {
82
+ "cell_type": "code",
83
+ "execution_count": 4,
84
+ "metadata": {},
85
+ "outputs": [],
86
+ "source": [
87
+ "OLLAMA_API = \"http://localhost:11434/api/chat\"\n",
88
+ "HEADERS = {\"Content-Type\": \"application/json\"}\n",
89
+ "OLLAMA_MODEL = \"llama3.2\""
90
+ ]
91
+ },
92
+ {
93
+ "cell_type": "markdown",
94
+ "metadata": {},
95
+ "source": [
96
+ "## Code execution"
97
+ ]
98
+ },
99
+ {
100
+ "cell_type": "code",
101
+ "execution_count": 5,
102
+ "metadata": {},
103
+ "outputs": [],
104
+ "source": [
105
+ "\n",
106
+ "def extract_code(text):\n",
107
+ " # Regular expression to find text between ``python and ``\n",
108
+ " match = re.search(r\"```python(.*?)```\", text, re.DOTALL)\n",
109
+ "\n",
110
+ " if match:\n",
111
+ " code = match.group(0).strip() # Extract and strip extra spaces\n",
112
+ " else:\n",
113
+ " code = \"\"\n",
114
+ " print(\"No matching substring found.\")\n",
115
+ "\n",
116
+ " return code.replace(\"```python\\n\", \"\").replace(\"```\", \"\")\n",
117
+ "\n",
118
+ "\n",
119
+ "def execute_coverage_report(python_interpreter=sys.executable):\n",
120
+ " if not python_interpreter:\n",
121
+ " raise EnvironmentError(\"Python interpreter not found in the specified virtual environment.\")\n",
122
+ " \n",
123
+ " command = [\"coverage\", \"run\", \"-m\", \"pytest\"]\n",
124
+ "\n",
125
+ " try:\n",
126
+ " result = subprocess.run(command, check=True, capture_output=True, text=True)\n",
127
+ " print(\"Tests ran successfully!\")\n",
128
+ " print(result.stdout)\n",
129
+ " return result.stdout\n",
130
+ " except subprocess.CalledProcessError as e:\n",
131
+ " print(\"Some tests failed!\")\n",
132
+ " print(\"Output:\\n\", e.stdout)\n",
133
+ " print(\"Errors:\\n\", e.stderr)\n",
134
+ " # Extracting failed test information\n",
135
+ " return e.stdout\n",
136
+ "\n",
137
+ "def save_unit_tests(code):\n",
138
+ "\n",
139
+ " match = re.search(r\"def\\s+(\\w+)\\(\", code, re.DOTALL)\n",
140
+ "\n",
141
+ " if match:\n",
142
+ " function_name = match.group(1).strip() # Extract and strip extra spaces\n",
143
+ " else:\n",
144
+ " function_name = \"\"\n",
145
+ " print(\"No matching substring found.\")\n",
146
+ "\n",
147
+ " test_code_path = Path(\"tests\")\n",
148
+ " (test_code_path / f\"test_{function_name}.py\").write_text(extract_code(code))\n",
149
+ " Path(\"tests\", \"test_code.py\").unlink()\n",
150
+ " \n",
151
+ "\n",
152
+ "def execute_tests_in_venv(code_to_test, tests, python_interpreter=sys.executable):\n",
153
+ " \"\"\"\n",
154
+ " Execute the given Python code string within the specified virtual environment.\n",
155
+ " \n",
156
+ " Args:\n",
157
+ " - code_str: str, the Python code to execute.\n",
158
+ " - venv_dir: str, the directory path to the virtual environment created by pipenv.\n",
159
+ " \"\"\"\n",
160
+ " \n",
161
+ " if not python_interpreter:\n",
162
+ " raise EnvironmentError(\"Python interpreter not found in the specified virtual environment.\")\n",
163
+ "\n",
164
+ " # Prepare the command to execute the code\n",
165
+ " code_str = textwrap.dedent(code_to_test) + \"\\n\" + extract_code(tests)\n",
166
+ " test_code_path = Path(\"tests\")\n",
167
+ " test_code_path.mkdir(parents=True, exist_ok=True)\n",
168
+ " (test_code_path / f\"test_code.py\").write_text(code_str)\n",
169
+ " command = [\"pytest\", str(test_code_path)]\n",
170
+ "\n",
171
+ " try:\n",
172
+ " result = subprocess.run(command, check=True, capture_output=True, text=True)\n",
173
+ " print(\"Tests ran successfully!\")\n",
174
+ " print(result.stderr)\n",
175
+ " return result.stdout\n",
176
+ " except subprocess.CalledProcessError as e:\n",
177
+ " print(\"Some tests failed!\")\n",
178
+ " print(\"Output:\\n\", e.stdout)\n",
179
+ " print(\"Errors:\\n\", e.stderr)\n",
180
+ " # Extracting failed test information\n",
181
+ " failed_tests = []\n",
182
+ " for line in e.stdout.splitlines():\n",
183
+ " if \"FAILED\" in line and \"::\" in line:\n",
184
+ " failed_tests.append(line.strip())\n",
185
+ " if failed_tests:\n",
186
+ " print(\"Failed Tests:\")\n",
187
+ " for test in failed_tests:\n",
188
+ " print(test)\n",
189
+ " \n",
190
+ " return e.stdout\n",
191
+ " "
192
+ ]
193
+ },
194
+ {
195
+ "cell_type": "markdown",
196
+ "metadata": {},
197
+ "source": [
198
+ "## Prompts and calls to the models"
199
+ ]
200
+ },
201
+ {
202
+ "cell_type": "code",
203
+ "execution_count": 6,
204
+ "metadata": {},
205
+ "outputs": [],
206
+ "source": [
207
+ "system_message = \"\"\"You are a helpful assistant which helps developers to write unit test cases for their code.\"\"\""
208
+ ]
209
+ },
210
+ {
211
+ "cell_type": "code",
212
+ "execution_count": 7,
213
+ "metadata": {},
214
+ "outputs": [],
215
+ "source": [
216
+ "def get_user_prompt(code):\n",
217
+ "\n",
218
+ " user_prompt = \"\"\"Test include:\n",
219
+ "\n",
220
+ " - Valid inputs with expected results.\n",
221
+ " - Inputs that test the boundaries or limits of the function's behavior.\n",
222
+ " - Invalid inputs or scenarios where the function is expected to raise exceptions.\n",
223
+ "\n",
224
+ " Structure:\n",
225
+ "\n",
226
+ " - Begin with all necessary imports. \n",
227
+ " - Do not create custom imports. \n",
228
+ " - Do not insert in the response the function for the tests.\n",
229
+ " - Ensure proper error handling for tests that expect exceptions.\n",
230
+ " - Clearly name the test functions to indicate their purpose (e.g., test_function_name).\n",
231
+ "\n",
232
+ " Example Structure:\n",
233
+ "\n",
234
+ " - Use pytest.raises to validate exceptions.\n",
235
+ " - Use assertions to verify correct outputs for successful and edge cases.\n",
236
+ "\n",
237
+ " Documentation:\n",
238
+ "\n",
239
+ " - Add docstrings explaining what each test verifies.\"\"\"\n",
240
+ " user_prompt += code\n",
241
+ "\n",
242
+ " return user_prompt"
243
+ ]
244
+ },
245
+ {
246
+ "cell_type": "code",
247
+ "execution_count": 8,
248
+ "metadata": {},
249
+ "outputs": [],
250
+ "source": [
251
+ "def stream_gpt(code):\n",
252
+ "\n",
253
+ " user_prompt = get_user_prompt(code)\n",
254
+ " stream = openai.chat.completions.create(\n",
255
+ " model=OPENAI_MODEL,\n",
256
+ " messages=[\n",
257
+ " {\"role\": \"system\", \"content\": system_message},\n",
258
+ " {\n",
259
+ " \"role\": \"user\",\n",
260
+ " \"content\": user_prompt,\n",
261
+ " },\n",
262
+ " ],\n",
263
+ " stream=True,\n",
264
+ " )\n",
265
+ "\n",
266
+ " response = \"\"\n",
267
+ " for chunk in stream:\n",
268
+ " response += chunk.choices[0].delta.content or \"\"\n",
269
+ " yield response\n",
270
+ " \n",
271
+ " return response\n",
272
+ "\n",
273
+ "def stream_ollama(code):\n",
274
+ "\n",
275
+ " user_prompt = get_user_prompt(code)\n",
276
+ " ollama_via_openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
277
+ " stream = ollama_via_openai.chat.completions.create(\n",
278
+ " model=OLLAMA_MODEL,\n",
279
+ " messages=[\n",
280
+ " {\"role\": \"system\", \"content\": system_message},\n",
281
+ " {\n",
282
+ " \"role\": \"user\",\n",
283
+ " \"content\": user_prompt,\n",
284
+ " },\n",
285
+ " ],\n",
286
+ " stream=True,\n",
287
+ " )\n",
288
+ "\n",
289
+ " response = \"\"\n",
290
+ " for chunk in stream:\n",
291
+ " response += chunk.choices[0].delta.content or \"\"\n",
292
+ " yield response\n",
293
+ " \n",
294
+ " return response\n",
295
+ "\n",
296
+ "\n",
297
+ "def stream_claude(code):\n",
298
+ " user_prompt = get_user_prompt(code)\n",
299
+ " result = claude.messages.stream(\n",
300
+ " model=CLAUDE_MODEL,\n",
301
+ " max_tokens=2000,\n",
302
+ " system=system_message,\n",
303
+ " messages=[\n",
304
+ " {\n",
305
+ " \"role\": \"user\",\n",
306
+ " \"content\": user_prompt,\n",
307
+ " }\n",
308
+ " ],\n",
309
+ " )\n",
310
+ " reply = \"\"\n",
311
+ " with result as stream:\n",
312
+ " for text in stream.text_stream:\n",
313
+ " reply += text\n",
314
+ " yield reply\n",
315
+ " print(text, end=\"\", flush=True)\n",
316
+ " return reply"
317
+ ]
318
+ },
319
+ {
320
+ "cell_type": "markdown",
321
+ "metadata": {},
322
+ "source": [
323
+ "## Code examples to test the inteface"
324
+ ]
325
+ },
326
+ {
327
+ "cell_type": "code",
328
+ "execution_count": 9,
329
+ "metadata": {},
330
+ "outputs": [],
331
+ "source": [
332
+ "function_to_test = \"\"\"\n",
333
+ " def lengthOfLongestSubstring(s):\n",
334
+ " if not isinstance(s, str):\n",
335
+ " raise TypeError(\"Input must be a string\")\n",
336
+ " max_length = 0\n",
337
+ " substring = \"\"\n",
338
+ " start_idx = 0\n",
339
+ " while start_idx < len(s):\n",
340
+ " string = s[start_idx:]\n",
341
+ " for i, x in enumerate(string):\n",
342
+ " substring += x\n",
343
+ " if len(substring) == len(set((list(substring)))):\n",
344
+ " \n",
345
+ " if len(set((list(substring)))) > max_length:\n",
346
+ " \n",
347
+ " max_length = len(substring)\n",
348
+ "\n",
349
+ " start_idx += 1\n",
350
+ " substring = \"\"\n",
351
+ " \n",
352
+ " \n",
353
+ " return max_length\"\"\""
354
+ ]
355
+ },
356
+ {
357
+ "cell_type": "code",
358
+ "execution_count": 10,
359
+ "metadata": {},
360
+ "outputs": [],
361
+ "source": [
362
+ "test_code = \"\"\"```python\n",
363
+ "import pytest\n",
364
+ "\n",
365
+ "# Unit tests using pytest\n",
366
+ "def test_lengthOfLongestSubstring():\n",
367
+ " assert lengthOfLongestSubstring(\"abcabcbb\") == 3 # Case with repeating characters\n",
368
+ " assert lengthOfLongestSubstring(\"bbbbb\") == 1 # Case with all same characters\n",
369
+ " assert lengthOfLongestSubstring(\"pwwkew\") == 3 # Case with mixed characters\n",
370
+ " assert lengthOfLongestSubstring(\"\") == 0 # Empty string case\n",
371
+ " assert lengthOfLongestSubstring(\"abcdef\") == 6 # All unique characters\n",
372
+ " assert lengthOfLongestSubstring(\"abca\") == 3 # Case with pattern and repeat\n",
373
+ " assert lengthOfLongestSubstring(\"dvdf\") == 3 # Case with repeated characters separated\n",
374
+ " assert lengthOfLongestSubstring(\"a\") == 1 # Case with single character\n",
375
+ " assert lengthOfLongestSubstring(\"au\") == 2 # Case with unique two characters\n",
376
+ "```\"\"\""
377
+ ]
378
+ },
379
+ {
380
+ "cell_type": "code",
381
+ "execution_count": 11,
382
+ "metadata": {},
383
+ "outputs": [],
384
+ "source": [
385
+ "def optimize(code, model):\n",
386
+ " if model == \"GPT\":\n",
387
+ " result = stream_gpt(code)\n",
388
+ " elif model == \"Claude\":\n",
389
+ " result = stream_claude(code)\n",
390
+ " elif model == \"Ollama\":\n",
391
+ " result = stream_ollama(code)\n",
392
+ " else:\n",
393
+ " raise ValueError(\"Unknown model\")\n",
394
+ " for stream_so_far in result:\n",
395
+ " yield stream_so_far\n",
396
+ " return result"
397
+ ]
398
+ },
399
+ {
400
+ "cell_type": "markdown",
401
+ "metadata": {},
402
+ "source": [
403
+ "## Gradio interface"
404
+ ]
405
+ },
406
+ {
407
+ "cell_type": "code",
408
+ "execution_count": null,
409
+ "metadata": {},
410
+ "outputs": [],
411
+ "source": [
412
+ "with gr.Blocks() as ui:\n",
413
+ " gr.Markdown(\"## Write unit tests for Python code\")\n",
414
+ " with gr.Row():\n",
415
+ " with gr.Column(scale=1, min_width=300):\n",
416
+ " python = gr.Textbox(label=\"Python code:\", value=function_to_test, lines=10)\n",
417
+ " model = gr.Dropdown([\"GPT\", \"Claude\", \"Ollama\"], label=\"Select model\", value=\"GPT\")\n",
418
+ " unit_tests = gr.Button(\"Write unit tests\")\n",
419
+ " with gr.Column(scale=1, min_width=300):\n",
420
+ " unit_tests_out = gr.TextArea(label=\"Unit tests\", value=test_code, elem_classes=[\"python\"])\n",
421
+ " unit_tests_run = gr.Button(\"Run unit tests\")\n",
422
+ " coverage_run = gr.Button(\"Coverage report\")\n",
423
+ " save_test_run = gr.Button(\"Save unit tests\")\n",
424
+ " with gr.Row():\n",
425
+ " \n",
426
+ " python_out = gr.TextArea(label=\"Unit tests result\", elem_classes=[\"python\"])\n",
427
+ " coverage_out = gr.TextArea(label=\"Coverage report\", elem_classes=[\"python\"])\n",
428
+ " \n",
429
+ "\n",
430
+ " unit_tests.click(optimize, inputs=[python, model], outputs=[unit_tests_out])\n",
431
+ " unit_tests_run.click(execute_tests_in_venv, inputs=[python, unit_tests_out], outputs=[python_out])\n",
432
+ " coverage_run.click(execute_coverage_report, outputs=[coverage_out])\n",
433
+ " save_test_run.click(save_unit_tests, inputs=[unit_tests_out])\n",
434
+ "\n",
435
+ "\n",
436
+ "ui.launch(inbrowser=True)\n",
437
+ "# ui.launch()"
438
+ ]
439
+ }
440
+ ],
441
+ "metadata": {
442
+ "kernelspec": {
443
+ "display_name": "llm_engineering-yg2xCEUG",
444
+ "language": "python",
445
+ "name": "python3"
446
+ },
447
+ "language_info": {
448
+ "codemirror_mode": {
449
+ "name": "ipython",
450
+ "version": 3
451
+ },
452
+ "file_extension": ".py",
453
+ "mimetype": "text/x-python",
454
+ "name": "python",
455
+ "nbconvert_exporter": "python",
456
+ "pygments_lexer": "ipython3",
457
+ "version": "3.10.8"
458
+ }
459
+ },
460
+ "nbformat": 4,
461
+ "nbformat_minor": 2
462
+ }
community-contributions/w4_lang_converter.py ADDED
@@ -0,0 +1,346 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import io
3
+ import sys
4
+ import re
5
+ import subprocess
6
+ from dotenv import load_dotenv
7
+ from openai import OpenAI
8
+ from anthropic import Anthropic
9
+ import gradio as gr
10
+
11
+ # Load environment variables and initialize APIs
12
+ load_dotenv(override=True)
13
+ openai = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
14
+ anthropic = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
15
+ MACHINE_SPEC = "MacbookPro, Apple M1 Chip"
16
+
17
+ # Define global variables for HF integration
18
+ # For HF chat-based CodeQwen model
19
+ code_qwen = "Qwen/CodeQwen1.5-7B-Chat"
20
+ CODE_QWEN_URL = ""
21
+
22
+
23
+ def clean_code(code, target_language):
24
+ """
25
+ Remove markdown code fences and stray language indicators.
26
+ Also apply language-specific replacements.
27
+ """
28
+ raw_lines = code.splitlines()
29
+ cleaned_lines = []
30
+ for line in raw_lines:
31
+ if "```" in line:
32
+ continue
33
+ if line.strip().lower() in ["c", "cpp", "c++", "rust"]:
34
+ continue
35
+ cleaned_lines.append(line)
36
+ cleaned = "\n".join(cleaned_lines)
37
+ if target_language == "C":
38
+ cleaned = cleaned.replace("1U << 32", "(1ULL << 32)")
39
+ if target_language == "Rust":
40
+ cleaned = process_rust_code(cleaned)
41
+ return cleaned
42
+
43
+ # Conversion prompt functions (target language-aware)
44
+ def user_prompt_for(python_code, target_language):
45
+ return (
46
+ f"Rewrite this Python code in {target_language} with the fastest possible implementation that produces identical output. "
47
+ f"Respond only with {target_language} code; do not explain your work. "
48
+ "Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\n\n"
49
+ + python_code
50
+ )
51
+
52
+ def messages_for(python_code, target_language):
53
+ system_message = (
54
+ f"You are an assistant that reimplements Python code in high performance {target_language} for an {MACHINE_SPEC}. "
55
+ f"Respond only with {target_language} code; use comments sparingly. "
56
+ f"The {target_language} response needs to produce an identical output in the fastest possible time."
57
+ )
58
+ return [
59
+ {"role": "system", "content": system_message},
60
+ {"role": "user", "content": user_prompt_for(python_code, target_language)},
61
+ ]
62
+
63
+ def write_output(code, target_language):
64
+ """Write the converted code to a file based on target language."""
65
+ tag = target_language.lower() if target_language is not None else ""
66
+ if target_language == "C++":
67
+ filename = "optimized.cpp"
68
+ elif target_language == "C":
69
+ filename = "optimized.c"
70
+ elif target_language == "Rust":
71
+ filename = "optimized.rs"
72
+ else:
73
+ filename = "optimized.txt"
74
+ cleaned = code.replace(f"```{tag}\n", "").replace("```", "")
75
+ lines = cleaned.splitlines()
76
+ if lines and lines[0].strip().lower() in ["cpp", "c++", "c", "rust"]:
77
+ lines = lines[1:]
78
+ cleaned = "\n".join(lines)
79
+ cleaned = clean_code(cleaned, target_language)
80
+ with open(filename, "w") as f:
81
+ f.write(cleaned)
82
+ return filename
83
+
84
+ # GPT integration for conversion
85
+ def stream_gpt(python_code, target_language, model_version):
86
+ stream = openai.chat.completions.create(
87
+ model=model_version, # Use selected GPT model version
88
+ messages=messages_for(python_code, target_language),
89
+ stream=True,
90
+ )
91
+ reply = ""
92
+ for chunk in stream:
93
+ if not hasattr(chunk, "choices") or not chunk.choices:
94
+ continue
95
+ fragment = chunk.choices[0].delta.content or ""
96
+ reply += fragment
97
+ yield reply.replace(f"```{target_language}\n", "").replace("```", "")
98
+
99
+ # Claude integration for conversion
100
+ def stream_claude(python_code, target_language, model_version):
101
+ prompt = user_prompt_for(python_code, target_language)
102
+ response = anthropic.completions.create(
103
+ prompt=prompt,
104
+ model=model_version,
105
+ stream=True,
106
+ )
107
+ reply = ""
108
+ for chunk in response:
109
+ fragment = chunk.get("completion", "")
110
+ reply += fragment
111
+ yield reply.replace(f"```{target_language}\n", "").replace("```", "")
112
+
113
+ # Hugging Face integration functions
114
+ def stream_code_qwen(python_code, target_language, model_version):
115
+ """
116
+ HF chat-based model using CodeQwen.
117
+ """
118
+ from transformers import AutoTokenizer
119
+ tokenizer = AutoTokenizer.from_pretrained(code_qwen)
120
+ messages = messages_for(python_code, target_language)
121
+ # Convert messages to chat format as expected by Qwen.
122
+ text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
123
+ from huggingface_hub import InferenceClient
124
+ client = InferenceClient(CODE_QWEN_URL, token=os.getenv("HF_TOKEN"))
125
+ stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)
126
+ result = ""
127
+ for r in stream:
128
+ result += r.token.text
129
+ yield result.replace(f"```{target_language}\n", "").replace("```", "")
130
+
131
+ def stream_huggingface(python_code, target_language, model_version):
132
+ """
133
+ HF single-prompt model integration.
134
+ """
135
+ prompt = user_prompt_for(python_code, target_language)
136
+ from huggingface_hub import InferenceClient
137
+ client = InferenceClient(model_name=model_version, token=os.getenv("HF_TOKEN"))
138
+ stream = client.text_generation(prompt, stream=True, details=True, max_new_tokens=3000)
139
+ reply = ""
140
+ for chunk in stream:
141
+ reply += chunk.token.text
142
+ yield reply.replace(f"```{target_language}\n", "").replace("```", "")
143
+
144
+
145
+ def optimize(python_code, combined_model, target_language):
146
+ """
147
+ combined_model is a string like "GPT: gpt-4o", "CLAUDE: claude-3-5-sonnet-20240620" or "HF: model_name"
148
+ """
149
+ provider, model_version = [x.strip() for x in combined_model.split(":")]
150
+ if provider == "GPT":
151
+ for partial in stream_gpt(python_code, target_language, model_version):
152
+ yield partial
153
+ elif provider == "CLAUDE":
154
+ for partial in stream_claude(python_code, target_language, model_version):
155
+ yield partial
156
+ elif provider == "HF":
157
+ if "CodeQwen" in model_version:
158
+ for partial in stream_code_qwen(python_code, target_language, model_version):
159
+ yield partial
160
+ else:
161
+ for partial in stream_huggingface(python_code, target_language, model_version):
162
+ yield partial
163
+ else:
164
+ raise ValueError("Unknown model provider")
165
+
166
+ def execute_python(code):
167
+ """Execute Python code and return its output."""
168
+ env = {} # Dedicated global namespace
169
+ try:
170
+ output = io.StringIO()
171
+ sys.stdout = output
172
+ exec(code, env)
173
+ finally:
174
+ sys.stdout = sys.__stdout__
175
+ return output.getvalue()
176
+
177
+ def execute_cpp(code):
178
+ write_output(code, target_language="C++")
179
+ try:
180
+ compile_cmd = [
181
+ "clang++", "-Ofast", "-std=c++17", "-march=armv8.5-a",
182
+ "-mtune=apple-m1", "-mcpu=apple-m1", "-o", "optimized", "optimized.cpp"
183
+ ]
184
+ subprocess.run(compile_cmd, check=True, text=True, capture_output=True)
185
+ run_cmd = ["./optimized"]
186
+ run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)
187
+ return run_result.stdout
188
+ except subprocess.CalledProcessError as e:
189
+ return f"Error:\n{e.stderr}"
190
+
191
+ def execute_c(code):
192
+ cleaned_code = clean_code(code, "C")
193
+ with open("optimized.c", "w") as f:
194
+ f.write(cleaned_code)
195
+ try:
196
+ compile_cmd = ["clang", "-O2", "-std=c11", "-o", "optimized_c", "optimized.c"]
197
+ subprocess.run(compile_cmd, check=True, text=True, capture_output=True)
198
+ run_cmd = ["./optimized_c"]
199
+ run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)
200
+ return run_result.stdout
201
+ except subprocess.CalledProcessError as e:
202
+ return f"Error:\n{e.stderr}"
203
+
204
+ def process_rust_code(code):
205
+ code = code.replace("{:.6f}", "{:.6}")
206
+ code = re.sub(
207
+ r'(println!$begin:math:text$"Execution Time: \\{\\:\\.6\\} seconds", duration\\.as_secs_f64)(\\s*)$',
208
+ r'\\1())',
209
+ code,
210
+ flags=re.MULTILINE,
211
+ )
212
+ code = code.replace("max_val - min_val as u32 + 1", "((max_val - min_val + 1) as u32)")
213
+ code = code.replace("1 << 32", "1u64 << 32")
214
+ code = re.sub(r'($end:math:text$\s*as i64)\)', r'\1', code)
215
+ return code
216
+
217
+ def execute_rust(code):
218
+ code = code.replace("```rust\n", "").replace("```", "")
219
+ lines = code.split('\n', 1)
220
+ if lines and lines[0].strip().lower() == "rust":
221
+ code = lines[1] if len(lines) > 1 else ""
222
+ code = process_rust_code(code)
223
+ with open("optimized.rs", "w") as f:
224
+ f.write(code)
225
+ try:
226
+ compile_cmd = ["rustc", "optimized.rs", "-O", "-o", "optimized_rust"]
227
+ subprocess.run(compile_cmd, check=True, text=True, capture_output=True)
228
+ run_cmd = ["./optimized_rust"]
229
+ run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)
230
+ return run_result.stdout
231
+ except subprocess.CalledProcessError as e:
232
+ return f"Error:\n{e.stderr}"
233
+
234
+ def execute_target_code(code, target_language):
235
+ """Select the appropriate execution function based on target language."""
236
+ if target_language == "C++":
237
+ return execute_cpp(code)
238
+ elif target_language == "C":
239
+ return execute_c(code)
240
+ elif target_language == "Rust":
241
+ return execute_rust(code)
242
+ else:
243
+ return "Unsupported language"
244
+
245
+ # Gradio UI setup
246
+ css = """
247
+ .python {background-color: #306998;}
248
+ .code {background-color: #050;}
249
+ """
250
+
251
+ def launch_ui():
252
+ with gr.Blocks(css=css) as ui:
253
+ gr.Markdown("## Convert Python Code to C/C++/Rust")
254
+ with gr.Row():
255
+ python_box = gr.Textbox(label="Python code:", value=PYTHON_HARD, lines=10)
256
+ converted_box = gr.Textbox(label="Converted Code:", lines=10)
257
+ with gr.Row():
258
+ model_dropdown = gr.Dropdown(
259
+ ["GPT: gpt-4o", "GPT: gpt-4o-mini", "CLAUDE: claude-3-5-sonnet-20240620", "CLAUDE: claude-3-haiku-20240307", "HF: CodeQwen1.5-7B-Chat", "HF: bigcode/starcoder"],
260
+ label="Select Model",
261
+ value="GPT: gpt-4o"
262
+ )
263
+ target_lang_dropdown = gr.Dropdown(
264
+ ["C++", "C", "Rust"],
265
+ label="Select target language",
266
+ value="C++"
267
+ )
268
+ with gr.Row():
269
+ convert_btn = gr.Button("Convert code")
270
+ with gr.Row():
271
+ python_run_btn = gr.Button("Run Python")
272
+ run_converted_btn = gr.Button("Run Converted Code")
273
+ with gr.Row():
274
+ python_out = gr.TextArea(label="Python result:", elem_classes=["python"])
275
+ converted_out = gr.TextArea(label="Converted Code result:", elem_classes=["code"])
276
+ convert_btn.click(
277
+ optimize,
278
+ inputs=[python_box, model_dropdown, target_lang_dropdown],
279
+ outputs=[converted_box],
280
+ )
281
+ python_run_btn.click(execute_python, inputs=[python_box], outputs=[python_out])
282
+ run_converted_btn.click(
283
+ execute_target_code,
284
+ inputs=[converted_box, target_lang_dropdown],
285
+ outputs=[converted_out],
286
+ )
287
+ ui.launch()
288
+
289
+ # Example Python code blocks
290
+ PYTHON_HARD = """
291
+ # Support large number sizes
292
+ def lcg(seed, a=1664525, c=1013904223, m=2**32):
293
+ value = seed
294
+ while True:
295
+ value = (a * value + c) % m
296
+ yield value
297
+ def max_subarray_sum(n, seed, min_val, max_val):
298
+ lcg_gen = lcg(seed)
299
+ random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]
300
+ max_sum = float('-inf')
301
+ for i in range(n):
302
+ current_sum = 0
303
+ for j in range(i, n):
304
+ current_sum += random_numbers[j]
305
+ if current_sum > max_sum:
306
+ max_sum = current_sum
307
+ return max_sum
308
+ def total_max_subarray_sum(n, initial_seed, min_val, max_val):
309
+ total_sum = 0
310
+ lcg_gen = lcg(initial_seed)
311
+ for _ in range(20):
312
+ seed = next(lcg_gen)
313
+ total_sum += max_subarray_sum(n, seed, min_val, max_val)
314
+ return total_sum
315
+ n = 10000
316
+ initial_seed = 42
317
+ min_val = -10
318
+ max_val = 10
319
+ import time
320
+ start_time = time.time()
321
+ result = total_max_subarray_sum(n, initial_seed, min_val, max_val)
322
+ end_time = time.time()
323
+ print("Total Maximum Subarray Sum (20 runs):", result)
324
+ print("Execution Time: {:.6f} seconds".format(end_time - start_time))
325
+ """
326
+
327
+ if __name__ == "__main__":
328
+ import argparse
329
+ parser = argparse.ArgumentParser(
330
+ description="Single script with multiple executable sections and target language support"
331
+ )
332
+ parser.add_argument(
333
+ "--mode",
334
+ choices=["direct", "ui"],
335
+ default="ui",
336
+ help="Run direct conversion or launch Gradio UI",
337
+ )
338
+ args = parser.parse_args()
339
+
340
+ if args.mode == "direct":
341
+ print("\nExecuting Python code (PYTHON_HARD)...")
342
+ exec(PYTHON_HARD)
343
+ for partial in optimize(PYTHON_HARD, "GPT: gpt-4o", "C++"):
344
+ print(partial, end="")
345
+ elif args.mode == "ui":
346
+ launch_ui()
community-contributions/week4-day4-challenge.ipynb ADDED
@@ -0,0 +1,689 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Code Generator\n",
9
+ "\n",
10
+ "The requirement: use an Open Source model to generate high performance C++ code from Python code\n",
11
+ "\n",
12
+ "To replicate this, you'll need to set up a HuggingFace endpoint as I do in the video. It's simple to do, and it's quite satisfying to see the results!\n",
13
+ "\n",
14
+ "It's also an important part of your learning; this is the first example of deploying an open source model to be behind an API. We'll return to this in Week 8, but this should plant a seed in your mind for what's involved in moving open source models into production."
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "markdown",
19
+ "id": "22e1567b-33fd-49e7-866e-4b635d15715a",
20
+ "metadata": {},
21
+ "source": [
22
+ "<table style=\"margin: 0; text-align: left;\">\n",
23
+ " <tr>\n",
24
+ " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
25
+ " <img src=\"../../important.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
26
+ " </td>\n",
27
+ " <td>\n",
28
+ " <h1 style=\"color:#900;\">Important - Pause Endpoints when not in use</h1>\n",
29
+ " <span style=\"color:#900;\">\n",
30
+ " If you do decide to use HuggingFace endpoints for this project, you should stop or pause the endpoints when you are done to avoid accruing unnecessary running cost. The costs are very low as long as you only run the endpoint when you're using it. Navigate to the HuggingFace endpoint UI <a href=\"https://ui.endpoints.huggingface.co/\">here,</a> open your endpoint, and click Pause to put it on pause so you no longer pay for it. \n",
31
+ "Many thanks to student John L. for raising this.\n",
32
+ "<br/><br/>\n",
33
+ "In week 8 we will use Modal instead of HuggingFace endpoints; with Modal you only pay for the time that you use it and you should get free credits.\n",
34
+ " </span>\n",
35
+ " </td>\n",
36
+ " </tr>\n",
37
+ "</table>"
38
+ ]
39
+ },
40
+ {
41
+ "cell_type": "code",
42
+ "execution_count": 51,
43
+ "id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
44
+ "metadata": {},
45
+ "outputs": [],
46
+ "source": [
47
+ "# imports\n",
48
+ "\n",
49
+ "import os\n",
50
+ "import io\n",
51
+ "import sys\n",
52
+ "import json\n",
53
+ "import requests\n",
54
+ "from dotenv import load_dotenv\n",
55
+ "from openai import OpenAI\n",
56
+ "import google.generativeai\n",
57
+ "import anthropic\n",
58
+ "from IPython.display import Markdown, display, update_display\n",
59
+ "import gradio as gr\n",
60
+ "import subprocess\n",
61
+ "import platform"
62
+ ]
63
+ },
64
+ {
65
+ "cell_type": "code",
66
+ "execution_count": 52,
67
+ "id": "4f672e1c-87e9-4865-b760-370fa605e614",
68
+ "metadata": {},
69
+ "outputs": [],
70
+ "source": [
71
+ "# environment\n",
72
+ "\n",
73
+ "load_dotenv()\n",
74
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
75
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
76
+ "os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
77
+ ]
78
+ },
79
+ {
80
+ "cell_type": "code",
81
+ "execution_count": 53,
82
+ "id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
83
+ "metadata": {},
84
+ "outputs": [],
85
+ "source": [
86
+ "# initialize\n",
87
+ "\n",
88
+ "openai = OpenAI()\n",
89
+ "claude = anthropic.Anthropic()\n",
90
+ "OPENAI_MODEL = \"gpt-4o\"\n",
91
+ "CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\""
92
+ ]
93
+ },
94
+ {
95
+ "cell_type": "code",
96
+ "execution_count": 166,
97
+ "id": "6896636f-923e-4a2c-9d6c-fac07828a201",
98
+ "metadata": {},
99
+ "outputs": [],
100
+ "source": [
101
+ "# Define the different actions available\n",
102
+ "\n",
103
+ "prompt_options = [\"Convert to C\", \"Add comments\", \"Write unit tests\"]\n",
104
+ "\n",
105
+ "system_prompts = {\n",
106
+ " prompt_options[0]: \"\"\"\n",
107
+ "You are an assistant that reimplements Python code in high performance C++.\n",
108
+ "Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments.\n",
109
+ "The C++ response needs to produce an identical output in the fastest possible time. Keep implementations of random number generators identical so that results match exactly.\n",
110
+ "\"\"\",\n",
111
+ " \n",
112
+ " prompt_options[1]: \"\"\"\n",
113
+ "You are an assistant that adds succinct comments and docstrings to Python code. Respond only with valid Python code.\n",
114
+ "\"\"\",\n",
115
+ " \n",
116
+ " prompt_options[2]: \"\"\"\n",
117
+ "You are an assistant that creates unit tests for Python code. Respond only with valid Python code.\n",
118
+ "\"\"\"\n",
119
+ "}\n",
120
+ "\n",
121
+ "user_prompts = {\n",
122
+ " prompt_options[0]: \"\"\"\n",
123
+ "Rewrite this Python code in C++ with the fastest possible implementation that produces identical output in the least time. \n",
124
+ "Respond only with C++ code; do not explain your work other than a few comments.\n",
125
+ "Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\\n\\n\n",
126
+ "\"\"\",\n",
127
+ " \n",
128
+ " prompt_options[1]: \"\"\"\n",
129
+ "Keep this Python code but insert appropriate comments and docstrings.\n",
130
+ "\"\"\",\n",
131
+ " \n",
132
+ " prompt_options[2]: \"\"\"\n",
133
+ "Create unit tests for this Python code.\n",
134
+ "\"\"\"\n",
135
+ "}"
136
+ ]
137
+ },
138
+ {
139
+ "cell_type": "code",
140
+ "execution_count": 157,
141
+ "id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
142
+ "metadata": {},
143
+ "outputs": [],
144
+ "source": [
145
+ "python_sample_options = [\"Hello, World\", \"Calculate pi\", \"Kadane's Algorithm\", \"Sieve of Eratosthenes\"]\n",
146
+ "\n",
147
+ "python_code_samples = {\n",
148
+ " python_sample_options[0]: \"\"\"\n",
149
+ "import time\n",
150
+ "\n",
151
+ "start_time = time.time()\n",
152
+ "\n",
153
+ "print(\"Hello, world\")\n",
154
+ "\n",
155
+ "end_time = time.time()\n",
156
+ "\n",
157
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
158
+ "\"\"\",\n",
159
+ "\n",
160
+ " python_sample_options[1]: \"\"\"\n",
161
+ "import time\n",
162
+ "\n",
163
+ "def calculate(iterations, param1, param2):\n",
164
+ " result = 1.0\n",
165
+ " for i in range(1, iterations+1):\n",
166
+ " j = i * param1 - param2\n",
167
+ " result -= (1/j)\n",
168
+ " j = i * param1 + param2\n",
169
+ " result += (1/j)\n",
170
+ " return result\n",
171
+ "\n",
172
+ "start_time = time.time()\n",
173
+ "result = calculate(100_000_000, 4, 1) * 4\n",
174
+ "end_time = time.time()\n",
175
+ "\n",
176
+ "print(f\"Result: {result:.12f}\")\n",
177
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
178
+ "\"\"\",\n",
179
+ "\n",
180
+ " python_sample_options[2]: \"\"\"\n",
181
+ "# Be careful to support large number sizes\n",
182
+ "\n",
183
+ "def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
184
+ " value = seed\n",
185
+ " while True:\n",
186
+ " value = (a * value + c) % m\n",
187
+ " yield value\n",
188
+ " \n",
189
+ "def max_subarray_sum(n, seed, min_val, max_val):\n",
190
+ " lcg_gen = lcg(seed)\n",
191
+ " random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
192
+ " max_sum = float('-inf')\n",
193
+ " for i in range(n):\n",
194
+ " current_sum = 0\n",
195
+ " for j in range(i, n):\n",
196
+ " current_sum += random_numbers[j]\n",
197
+ " if current_sum > max_sum:\n",
198
+ " max_sum = current_sum\n",
199
+ " return max_sum\n",
200
+ "\n",
201
+ "def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
202
+ " total_sum = 0\n",
203
+ " lcg_gen = lcg(initial_seed)\n",
204
+ " for _ in range(20):\n",
205
+ " seed = next(lcg_gen)\n",
206
+ " total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
207
+ " return total_sum\n",
208
+ "\n",
209
+ "# Parameters\n",
210
+ "n = 10000 # Number of random numbers\n",
211
+ "initial_seed = 42 # Initial seed for the LCG\n",
212
+ "min_val = -10 # Minimum value of random numbers\n",
213
+ "max_val = 10 # Maximum value of random numbers\n",
214
+ "\n",
215
+ "# Timing the function\n",
216
+ "import time\n",
217
+ "start_time = time.time()\n",
218
+ "result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
219
+ "end_time = time.time()\n",
220
+ "\n",
221
+ "print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
222
+ "print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
223
+ "\"\"\",\n",
224
+ "\n",
225
+ " python_sample_options[3]: \"\"\"\n",
226
+ "import time\n",
227
+ "start_time = time.time()\n",
228
+ "stop_at=100_000_000\n",
229
+ "prime = [True] * (stop_at + 1)\n",
230
+ "p = 2\n",
231
+ "\n",
232
+ "while p * p <= stop_at:\n",
233
+ " # If prime[p] is True, then p is a prime\n",
234
+ " if prime[p]:\n",
235
+ " # Mark all multiples of p as non-prime\n",
236
+ " for i in range(p * p, stop_at + 1, p):\n",
237
+ " prime[i] = False\n",
238
+ " p += 1\n",
239
+ "\n",
240
+ "# Collect all prime numbers\n",
241
+ "primes = [p for p in range(2, stop_at + 1) if prime[p]]\n",
242
+ "\n",
243
+ "end_time = time.time()\n",
244
+ "print(\"Maximum prime:, {:,}\".format(primes[-1]))\n",
245
+ "print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
246
+ "\"\"\"\n",
247
+ "}"
248
+ ]
249
+ },
250
+ {
251
+ "cell_type": "code",
252
+ "execution_count": 56,
253
+ "id": "e33565c0-cba8-46d3-a0c5-9440d7fe4d2c",
254
+ "metadata": {},
255
+ "outputs": [],
256
+ "source": [
257
+ "# Create a complete user prompt given descriptive text plus the python code to convert.\n",
258
+ "def create_user_prompt(user_prompt, python_code):\n",
259
+ " return user_prompt + '\\n' + python_code\n",
260
+ "\n",
261
+ "# Create the list the GPT. Claude doesn't need this because it does not combine the system and user prompts.\n",
262
+ "def create_messages_for_gpt(system_prompt, user_prompt):\n",
263
+ " return [\n",
264
+ " {\"role\": \"system\", \"content\": system_prompt},\n",
265
+ " {\"role\": \"user\", \"content\": user_prompt}\n",
266
+ " ]"
267
+ ]
268
+ },
269
+ {
270
+ "cell_type": "code",
271
+ "execution_count": 57,
272
+ "id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
273
+ "metadata": {},
274
+ "outputs": [],
275
+ "source": [
276
+ "def write_cpp_file(filename_base, cpp_code):\n",
277
+ " code_to_write = cpp.replace(\"```cpp\",\"\").replace(\"```\",\"\")\n",
278
+ " with open(f\"{filename_base}.cpp\", \"w\") as f:\n",
279
+ " f.write(code)"
280
+ ]
281
+ },
282
+ {
283
+ "cell_type": "code",
284
+ "execution_count": 164,
285
+ "id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
286
+ "metadata": {},
287
+ "outputs": [],
288
+ "source": [
289
+ "# This is where additional models can be easily added. \n",
290
+ "# Just update the model_options list, add a streaming function, and update the call_llm function. \n",
291
+ "\n",
292
+ "model_options = [\"GPT\", \"Claude\"]\n",
293
+ "# model_options = [\"GPT\", \"Claude\", \"CodeQwen\"]\n",
294
+ "default_model = model_options[0]\n",
295
+ "\n",
296
+ "def stream_gpt(system_prompt, user_prompt, python_code): \n",
297
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=create_messages_for_gpt(system_prompt, create_user_prompt(user_prompt, python_code)), stream=True)\n",
298
+ " reply = \"\"\n",
299
+ " for chunk in stream:\n",
300
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
301
+ " reply += fragment\n",
302
+ " yield reply.replace('```cpp\\n','').replace('```','')\n",
303
+ "\n",
304
+ "def stream_claude(system_prompt, user_prompt, python_code):\n",
305
+ " result = claude.messages.stream(\n",
306
+ " model=CLAUDE_MODEL,\n",
307
+ " max_tokens=2000,\n",
308
+ " system=system_prompt,\n",
309
+ " messages=[{\"role\": \"user\", \"content\": create_user_prompt(user_prompt, python_code)}],\n",
310
+ " )\n",
311
+ " reply = \"\"\n",
312
+ " with result as stream:\n",
313
+ " for text in stream.text_stream:\n",
314
+ " reply += text\n",
315
+ " yield reply.replace('```cpp\\n','').replace('```','')\n",
316
+ "\n",
317
+ "def call_llm(system_prompt, user_prompt, python_code, model):\n",
318
+ " if model==\"GPT\":\n",
319
+ " result = stream_gpt(system_prompt, user_prompt, python_code)\n",
320
+ " elif model==\"Claude\":\n",
321
+ " result = stream_claude(system_prompt, user_prompt, python_code)\n",
322
+ " # elif model==\"CodeQwen\":\n",
323
+ " # result = stream_code_qwen(python)\n",
324
+ " else:\n",
325
+ " raise ValueError(\"Unknown model\")\n",
326
+ " for stream_so_far in result:\n",
327
+ " yield stream_so_far "
328
+ ]
329
+ },
330
+ {
331
+ "cell_type": "code",
332
+ "execution_count": null,
333
+ "id": "bb8c5b4e-ec51-4f21-b3f8-6aa94fede86d",
334
+ "metadata": {},
335
+ "outputs": [],
336
+ "source": [
337
+ "from huggingface_hub import login, InferenceClient\n",
338
+ "from transformers import AutoTokenizer\n",
339
+ "\n",
340
+ "code_qwen = \"Qwen/CodeQwen1.5-7B-Chat\"\n",
341
+ "code_gemma = \"google/codegemma-7b-it\"\n",
342
+ "CODE_QWEN_URL = \"https://h1vdol7jxhje3mpn.us-east-1.aws.endpoints.huggingface.cloud\"\n",
343
+ "CODE_GEMMA_URL = \"https://c5hggiyqachmgnqg.us-east-1.aws.endpoints.huggingface.cloud\"\n",
344
+ "\n",
345
+ "hf_token = os.environ['HF_TOKEN']\n",
346
+ "login(hf_token, add_to_git_credential=True)\n",
347
+ "\n",
348
+ "def stream_code_qwen(python):\n",
349
+ " tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
350
+ " messages = messages_for(python)\n",
351
+ " text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
352
+ " client = InferenceClient(CODE_QWEN_URL, token=hf_token)\n",
353
+ " stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)\n",
354
+ " result = \"\"\n",
355
+ " for r in stream:\n",
356
+ " result += r.token.text\n",
357
+ " yield result "
358
+ ]
359
+ },
360
+ {
361
+ "cell_type": "code",
362
+ "execution_count": 61,
363
+ "id": "19bf2bff-a822-4009-a539-f003b1651383",
364
+ "metadata": {},
365
+ "outputs": [],
366
+ "source": [
367
+ "def execute_python(code):\n",
368
+ " try:\n",
369
+ " output = io.StringIO()\n",
370
+ " sys.stdout = output\n",
371
+ " exec(code)\n",
372
+ " finally:\n",
373
+ " sys.stdout = sys.__stdout__\n",
374
+ " return output.getvalue()\n",
375
+ "\n",
376
+ "def execute_cpp(code):\n",
377
+ " write_output(code)\n",
378
+ " try:\n",
379
+ " compile_result = subprocess.run(compiler_cmd[2], check=True, text=True, capture_output=True)\n",
380
+ " run_cmd = [\"./optimized\"]\n",
381
+ " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
382
+ " return run_result.stdout\n",
383
+ " except subprocess.CalledProcessError as e:\n",
384
+ " return f\"An error occurred:\\n{e.stderr}\""
385
+ ]
386
+ },
387
+ {
388
+ "cell_type": "code",
389
+ "execution_count": 62,
390
+ "id": "4ba311ec-c16a-4fe0-946b-4b940704cf65",
391
+ "metadata": {},
392
+ "outputs": [],
393
+ "source": [
394
+ "def select_python_sample(python_sample):\n",
395
+ " if python_sample in python_sample_options:\n",
396
+ " return python_code_samples[python_sample]\n",
397
+ " else:\n",
398
+ " return next(iter(donedone.values()), \"# Type in your Python program here\")"
399
+ ]
400
+ },
401
+ {
402
+ "cell_type": "code",
403
+ "execution_count": 63,
404
+ "id": "e42286bc-085c-45dc-b101-234308e58269",
405
+ "metadata": {},
406
+ "outputs": [],
407
+ "source": [
408
+ "import platform\n",
409
+ "\n",
410
+ "VISUAL_STUDIO_2022_TOOLS = \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\Common7\\Tools\\\\VsDevCmd.bat\"\n",
411
+ "VISUAL_STUDIO_2019_TOOLS = \"C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2019\\\\BuildTools\\\\Common7\\\\Tools\\\\VsDevCmd.bat\"\n",
412
+ "\n",
413
+ "simple_cpp = \"\"\"\n",
414
+ "#include <iostream>\n",
415
+ "\n",
416
+ "int main() {\n",
417
+ " std::cout << \"Hello\";\n",
418
+ " return 0;\n",
419
+ "}\n",
420
+ "\"\"\"\n",
421
+ "\n",
422
+ "def run_cmd(command_to_run):\n",
423
+ " try:\n",
424
+ " run_result = subprocess.run(command_to_run, check=True, text=True, capture_output=True)\n",
425
+ " return run_result.stdout if run_result.stdout else \"SUCCESS\"\n",
426
+ " except:\n",
427
+ " return \"\"\n",
428
+ "\n",
429
+ "def c_compiler_cmd(filename_base):\n",
430
+ " my_platform = platform.system()\n",
431
+ " my_compiler = []\n",
432
+ "\n",
433
+ " try:\n",
434
+ " with open(\"simple.cpp\", \"w\") as f:\n",
435
+ " f.write(simple_cpp)\n",
436
+ " \n",
437
+ " if my_platform == \"Windows\":\n",
438
+ " if os.path.isfile(VISUAL_STUDIO_2022_TOOLS):\n",
439
+ " if os.path.isfile(\"./simple.exe\"):\n",
440
+ " os.remove(\"./simple.exe\")\n",
441
+ " compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
442
+ " if run_cmd(compile_cmd):\n",
443
+ " if run_cmd([\"./simple.exe\"]) == \"Hello\":\n",
444
+ " my_compiler = [\"Windows\", \"Visual Studio 2022\", [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
445
+ " \n",
446
+ " if not my_compiler:\n",
447
+ " if os.path.isfile(VISUAL_STUDIO_2019_TOOLS):\n",
448
+ " if os.path.isfile(\"./simple.exe\"):\n",
449
+ " os.remove(\"./simple.exe\")\n",
450
+ " compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
451
+ " if run_cmd(compile_cmd):\n",
452
+ " if run_cmd([\"./simple.exe\"]) == \"Hello\":\n",
453
+ " my_compiler = [\"Windows\", \"Visual Studio 2019\", [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
454
+ " \n",
455
+ " if not my_compiler:\n",
456
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
457
+ " \n",
458
+ " elif my_platform == \"Linux\":\n",
459
+ " if os.path.isfile(\"./simple\"):\n",
460
+ " os.remove(\"./simple\")\n",
461
+ " compile_cmd = [\"g++\", \"simple.cpp\", \"-o\", \"simple\"]\n",
462
+ " if run_cmd(compile_cmd):\n",
463
+ " if run_cmd([\"./simple\"]) == \"Hello\":\n",
464
+ " my_compiler = [\"Linux\", \"GCC (g++)\", [\"g++\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\" ]]\n",
465
+ " \n",
466
+ " if not my_compiler:\n",
467
+ " if os.path.isfile(\"./simple\"):\n",
468
+ " os.remove(\"./simple\")\n",
469
+ " compile_cmd = [\"clang++\", \"simple.cpp\", \"-o\", \"simple\"]\n",
470
+ " if run_cmd(compile_cmd):\n",
471
+ " if run_cmd([\"./simple\"]) == \"Hello\":\n",
472
+ " my_compiler = [\"Linux\", \"Clang++\", [\"clang++\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\"]]\n",
473
+ " \n",
474
+ " if not my_compiler:\n",
475
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
476
+ " \n",
477
+ " elif my_platform == \"Darwin\":\n",
478
+ " if os.path.isfile(\"./simple\"):\n",
479
+ " os.remove(\"./simple\")\n",
480
+ " compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", \"simple\", \"simple.cpp\"]\n",
481
+ " if run_cmd(compile_cmd):\n",
482
+ " if run_cmd([\"./simple\"]) == \"Hello\":\n",
483
+ " my_compiler = [\"Macintosh\", \"Clang++\", [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", f\"{filename_base}\", f\"{filename_base}.cpp\"]]\n",
484
+ " \n",
485
+ " if not my_compiler:\n",
486
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
487
+ " except:\n",
488
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
489
+ " \n",
490
+ " if my_compiler:\n",
491
+ " return my_compiler\n",
492
+ " else:\n",
493
+ " return [\"Unknown\", \"Unavailable\", []]\n"
494
+ ]
495
+ },
496
+ {
497
+ "cell_type": "code",
498
+ "execution_count": 167,
499
+ "id": "f9ca2e6f-60c1-4e5f-b570-63c75b2d189b",
500
+ "metadata": {
501
+ "scrolled": true
502
+ },
503
+ "outputs": [
504
+ {
505
+ "data": {
506
+ "text/html": [
507
+ "<div><iframe src=\"http://127.0.0.1:7916/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
508
+ ],
509
+ "text/plain": [
510
+ "<IPython.core.display.HTML object>"
511
+ ]
512
+ },
513
+ "metadata": {},
514
+ "output_type": "display_data"
515
+ },
516
+ {
517
+ "data": {
518
+ "text/plain": []
519
+ },
520
+ "execution_count": 167,
521
+ "metadata": {},
522
+ "output_type": "execute_result"
523
+ }
524
+ ],
525
+ "source": [
526
+ "css = \"\"\"\n",
527
+ ".python {background-color: #306998;}\n",
528
+ ".cpp {background-color: #050;}\n",
529
+ "\"\"\"\n",
530
+ "\n",
531
+ "model = default_model\n",
532
+ "selected_tab = prompt_options[0]\n",
533
+ "\n",
534
+ "# Determine the C (C++, really) compiler to use based on the platform\n",
535
+ "compiler_cmd = c_compiler_cmd(\"optimized\")\n",
536
+ "\n",
537
+ "def any_tab_on_select(evt: gr.SelectData):\n",
538
+ " global selected_tab\n",
539
+ " selected_tab = evt.value\n",
540
+ "\n",
541
+ "def reset_prompts():\n",
542
+ " return system_prompts[selected_tab], user_prompts[selected_tab]\n",
543
+ "\n",
544
+ "def change_python_sample(python_sample, python_code):\n",
545
+ " if not python_sample == \"Custom\":\n",
546
+ " if python_sample in python_sample_options:\n",
547
+ " return python_code_samples[python_sample]\n",
548
+ " else:\n",
549
+ " return python_code\n",
550
+ " else:\n",
551
+ " return python_code\n",
552
+ "\n",
553
+ "def change_python_sample_to_custom():\n",
554
+ " return \"Custom\"\n",
555
+ "\n",
556
+ "# Display the interface\n",
557
+ "with gr.Blocks(css=css) as ui:\n",
558
+ " with gr.Tab(prompt_options[0]) as first_tab:\n",
559
+ " gr.Markdown(\"# \" + prompt_options[0])\n",
560
+ " with gr.Group():\n",
561
+ " with gr.Row():\n",
562
+ " first_system_prompt_txt = gr.Textbox(label=\"System prompt\", value=system_prompts[prompt_options[0]], lines=10, interactive=True )\n",
563
+ " first_user_prompt_txt = gr.Textbox(label=\"User prompt\", value=user_prompts[prompt_options[0]], lines=10, interactive=True )\n",
564
+ " with gr.Row():\n",
565
+ " first_reset_prompts_btn = gr.Button(\"Reset prompts\")\n",
566
+ " with gr.Row():\n",
567
+ " with gr.Column():\n",
568
+ " first_sample_program_rad = gr.Radio(python_sample_options + [\"Custom\"], label=\"Sample program\", value=python_sample_options[0])\n",
569
+ " first_python_code_txt = gr.Textbox(label=\"Python code:\", value=python_code_samples[python_sample_options[0]], lines=10, interactive=True)\n",
570
+ " with gr.Column():\n",
571
+ " first_model_drp = gr.Dropdown(model_options, label=\"Select model\", value=default_model, interactive=True)\n",
572
+ " first_convert_btn = gr.Button(\"Convert code\", interactive=True)\n",
573
+ " first_cpp_txt = gr.Textbox(label=\"C++ code:\", lines=10, interactive=True)\n",
574
+ " with gr.Row():\n",
575
+ " with gr.Column():\n",
576
+ " with gr.Group():\n",
577
+ " first_python_run_btn = gr.Button(\"Run Python\", interactive=True)\n",
578
+ " first_python_out_txt = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
579
+ " with gr.Column():\n",
580
+ " with gr.Group():\n",
581
+ " if not compiler_cmd[1] == \"Unavailable\":\n",
582
+ " first_cpp_run_btn = gr.Button(\"Run C++\")\n",
583
+ " else:\n",
584
+ " first_cpp_run_btn = gr.Button(\"No compiler to run C++\", interactive=False)\n",
585
+ " first_cpp_out_txt = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
586
+ " first_architecture_rad = gr.Radio([compiler_cmd[0]], label=\"Architecture\", interactive=False, value=compiler_cmd[0])\n",
587
+ " first_compiler_rad = gr.Radio([compiler_cmd[1]], label=\"Compiler\", interactive=False, value=compiler_cmd[1])\n",
588
+ " \n",
589
+ " with gr.Tab(prompt_options[1]) as second_tab:\n",
590
+ " gr.Markdown(\"# \" + prompt_options[1])\n",
591
+ " with gr.Group():\n",
592
+ " with gr.Row():\n",
593
+ " second_system_prompt_txt = gr.Textbox(label=\"System prompt\", value=system_prompts[prompt_options[1]], lines=10, interactive=True )\n",
594
+ " second_user_prompt_txt = gr.Textbox(label=\"User prompt\", value=user_prompts[prompt_options[1]], lines=10, interactive=True )\n",
595
+ " with gr.Row():\n",
596
+ " second_reset_prompts_btn = gr.Button(\"Reset prompts\")\n",
597
+ " with gr.Row():\n",
598
+ " with gr.Column():\n",
599
+ " second_sample_program_rad = gr.Radio(python_sample_options + [\"Custom\"], label=\"Sample program\", value=python_sample_options[1])\n",
600
+ " second_python_code_txt = gr.Textbox(label=\"Python code:\", value=python_code_samples[python_sample_options[1]], lines=10)\n",
601
+ " with gr.Column():\n",
602
+ " second_model_drp = gr.Dropdown(model_options, label=\"Select model\", value=default_model)\n",
603
+ " second_comment_btn = gr.Button(\"Comment code\")\n",
604
+ " second_python_code_comments_txt = gr.Textbox(label=\"Commented code\", lines=20)\n",
605
+ "\n",
606
+ " \n",
607
+ " with gr.Tab(prompt_options[2]) as third_tab:\n",
608
+ " gr.Markdown(\"# \" + prompt_options[2])\n",
609
+ " with gr.Group():\n",
610
+ " with gr.Row():\n",
611
+ " third_system_prompt_txt = gr.Textbox(label=\"System prompt\", value=system_prompts[prompt_options[2]], lines=10, interactive=True )\n",
612
+ " third_user_prompt_txt = gr.Textbox(label=\"User prompt\", value=user_prompts[prompt_options[2]], lines=10, interactive=True )\n",
613
+ " with gr.Row():\n",
614
+ " third_reset_prompts_btn = gr.Button(\"Reset prompts\")\n",
615
+ " with gr.Row():\n",
616
+ " with gr.Column():\n",
617
+ " third_sample_program_rad = gr.Radio(python_sample_options + [\"Custom\"], label=\"Sample program\", value=python_sample_options[1])\n",
618
+ " third_python_code_txt = gr.Textbox(label=\"Python code:\", value=python_code_samples[python_sample_options[1]], lines=10)\n",
619
+ " with gr.Column():\n",
620
+ " third_model_drp = gr.Dropdown(model_options, label=\"Select model\", value=default_model)\n",
621
+ " third_unit_test_btn = gr.Button(\"Create unit tests\")\n",
622
+ " third_python_unit_tests_txt = gr.Textbox(label=\"Unit tests\", lines=20)\n",
623
+ "\n",
624
+ " first_tab.select(any_tab_on_select)\n",
625
+ " second_tab.select(any_tab_on_select)\n",
626
+ " third_tab.select(any_tab_on_select)\n",
627
+ " \n",
628
+ " first_reset_prompts_btn.click(reset_prompts, outputs=[first_system_prompt_txt, first_user_prompt_txt])\n",
629
+ " second_reset_prompts_btn.click(reset_prompts, outputs=[second_system_prompt_txt, second_user_prompt_txt])\n",
630
+ " third_reset_prompts_btn.click(reset_prompts, outputs=[third_system_prompt_txt, third_user_prompt_txt])\n",
631
+ "\n",
632
+ " first_sample_program_rad.input(change_python_sample, inputs=[first_sample_program_rad, first_python_code_txt], outputs=[first_python_code_txt])\n",
633
+ " first_python_code_txt.input(change_python_sample_to_custom, inputs=[], outputs=[first_sample_program_rad])\n",
634
+ " first_convert_btn.click(call_llm, inputs=[first_system_prompt_txt, first_user_prompt_txt, first_python_code_txt, first_model_drp], outputs=[first_cpp_txt])\n",
635
+ " first_python_run_btn.click(execute_python, inputs=[first_python_code_txt], outputs=[first_python_out_txt])\n",
636
+ " first_cpp_run_btn.click(execute_cpp, inputs=[first_cpp_txt], outputs=[first_cpp_out_txt])\n",
637
+ "\n",
638
+ " second_sample_program_rad.input(change_python_sample, inputs=[second_sample_program_rad, second_python_code_txt], outputs=[second_python_code_txt])\n",
639
+ " second_python_code_txt.input(change_python_sample_to_custom, inputs=[], outputs=[second_sample_program_rad])\n",
640
+ " second_comment_btn.click(call_llm, inputs=[second_system_prompt_txt, second_user_prompt_txt, second_python_code_txt, second_model_drp], outputs=[second_python_code_comments_txt])\n",
641
+ "\n",
642
+ " third_sample_program_rad.input(change_python_sample, inputs=[third_sample_program_rad, third_python_code_txt], outputs=[third_python_code_txt])\n",
643
+ " third_python_code_txt.input(change_python_sample_to_custom, inputs=[], outputs=[second_sample_program_rad])\n",
644
+ " third_unit_test_btn.click(call_llm, inputs=[third_system_prompt_txt, third_user_prompt_txt, third_python_code_txt, third_model_drp], outputs=[third_python_unit_tests_txt])\n",
645
+ "ui.launch(inbrowser=True)"
646
+ ]
647
+ },
648
+ {
649
+ "cell_type": "code",
650
+ "execution_count": 152,
651
+ "id": "9d0ad093-425b-488e-8c3f-67f729dd9c06",
652
+ "metadata": {},
653
+ "outputs": [],
654
+ "source": [
655
+ "import time\n",
656
+ "\n",
657
+ "start_time = time.time()\n",
658
+ " \n",
659
+ "print(\"Hello, world\")\n",
660
+ " \n",
661
+ "end_time = time.time()\n",
662
+ " \n",
663
+ "\n",
664
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")"
665
+ ]
666
+ }
667
+ ],
668
+ "metadata": {
669
+ "kernelspec": {
670
+ "display_name": "Python 3 (ipykernel)",
671
+ "language": "python",
672
+ "name": "python3"
673
+ },
674
+ "language_info": {
675
+ "codemirror_mode": {
676
+ "name": "ipython",
677
+ "version": 3
678
+ },
679
+ "file_extension": ".py",
680
+ "mimetype": "text/x-python",
681
+ "name": "python",
682
+ "nbconvert_exporter": "python",
683
+ "pygments_lexer": "ipython3",
684
+ "version": "3.11.11"
685
+ }
686
+ },
687
+ "nbformat": 4,
688
+ "nbformat_minor": 5
689
+ }
community-contributions/week4-day5-code-commenter.ipynb ADDED
@@ -0,0 +1,401 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Code Commenter\n",
9
+ "\n",
10
+ "The requirement: use an LLM to generate docstring and comments for Python code\n",
11
+ "\n",
12
+ "This is my week 4 day 5 project. \n",
13
+ "\n",
14
+ "Note: I used gpt to find out the most effective system and user prompt (very effective). I also decided not to use the open source models due to inference api costs with HF"
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 1,
20
+ "id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
21
+ "metadata": {},
22
+ "outputs": [],
23
+ "source": [
24
+ "# imports\n",
25
+ "\n",
26
+ "import os\n",
27
+ "import io\n",
28
+ "import sys\n",
29
+ "import json\n",
30
+ "import requests\n",
31
+ "from dotenv import load_dotenv\n",
32
+ "from openai import OpenAI\n",
33
+ "import google.generativeai\n",
34
+ "import anthropic\n",
35
+ "from IPython.display import Markdown, display, update_display\n",
36
+ "import gradio as gr\n",
37
+ "import subprocess"
38
+ ]
39
+ },
40
+ {
41
+ "cell_type": "code",
42
+ "execution_count": 2,
43
+ "id": "4f672e1c-87e9-4865-b760-370fa605e614",
44
+ "metadata": {},
45
+ "outputs": [],
46
+ "source": [
47
+ "# environment\n",
48
+ "\n",
49
+ "load_dotenv()\n",
50
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
51
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
52
+ "google_api_key = os.getenv('GOOGLE_API_KEY')\n"
53
+ ]
54
+ },
55
+ {
56
+ "cell_type": "code",
57
+ "execution_count": 3,
58
+ "id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
59
+ "metadata": {},
60
+ "outputs": [],
61
+ "source": [
62
+ "# initialize\n",
63
+ "\n",
64
+ "openai = OpenAI()\n",
65
+ "claude = anthropic.Anthropic()\n",
66
+ "google.generativeai.configure()\n",
67
+ "\n",
68
+ "OPENAI_MODEL = \"gpt-4o\"\n",
69
+ "CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\"\n",
70
+ "GOOGLE_MODEL = \"gemini-1.5-pro\""
71
+ ]
72
+ },
73
+ {
74
+ "cell_type": "code",
75
+ "execution_count": 4,
76
+ "id": "6896636f-923e-4a2c-9d6c-fac07828a201",
77
+ "metadata": {},
78
+ "outputs": [],
79
+ "source": [
80
+ "system_message = \"You are a Python code assistant. Your task is to analyze Python code and generate high-quality, concise comments and docstrings. Follow these guidelines:\"\n",
81
+ "system_message += \"Docstrings: Add a docstring for every function, class, and module. Describe the purpose of the function/class, its parameters, and its return value. Keep the description concise but informative, using proper Python docstring conventions (e.g., Google, NumPy, or reStructuredText format).\"\n",
82
+ "system_message += \"Inline Comments: Add inline comments only where necessary to clarify complex logic, important steps, or non-obvious behavior. Avoid commenting on obvious operations like x += 1 unless it involves a nuanced concept. Keep comments short, clear, and relevant.\"\n",
83
+ "system_message += \"General Instructions: Maintain consistency in style and tone. Use technical terminology where appropriate, but ensure clarity for someone with intermediate Python knowledge. Do not over-explain or add redundant comments for self-explanatory code. Follow PEP 257 and PEP 8 standards for style and formatting.\"\n"
84
+ ]
85
+ },
86
+ {
87
+ "cell_type": "code",
88
+ "execution_count": 5,
89
+ "id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
90
+ "metadata": {},
91
+ "outputs": [],
92
+ "source": [
93
+ "def user_prompt_for(python):\n",
94
+ " user_prompt = \"Analyze the following Python code and enhance it by adding high-quality, concise docstrings and comments. \"\n",
95
+ " user_prompt += \"Ensure all functions, classes, and modules have appropriate docstrings describing their purpose, parameters, and return values. \"\n",
96
+ " user_prompt += \"Add inline comments only for complex or non-obvious parts of the code. \"\n",
97
+ " user_prompt += \"Follow Python's PEP 257 and PEP 8 standards for documentation and formatting. \"\n",
98
+ " user_prompt += \"Do not modify the code itself; only add annotations.\\n\\n\"\n",
99
+ " user_prompt += python\n",
100
+ " return user_prompt\n"
101
+ ]
102
+ },
103
+ {
104
+ "cell_type": "code",
105
+ "execution_count": 6,
106
+ "id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
107
+ "metadata": {},
108
+ "outputs": [],
109
+ "source": [
110
+ "def messages_for(python):\n",
111
+ " return [\n",
112
+ " {\"role\": \"system\", \"content\": system_message},\n",
113
+ " {\"role\": \"user\", \"content\": user_prompt_for(python)}\n",
114
+ " ]"
115
+ ]
116
+ },
117
+ {
118
+ "cell_type": "code",
119
+ "execution_count": 7,
120
+ "id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
121
+ "metadata": {},
122
+ "outputs": [],
123
+ "source": [
124
+ "pi = \"\"\"\n",
125
+ "import time\n",
126
+ "\n",
127
+ "def calculate(iterations, param1, param2):\n",
128
+ " result = 1.0\n",
129
+ " for i in range(1, iterations+1):\n",
130
+ " j = i * param1 - param2\n",
131
+ " result -= (1/j)\n",
132
+ " j = i * param1 + param2\n",
133
+ " result += (1/j)\n",
134
+ " return result\n",
135
+ "\n",
136
+ "start_time = time.time()\n",
137
+ "result = calculate(100_000_000, 4, 1) * 4\n",
138
+ "end_time = time.time()\n",
139
+ "\n",
140
+ "print(f\"Result: {result:.12f}\")\n",
141
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
142
+ "\"\"\""
143
+ ]
144
+ },
145
+ {
146
+ "cell_type": "code",
147
+ "execution_count": 8,
148
+ "id": "c3b497b3-f569-420e-b92e-fb0f49957ce0",
149
+ "metadata": {},
150
+ "outputs": [],
151
+ "source": [
152
+ "python_hard = \"\"\"# Be careful to support large number sizes\n",
153
+ "\n",
154
+ "def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
155
+ " value = seed\n",
156
+ " while True:\n",
157
+ " value = (a * value + c) % m\n",
158
+ " yield value\n",
159
+ " \n",
160
+ "def max_subarray_sum(n, seed, min_val, max_val):\n",
161
+ " lcg_gen = lcg(seed)\n",
162
+ " random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
163
+ " max_sum = float('-inf')\n",
164
+ " for i in range(n):\n",
165
+ " current_sum = 0\n",
166
+ " for j in range(i, n):\n",
167
+ " current_sum += random_numbers[j]\n",
168
+ " if current_sum > max_sum:\n",
169
+ " max_sum = current_sum\n",
170
+ " return max_sum\n",
171
+ "\n",
172
+ "def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
173
+ " total_sum = 0\n",
174
+ " lcg_gen = lcg(initial_seed)\n",
175
+ " for _ in range(20):\n",
176
+ " seed = next(lcg_gen)\n",
177
+ " total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
178
+ " return total_sum\n",
179
+ "\n",
180
+ "# Parameters\n",
181
+ "n = 10000 # Number of random numbers\n",
182
+ "initial_seed = 42 # Initial seed for the LCG\n",
183
+ "min_val = -10 # Minimum value of random numbers\n",
184
+ "max_val = 10 # Maximum value of random numbers\n",
185
+ "\n",
186
+ "# Timing the function\n",
187
+ "import time\n",
188
+ "start_time = time.time()\n",
189
+ "result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
190
+ "end_time = time.time()\n",
191
+ "\n",
192
+ "print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
193
+ "print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
194
+ "\"\"\""
195
+ ]
196
+ },
197
+ {
198
+ "cell_type": "code",
199
+ "execution_count": 9,
200
+ "id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
201
+ "metadata": {},
202
+ "outputs": [],
203
+ "source": [
204
+ "def stream_gpt(python): \n",
205
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n",
206
+ " reply = \"\"\n",
207
+ " for chunk in stream:\n",
208
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
209
+ " reply += fragment\n",
210
+ " yield reply.replace('```python\\n','').replace('```','')"
211
+ ]
212
+ },
213
+ {
214
+ "cell_type": "code",
215
+ "execution_count": 10,
216
+ "id": "8669f56b-8314-4582-a167-78842caea131",
217
+ "metadata": {},
218
+ "outputs": [],
219
+ "source": [
220
+ "def stream_claude(python):\n",
221
+ " result = claude.messages.stream(\n",
222
+ " model=CLAUDE_MODEL,\n",
223
+ " max_tokens=2000,\n",
224
+ " system=system_message,\n",
225
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
226
+ " )\n",
227
+ " reply = \"\"\n",
228
+ " with result as stream:\n",
229
+ " for text in stream.text_stream:\n",
230
+ " reply += text\n",
231
+ " yield reply.replace('```python\\n','').replace('```','')"
232
+ ]
233
+ },
234
+ {
235
+ "cell_type": "code",
236
+ "execution_count": 11,
237
+ "id": "25f8d215-67a8-4179-8834-0e1da5a7dd32",
238
+ "metadata": {},
239
+ "outputs": [],
240
+ "source": [
241
+ "def stream_google(python):\n",
242
+ " # Initialize empty reply string\n",
243
+ " reply = \"\"\n",
244
+ " \n",
245
+ " # The API for Gemini has a slightly different structure\n",
246
+ " gemini = google.generativeai.GenerativeModel(\n",
247
+ " model_name=GOOGLE_MODEL,\n",
248
+ " system_instruction=system_message\n",
249
+ " )\n",
250
+ " \n",
251
+ " response = gemini.generate_content(\n",
252
+ " user_prompt_for(python),\n",
253
+ " stream=True\n",
254
+ " )\n",
255
+ " \n",
256
+ " # Process the stream\n",
257
+ " for chunk in response:\n",
258
+ " # Extract text from the chunk\n",
259
+ " if chunk.text:\n",
260
+ " reply += chunk.text\n",
261
+ " yield reply.replace('```python\\n','').replace('```','')"
262
+ ]
263
+ },
264
+ {
265
+ "cell_type": "code",
266
+ "execution_count": 12,
267
+ "id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
268
+ "metadata": {},
269
+ "outputs": [],
270
+ "source": [
271
+ "def optimize(python, model):\n",
272
+ " if model==\"GPT\":\n",
273
+ " result = stream_gpt(python)\n",
274
+ " elif model==\"Claude\":\n",
275
+ " result = stream_claude(python)\n",
276
+ " elif model==\"Gemini\":\n",
277
+ " result = stream_google(python)\n",
278
+ " else:\n",
279
+ " raise ValueError(\"Unknown model\")\n",
280
+ " for stream_so_far in result:\n",
281
+ " yield stream_so_far "
282
+ ]
283
+ },
284
+ {
285
+ "cell_type": "code",
286
+ "execution_count": 13,
287
+ "id": "43a6b5f5-5d7c-4511-9d0c-21640070b3cf",
288
+ "metadata": {},
289
+ "outputs": [],
290
+ "source": [
291
+ "def execute_python(code):\n",
292
+ " try:\n",
293
+ " output = io.StringIO()\n",
294
+ " sys.stdout = output\n",
295
+ " exec(code)\n",
296
+ " finally:\n",
297
+ " sys.stdout = sys.__stdout__\n",
298
+ " return output.getvalue()"
299
+ ]
300
+ },
301
+ {
302
+ "cell_type": "code",
303
+ "execution_count": 14,
304
+ "id": "f35b0602-84f9-4ed6-aa35-87be4290ed24",
305
+ "metadata": {},
306
+ "outputs": [],
307
+ "source": [
308
+ "css = \"\"\"\n",
309
+ ".python {background-color: #306998;}\n",
310
+ ".cpp {background-color: #050;}\n",
311
+ "\"\"\""
312
+ ]
313
+ },
314
+ {
315
+ "cell_type": "code",
316
+ "execution_count": 15,
317
+ "id": "62488014-d34c-4de8-ba72-9516e05e9dde",
318
+ "metadata": {},
319
+ "outputs": [
320
+ {
321
+ "name": "stdout",
322
+ "output_type": "stream",
323
+ "text": [
324
+ "* Running on local URL: http://127.0.0.1:7860\n",
325
+ "\n",
326
+ "To create a public link, set `share=True` in `launch()`.\n"
327
+ ]
328
+ },
329
+ {
330
+ "data": {
331
+ "text/html": [
332
+ "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
333
+ ],
334
+ "text/plain": [
335
+ "<IPython.core.display.HTML object>"
336
+ ]
337
+ },
338
+ "metadata": {},
339
+ "output_type": "display_data"
340
+ },
341
+ {
342
+ "data": {
343
+ "text/plain": []
344
+ },
345
+ "execution_count": 15,
346
+ "metadata": {},
347
+ "output_type": "execute_result"
348
+ }
349
+ ],
350
+ "source": [
351
+ "with gr.Blocks(css=css) as ui:\n",
352
+ " gr.Markdown(\"## Convert code from Python to C++\")\n",
353
+ " with gr.Row():\n",
354
+ " python = gr.Textbox(label=\"Python code:\", value=pi, lines=10)\n",
355
+ " commented_python = gr.Textbox(label=\"Commented code:\", lines=10)\n",
356
+ " with gr.Row():\n",
357
+ " model = gr.Dropdown([\"GPT\", \"Claude\", \"Gemini\"], label=\"Select model\", value=\"GPT\")\n",
358
+ " with gr.Row():\n",
359
+ " comment = gr.Button(\"Comment code\")\n",
360
+ " with gr.Row():\n",
361
+ " python_run = gr.Button(\"Check Commented Python\")\n",
362
+ " with gr.Row():\n",
363
+ " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
364
+ "\n",
365
+ " comment.click(optimize, inputs=[python, model], outputs=[commented_python])\n",
366
+ " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
367
+ "\n",
368
+ "ui.launch(inbrowser=True)"
369
+ ]
370
+ },
371
+ {
372
+ "cell_type": "code",
373
+ "execution_count": null,
374
+ "id": "b084760b-c327-4fe7-9b7c-a01b1a383dc3",
375
+ "metadata": {},
376
+ "outputs": [],
377
+ "source": []
378
+ }
379
+ ],
380
+ "metadata": {
381
+ "kernelspec": {
382
+ "display_name": "Python 3 (ipykernel)",
383
+ "language": "python",
384
+ "name": "python3"
385
+ },
386
+ "language_info": {
387
+ "codemirror_mode": {
388
+ "name": "ipython",
389
+ "version": 3
390
+ },
391
+ "file_extension": ".py",
392
+ "mimetype": "text/x-python",
393
+ "name": "python",
394
+ "nbconvert_exporter": "python",
395
+ "pygments_lexer": "ipython3",
396
+ "version": "3.11.11"
397
+ }
398
+ },
399
+ "nbformat": 4,
400
+ "nbformat_minor": 5
401
+ }
day3.ipynb ADDED
@@ -0,0 +1,785 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Code Generator\n",
9
+ "\n",
10
+ "The requirement: use a Frontier model to generate high performance C++ code from Python code\n"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "markdown",
15
+ "id": "d5ccb926-7b49-44a4-99ab-8ef20b5778c0",
16
+ "metadata": {},
17
+ "source": [
18
+ "<table style=\"margin: 0; text-align: left;\">\n",
19
+ " <tr>\n",
20
+ " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
21
+ " <img src=\"../resources.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
22
+ " </td>\n",
23
+ " <td>\n",
24
+ " <h2 style=\"color:#f71;\">Reminder: fetch latest code</h2>\n",
25
+ " <span style=\"color:#f71;\">I'm continually improving these labs, adding more examples and exercises.\n",
26
+ " At the start of each week, it's worth checking you have the latest code.<br/>\n",
27
+ " First do a <a href=\"https://chatgpt.com/share/6734e705-3270-8012-a074-421661af6ba9\">git pull and merge your changes as needed</a>. Any problems? Try asking ChatGPT to clarify how to merge - or contact me!<br/><br/>\n",
28
+ " After you've pulled the code, from the llm_engineering directory, in an Anaconda prompt (PC) or Terminal (Mac), run:<br/>\n",
29
+ " <code>conda env update --f environment.yml --prune</code><br/>\n",
30
+ " Or if you used virtualenv rather than Anaconda, then run this from your activated environment in a Powershell (PC) or Terminal (Mac):<br/>\n",
31
+ " <code>pip install -r requirements.txt</code>\n",
32
+ " <br/>Then restart the kernel (Kernel menu >> Restart Kernel and Clear Outputs Of All Cells) to pick up the changes.\n",
33
+ " </span>\n",
34
+ " </td>\n",
35
+ " </tr>\n",
36
+ "</table>"
37
+ ]
38
+ },
39
+ {
40
+ "cell_type": "markdown",
41
+ "id": "d90e04a2-5b8a-4fd5-9db8-27c02f033313",
42
+ "metadata": {},
43
+ "source": [
44
+ "<table style=\"margin: 0; text-align: left;\">\n",
45
+ " <tr>\n",
46
+ " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
47
+ " <img src=\"../important.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
48
+ " </td>\n",
49
+ " <td>\n",
50
+ " <h1 style=\"color:#900;\">Important Note</h1>\n",
51
+ " <span style=\"color:#900;\">\n",
52
+ " In this lab, I use GPT-4o and Claude-3.5-Sonnet, which are the slightly higher priced models. The costs are still low, but if you'd prefer to keep costs ultra low, please make the suggested switches to the models (3 cells down from here).\n",
53
+ " </span>\n",
54
+ " </td>\n",
55
+ " </tr>\n",
56
+ "</table>"
57
+ ]
58
+ },
59
+ {
60
+ "cell_type": "code",
61
+ "execution_count": null,
62
+ "id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
63
+ "metadata": {},
64
+ "outputs": [],
65
+ "source": [
66
+ "# imports\n",
67
+ "\n",
68
+ "import os\n",
69
+ "import io\n",
70
+ "import sys\n",
71
+ "from dotenv import load_dotenv\n",
72
+ "from openai import OpenAI\n",
73
+ "import google.generativeai\n",
74
+ "import anthropic\n",
75
+ "from IPython.display import Markdown, display, update_display\n",
76
+ "import gradio as gr\n",
77
+ "import subprocess"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "execution_count": null,
83
+ "id": "4f672e1c-87e9-4865-b760-370fa605e614",
84
+ "metadata": {},
85
+ "outputs": [],
86
+ "source": [
87
+ "# environment\n",
88
+ "\n",
89
+ "load_dotenv(override=True)\n",
90
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
91
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')"
92
+ ]
93
+ },
94
+ {
95
+ "cell_type": "code",
96
+ "execution_count": null,
97
+ "id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
98
+ "metadata": {},
99
+ "outputs": [],
100
+ "source": [
101
+ "# initialize\n",
102
+ "# NOTE - option to use ultra-low cost models by uncommenting last 2 lines\n",
103
+ "\n",
104
+ "openai = OpenAI()\n",
105
+ "claude = anthropic.Anthropic()\n",
106
+ "OPENAI_MODEL = \"gpt-4o\"\n",
107
+ "CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\"\n",
108
+ "\n",
109
+ "# Want to keep costs ultra-low? Uncomment these lines:\n",
110
+ "# OPENAI_MODEL = \"gpt-4o-mini\"\n",
111
+ "# CLAUDE_MODEL = \"claude-3-haiku-20240307\""
112
+ ]
113
+ },
114
+ {
115
+ "cell_type": "code",
116
+ "execution_count": null,
117
+ "id": "6896636f-923e-4a2c-9d6c-fac07828a201",
118
+ "metadata": {},
119
+ "outputs": [],
120
+ "source": [
121
+ "system_message = \"You are an assistant that reimplements Python code in high performance C++ for an M1 Mac. \"\n",
122
+ "system_message += \"Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments. \"\n",
123
+ "system_message += \"The C++ response needs to produce an identical output in the fastest possible time.\""
124
+ ]
125
+ },
126
+ {
127
+ "cell_type": "code",
128
+ "execution_count": null,
129
+ "id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
130
+ "metadata": {},
131
+ "outputs": [],
132
+ "source": [
133
+ "def user_prompt_for(python):\n",
134
+ " user_prompt = \"Rewrite this Python code in C++ with the fastest possible implementation that produces identical output in the least time. \"\n",
135
+ " user_prompt += \"Respond only with C++ code; do not explain your work other than a few comments. \"\n",
136
+ " user_prompt += \"Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\\n\\n\"\n",
137
+ " user_prompt += python\n",
138
+ " return user_prompt"
139
+ ]
140
+ },
141
+ {
142
+ "cell_type": "code",
143
+ "execution_count": null,
144
+ "id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
145
+ "metadata": {},
146
+ "outputs": [],
147
+ "source": [
148
+ "def messages_for(python):\n",
149
+ " return [\n",
150
+ " {\"role\": \"system\", \"content\": system_message},\n",
151
+ " {\"role\": \"user\", \"content\": user_prompt_for(python)}\n",
152
+ " ]"
153
+ ]
154
+ },
155
+ {
156
+ "cell_type": "code",
157
+ "execution_count": null,
158
+ "id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
159
+ "metadata": {},
160
+ "outputs": [],
161
+ "source": [
162
+ "# write to a file called optimized.cpp\n",
163
+ "\n",
164
+ "def write_output(cpp):\n",
165
+ " code = cpp.replace(\"```cpp\",\"\").replace(\"```\",\"\")\n",
166
+ " with open(\"optimized.cpp\", \"w\") as f:\n",
167
+ " f.write(code)"
168
+ ]
169
+ },
170
+ {
171
+ "cell_type": "code",
172
+ "execution_count": null,
173
+ "id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
174
+ "metadata": {},
175
+ "outputs": [],
176
+ "source": [
177
+ "def optimize_gpt(python): \n",
178
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n",
179
+ " reply = \"\"\n",
180
+ " for chunk in stream:\n",
181
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
182
+ " reply += fragment\n",
183
+ " print(fragment, end='', flush=True)\n",
184
+ " write_output(reply)"
185
+ ]
186
+ },
187
+ {
188
+ "cell_type": "code",
189
+ "execution_count": null,
190
+ "id": "7cd84ad8-d55c-4fe0-9eeb-1895c95c4a9d",
191
+ "metadata": {},
192
+ "outputs": [],
193
+ "source": [
194
+ "def optimize_claude(python):\n",
195
+ " result = claude.messages.stream(\n",
196
+ " model=CLAUDE_MODEL,\n",
197
+ " max_tokens=2000,\n",
198
+ " system=system_message,\n",
199
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
200
+ " )\n",
201
+ " reply = \"\"\n",
202
+ " with result as stream:\n",
203
+ " for text in stream.text_stream:\n",
204
+ " reply += text\n",
205
+ " print(text, end=\"\", flush=True)\n",
206
+ " write_output(reply)"
207
+ ]
208
+ },
209
+ {
210
+ "cell_type": "code",
211
+ "execution_count": 1,
212
+ "id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
213
+ "metadata": {},
214
+ "outputs": [],
215
+ "source": [
216
+ "pi = \"\"\"\n",
217
+ "import time\n",
218
+ "\n",
219
+ "def calculate(iterations, param1, param2):\n",
220
+ " result = 1.0\n",
221
+ " for i in range(1, iterations+1):\n",
222
+ " j = i * param1 - param2\n",
223
+ " result -= (1/j)\n",
224
+ " j = i * param1 + param2\n",
225
+ " result += (1/j)\n",
226
+ " return result\n",
227
+ "\n",
228
+ "start_time = time.time()\n",
229
+ "result = calculate(100_000_000, 4, 1) * 4\n",
230
+ "end_time = time.time()\n",
231
+ "\n",
232
+ "print(f\"Result: {result:.12f}\")\n",
233
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
234
+ "\"\"\""
235
+ ]
236
+ },
237
+ {
238
+ "cell_type": "code",
239
+ "execution_count": 2,
240
+ "id": "7fe1cd4b-d2c5-4303-afed-2115a3fef200",
241
+ "metadata": {},
242
+ "outputs": [
243
+ {
244
+ "name": "stdout",
245
+ "output_type": "stream",
246
+ "text": [
247
+ "Result: 3.141592658589\n",
248
+ "Execution Time: 52.224082 seconds\n"
249
+ ]
250
+ }
251
+ ],
252
+ "source": [
253
+ "exec(pi)"
254
+ ]
255
+ },
256
+ {
257
+ "cell_type": "code",
258
+ "execution_count": null,
259
+ "id": "105db6f9-343c-491d-8e44-3a5328b81719",
260
+ "metadata": {},
261
+ "outputs": [],
262
+ "source": [
263
+ "optimize_gpt(pi)"
264
+ ]
265
+ },
266
+ {
267
+ "cell_type": "code",
268
+ "execution_count": null,
269
+ "id": "bf26ee95-0c77-491d-9a91-579a1e96a8a3",
270
+ "metadata": {},
271
+ "outputs": [],
272
+ "source": [
273
+ "exec(pi)"
274
+ ]
275
+ },
276
+ {
277
+ "cell_type": "markdown",
278
+ "id": "bf8f8018-f64d-425c-a0e1-d7862aa9592d",
279
+ "metadata": {},
280
+ "source": [
281
+ "# Compiling C++ and executing\n",
282
+ "\n",
283
+ "This next cell contains the command to compile a C++ file on my M1 Mac. \n",
284
+ "It compiles the file `optimized.cpp` into an executable called `optimized` \n",
285
+ "Then it runs the program called `optimized`\n",
286
+ "\n",
287
+ "In the next lab (day4), a student has contributed a full solution that compiles to efficient code on Mac, PC and Linux!\n",
288
+ "\n",
289
+ "You can wait for this, or you can google (or ask ChatGPT!) for how to do this on your platform, then replace the lines below.\n",
290
+ "If you're not comfortable with this step, you can skip it for sure - I'll show you exactly how it performs on my Mac.\n",
291
+ "\n",
292
+ "\n",
293
+ "OR alternatively: student Sandeep K.G. points out that you can run Python and C++ code online to test it out that way. Thank you Sandeep! \n",
294
+ "> Not an exact comparison but you can still get the idea of performance difference.\n",
295
+ "> For example here: https://www.programiz.com/cpp-programming/online-compiler/"
296
+ ]
297
+ },
298
+ {
299
+ "cell_type": "code",
300
+ "execution_count": null,
301
+ "id": "4194e40c-04ab-4940-9d64-b4ad37c5bb40",
302
+ "metadata": {},
303
+ "outputs": [],
304
+ "source": [
305
+ "# Compile C++ and run the executable\n",
306
+ "\n",
307
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
308
+ "!./optimized"
309
+ ]
310
+ },
311
+ {
312
+ "cell_type": "code",
313
+ "execution_count": null,
314
+ "id": "983a11fe-e24d-4c65-8269-9802c5ef3ae6",
315
+ "metadata": {},
316
+ "outputs": [],
317
+ "source": [
318
+ "optimize_claude(pi)"
319
+ ]
320
+ },
321
+ {
322
+ "cell_type": "code",
323
+ "execution_count": null,
324
+ "id": "d5a766f9-3d23-4bb4-a1d4-88ec44b61ddf",
325
+ "metadata": {},
326
+ "outputs": [],
327
+ "source": [
328
+ "# Repeat for Claude - again, use the right approach for your platform\n",
329
+ "\n",
330
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
331
+ "!./optimized"
332
+ ]
333
+ },
334
+ {
335
+ "cell_type": "code",
336
+ "execution_count": null,
337
+ "id": "c3b497b3-f569-420e-b92e-fb0f49957ce0",
338
+ "metadata": {},
339
+ "outputs": [],
340
+ "source": [
341
+ "python_hard = \"\"\"# Be careful to support large number sizes\n",
342
+ "\n",
343
+ "def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
344
+ " value = seed\n",
345
+ " while True:\n",
346
+ " value = (a * value + c) % m\n",
347
+ " yield value\n",
348
+ " \n",
349
+ "def max_subarray_sum(n, seed, min_val, max_val):\n",
350
+ " lcg_gen = lcg(seed)\n",
351
+ " random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
352
+ " max_sum = float('-inf')\n",
353
+ " for i in range(n):\n",
354
+ " current_sum = 0\n",
355
+ " for j in range(i, n):\n",
356
+ " current_sum += random_numbers[j]\n",
357
+ " if current_sum > max_sum:\n",
358
+ " max_sum = current_sum\n",
359
+ " return max_sum\n",
360
+ "\n",
361
+ "def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
362
+ " total_sum = 0\n",
363
+ " lcg_gen = lcg(initial_seed)\n",
364
+ " for _ in range(20):\n",
365
+ " seed = next(lcg_gen)\n",
366
+ " total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
367
+ " return total_sum\n",
368
+ "\n",
369
+ "# Parameters\n",
370
+ "n = 10000 # Number of random numbers\n",
371
+ "initial_seed = 42 # Initial seed for the LCG\n",
372
+ "min_val = -10 # Minimum value of random numbers\n",
373
+ "max_val = 10 # Maximum value of random numbers\n",
374
+ "\n",
375
+ "# Timing the function\n",
376
+ "import time\n",
377
+ "start_time = time.time()\n",
378
+ "result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
379
+ "end_time = time.time()\n",
380
+ "\n",
381
+ "print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
382
+ "print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
383
+ "\"\"\""
384
+ ]
385
+ },
386
+ {
387
+ "cell_type": "code",
388
+ "execution_count": null,
389
+ "id": "dab5e4bc-276c-4555-bd4c-12c699d5e899",
390
+ "metadata": {},
391
+ "outputs": [],
392
+ "source": [
393
+ "exec(python_hard)"
394
+ ]
395
+ },
396
+ {
397
+ "cell_type": "code",
398
+ "execution_count": null,
399
+ "id": "e8d24ed5-2c15-4f55-80e7-13a3952b3cb8",
400
+ "metadata": {},
401
+ "outputs": [],
402
+ "source": [
403
+ "optimize_gpt(python_hard)"
404
+ ]
405
+ },
406
+ {
407
+ "cell_type": "code",
408
+ "execution_count": null,
409
+ "id": "e0b3d073-88a2-40b2-831c-6f0c345c256f",
410
+ "metadata": {},
411
+ "outputs": [],
412
+ "source": [
413
+ "# Replace this with the right C++ compile + execute command for your platform\n",
414
+ "\n",
415
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
416
+ "!./optimized"
417
+ ]
418
+ },
419
+ {
420
+ "cell_type": "code",
421
+ "execution_count": null,
422
+ "id": "e9305446-1d0c-4b51-866a-b8c1e299bf5c",
423
+ "metadata": {},
424
+ "outputs": [],
425
+ "source": [
426
+ "optimize_claude(python_hard)"
427
+ ]
428
+ },
429
+ {
430
+ "cell_type": "code",
431
+ "execution_count": null,
432
+ "id": "0c181036-8193-4fdd-aef3-fc513b218d43",
433
+ "metadata": {},
434
+ "outputs": [],
435
+ "source": [
436
+ "# Replace this with the right C++ compile + execute command for your platform\n",
437
+ "\n",
438
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
439
+ "!./optimized"
440
+ ]
441
+ },
442
+ {
443
+ "cell_type": "code",
444
+ "execution_count": null,
445
+ "id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
446
+ "metadata": {},
447
+ "outputs": [],
448
+ "source": [
449
+ "def stream_gpt(python): \n",
450
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n",
451
+ " reply = \"\"\n",
452
+ " for chunk in stream:\n",
453
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
454
+ " reply += fragment\n",
455
+ " yield reply.replace('```cpp\\n','').replace('```','')"
456
+ ]
457
+ },
458
+ {
459
+ "cell_type": "code",
460
+ "execution_count": null,
461
+ "id": "8669f56b-8314-4582-a167-78842caea131",
462
+ "metadata": {},
463
+ "outputs": [],
464
+ "source": [
465
+ "def stream_claude(python):\n",
466
+ " result = claude.messages.stream(\n",
467
+ " model=CLAUDE_MODEL,\n",
468
+ " max_tokens=2000,\n",
469
+ " system=system_message,\n",
470
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
471
+ " )\n",
472
+ " reply = \"\"\n",
473
+ " with result as stream:\n",
474
+ " for text in stream.text_stream:\n",
475
+ " reply += text\n",
476
+ " yield reply.replace('```cpp\\n','').replace('```','')"
477
+ ]
478
+ },
479
+ {
480
+ "cell_type": "code",
481
+ "execution_count": null,
482
+ "id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
483
+ "metadata": {},
484
+ "outputs": [],
485
+ "source": [
486
+ "def optimize(python, model):\n",
487
+ " if model==\"GPT\":\n",
488
+ " result = stream_gpt(python)\n",
489
+ " elif model==\"Claude\":\n",
490
+ " result = stream_claude(python)\n",
491
+ " else:\n",
492
+ " raise ValueError(\"Unknown model\")\n",
493
+ " for stream_so_far in result:\n",
494
+ " yield stream_so_far "
495
+ ]
496
+ },
497
+ {
498
+ "cell_type": "code",
499
+ "execution_count": null,
500
+ "id": "f1ddb38e-6b0a-4c37-baa4-ace0b7de887a",
501
+ "metadata": {},
502
+ "outputs": [],
503
+ "source": [
504
+ "with gr.Blocks() as ui:\n",
505
+ " with gr.Row():\n",
506
+ " python = gr.Textbox(label=\"Python code:\", lines=10, value=python_hard)\n",
507
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
508
+ " with gr.Row():\n",
509
+ " model = gr.Dropdown([\"GPT\", \"Claude\"], label=\"Select model\", value=\"GPT\")\n",
510
+ " convert = gr.Button(\"Convert code\")\n",
511
+ "\n",
512
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
513
+ "\n",
514
+ "ui.launch(inbrowser=True)"
515
+ ]
516
+ },
517
+ {
518
+ "cell_type": "code",
519
+ "execution_count": null,
520
+ "id": "19bf2bff-a822-4009-a539-f003b1651383",
521
+ "metadata": {},
522
+ "outputs": [],
523
+ "source": [
524
+ "def execute_python(code):\n",
525
+ " try:\n",
526
+ " output = io.StringIO()\n",
527
+ " sys.stdout = output\n",
528
+ " exec(code)\n",
529
+ " finally:\n",
530
+ " sys.stdout = sys.__stdout__\n",
531
+ " return output.getvalue()"
532
+ ]
533
+ },
534
+ {
535
+ "cell_type": "code",
536
+ "execution_count": null,
537
+ "id": "77f3ab5d-fcfb-4d3f-8728-9cacbf833ea6",
538
+ "metadata": {},
539
+ "outputs": [],
540
+ "source": [
541
+ "# You'll need to change the code in the try block to compile the C++ code for your platform\n",
542
+ "# I pasted this into Claude's chat UI with a request for it to give me a version for an Intel PC,\n",
543
+ "# and it responded with something that looks perfect - you can try a similar approach for your platform.\n",
544
+ "\n",
545
+ "# M1 Mac version to compile and execute optimized C++ code:\n",
546
+ "\n",
547
+ "def execute_cpp(code):\n",
548
+ " write_output(code)\n",
549
+ " try:\n",
550
+ " compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", \"optimized\", \"optimized.cpp\"]\n",
551
+ " compile_result = subprocess.run(compile_cmd, check=True, text=True, capture_output=True)\n",
552
+ " run_cmd = [\"./optimized\"]\n",
553
+ " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
554
+ " return run_result.stdout\n",
555
+ " except subprocess.CalledProcessError as e:\n",
556
+ " return f\"An error occurred:\\n{e.stderr}\""
557
+ ]
558
+ },
559
+ {
560
+ "cell_type": "code",
561
+ "execution_count": null,
562
+ "id": "9a2274f1-d03b-42c0-8dcc-4ce159b18442",
563
+ "metadata": {},
564
+ "outputs": [],
565
+ "source": [
566
+ "css = \"\"\"\n",
567
+ ".python {background-color: #306998;}\n",
568
+ ".cpp {background-color: #050;}\n",
569
+ "\"\"\""
570
+ ]
571
+ },
572
+ {
573
+ "cell_type": "code",
574
+ "execution_count": null,
575
+ "id": "f1303932-160c-424b-97a8-d28c816721b2",
576
+ "metadata": {},
577
+ "outputs": [],
578
+ "source": [
579
+ "with gr.Blocks(css=css) as ui:\n",
580
+ " gr.Markdown(\"## Convert code from Python to C++\")\n",
581
+ " with gr.Row():\n",
582
+ " python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
583
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
584
+ " with gr.Row():\n",
585
+ " model = gr.Dropdown([\"GPT\", \"Claude\"], label=\"Select model\", value=\"GPT\")\n",
586
+ " with gr.Row():\n",
587
+ " convert = gr.Button(\"Convert code\")\n",
588
+ " with gr.Row():\n",
589
+ " python_run = gr.Button(\"Run Python\")\n",
590
+ " cpp_run = gr.Button(\"Run C++\")\n",
591
+ " with gr.Row():\n",
592
+ " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
593
+ " cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
594
+ "\n",
595
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
596
+ " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
597
+ " cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
598
+ "\n",
599
+ "ui.launch(inbrowser=True)"
600
+ ]
601
+ },
602
+ {
603
+ "cell_type": "code",
604
+ "execution_count": 2,
605
+ "id": "f6e69518-a44f-4a8b-b016-da87a78c152e",
606
+ "metadata": {},
607
+ "outputs": [
608
+ {
609
+ "name": "stdout",
610
+ "output_type": "stream",
611
+ "text": [
612
+ "* Running on local URL: http://127.0.0.1:7860\n",
613
+ "\n",
614
+ "To create a public link, set `share=True` in `launch()`.\n"
615
+ ]
616
+ },
617
+ {
618
+ "data": {
619
+ "text/html": [
620
+ "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
621
+ ],
622
+ "text/plain": [
623
+ "<IPython.core.display.HTML object>"
624
+ ]
625
+ },
626
+ "metadata": {},
627
+ "output_type": "display_data"
628
+ },
629
+ {
630
+ "data": {
631
+ "text/plain": []
632
+ },
633
+ "execution_count": 2,
634
+ "metadata": {},
635
+ "output_type": "execute_result"
636
+ }
637
+ ],
638
+ "source": [
639
+ "import gradio as gr\n",
640
+ "import ollama\n",
641
+ "\n",
642
+ "llama_system = \"You are an assistant who solve quadratic equations provided. Just tell the steps and the answer and nothing else\"\n",
643
+ "deepseek_system = \"You are an assistant who solve quadratic equations provided. Just tell the steps and the answer and nothing else\"\n",
644
+ "llama_user = \"Please solve this quadratic equation: \"\n",
645
+ "deepseek_user = \"Please solve this quadratic equation: \"\n",
646
+ "\n",
647
+ "\n",
648
+ "def solver_e(model, equation):\n",
649
+ " if model == \"LLAMA\":\n",
650
+ " res = ollama.chat(model=\"llama3.2\", messages=[{'role':'system', 'content':llama_system}, {'role':'user','content':llama_user + equation}])\n",
651
+ " return res['content']['message']\n",
652
+ " else:\n",
653
+ " res = ollama.chat(model=\"deepseek-r1:1.5b\", messages=[{'role':'system', 'content':llama_system}, {'role':'user','content':llama_user + equation}])\n",
654
+ " return res['content']['message']\n",
655
+ " \n",
656
+ "\n",
657
+ "qua_equ = \"x² - 5x + 6\"\n",
658
+ "\n",
659
+ "with gr.Blocks() as ui:\n",
660
+ " gr.Markdown(\"## Solve quadratic equations\")\n",
661
+ " with gr.Row():\n",
662
+ " llama_text = gr.Textbox(label=\"Llama_Equation\", value=qua_equ, lines=2)\n",
663
+ " deepseek_text = gr.Textbox(label=\"Deepseek_equation\", value=qua_equ, lines=2)\n",
664
+ " with gr.Row():\n",
665
+ " model = gr.Dropdown([\"LLAMA\", \"DEEPSEEK\"], label=\"Select model\", value=\"LLAMA\")\n",
666
+ " with gr.Row():\n",
667
+ " solver = gr.Button(\"Solve Equation\")\n",
668
+ " with gr.Row():\n",
669
+ " convert_llama = gr.Button(\"Say Deepseek to solve Lamma's equation\")\n",
670
+ " convert_deepseek = gr.Button(\"Say Llama to solve Deepseek's equation\")\n",
671
+ " with gr.Row():\n",
672
+ " llama_dis = gr.TextArea(label = \"LLama result: \")\n",
673
+ " DeepSeek_dis = gr.TextArea(label = \"DeepSeek result: \")\n",
674
+ "\n",
675
+ " solver.click(solver_e, inputs=[model, ])\n",
676
+ " \n",
677
+ "\n",
678
+ " \n",
679
+ " \n",
680
+ " \n",
681
+ " \n",
682
+ " "
683
+ ]
684
+ },
685
+ {
686
+ "cell_type": "code",
687
+ "execution_count": 3,
688
+ "id": "8bca3390-d61a-45e5-911d-ed1e2a3778b1",
689
+ "metadata": {},
690
+ "outputs": [
691
+ {
692
+ "name": "stdout",
693
+ "output_type": "stream",
694
+ "text": [
695
+ "Collecting pygame\n",
696
+ " Downloading pygame-2.6.1-cp311-cp311-win_amd64.whl.metadata (13 kB)\n",
697
+ "Downloading pygame-2.6.1-cp311-cp311-win_amd64.whl (10.6 MB)\n",
698
+ " ---------------------------------------- 0.0/10.6 MB ? eta -:--:--\n",
699
+ " --------------------------------------- 0.3/10.6 MB ? eta -:--:--\n",
700
+ " - -------------------------------------- 0.5/10.6 MB 1.9 MB/s eta 0:00:06\n",
701
+ " --- ------------------------------------ 1.0/10.6 MB 1.6 MB/s eta 0:00:06\n",
702
+ " ----- ---------------------------------- 1.6/10.6 MB 1.9 MB/s eta 0:00:05\n",
703
+ " ------ --------------------------------- 1.8/10.6 MB 1.9 MB/s eta 0:00:05\n",
704
+ " -------- ------------------------------- 2.4/10.6 MB 1.9 MB/s eta 0:00:05\n",
705
+ " --------- ------------------------------ 2.6/10.6 MB 1.9 MB/s eta 0:00:05\n",
706
+ " ---------- ----------------------------- 2.9/10.6 MB 1.9 MB/s eta 0:00:05\n",
707
+ " ------------ --------------------------- 3.4/10.6 MB 1.8 MB/s eta 0:00:04\n",
708
+ " -------------- ------------------------- 3.9/10.6 MB 1.8 MB/s eta 0:00:04\n",
709
+ " --------------- ------------------------ 4.2/10.6 MB 1.9 MB/s eta 0:00:04\n",
710
+ " ----------------- ---------------------- 4.7/10.6 MB 1.8 MB/s eta 0:00:04\n",
711
+ " ------------------ --------------------- 5.0/10.6 MB 1.9 MB/s eta 0:00:04\n",
712
+ " --------------------- ------------------ 5.8/10.6 MB 1.9 MB/s eta 0:00:03\n",
713
+ " ----------------------- ---------------- 6.3/10.6 MB 1.9 MB/s eta 0:00:03\n",
714
+ " ------------------------ --------------- 6.6/10.6 MB 1.9 MB/s eta 0:00:03\n",
715
+ " ------------------------- -------------- 6.8/10.6 MB 1.9 MB/s eta 0:00:02\n",
716
+ " --------------------------- ------------ 7.3/10.6 MB 1.9 MB/s eta 0:00:02\n",
717
+ " ----------------------------- ---------- 7.9/10.6 MB 1.9 MB/s eta 0:00:02\n",
718
+ " ------------------------------ --------- 8.1/10.6 MB 2.0 MB/s eta 0:00:02\n",
719
+ " ------------------------------- -------- 8.4/10.6 MB 1.9 MB/s eta 0:00:02\n",
720
+ " --------------------------------- ------ 8.9/10.6 MB 1.9 MB/s eta 0:00:01\n",
721
+ " ----------------------------------- ---- 9.4/10.6 MB 1.9 MB/s eta 0:00:01\n",
722
+ " ------------------------------------- -- 10.0/10.6 MB 1.9 MB/s eta 0:00:01\n",
723
+ " --------------------------------------- 10.5/10.6 MB 1.9 MB/s eta 0:00:01\n",
724
+ " ---------------------------------------- 10.6/10.6 MB 1.9 MB/s eta 0:00:00\n",
725
+ "Installing collected packages: pygame\n",
726
+ "Successfully installed pygame-2.6.1\n",
727
+ "Note: you may need to restart the kernel to use updated packages.\n"
728
+ ]
729
+ }
730
+ ],
731
+ "source": [
732
+ "pip install pygame"
733
+ ]
734
+ },
735
+ {
736
+ "cell_type": "code",
737
+ "execution_count": 2,
738
+ "id": "dfda28fd-ec36-4b59-a047-3ae69b690b6c",
739
+ "metadata": {},
740
+ "outputs": [
741
+ {
742
+ "name": "stdout",
743
+ "output_type": "stream",
744
+ "text": [
745
+ "Hi Hello\n"
746
+ ]
747
+ }
748
+ ],
749
+ "source": [
750
+ "hi = \"Hi \"\n",
751
+ "hello = \"Hello\"\n",
752
+ "print(hi + hello)"
753
+ ]
754
+ },
755
+ {
756
+ "cell_type": "code",
757
+ "execution_count": null,
758
+ "id": "09463243-6cca-4778-b76b-5e3aa0e304e1",
759
+ "metadata": {},
760
+ "outputs": [],
761
+ "source": []
762
+ }
763
+ ],
764
+ "metadata": {
765
+ "kernelspec": {
766
+ "display_name": "Python 3 (ipykernel)",
767
+ "language": "python",
768
+ "name": "python3"
769
+ },
770
+ "language_info": {
771
+ "codemirror_mode": {
772
+ "name": "ipython",
773
+ "version": 3
774
+ },
775
+ "file_extension": ".py",
776
+ "mimetype": "text/x-python",
777
+ "name": "python",
778
+ "nbconvert_exporter": "python",
779
+ "pygments_lexer": "ipython3",
780
+ "version": "3.11.11"
781
+ }
782
+ },
783
+ "nbformat": 4,
784
+ "nbformat_minor": 5
785
+ }
day4.ipynb ADDED
@@ -0,0 +1,848 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Code Generator\n",
9
+ "\n",
10
+ "The requirement: use an Open Source model to generate high performance C++ code from Python code\n",
11
+ "\n",
12
+ "To replicate this, you'll need to set up a HuggingFace endpoint as I do in the video. It's simple to do, and it's quite satisfying to see the results!\n",
13
+ "\n",
14
+ "It's also an important part of your learning; this is the first example of deploying an open source model to be behind an API. We'll return to this in Week 8, but this should plant a seed in your mind for what's involved in moving open source models into production."
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "markdown",
19
+ "id": "22e1567b-33fd-49e7-866e-4b635d15715a",
20
+ "metadata": {},
21
+ "source": [
22
+ "<table style=\"margin: 0; text-align: left;\">\n",
23
+ " <tr>\n",
24
+ " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
25
+ " <img src=\"../important.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
26
+ " </td>\n",
27
+ " <td>\n",
28
+ " <h1 style=\"color:#900;\">Important - Pause Endpoints when not in use</h1>\n",
29
+ " <span style=\"color:#900;\">\n",
30
+ " If you do decide to use HuggingFace endpoints for this project, you should stop or pause the endpoints when you are done to avoid accruing unnecessary running cost. The costs are very low as long as you only run the endpoint when you're using it. Navigate to the HuggingFace endpoint UI <a href=\"https://ui.endpoints.huggingface.co/\">here,</a> open your endpoint, and click Pause to put it on pause so you no longer pay for it. \n",
31
+ "Many thanks to student John L. for raising this.\n",
32
+ "<br/><br/>\n",
33
+ "In week 8 we will use Modal instead of HuggingFace endpoints; with Modal you only pay for the time that you use it and you should get free credits.\n",
34
+ " </span>\n",
35
+ " </td>\n",
36
+ " </tr>\n",
37
+ "</table>"
38
+ ]
39
+ },
40
+ {
41
+ "cell_type": "code",
42
+ "execution_count": null,
43
+ "id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
44
+ "metadata": {},
45
+ "outputs": [],
46
+ "source": [
47
+ "# imports\n",
48
+ "\n",
49
+ "import os\n",
50
+ "import io\n",
51
+ "import sys\n",
52
+ "import json\n",
53
+ "import requests\n",
54
+ "from dotenv import load_dotenv\n",
55
+ "from openai import OpenAI\n",
56
+ "import google.generativeai\n",
57
+ "import anthropic\n",
58
+ "from IPython.display import Markdown, display, update_display\n",
59
+ "import gradio as gr\n",
60
+ "import subprocess"
61
+ ]
62
+ },
63
+ {
64
+ "cell_type": "code",
65
+ "execution_count": null,
66
+ "id": "4f672e1c-87e9-4865-b760-370fa605e614",
67
+ "metadata": {},
68
+ "outputs": [],
69
+ "source": [
70
+ "# environment\n",
71
+ "\n",
72
+ "load_dotenv(override=True)\n",
73
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
74
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
75
+ "os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
76
+ ]
77
+ },
78
+ {
79
+ "cell_type": "code",
80
+ "execution_count": null,
81
+ "id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
82
+ "metadata": {},
83
+ "outputs": [],
84
+ "source": [
85
+ "# initialize\n",
86
+ "\n",
87
+ "openai = OpenAI()\n",
88
+ "claude = anthropic.Anthropic()\n",
89
+ "OPENAI_MODEL = \"gpt-4o\"\n",
90
+ "CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\""
91
+ ]
92
+ },
93
+ {
94
+ "cell_type": "code",
95
+ "execution_count": null,
96
+ "id": "6896636f-923e-4a2c-9d6c-fac07828a201",
97
+ "metadata": {},
98
+ "outputs": [],
99
+ "source": [
100
+ "system_message = \"You are an assistant that reimplements Python code in high performance C++ for an M1 Mac. \"\n",
101
+ "system_message += \"Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments. \"\n",
102
+ "system_message += \"The C++ response needs to produce an identical output in the fastest possible time. Keep implementations of random number generators identical so that results match exactly.\""
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": null,
108
+ "id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
109
+ "metadata": {},
110
+ "outputs": [],
111
+ "source": [
112
+ "def user_prompt_for(python):\n",
113
+ " user_prompt = \"Rewrite this Python code in C++ with the fastest possible implementation that produces identical output in the least time. \"\n",
114
+ " user_prompt += \"Respond only with C++ code; do not explain your work other than a few comments. \"\n",
115
+ " user_prompt += \"Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\\n\\n\"\n",
116
+ " user_prompt += python\n",
117
+ " return user_prompt"
118
+ ]
119
+ },
120
+ {
121
+ "cell_type": "code",
122
+ "execution_count": null,
123
+ "id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
124
+ "metadata": {},
125
+ "outputs": [],
126
+ "source": [
127
+ "def messages_for(python):\n",
128
+ " return [\n",
129
+ " {\"role\": \"system\", \"content\": system_message},\n",
130
+ " {\"role\": \"user\", \"content\": user_prompt_for(python)}\n",
131
+ " ]"
132
+ ]
133
+ },
134
+ {
135
+ "cell_type": "code",
136
+ "execution_count": null,
137
+ "id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
138
+ "metadata": {},
139
+ "outputs": [],
140
+ "source": [
141
+ "# write to a file called optimized.cpp\n",
142
+ "\n",
143
+ "def write_output(cpp):\n",
144
+ " code = cpp.replace(\"```cpp\",\"\").replace(\"```\",\"\")\n",
145
+ " with open(\"optimized.cpp\", \"w\") as f:\n",
146
+ " f.write(code)"
147
+ ]
148
+ },
149
+ {
150
+ "cell_type": "code",
151
+ "execution_count": null,
152
+ "id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
153
+ "metadata": {},
154
+ "outputs": [],
155
+ "source": [
156
+ "def optimize_gpt(python): \n",
157
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n",
158
+ " reply = \"\"\n",
159
+ " for chunk in stream:\n",
160
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
161
+ " reply += fragment\n",
162
+ " print(fragment, end='', flush=True)\n",
163
+ " write_output(reply)"
164
+ ]
165
+ },
166
+ {
167
+ "cell_type": "code",
168
+ "execution_count": null,
169
+ "id": "7cd84ad8-d55c-4fe0-9eeb-1895c95c4a9d",
170
+ "metadata": {},
171
+ "outputs": [],
172
+ "source": [
173
+ "def optimize_claude(python):\n",
174
+ " result = claude.messages.stream(\n",
175
+ " model=CLAUDE_MODEL,\n",
176
+ " max_tokens=2000,\n",
177
+ " system=system_message,\n",
178
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
179
+ " )\n",
180
+ " reply = \"\"\n",
181
+ " with result as stream:\n",
182
+ " for text in stream.text_stream:\n",
183
+ " reply += text\n",
184
+ " print(text, end=\"\", flush=True)\n",
185
+ " write_output(reply)"
186
+ ]
187
+ },
188
+ {
189
+ "cell_type": "code",
190
+ "execution_count": null,
191
+ "id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
192
+ "metadata": {},
193
+ "outputs": [],
194
+ "source": [
195
+ "pi = \"\"\"\n",
196
+ "import time\n",
197
+ "\n",
198
+ "def calculate(iterations, param1, param2):\n",
199
+ " result = 1.0\n",
200
+ " for i in range(1, iterations+1):\n",
201
+ " j = i * param1 - param2\n",
202
+ " result -= (1/j)\n",
203
+ " j = i * param1 + param2\n",
204
+ " result += (1/j)\n",
205
+ " return result\n",
206
+ "\n",
207
+ "start_time = time.time()\n",
208
+ "result = calculate(100_000_000, 4, 1) * 4\n",
209
+ "end_time = time.time()\n",
210
+ "\n",
211
+ "print(f\"Result: {result:.12f}\")\n",
212
+ "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
213
+ "\"\"\""
214
+ ]
215
+ },
216
+ {
217
+ "cell_type": "code",
218
+ "execution_count": null,
219
+ "id": "7fe1cd4b-d2c5-4303-afed-2115a3fef200",
220
+ "metadata": {},
221
+ "outputs": [],
222
+ "source": [
223
+ "exec(pi)"
224
+ ]
225
+ },
226
+ {
227
+ "cell_type": "code",
228
+ "execution_count": null,
229
+ "id": "105db6f9-343c-491d-8e44-3a5328b81719",
230
+ "metadata": {},
231
+ "outputs": [],
232
+ "source": [
233
+ "optimize_gpt(pi)"
234
+ ]
235
+ },
236
+ {
237
+ "cell_type": "code",
238
+ "execution_count": null,
239
+ "id": "bf26ee95-0c77-491d-9a91-579a1e96a8a3",
240
+ "metadata": {},
241
+ "outputs": [],
242
+ "source": [
243
+ "exec(pi)"
244
+ ]
245
+ },
246
+ {
247
+ "cell_type": "code",
248
+ "execution_count": null,
249
+ "id": "4194e40c-04ab-4940-9d64-b4ad37c5bb40",
250
+ "metadata": {},
251
+ "outputs": [],
252
+ "source": [
253
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
254
+ "!./optimized"
255
+ ]
256
+ },
257
+ {
258
+ "cell_type": "code",
259
+ "execution_count": null,
260
+ "id": "983a11fe-e24d-4c65-8269-9802c5ef3ae6",
261
+ "metadata": {},
262
+ "outputs": [],
263
+ "source": [
264
+ "optimize_claude(pi)"
265
+ ]
266
+ },
267
+ {
268
+ "cell_type": "code",
269
+ "execution_count": null,
270
+ "id": "d5a766f9-3d23-4bb4-a1d4-88ec44b61ddf",
271
+ "metadata": {},
272
+ "outputs": [],
273
+ "source": [
274
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
275
+ "!./optimized"
276
+ ]
277
+ },
278
+ {
279
+ "cell_type": "code",
280
+ "execution_count": null,
281
+ "id": "c3b497b3-f569-420e-b92e-fb0f49957ce0",
282
+ "metadata": {},
283
+ "outputs": [],
284
+ "source": [
285
+ "python_hard = \"\"\"# Be careful to support large number sizes\n",
286
+ "\n",
287
+ "def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
288
+ " value = seed\n",
289
+ " while True:\n",
290
+ " value = (a * value + c) % m\n",
291
+ " yield value\n",
292
+ " \n",
293
+ "def max_subarray_sum(n, seed, min_val, max_val):\n",
294
+ " lcg_gen = lcg(seed)\n",
295
+ " random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
296
+ " max_sum = float('-inf')\n",
297
+ " for i in range(n):\n",
298
+ " current_sum = 0\n",
299
+ " for j in range(i, n):\n",
300
+ " current_sum += random_numbers[j]\n",
301
+ " if current_sum > max_sum:\n",
302
+ " max_sum = current_sum\n",
303
+ " return max_sum\n",
304
+ "\n",
305
+ "def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
306
+ " total_sum = 0\n",
307
+ " lcg_gen = lcg(initial_seed)\n",
308
+ " for _ in range(20):\n",
309
+ " seed = next(lcg_gen)\n",
310
+ " total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
311
+ " return total_sum\n",
312
+ "\n",
313
+ "# Parameters\n",
314
+ "n = 10000 # Number of random numbers\n",
315
+ "initial_seed = 42 # Initial seed for the LCG\n",
316
+ "min_val = -10 # Minimum value of random numbers\n",
317
+ "max_val = 10 # Maximum value of random numbers\n",
318
+ "\n",
319
+ "# Timing the function\n",
320
+ "import time\n",
321
+ "start_time = time.time()\n",
322
+ "result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
323
+ "end_time = time.time()\n",
324
+ "\n",
325
+ "print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
326
+ "print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
327
+ "\"\"\""
328
+ ]
329
+ },
330
+ {
331
+ "cell_type": "code",
332
+ "execution_count": null,
333
+ "id": "dab5e4bc-276c-4555-bd4c-12c699d5e899",
334
+ "metadata": {},
335
+ "outputs": [],
336
+ "source": [
337
+ "exec(python_hard)"
338
+ ]
339
+ },
340
+ {
341
+ "cell_type": "code",
342
+ "execution_count": null,
343
+ "id": "e8d24ed5-2c15-4f55-80e7-13a3952b3cb8",
344
+ "metadata": {},
345
+ "outputs": [],
346
+ "source": [
347
+ "optimize_gpt(python_hard)"
348
+ ]
349
+ },
350
+ {
351
+ "cell_type": "code",
352
+ "execution_count": null,
353
+ "id": "e0b3d073-88a2-40b2-831c-6f0c345c256f",
354
+ "metadata": {},
355
+ "outputs": [],
356
+ "source": [
357
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
358
+ "!./optimized"
359
+ ]
360
+ },
361
+ {
362
+ "cell_type": "code",
363
+ "execution_count": null,
364
+ "id": "e9305446-1d0c-4b51-866a-b8c1e299bf5c",
365
+ "metadata": {},
366
+ "outputs": [],
367
+ "source": [
368
+ "optimize_claude(python_hard)"
369
+ ]
370
+ },
371
+ {
372
+ "cell_type": "code",
373
+ "execution_count": null,
374
+ "id": "0c181036-8193-4fdd-aef3-fc513b218d43",
375
+ "metadata": {},
376
+ "outputs": [],
377
+ "source": [
378
+ "!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
379
+ "!./optimized"
380
+ ]
381
+ },
382
+ {
383
+ "cell_type": "code",
384
+ "execution_count": null,
385
+ "id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
386
+ "metadata": {},
387
+ "outputs": [],
388
+ "source": [
389
+ "def stream_gpt(python): \n",
390
+ " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n",
391
+ " reply = \"\"\n",
392
+ " for chunk in stream:\n",
393
+ " fragment = chunk.choices[0].delta.content or \"\"\n",
394
+ " reply += fragment\n",
395
+ " yield reply.replace('```cpp\\n','').replace('```','')"
396
+ ]
397
+ },
398
+ {
399
+ "cell_type": "code",
400
+ "execution_count": null,
401
+ "id": "8669f56b-8314-4582-a167-78842caea131",
402
+ "metadata": {},
403
+ "outputs": [],
404
+ "source": [
405
+ "def stream_claude(python):\n",
406
+ " result = claude.messages.stream(\n",
407
+ " model=CLAUDE_MODEL,\n",
408
+ " max_tokens=2000,\n",
409
+ " system=system_message,\n",
410
+ " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
411
+ " )\n",
412
+ " reply = \"\"\n",
413
+ " with result as stream:\n",
414
+ " for text in stream.text_stream:\n",
415
+ " reply += text\n",
416
+ " yield reply.replace('```cpp\\n','').replace('```','')"
417
+ ]
418
+ },
419
+ {
420
+ "cell_type": "code",
421
+ "execution_count": null,
422
+ "id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
423
+ "metadata": {},
424
+ "outputs": [],
425
+ "source": [
426
+ "def optimize(python, model):\n",
427
+ " if model==\"GPT\":\n",
428
+ " result = stream_gpt(python)\n",
429
+ " elif model==\"Claude\":\n",
430
+ " result = stream_claude(python)\n",
431
+ " else:\n",
432
+ " raise ValueError(\"Unknown model\")\n",
433
+ " for stream_so_far in result:\n",
434
+ " yield stream_so_far "
435
+ ]
436
+ },
437
+ {
438
+ "cell_type": "code",
439
+ "execution_count": null,
440
+ "id": "f1ddb38e-6b0a-4c37-baa4-ace0b7de887a",
441
+ "metadata": {},
442
+ "outputs": [],
443
+ "source": [
444
+ "with gr.Blocks() as ui:\n",
445
+ " with gr.Row():\n",
446
+ " python = gr.Textbox(label=\"Python code:\", lines=10, value=python_hard)\n",
447
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
448
+ " with gr.Row():\n",
449
+ " model = gr.Dropdown([\"GPT\", \"Claude\"], label=\"Select model\", value=\"GPT\")\n",
450
+ " convert = gr.Button(\"Convert code\")\n",
451
+ "\n",
452
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
453
+ "\n",
454
+ "ui.launch(inbrowser=True)"
455
+ ]
456
+ },
457
+ {
458
+ "cell_type": "code",
459
+ "execution_count": null,
460
+ "id": "19bf2bff-a822-4009-a539-f003b1651383",
461
+ "metadata": {},
462
+ "outputs": [],
463
+ "source": [
464
+ "def execute_python(code):\n",
465
+ " try:\n",
466
+ " output = io.StringIO()\n",
467
+ " sys.stdout = output\n",
468
+ " exec(code)\n",
469
+ " finally:\n",
470
+ " sys.stdout = sys.__stdout__\n",
471
+ " return output.getvalue()"
472
+ ]
473
+ },
474
+ {
475
+ "cell_type": "code",
476
+ "execution_count": null,
477
+ "id": "77f3ab5d-fcfb-4d3f-8728-9cacbf833ea6",
478
+ "metadata": {},
479
+ "outputs": [],
480
+ "source": [
481
+ "def execute_cpp(code):\n",
482
+ " write_output(code)\n",
483
+ " compiler_cmd = [\"clang++\", \"-O3\", \"-std=c++17\", \"-march=armv8.3-a\", \"-o\", \"optimized\", \"optimized.cpp\"]\n",
484
+ " try:\n",
485
+ " compile_result = subprocess.run(compiler_cmd, check=True, text=True, capture_output=True)\n",
486
+ " run_cmd = [\"./optimized\"]\n",
487
+ " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
488
+ " return run_result.stdout\n",
489
+ " except subprocess.CalledProcessError as e:\n",
490
+ " return f\"An error occurred:\\n{e.stderr}\""
491
+ ]
492
+ },
493
+ {
494
+ "cell_type": "code",
495
+ "execution_count": null,
496
+ "id": "9a2274f1-d03b-42c0-8dcc-4ce159b18442",
497
+ "metadata": {},
498
+ "outputs": [],
499
+ "source": [
500
+ "css = \"\"\"\n",
501
+ ".python {background-color: #306998;}\n",
502
+ ".cpp {background-color: #050;}\n",
503
+ "\"\"\""
504
+ ]
505
+ },
506
+ {
507
+ "cell_type": "code",
508
+ "execution_count": null,
509
+ "id": "f1303932-160c-424b-97a8-d28c816721b2",
510
+ "metadata": {},
511
+ "outputs": [],
512
+ "source": [
513
+ "with gr.Blocks(css=css) as ui:\n",
514
+ " gr.Markdown(\"## Convert code from Python to C++\")\n",
515
+ " with gr.Row():\n",
516
+ " python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
517
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
518
+ " with gr.Row():\n",
519
+ " model = gr.Dropdown([\"GPT\", \"Claude\"], label=\"Select model\", value=\"GPT\")\n",
520
+ " with gr.Row():\n",
521
+ " convert = gr.Button(\"Convert code\")\n",
522
+ " with gr.Row():\n",
523
+ " python_run = gr.Button(\"Run Python\")\n",
524
+ " cpp_run = gr.Button(\"Run C++\")\n",
525
+ " with gr.Row():\n",
526
+ " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
527
+ " cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
528
+ "\n",
529
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
530
+ " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
531
+ " cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
532
+ "\n",
533
+ "ui.launch(inbrowser=True)"
534
+ ]
535
+ },
536
+ {
537
+ "cell_type": "code",
538
+ "execution_count": null,
539
+ "id": "bb8c5b4e-ec51-4f21-b3f8-6aa94fede86d",
540
+ "metadata": {},
541
+ "outputs": [],
542
+ "source": [
543
+ "from huggingface_hub import login, InferenceClient\n",
544
+ "from transformers import AutoTokenizer"
545
+ ]
546
+ },
547
+ {
548
+ "cell_type": "code",
549
+ "execution_count": null,
550
+ "id": "13347633-4606-4e38-9927-80c39e65c1f1",
551
+ "metadata": {},
552
+ "outputs": [],
553
+ "source": [
554
+ "hf_token = os.environ['HF_TOKEN']\n",
555
+ "login(hf_token, add_to_git_credential=True)"
556
+ ]
557
+ },
558
+ {
559
+ "cell_type": "code",
560
+ "execution_count": null,
561
+ "id": "ef60a4df-6267-4ebd-8eed-dcb917af0a5e",
562
+ "metadata": {},
563
+ "outputs": [],
564
+ "source": [
565
+ "code_qwen = \"Qwen/CodeQwen1.5-7B-Chat\"\n",
566
+ "code_gemma = \"google/codegemma-7b-it\"\n",
567
+ "CODE_QWEN_URL = \"https://h1vdol7jxhje3mpn.us-east-1.aws.endpoints.huggingface.cloud\"\n",
568
+ "CODE_GEMMA_URL = \"https://c5hggiyqachmgnqg.us-east-1.aws.endpoints.huggingface.cloud\""
569
+ ]
570
+ },
571
+ {
572
+ "cell_type": "code",
573
+ "execution_count": null,
574
+ "id": "695ce389-a903-4533-a2f1-cd9e2a6af8f2",
575
+ "metadata": {},
576
+ "outputs": [],
577
+ "source": [
578
+ "tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
579
+ "messages = messages_for(pi)\n",
580
+ "text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)"
581
+ ]
582
+ },
583
+ {
584
+ "cell_type": "code",
585
+ "execution_count": null,
586
+ "id": "d4548e96-0b32-4793-bdd6-1b072c2f26ab",
587
+ "metadata": {},
588
+ "outputs": [],
589
+ "source": [
590
+ "print(text)"
591
+ ]
592
+ },
593
+ {
594
+ "cell_type": "code",
595
+ "execution_count": null,
596
+ "id": "bb2a126b-09e7-4966-bc97-0ef5c2cc7896",
597
+ "metadata": {},
598
+ "outputs": [],
599
+ "source": [
600
+ "client = InferenceClient(CODE_QWEN_URL, token=hf_token)\n",
601
+ "stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)\n",
602
+ "for r in stream:\n",
603
+ " print(r.token.text, end = \"\")"
604
+ ]
605
+ },
606
+ {
607
+ "cell_type": "code",
608
+ "execution_count": null,
609
+ "id": "127a52e5-ad85-42b7-a0f5-9afda5efe090",
610
+ "metadata": {},
611
+ "outputs": [],
612
+ "source": [
613
+ "def stream_code_qwen(python):\n",
614
+ " tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
615
+ " messages = messages_for(python)\n",
616
+ " text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
617
+ " client = InferenceClient(CODE_QWEN_URL, token=hf_token)\n",
618
+ " stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)\n",
619
+ " result = \"\"\n",
620
+ " for r in stream:\n",
621
+ " result += r.token.text\n",
622
+ " yield result "
623
+ ]
624
+ },
625
+ {
626
+ "cell_type": "code",
627
+ "execution_count": null,
628
+ "id": "a82387d1-7651-4923-995b-fe18356fcaa6",
629
+ "metadata": {},
630
+ "outputs": [],
631
+ "source": [
632
+ "def optimize(python, model):\n",
633
+ " if model==\"GPT\":\n",
634
+ " result = stream_gpt(python)\n",
635
+ " elif model==\"Claude\":\n",
636
+ " result = stream_claude(python)\n",
637
+ " elif model==\"CodeQwen\":\n",
638
+ " result = stream_code_qwen(python)\n",
639
+ " else:\n",
640
+ " raise ValueError(\"Unknown model\")\n",
641
+ " for stream_so_far in result:\n",
642
+ " yield stream_so_far "
643
+ ]
644
+ },
645
+ {
646
+ "cell_type": "markdown",
647
+ "id": "4b0a6a97-5b8a-4a9b-8ee0-7561e0ced673",
648
+ "metadata": {},
649
+ "source": [
650
+ "<table style=\"margin: 0; text-align: left;\">\n",
651
+ " <tr>\n",
652
+ " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
653
+ " <img src=\"../thankyou.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
654
+ " </td>\n",
655
+ " <td>\n",
656
+ " <h2 style=\"color:#090;\">Thank you to @CloudLlama for an amazing contribution</h2>\n",
657
+ " <span style=\"color:#090;\">\n",
658
+ " A student has contributed a chunk of code to improve this, in the next 2 cells. You can now select which Python porgram to run,\n",
659
+ " and a compiler is automatically selected that will work on PC, Windows and Mac. Massive thank you @CloudLlama!\n",
660
+ " </span>\n",
661
+ " </td>\n",
662
+ " </tr>\n",
663
+ "</table>"
664
+ ]
665
+ },
666
+ {
667
+ "cell_type": "code",
668
+ "execution_count": null,
669
+ "id": "4ba311ec-c16a-4fe0-946b-4b940704cf65",
670
+ "metadata": {},
671
+ "outputs": [],
672
+ "source": [
673
+ "def select_sample_program(sample_program):\n",
674
+ " if sample_program==\"pi\":\n",
675
+ " return pi\n",
676
+ " elif sample_program==\"python_hard\":\n",
677
+ " return python_hard\n",
678
+ " else:\n",
679
+ " return \"Type your Python program here\""
680
+ ]
681
+ },
682
+ {
683
+ "cell_type": "code",
684
+ "execution_count": null,
685
+ "id": "e42286bc-085c-45dc-b101-234308e58269",
686
+ "metadata": {},
687
+ "outputs": [],
688
+ "source": [
689
+ "import platform\n",
690
+ "\n",
691
+ "VISUAL_STUDIO_2022_TOOLS = \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\Common7\\Tools\\\\VsDevCmd.bat\"\n",
692
+ "VISUAL_STUDIO_2019_TOOLS = \"C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2019\\\\BuildTools\\\\Common7\\\\Tools\\\\VsDevCmd.bat\"\n",
693
+ "\n",
694
+ "simple_cpp = \"\"\"\n",
695
+ "#include <iostream>\n",
696
+ "\n",
697
+ "int main() {\n",
698
+ " std::cout << \"Hello\";\n",
699
+ " return 0;\n",
700
+ "}\n",
701
+ "\"\"\"\n",
702
+ "\n",
703
+ "def run_cmd(command_to_run):\n",
704
+ " try:\n",
705
+ " run_result = subprocess.run(command_to_run, check=True, text=True, capture_output=True)\n",
706
+ " return run_result.stdout if run_result.stdout else \"SUCCESS\"\n",
707
+ " except:\n",
708
+ " return \"\"\n",
709
+ "\n",
710
+ "def c_compiler_cmd(filename_base):\n",
711
+ " my_platform = platform.system()\n",
712
+ " my_compiler = []\n",
713
+ "\n",
714
+ " try:\n",
715
+ " with open(\"simple.cpp\", \"w\") as f:\n",
716
+ " f.write(simple_cpp)\n",
717
+ " \n",
718
+ " if my_platform == \"Windows\":\n",
719
+ " if os.path.isfile(VISUAL_STUDIO_2022_TOOLS):\n",
720
+ " if os.path.isfile(\"./simple.exe\"):\n",
721
+ " os.remove(\"./simple.exe\")\n",
722
+ " compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
723
+ " if run_cmd(compile_cmd):\n",
724
+ " if run_cmd([\"./simple.exe\"]) == \"Hello\":\n",
725
+ " my_compiler = [\"Windows\", \"Visual Studio 2022\", [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
726
+ " \n",
727
+ " if not my_compiler:\n",
728
+ " if os.path.isfile(VISUAL_STUDIO_2019_TOOLS):\n",
729
+ " if os.path.isfile(\"./simple.exe\"):\n",
730
+ " os.remove(\"./simple.exe\")\n",
731
+ " compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
732
+ " if run_cmd(compile_cmd):\n",
733
+ " if run_cmd([\"./simple.exe\"]) == \"Hello\":\n",
734
+ " my_compiler = [\"Windows\", \"Visual Studio 2019\", [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
735
+ " \n",
736
+ " if not my_compiler:\n",
737
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
738
+ " \n",
739
+ " elif my_platform == \"Linux\":\n",
740
+ " if os.path.isfile(\"./simple\"):\n",
741
+ " os.remove(\"./simple\")\n",
742
+ " compile_cmd = [\"g++\", \"simple.cpp\", \"-o\", \"simple\"]\n",
743
+ " if run_cmd(compile_cmd):\n",
744
+ " if run_cmd([\"./simple\"]) == \"Hello\":\n",
745
+ " my_compiler = [\"Linux\", \"GCC (g++)\", [\"g++\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\" ]]\n",
746
+ " \n",
747
+ " if not my_compiler:\n",
748
+ " if os.path.isfile(\"./simple\"):\n",
749
+ " os.remove(\"./simple\")\n",
750
+ " compile_cmd = [\"clang++\", \"simple.cpp\", \"-o\", \"simple\"]\n",
751
+ " if run_cmd(compile_cmd):\n",
752
+ " if run_cmd([\"./simple\"]) == \"Hello\":\n",
753
+ " my_compiler = [\"Linux\", \"Clang++\", [\"clang++\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\"]]\n",
754
+ " \n",
755
+ " if not my_compiler:\n",
756
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
757
+ " \n",
758
+ " elif my_platform == \"Darwin\":\n",
759
+ " if os.path.isfile(\"./simple\"):\n",
760
+ " os.remove(\"./simple\")\n",
761
+ " compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", \"simple\", \"simple.cpp\"]\n",
762
+ " if run_cmd(compile_cmd):\n",
763
+ " if run_cmd([\"./simple\"]) == \"Hello\":\n",
764
+ " my_compiler = [\"Macintosh\", \"Clang++\", [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", f\"{filename_base}\", f\"{filename_base}.cpp\"]]\n",
765
+ " \n",
766
+ " if not my_compiler:\n",
767
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
768
+ " except:\n",
769
+ " my_compiler=[my_platform, \"Unavailable\", []]\n",
770
+ " \n",
771
+ " if my_compiler:\n",
772
+ " return my_compiler\n",
773
+ " else:\n",
774
+ " return [\"Unknown\", \"Unavailable\", []]\n"
775
+ ]
776
+ },
777
+ {
778
+ "cell_type": "code",
779
+ "execution_count": null,
780
+ "id": "f9ca2e6f-60c1-4e5f-b570-63c75b2d189b",
781
+ "metadata": {},
782
+ "outputs": [],
783
+ "source": [
784
+ "compiler_cmd = c_compiler_cmd(\"optimized\")\n",
785
+ "\n",
786
+ "with gr.Blocks(css=css) as ui:\n",
787
+ " gr.Markdown(\"## Convert code from Python to C++\")\n",
788
+ " with gr.Row():\n",
789
+ " python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
790
+ " cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
791
+ " with gr.Row():\n",
792
+ " with gr.Column():\n",
793
+ " sample_program = gr.Radio([\"pi\", \"python_hard\"], label=\"Sample program\", value=\"python_hard\")\n",
794
+ " model = gr.Dropdown([\"GPT\", \"Claude\", \"CodeQwen\"], label=\"Select model\", value=\"GPT\")\n",
795
+ " with gr.Column():\n",
796
+ " architecture = gr.Radio([compiler_cmd[0]], label=\"Architecture\", interactive=False, value=compiler_cmd[0])\n",
797
+ " compiler = gr.Radio([compiler_cmd[1]], label=\"Compiler\", interactive=False, value=compiler_cmd[1])\n",
798
+ " with gr.Row():\n",
799
+ " convert = gr.Button(\"Convert code\")\n",
800
+ " with gr.Row():\n",
801
+ " python_run = gr.Button(\"Run Python\")\n",
802
+ " if not compiler_cmd[1] == \"Unavailable\":\n",
803
+ " cpp_run = gr.Button(\"Run C++\")\n",
804
+ " else:\n",
805
+ " cpp_run = gr.Button(\"No compiler to run C++\", interactive=False)\n",
806
+ " with gr.Row():\n",
807
+ " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
808
+ " cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
809
+ "\n",
810
+ " sample_program.change(select_sample_program, inputs=[sample_program], outputs=[python])\n",
811
+ " convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
812
+ " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
813
+ " cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
814
+ "\n",
815
+ "ui.launch(inbrowser=True)"
816
+ ]
817
+ },
818
+ {
819
+ "cell_type": "code",
820
+ "execution_count": null,
821
+ "id": "9d0ad093-425b-488e-8c3f-67f729dd9c06",
822
+ "metadata": {},
823
+ "outputs": [],
824
+ "source": []
825
+ }
826
+ ],
827
+ "metadata": {
828
+ "kernelspec": {
829
+ "display_name": "Python 3 (ipykernel)",
830
+ "language": "python",
831
+ "name": "python3"
832
+ },
833
+ "language_info": {
834
+ "codemirror_mode": {
835
+ "name": "ipython",
836
+ "version": 3
837
+ },
838
+ "file_extension": ".py",
839
+ "mimetype": "text/x-python",
840
+ "name": "python",
841
+ "nbconvert_exporter": "python",
842
+ "pygments_lexer": "ipython3",
843
+ "version": "3.11.11"
844
+ }
845
+ },
846
+ "nbformat": 4,
847
+ "nbformat_minor": 5
848
+ }
optimized.cpp ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <vector>
3
+ #include <chrono>
4
+ #include <limits>
5
+ #include <iomanip>
6
+
7
+ class LCG {
8
+ private:
9
+ uint64_t value;
10
+ const uint64_t a = 1664525;
11
+ const uint64_t c = 1013904223;
12
+ const uint64_t m = 1ULL << 32;
13
+
14
+ public:
15
+ LCG(uint64_t seed) : value(seed) {}
16
+
17
+ uint64_t next() {
18
+ value = (a * value + c) % m;
19
+ return value;
20
+ }
21
+ };
22
+
23
+ int64_t max_subarray_sum(int n, uint64_t seed, int min_val, int max_val) {
24
+ LCG lcg(seed);
25
+ std::vector<int> random_numbers(n);
26
+ for (int i = 0; i < n; ++i) {
27
+ random_numbers[i] = static_cast<int>(lcg.next() % (max_val - min_val + 1) + min_val);
28
+ }
29
+
30
+ int64_t max_sum = std::numeric_limits<int64_t>::min();
31
+ int64_t current_sum = 0;
32
+ for (int i = 0; i < n; ++i) {
33
+ current_sum = std::max(static_cast<int64_t>(random_numbers[i]), current_sum + random_numbers[i]);
34
+ max_sum = std::max(max_sum, current_sum);
35
+ }
36
+ return max_sum;
37
+ }
38
+
39
+ int64_t total_max_subarray_sum(int n, uint64_t initial_seed, int min_val, int max_val) {
40
+ int64_t total_sum = 0;
41
+ LCG lcg(initial_seed);
42
+ for (int i = 0; i < 20; ++i) {
43
+ uint64_t seed = lcg.next();
44
+ total_sum += max_subarray_sum(n, seed, min_val, max_val);
45
+ }
46
+ return total_sum;
47
+ }
48
+
49
+ int main() {
50
+ int n = 10000;
51
+ uint64_t initial_seed = 42;
52
+ int min_val = -10;
53
+ int max_val = 10;
54
+
55
+ auto start_time = std::chrono::high_resolution_clock::now();
56
+ int64_t result = total_max_subarray_sum(n, initial_seed, min_val, max_val);
57
+ auto end_time = std::chrono::high_resolution_clock::now();
58
+
59
+ auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
60
+
61
+ std::cout << "Total Maximum Subarray Sum (20 runs): " << result << std::endl;
62
+ std::cout << "Execution Time: " << std::fixed << std::setprecision(6) << duration.count() / 1e6 << " seconds" << std::endl;
63
+
64
+ return 0;
65
+ }
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ ollama gradio
2
+ ollama gradio
simple.cpp ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+
2
+ #include <iostream>
3
+
4
+ int main() {
5
+ std::cout << "Hello";
6
+ return 0;
7
+ }
week6/.ipynb_checkpoints/Untitled-checkpoint.ipynb ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [],
3
+ "metadata": {},
4
+ "nbformat": 4,
5
+ "nbformat_minor": 5
6
+ }
week6/.ipynb_checkpoints/day1-checkpoint.ipynb ADDED
@@ -0,0 +1,437 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "28a0673e-96b5-43f2-8a8b-bd033bf851b0",
6
+ "metadata": {},
7
+ "source": [
8
+ "# The Big Project begins!!\n",
9
+ "\n",
10
+ "## The Product Pricer\n",
11
+ "\n",
12
+ "A model that can estimate how much something costs, from its description.\n",
13
+ "\n",
14
+ "## Data Curation Part 1\n",
15
+ "\n",
16
+ "Today we'll begin our scrubbing and curating our dataset by focusing on a subset of the data: Home Appliances.\n",
17
+ "\n",
18
+ "The dataset is here: \n",
19
+ "https://huggingface.co/datasets/McAuley-Lab/Amazon-Reviews-2023\n",
20
+ "\n",
21
+ "And the folder with all the product datasets is here: \n",
22
+ "https://huggingface.co/datasets/McAuley-Lab/Amazon-Reviews-2023/tree/main/raw/meta_categories"
23
+ ]
24
+ },
25
+ {
26
+ "cell_type": "code",
27
+ "execution_count": null,
28
+ "id": "67cedf85-8125-4322-998e-9375fe745597",
29
+ "metadata": {},
30
+ "outputs": [],
31
+ "source": [
32
+ "# imports\n",
33
+ "\n",
34
+ "import os\n",
35
+ "from dotenv import load_dotenv\n",
36
+ "from huggingface_hub import login\n",
37
+ "from datasets import load_dataset, Dataset, DatasetDict\n",
38
+ "import matplotlib.pyplot as plt"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": null,
44
+ "id": "7390a6aa-79cb-4dea-b6d7-de7e4b13e472",
45
+ "metadata": {},
46
+ "outputs": [],
47
+ "source": [
48
+ "# environment\n",
49
+ "\n",
50
+ "load_dotenv(override=True)\n",
51
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
52
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
53
+ "os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
54
+ ]
55
+ },
56
+ {
57
+ "cell_type": "code",
58
+ "execution_count": null,
59
+ "id": "0732274a-aa6a-44fc-aee2-40dc8a8e4451",
60
+ "metadata": {},
61
+ "outputs": [],
62
+ "source": [
63
+ "# Log in to HuggingFace\n",
64
+ "\n",
65
+ "hf_token = os.environ['HF_TOKEN']\n",
66
+ "login(hf_token, add_to_git_credential=True)"
67
+ ]
68
+ },
69
+ {
70
+ "cell_type": "markdown",
71
+ "id": "e7cb2e20-7fac-44c1-8a4b-131dd37ee06e",
72
+ "metadata": {},
73
+ "source": [
74
+ "## One more import - the Item class\n",
75
+ "\n",
76
+ "If you get an error that you need to agree to Meta's terms when you run this, then follow the link it provides you and follow their instructions. You should get approved by Meta within minutes.\n",
77
+ "\n",
78
+ "See the last cell in [this colab](https://colab.research.google.com/drive/1deJO03YZTXUwcq2vzxWbiBhrRuI29Vo8?usp=sharing#scrollTo=FqyF5jZQkIl_) for steps to take if Meta doesn't approve.\n",
79
+ "\n",
80
+ "Any problems - message me or email me! \n",
81
+ "\n",
82
+ "With thanks to student Dr John S. for pointing out that this import needs to come after signing in to HF"
83
+ ]
84
+ },
85
+ {
86
+ "cell_type": "code",
87
+ "execution_count": null,
88
+ "id": "b5521526-0da9-42d7-99e3-f950fab71662",
89
+ "metadata": {},
90
+ "outputs": [],
91
+ "source": [
92
+ "from items import Item"
93
+ ]
94
+ },
95
+ {
96
+ "cell_type": "code",
97
+ "execution_count": null,
98
+ "id": "1adcf323-de9d-4c24-a9c3-d7ae554d06ca",
99
+ "metadata": {},
100
+ "outputs": [],
101
+ "source": [
102
+ "%matplotlib inline"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": null,
108
+ "id": "049885d4-fdfa-4ff0-a932-4a2ed73928e2",
109
+ "metadata": {},
110
+ "outputs": [],
111
+ "source": [
112
+ "# Load in our dataset\n",
113
+ "\n",
114
+ "dataset = load_dataset(\"McAuley-Lab/Amazon-Reviews-2023\", f\"raw_meta_Appliances\", split=\"full\", trust_remote_code=True)"
115
+ ]
116
+ },
117
+ {
118
+ "cell_type": "code",
119
+ "execution_count": null,
120
+ "id": "cde08860-b393-49b8-a620-06a8c0990a64",
121
+ "metadata": {},
122
+ "outputs": [],
123
+ "source": [
124
+ "print(f\"Number of Appliances: {len(dataset):,}\")"
125
+ ]
126
+ },
127
+ {
128
+ "cell_type": "code",
129
+ "execution_count": null,
130
+ "id": "3e29a5ab-ca61-41cc-9b33-22d374681b85",
131
+ "metadata": {},
132
+ "outputs": [],
133
+ "source": [
134
+ "# Investigate a particular datapoint\n",
135
+ "datapoint = dataset[2]\n"
136
+ ]
137
+ },
138
+ {
139
+ "cell_type": "code",
140
+ "execution_count": null,
141
+ "id": "40a4e10f-6710-4780-a95e-6c0030c3fb87",
142
+ "metadata": {},
143
+ "outputs": [],
144
+ "source": [
145
+ "# Investigate\n",
146
+ "\n",
147
+ "print(datapoint[\"title\"])\n",
148
+ "print(datapoint[\"description\"])\n",
149
+ "print(datapoint[\"features\"])\n",
150
+ "print(datapoint[\"details\"])\n",
151
+ "print(datapoint[\"price\"])"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "code",
156
+ "execution_count": null,
157
+ "id": "9d356c6f-b6e8-4e01-98cd-c562d132aafa",
158
+ "metadata": {},
159
+ "outputs": [],
160
+ "source": [
161
+ "# How many have prices?\n",
162
+ "\n",
163
+ "prices = 0\n",
164
+ "for datapoint in dataset:\n",
165
+ " try:\n",
166
+ " price = float(datapoint[\"price\"])\n",
167
+ " if price > 0:\n",
168
+ " prices += 1\n",
169
+ " except ValueError as e:\n",
170
+ " pass\n",
171
+ "\n",
172
+ "print(f\"There are {prices:,} with prices which is {prices/len(dataset)*100:,.1f}%\")"
173
+ ]
174
+ },
175
+ {
176
+ "cell_type": "code",
177
+ "execution_count": null,
178
+ "id": "bd890259-aa25-4097-9524-f91c2bdd719b",
179
+ "metadata": {},
180
+ "outputs": [],
181
+ "source": [
182
+ "# For those with prices, gather the price and the length\n",
183
+ "\n",
184
+ "prices = []\n",
185
+ "lengths = []\n",
186
+ "for datapoint in dataset:\n",
187
+ " try:\n",
188
+ " price = float(datapoint[\"price\"])\n",
189
+ " if price > 0:\n",
190
+ " prices.append(price)\n",
191
+ " contents = datapoint[\"title\"] + str(datapoint[\"description\"]) + str(datapoint[\"features\"]) + str(datapoint[\"details\"])\n",
192
+ " lengths.append(len(contents))\n",
193
+ " except ValueError as e:\n",
194
+ " pass"
195
+ ]
196
+ },
197
+ {
198
+ "cell_type": "code",
199
+ "execution_count": null,
200
+ "id": "89078cb1-9679-4eb0-b295-599b8586bcd1",
201
+ "metadata": {},
202
+ "outputs": [],
203
+ "source": [
204
+ "# Plot the distribution of lengths\n",
205
+ "\n",
206
+ "plt.figure(figsize=(15, 6))\n",
207
+ "plt.title(f\"Lengths: Avg {sum(lengths)/len(lengths):,.0f} and highest {max(lengths):,}\\n\")\n",
208
+ "plt.xlabel('Length (chars)')\n",
209
+ "plt.ylabel('Count')\n",
210
+ "plt.hist(lengths, rwidth=0.7, color=\"lightblue\", bins=range(0, 6000, 100))\n",
211
+ "plt.show()"
212
+ ]
213
+ },
214
+ {
215
+ "cell_type": "code",
216
+ "execution_count": null,
217
+ "id": "c38e0c43-9f7a-450e-a911-c94d37d9b9c3",
218
+ "metadata": {},
219
+ "outputs": [],
220
+ "source": [
221
+ "# Plot the distribution of prices\n",
222
+ "\n",
223
+ "plt.figure(figsize=(15, 6))\n",
224
+ "plt.title(f\"Prices: Avg {sum(prices)/len(prices):,.2f} and highest {max(prices):,}\\n\")\n",
225
+ "plt.xlabel('Price ($)')\n",
226
+ "plt.ylabel('Count')\n",
227
+ "plt.hist(prices, rwidth=0.7, color=\"orange\", bins=range(0, 1000, 10))\n",
228
+ "plt.show()"
229
+ ]
230
+ },
231
+ {
232
+ "cell_type": "code",
233
+ "execution_count": null,
234
+ "id": "eabc7c61-0cd2-41f4-baa1-b85400bbf87f",
235
+ "metadata": {},
236
+ "outputs": [],
237
+ "source": [
238
+ "# So what is this item??\n",
239
+ "\n",
240
+ "for datapoint in dataset:\n",
241
+ " try:\n",
242
+ " price = float(datapoint[\"price\"])\n",
243
+ " if price > 21000:\n",
244
+ " print(datapoint['title'])\n",
245
+ " except ValueError as e:\n",
246
+ " pass"
247
+ ]
248
+ },
249
+ {
250
+ "cell_type": "markdown",
251
+ "id": "3668ae25-3461-4e6e-9ccb-221c1925a497",
252
+ "metadata": {},
253
+ "source": [
254
+ "This is the closest I can find - looks like it's going at a bargain price!!\n",
255
+ "\n",
256
+ "https://www.amazon.com/TurboChef-Electric-Countertop-Microwave-Convection/dp/B01D05U9NO/"
257
+ ]
258
+ },
259
+ {
260
+ "cell_type": "markdown",
261
+ "id": "a0d02f58-23f6-4f81-a779-7c0555afd13d",
262
+ "metadata": {},
263
+ "source": [
264
+ "## Now it's time to curate our dataset\n",
265
+ "\n",
266
+ "We select items that cost between 1 and 999 USD\n",
267
+ "\n",
268
+ "We will be create Item instances, which truncate the text to fit within 180 tokens using the right Tokenizer\n",
269
+ "\n",
270
+ "And will create a prompt to be used during Training.\n",
271
+ "\n",
272
+ "Items will be rejected if they don't have sufficient characters.\n",
273
+ "\n",
274
+ "## But why 180 tokens??\n",
275
+ "\n",
276
+ "A student asked me a great question - why are we truncating to 180 tokens? How did we determine that number? (Thank you Moataz A. for the excellent question).\n",
277
+ "\n",
278
+ "The answer: this is an example of a \"hyper-parameter\". In other words, it's basically trial and error! We want a sufficiently large number of tokens so that we have enough useful information to gauge the price. But we also want to keep the number low so that we can train efficiently. You'll see this in action in Week 7.\n",
279
+ "\n",
280
+ "I started with a number that seemed reasonable, and experimented with a few variations before settling on 180. If you have time, you should do the same! You might find that you can beat my results by finding a better balance. This kind of trial-and-error might sound a bit unsatisfactory, but it's a crucial part of the data science R&D process.\n",
281
+ "\n",
282
+ "There's another interesting reason why we might favor a lower number of tokens in the training data. When we eventually get to use our model at inference time, we'll want to provide new products and have it estimate a price. And we'll be using short descriptions of products - like 1-2 sentences. For best performance, we should size our training data to be similar to the inputs we will provide at inference time.\n",
283
+ "\n",
284
+ "## But I see in items.py it constrains inputs to 160 tokens?\n",
285
+ "\n",
286
+ "Another great question from Moataz A.! The description of the products is limited to 160 tokens because we add some more text before and after the description to turn it into a prompt. That brings it to around 180 tokens in total.\n",
287
+ "\n"
288
+ ]
289
+ },
290
+ {
291
+ "cell_type": "code",
292
+ "execution_count": null,
293
+ "id": "430b432f-b769-41da-9506-a238cb5cf1b6",
294
+ "metadata": {},
295
+ "outputs": [],
296
+ "source": [
297
+ "# Create an Item object for each with a price\n",
298
+ "\n",
299
+ "items = []\n",
300
+ "for datapoint in dataset:\n",
301
+ " try:\n",
302
+ " price = float(datapoint[\"price\"])\n",
303
+ " if price > 0:\n",
304
+ " item = Item(datapoint, price)\n",
305
+ " if item.include:\n",
306
+ " items.append(item)\n",
307
+ " except ValueError as e:\n",
308
+ " pass\n",
309
+ "\n",
310
+ "print(f\"There are {len(items):,} items\")"
311
+ ]
312
+ },
313
+ {
314
+ "cell_type": "code",
315
+ "execution_count": null,
316
+ "id": "0d570794-6f1d-462e-b567-a46bae3556a1",
317
+ "metadata": {},
318
+ "outputs": [],
319
+ "source": [
320
+ "# Look at the first item\n",
321
+ "\n",
322
+ "items[1]"
323
+ ]
324
+ },
325
+ {
326
+ "cell_type": "code",
327
+ "execution_count": null,
328
+ "id": "70219e99-22cc-4e08-9121-51f9707caef0",
329
+ "metadata": {},
330
+ "outputs": [],
331
+ "source": [
332
+ "# Investigate the prompt that will be used during training - the model learns to complete this\n",
333
+ "\n",
334
+ "print(items[100].prompt)"
335
+ ]
336
+ },
337
+ {
338
+ "cell_type": "code",
339
+ "execution_count": null,
340
+ "id": "d9998b8d-d746-4541-9ac2-701108e0e8fb",
341
+ "metadata": {},
342
+ "outputs": [],
343
+ "source": [
344
+ "# Investigate the prompt that will be used during testing - the model has to complete this\n",
345
+ "\n",
346
+ "print(items[100].test_prompt())"
347
+ ]
348
+ },
349
+ {
350
+ "cell_type": "code",
351
+ "execution_count": null,
352
+ "id": "7a116369-335a-412b-b70c-2add6675c2e3",
353
+ "metadata": {},
354
+ "outputs": [],
355
+ "source": [
356
+ "# Plot the distribution of token counts\n",
357
+ "\n",
358
+ "tokens = [item.token_count for item in items]\n",
359
+ "plt.figure(figsize=(15, 6))\n",
360
+ "plt.title(f\"Token counts: Avg {sum(tokens)/len(tokens):,.1f} and highest {max(tokens):,}\\n\")\n",
361
+ "plt.xlabel('Length (tokens)')\n",
362
+ "plt.ylabel('Count')\n",
363
+ "plt.hist(tokens, rwidth=0.7, color=\"green\", bins=range(0, 300, 10))\n",
364
+ "plt.show()"
365
+ ]
366
+ },
367
+ {
368
+ "cell_type": "code",
369
+ "execution_count": null,
370
+ "id": "8d1744aa-71e7-435e-876e-91f06583211a",
371
+ "metadata": {},
372
+ "outputs": [],
373
+ "source": [
374
+ "# Plot the distribution of prices\n",
375
+ "\n",
376
+ "prices = [item.price for item in items]\n",
377
+ "plt.figure(figsize=(15, 6))\n",
378
+ "plt.title(f\"Prices: Avg {sum(prices)/len(prices):,.1f} and highest {max(prices):,}\\n\")\n",
379
+ "plt.xlabel('Price ($)')\n",
380
+ "plt.ylabel('Count')\n",
381
+ "plt.hist(prices, rwidth=0.7, color=\"purple\", bins=range(0, 300, 10))\n",
382
+ "plt.show()"
383
+ ]
384
+ },
385
+ {
386
+ "cell_type": "markdown",
387
+ "id": "2b58dc61-747f-46f7-b9e0-c205db4f3e5e",
388
+ "metadata": {},
389
+ "source": [
390
+ "## Sidenote\n",
391
+ "\n",
392
+ "If you like the variety of colors that matplotlib can use in its charts, you should bookmark this:\n",
393
+ "\n",
394
+ "https://matplotlib.org/stable/gallery/color/named_colors.html\n",
395
+ "\n",
396
+ "## Todos for you:\n",
397
+ "\n",
398
+ "- Review the Item class and check you're comfortable with it\n",
399
+ "- Examine some Item objects, look at the training prompt with `item.prompt` and test prompt with `item.test_prompt()`\n",
400
+ "- Make some more histograms to better understand the data\n",
401
+ "\n",
402
+ "## Next time we will combine with many other types of product\n",
403
+ "\n",
404
+ "Like Electronics and Automotive. This will give us a massive dataset, and we can then be picky about choosing a subset that will be most suitable for training."
405
+ ]
406
+ },
407
+ {
408
+ "cell_type": "code",
409
+ "execution_count": null,
410
+ "id": "01401283-d111-40a7-96e5-0ca05bb20857",
411
+ "metadata": {},
412
+ "outputs": [],
413
+ "source": []
414
+ }
415
+ ],
416
+ "metadata": {
417
+ "kernelspec": {
418
+ "display_name": "Python 3 (ipykernel)",
419
+ "language": "python",
420
+ "name": "python3"
421
+ },
422
+ "language_info": {
423
+ "codemirror_mode": {
424
+ "name": "ipython",
425
+ "version": 3
426
+ },
427
+ "file_extension": ".py",
428
+ "mimetype": "text/x-python",
429
+ "name": "python",
430
+ "nbconvert_exporter": "python",
431
+ "pygments_lexer": "ipython3",
432
+ "version": "3.11.11"
433
+ }
434
+ },
435
+ "nbformat": 4,
436
+ "nbformat_minor": 5
437
+ }
week6/.ipynb_checkpoints/day2-checkpoint.ipynb ADDED
@@ -0,0 +1,632 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "28a0673e-96b5-43f2-8a8b-bd033bf851b0",
6
+ "metadata": {},
7
+ "source": [
8
+ "# The Product Pricer Continued\n",
9
+ "\n",
10
+ "A model that can estimate how much something costs, from its description.\n",
11
+ "\n",
12
+ "## Data Curation Part 2\n",
13
+ "\n",
14
+ "Today we'll extend our dataset to a greater coverage, and craft it into an excellent dataset for training. \n",
15
+ "Data curation can seem less exciting than other things we work on, but it's a crucial part of the LLM engineers' responsibility and an important craft to hone, so that you can build your own commercial solutions with high quality datasets.\n",
16
+ "\n",
17
+ "The dataset is here: \n",
18
+ "https://huggingface.co/datasets/McAuley-Lab/Amazon-Reviews-2023\n",
19
+ "\n",
20
+ "And the folder with all the product datasets is here: \n",
21
+ "https://huggingface.co/datasets/McAuley-Lab/Amazon-Reviews-2023/tree/main/raw/meta_categories\n",
22
+ "\n",
23
+ "## Important Note - read me first please\n",
24
+ "\n",
25
+ "We are about to craft a massive dataset of 400,000 items covering multiple types of product. In Week 7 we will be using this data to train our own model. It's a pretty big dataset, and depending on the GPU you select, training could take 20+ hours. It will be really good fun, but it could cost a few dollars in compute units.\n",
26
+ "\n",
27
+ "As an alternative, if you want to keep things quick & low cost, you can work with a smaller dataset focused only on Home Appliances. You'll be able to cover the same learning points; the results will be good -- not quite as good as the full dataset, but still pretty amazing! If you'd prefer to do this, I've set up an alternative jupyter notebook in this folder called `lite.ipynb` that you should use in place of this one.\n",
28
+ "\n",
29
+ "Also, if you'd prefer, you can shortcut running all this data curation by downloading the pickle files that we save in the last cell. The pickle files are available here: https://drive.google.com/drive/folders/1f_IZGybvs9o0J5sb3xmtTEQB3BXllzrW"
30
+ ]
31
+ },
32
+ {
33
+ "cell_type": "code",
34
+ "execution_count": null,
35
+ "id": "67cedf85-8125-4322-998e-9375fe745597",
36
+ "metadata": {},
37
+ "outputs": [],
38
+ "source": [
39
+ "# imports\n",
40
+ "\n",
41
+ "import os\n",
42
+ "import random\n",
43
+ "from dotenv import load_dotenv\n",
44
+ "from huggingface_hub import login\n",
45
+ "from datasets import load_dataset, Dataset, DatasetDict\n",
46
+ "from loaders import ItemLoader\n",
47
+ "import matplotlib.pyplot as plt\n",
48
+ "from collections import Counter, defaultdict\n",
49
+ "import numpy as np\n",
50
+ "import pickle"
51
+ ]
52
+ },
53
+ {
54
+ "cell_type": "code",
55
+ "execution_count": null,
56
+ "id": "7390a6aa-79cb-4dea-b6d7-de7e4b13e472",
57
+ "metadata": {},
58
+ "outputs": [],
59
+ "source": [
60
+ "# environment\n",
61
+ "\n",
62
+ "load_dotenv(override=True)\n",
63
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
64
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
65
+ "os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "execution_count": null,
71
+ "id": "0732274a-aa6a-44fc-aee2-40dc8a8e4451",
72
+ "metadata": {},
73
+ "outputs": [],
74
+ "source": [
75
+ "# Log in to HuggingFace\n",
76
+ "\n",
77
+ "hf_token = os.environ['HF_TOKEN']\n",
78
+ "login(hf_token, add_to_git_credential=True)"
79
+ ]
80
+ },
81
+ {
82
+ "cell_type": "code",
83
+ "execution_count": null,
84
+ "id": "6746144c-2e19-485a-8086-368c144722b4",
85
+ "metadata": {},
86
+ "outputs": [],
87
+ "source": [
88
+ "# One more import after HF login\n",
89
+ "\n",
90
+ "from items import Item"
91
+ ]
92
+ },
93
+ {
94
+ "cell_type": "code",
95
+ "execution_count": null,
96
+ "id": "1adcf323-de9d-4c24-a9c3-d7ae554d06ca",
97
+ "metadata": {},
98
+ "outputs": [],
99
+ "source": [
100
+ "%matplotlib inline"
101
+ ]
102
+ },
103
+ {
104
+ "cell_type": "markdown",
105
+ "id": "01065d69-765c-42c8-9f90-68b8c8754068",
106
+ "metadata": {},
107
+ "source": [
108
+ "## The ItemLoader code\n",
109
+ "\n",
110
+ "Look in loaders.py - there's some useful code to make life easier for us"
111
+ ]
112
+ },
113
+ {
114
+ "cell_type": "code",
115
+ "execution_count": null,
116
+ "id": "049885d4-fdfa-4ff0-a932-4a2ed73928e2",
117
+ "metadata": {},
118
+ "outputs": [],
119
+ "source": [
120
+ "# Load in the same dataset as last time\n",
121
+ "\n",
122
+ "items = ItemLoader(\"Appliances\").load()"
123
+ ]
124
+ },
125
+ {
126
+ "cell_type": "code",
127
+ "execution_count": null,
128
+ "id": "ffba41b5-ddb6-4359-9790-9b2db900eee1",
129
+ "metadata": {},
130
+ "outputs": [],
131
+ "source": [
132
+ "# Look for a familiar item..\n",
133
+ "print(items[1].prompt)"
134
+ ]
135
+ },
136
+ {
137
+ "cell_type": "markdown",
138
+ "id": "e2b6dc50-ac5c-4cf2-af2e-968ed8ef86d7",
139
+ "metadata": {},
140
+ "source": [
141
+ "## Now to SCALE UP\n",
142
+ "\n",
143
+ "Let's look at all datasets of all the items that you might find in a large home retail store - electrical, electronic, office and related, but not clothes / beauty / books."
144
+ ]
145
+ },
146
+ {
147
+ "cell_type": "code",
148
+ "execution_count": null,
149
+ "id": "d1d06cd3-f3c2-44f0-a9f2-13b54ff8be5c",
150
+ "metadata": {},
151
+ "outputs": [],
152
+ "source": [
153
+ "dataset_names = [\n",
154
+ " \"Automotive\",\n",
155
+ " \"Electronics\",\n",
156
+ " \"Office_Products\",\n",
157
+ " \"Tools_and_Home_Improvement\",\n",
158
+ " \"Cell_Phones_and_Accessories\",\n",
159
+ " \"Toys_and_Games\",\n",
160
+ " \"Appliances\",\n",
161
+ " \"Musical_Instruments\",\n",
162
+ "]"
163
+ ]
164
+ },
165
+ {
166
+ "cell_type": "code",
167
+ "execution_count": null,
168
+ "id": "aa8fd0f0-509a-4298-8fcc-e499a061e1be",
169
+ "metadata": {},
170
+ "outputs": [],
171
+ "source": [
172
+ "items = []\n",
173
+ "for dataset_name in dataset_names:\n",
174
+ " loader = ItemLoader(dataset_name)\n",
175
+ " items.extend(loader.load())\n",
176
+ "\n",
177
+ "# Now, time for a coffee break!!\n",
178
+ "# By the way, I put the biggest datasets first.. it gets faster."
179
+ ]
180
+ },
181
+ {
182
+ "cell_type": "code",
183
+ "execution_count": null,
184
+ "id": "3e29a5ab-ca61-41cc-9b33-22d374681b85",
185
+ "metadata": {},
186
+ "outputs": [],
187
+ "source": [
188
+ "print(f\"A grand total of {len(items):,} items\")"
189
+ ]
190
+ },
191
+ {
192
+ "cell_type": "code",
193
+ "execution_count": null,
194
+ "id": "89078cb1-9679-4eb0-b295-599b8586bcd1",
195
+ "metadata": {},
196
+ "outputs": [],
197
+ "source": [
198
+ "# Plot the distribution of token counts again\n",
199
+ "\n",
200
+ "tokens = [item.token_count for item in items]\n",
201
+ "plt.figure(figsize=(15, 6))\n",
202
+ "plt.title(f\"Token counts: Avg {sum(tokens)/len(tokens):,.1f} and highest {max(tokens):,}\\n\")\n",
203
+ "plt.xlabel('Length (tokens)')\n",
204
+ "plt.ylabel('Count')\n",
205
+ "plt.hist(tokens, rwidth=0.7, color=\"skyblue\", bins=range(0, 300, 10))\n",
206
+ "plt.show()"
207
+ ]
208
+ },
209
+ {
210
+ "cell_type": "code",
211
+ "execution_count": null,
212
+ "id": "c38e0c43-9f7a-450e-a911-c94d37d9b9c3",
213
+ "metadata": {},
214
+ "outputs": [],
215
+ "source": [
216
+ "# Plot the distribution of prices\n",
217
+ "\n",
218
+ "prices = [item.price for item in items]\n",
219
+ "plt.figure(figsize=(15, 6))\n",
220
+ "plt.title(f\"Prices: Avg {sum(prices)/len(prices):,.1f} and highest {max(prices):,}\\n\")\n",
221
+ "plt.xlabel('Price ($)')\n",
222
+ "plt.ylabel('Count')\n",
223
+ "plt.hist(prices, rwidth=0.7, color=\"blueviolet\", bins=range(0, 1000, 10))\n",
224
+ "plt.show()"
225
+ ]
226
+ },
227
+ {
228
+ "cell_type": "code",
229
+ "execution_count": null,
230
+ "id": "eabc7c61-0cd2-41f4-baa1-b85400bbf87f",
231
+ "metadata": {},
232
+ "outputs": [],
233
+ "source": [
234
+ "category_counts = Counter()\n",
235
+ "for item in items:\n",
236
+ " category_counts[item.category]+=1\n",
237
+ "\n",
238
+ "categories = category_counts.keys()\n",
239
+ "counts = [category_counts[category] for category in categories]\n",
240
+ "\n",
241
+ "# Bar chart by category\n",
242
+ "plt.figure(figsize=(15, 6))\n",
243
+ "plt.bar(categories, counts, color=\"goldenrod\")\n",
244
+ "plt.title('How many in each category')\n",
245
+ "plt.xlabel('Categories')\n",
246
+ "plt.ylabel('Count')\n",
247
+ "\n",
248
+ "plt.xticks(rotation=30, ha='right')\n",
249
+ "\n",
250
+ "# Add value labels on top of each bar\n",
251
+ "for i, v in enumerate(counts):\n",
252
+ " plt.text(i, v, f\"{v:,}\", ha='center', va='bottom')\n",
253
+ "\n",
254
+ "# Display the chart\n",
255
+ "plt.show()"
256
+ ]
257
+ },
258
+ {
259
+ "cell_type": "markdown",
260
+ "id": "e5b6e987-83ba-4262-a082-57c6b0741062",
261
+ "metadata": {},
262
+ "source": [
263
+ "# Objective\n",
264
+ "\n",
265
+ "Craft a dataset which is more balanced in terms of prices. Less heavily scewed to cheap items, with an average that's higher than $60. Try to balance out the categories - fewer Automotive items."
266
+ ]
267
+ },
268
+ {
269
+ "cell_type": "code",
270
+ "execution_count": null,
271
+ "id": "3b9424c1-44e0-499a-b45e-a35246655469",
272
+ "metadata": {},
273
+ "outputs": [],
274
+ "source": [
275
+ "# Create a dict with a key of each price from $1 to $999\n",
276
+ "# And in the value, put a list of items with that price (to nearest round number)\n",
277
+ "\n",
278
+ "slots = defaultdict(list)\n",
279
+ "for item in items:\n",
280
+ " slots[round(item.price)].append(item)"
281
+ ]
282
+ },
283
+ {
284
+ "cell_type": "code",
285
+ "execution_count": null,
286
+ "id": "7805a7f1-4ad8-48f6-bea3-d64b64894804",
287
+ "metadata": {},
288
+ "outputs": [],
289
+ "source": [
290
+ "# Create a dataset called \"sample\" which tries to more evenly take from the range of prices\n",
291
+ "# And gives more weight to items from categories other than Automotive\n",
292
+ "# Set random seed for reproducibility\n",
293
+ "\n",
294
+ "np.random.seed(42)\n",
295
+ "random.seed(42)\n",
296
+ "sample = []\n",
297
+ "for i in range(1, 1000):\n",
298
+ " slot = slots[i]\n",
299
+ " if i>=240:\n",
300
+ " sample.extend(slot)\n",
301
+ " elif len(slot) <= 1200:\n",
302
+ " sample.extend(slot)\n",
303
+ " else:\n",
304
+ " weights = np.array([1 if item.category=='Automotive' else 5 for item in slot])\n",
305
+ " weights = weights / np.sum(weights)\n",
306
+ " selected_indices = np.random.choice(len(slot), size=1200, replace=False, p=weights)\n",
307
+ " selected = [slot[i] for i in selected_indices]\n",
308
+ " sample.extend(selected)\n",
309
+ "\n",
310
+ "print(f\"There are {len(sample):,} items in the sample\")"
311
+ ]
312
+ },
313
+ {
314
+ "cell_type": "code",
315
+ "execution_count": null,
316
+ "id": "430b432f-b769-41da-9506-a238cb5cf1b6",
317
+ "metadata": {},
318
+ "outputs": [],
319
+ "source": [
320
+ "# Plot the distribution of prices in sample\n",
321
+ "\n",
322
+ "prices = [float(item.price) for item in sample]\n",
323
+ "plt.figure(figsize=(15, 10))\n",
324
+ "plt.title(f\"Avg {sum(prices)/len(prices):.2f} and highest {max(prices):,.2f}\\n\")\n",
325
+ "plt.xlabel('Price ($)')\n",
326
+ "plt.ylabel('Count')\n",
327
+ "plt.hist(prices, rwidth=0.7, color=\"darkblue\", bins=range(0, 1000, 10))\n",
328
+ "plt.show()"
329
+ ]
330
+ },
331
+ {
332
+ "cell_type": "code",
333
+ "execution_count": null,
334
+ "id": "0d570794-6f1d-462e-b567-a46bae3556a1",
335
+ "metadata": {},
336
+ "outputs": [],
337
+ "source": [
338
+ "# OK, we did well in terms of raising the average price and having a smooth-ish population of prices\n",
339
+ "# Let's see the categories\n",
340
+ "\n",
341
+ "category_counts = Counter()\n",
342
+ "for item in sample:\n",
343
+ " category_counts[item.category]+=1\n",
344
+ "\n",
345
+ "categories = category_counts.keys()\n",
346
+ "counts = [category_counts[category] for category in categories]\n",
347
+ "\n",
348
+ "# Create bar chart\n",
349
+ "plt.figure(figsize=(15, 6))\n",
350
+ "plt.bar(categories, counts, color=\"lightgreen\")\n",
351
+ "\n",
352
+ "# Customize the chart\n",
353
+ "plt.title('How many in each category')\n",
354
+ "plt.xlabel('Categories')\n",
355
+ "plt.ylabel('Count')\n",
356
+ "\n",
357
+ "plt.xticks(rotation=30, ha='right')\n",
358
+ "\n",
359
+ "# Add value labels on top of each bar\n",
360
+ "for i, v in enumerate(counts):\n",
361
+ " plt.text(i, v, f\"{v:,}\", ha='center', va='bottom')\n",
362
+ "\n",
363
+ "# Display the chart\n",
364
+ "plt.show()"
365
+ ]
366
+ },
367
+ {
368
+ "cell_type": "code",
369
+ "execution_count": null,
370
+ "id": "6609d77c-3e0a-4679-9129-c7cdc3273070",
371
+ "metadata": {},
372
+ "outputs": [],
373
+ "source": [
374
+ "# Automotive still in the lead, but improved somewhat\n",
375
+ "# For another perspective, let's look at a pie\n",
376
+ "\n",
377
+ "plt.figure(figsize=(12, 10))\n",
378
+ "plt.pie(counts, labels=categories, autopct='%1.0f%%', startangle=90)\n",
379
+ "\n",
380
+ "# Add a circle at the center to create a donut chart (optional)\n",
381
+ "centre_circle = plt.Circle((0,0), 0.70, fc='white')\n",
382
+ "fig = plt.gcf()\n",
383
+ "fig.gca().add_artist(centre_circle)\n",
384
+ "plt.title('Categories')\n",
385
+ "\n",
386
+ "# Equal aspect ratio ensures that pie is drawn as a circle\n",
387
+ "plt.axis('equal') \n",
388
+ "\n",
389
+ "plt.show()"
390
+ ]
391
+ },
392
+ {
393
+ "cell_type": "markdown",
394
+ "id": "ac046cc1-2717-415b-96ad-b73b2950d235",
395
+ "metadata": {},
396
+ "source": [
397
+ "# Dataset Curated!\n",
398
+ "\n",
399
+ "We've crafted an excellent dataset.\n",
400
+ "\n",
401
+ "Let's do some final checks"
402
+ ]
403
+ },
404
+ {
405
+ "cell_type": "code",
406
+ "execution_count": null,
407
+ "id": "70219e99-22cc-4e08-9121-51f9707caef0",
408
+ "metadata": {},
409
+ "outputs": [],
410
+ "source": [
411
+ "# How does the price vary with the character count of the prompt?\n",
412
+ "\n",
413
+ "sizes = [len(item.prompt) for item in sample]\n",
414
+ "prices = [item.price for item in sample]\n",
415
+ "\n",
416
+ "# Create the scatter plot\n",
417
+ "plt.figure(figsize=(15, 8))\n",
418
+ "plt.scatter(sizes, prices, s=0.2, color=\"red\")\n",
419
+ "\n",
420
+ "# Add labels and title\n",
421
+ "plt.xlabel('Size')\n",
422
+ "plt.ylabel('Price')\n",
423
+ "plt.title('Is there a simple correlation?')\n",
424
+ "\n",
425
+ "# Display the plot\n",
426
+ "plt.show()"
427
+ ]
428
+ },
429
+ {
430
+ "cell_type": "code",
431
+ "execution_count": null,
432
+ "id": "30ae1453-b9fc-40db-8310-65d850c4b1da",
433
+ "metadata": {},
434
+ "outputs": [],
435
+ "source": [
436
+ "def report(item):\n",
437
+ " prompt = item.prompt\n",
438
+ " tokens = Item.tokenizer.encode(item.prompt)\n",
439
+ " print(prompt)\n",
440
+ " print(tokens[-10:])\n",
441
+ " print(Item.tokenizer.batch_decode(tokens[-10:]))"
442
+ ]
443
+ },
444
+ {
445
+ "cell_type": "code",
446
+ "execution_count": null,
447
+ "id": "d9998b8d-d746-4541-9ac2-701108e0e8fb",
448
+ "metadata": {},
449
+ "outputs": [],
450
+ "source": [
451
+ "report(sample[398000])"
452
+ ]
453
+ },
454
+ {
455
+ "cell_type": "markdown",
456
+ "id": "7aa0a3fc-d2fe-4e6e-8fdb-96913df2f588",
457
+ "metadata": {},
458
+ "source": [
459
+ "## Observation\n",
460
+ "\n",
461
+ "An interesting thing about the Llama tokenizer is that every number from 1 to 999 gets mapped to 1 token, much as we saw with gpt-4o. The same is not true of qwen2, gemma and phi3, which all map individual digits to tokens. This does turn out to be a bit useful for our project, although it's not an essential requirement."
462
+ ]
463
+ },
464
+ {
465
+ "cell_type": "markdown",
466
+ "id": "0f03c0ee-3103-4603-af5c-b484884a3aa2",
467
+ "metadata": {},
468
+ "source": [
469
+ "# Finally\n",
470
+ "\n",
471
+ "It's time to break down our data into a training, test and validation dataset.\n",
472
+ "\n",
473
+ "It's typical to use 5%-10% of your data for testing purposes, but actually we have far more than we need at this point. We'll take 400,000 points for training, and we'll reserve 2,000 for testing, although we won't use all of them.\n"
474
+ ]
475
+ },
476
+ {
477
+ "cell_type": "code",
478
+ "execution_count": null,
479
+ "id": "3b163ca2-18ef-4c26-8e9d-88eb55f114f6",
480
+ "metadata": {},
481
+ "outputs": [],
482
+ "source": [
483
+ "random.seed(42)\n",
484
+ "random.shuffle(sample)\n",
485
+ "train = sample[:400_000]\n",
486
+ "test = sample[400_000:402_000]\n",
487
+ "print(f\"Divided into a training set of {len(train):,} items and test set of {len(test):,} items\")"
488
+ ]
489
+ },
490
+ {
491
+ "cell_type": "code",
492
+ "execution_count": null,
493
+ "id": "299b9816-8885-4798-829a-69d66d60eb01",
494
+ "metadata": {},
495
+ "outputs": [],
496
+ "source": [
497
+ "print(train[0].prompt)"
498
+ ]
499
+ },
500
+ {
501
+ "cell_type": "code",
502
+ "execution_count": null,
503
+ "id": "97222da3-9f2c-4d15-a5cd-5e5f8dbde6cc",
504
+ "metadata": {},
505
+ "outputs": [],
506
+ "source": [
507
+ "print(test[0].test_prompt())"
508
+ ]
509
+ },
510
+ {
511
+ "cell_type": "code",
512
+ "execution_count": null,
513
+ "id": "7a116369-335a-412b-b70c-2add6675c2e3",
514
+ "metadata": {},
515
+ "outputs": [],
516
+ "source": [
517
+ "# Plot the distribution of prices in the first 250 test points\n",
518
+ "\n",
519
+ "prices = [float(item.price) for item in test[:250]]\n",
520
+ "plt.figure(figsize=(15, 6))\n",
521
+ "plt.title(f\"Avg {sum(prices)/len(prices):.2f} and highest {max(prices):,.2f}\\n\")\n",
522
+ "plt.xlabel('Price ($)')\n",
523
+ "plt.ylabel('Count')\n",
524
+ "plt.hist(prices, rwidth=0.7, color=\"darkblue\", bins=range(0, 1000, 10))\n",
525
+ "plt.show()"
526
+ ]
527
+ },
528
+ {
529
+ "cell_type": "markdown",
530
+ "id": "d522d752-6f66-4786-a4dc-8ef51842558c",
531
+ "metadata": {},
532
+ "source": [
533
+ "# Finally - upload your brand new dataset\n",
534
+ "\n",
535
+ "Convert to prompts and upload to HuggingFace hub"
536
+ ]
537
+ },
538
+ {
539
+ "cell_type": "code",
540
+ "execution_count": null,
541
+ "id": "fa11b3e5-fcf4-4efc-a573-f6f67fec3e73",
542
+ "metadata": {},
543
+ "outputs": [],
544
+ "source": [
545
+ "train_prompts = [item.prompt for item in train]\n",
546
+ "train_prices = [item.price for item in train]\n",
547
+ "test_prompts = [item.test_prompt() for item in test]\n",
548
+ "test_prices = [item.price for item in test]"
549
+ ]
550
+ },
551
+ {
552
+ "cell_type": "code",
553
+ "execution_count": null,
554
+ "id": "b020ab1b-7153-4e5f-b8a3-d5bc2fafb6df",
555
+ "metadata": {},
556
+ "outputs": [],
557
+ "source": [
558
+ "# Create a Dataset from the lists\n",
559
+ "\n",
560
+ "train_dataset = Dataset.from_dict({\"text\": train_prompts, \"price\": train_prices})\n",
561
+ "test_dataset = Dataset.from_dict({\"text\": test_prompts, \"price\": test_prices})\n",
562
+ "dataset = DatasetDict({\n",
563
+ " \"train\": train_dataset,\n",
564
+ " \"test\": test_dataset\n",
565
+ "})"
566
+ ]
567
+ },
568
+ {
569
+ "cell_type": "code",
570
+ "execution_count": null,
571
+ "id": "17639641-fb55-44e2-a463-b0b394d00f32",
572
+ "metadata": {},
573
+ "outputs": [],
574
+ "source": [
575
+ "# Uncomment these lines if you're ready to push to the hub, and replace my name with your HF username\n",
576
+ "\n",
577
+ "# HF_USER = \"ed-donner\"\n",
578
+ "# DATASET_NAME = f\"{HF_USER}/pricer-data\"\n",
579
+ "# dataset.push_to_hub(DATASET_NAME, private=True)"
580
+ ]
581
+ },
582
+ {
583
+ "cell_type": "code",
584
+ "execution_count": null,
585
+ "id": "b85733ba-d165-4f07-b055-46803543edfe",
586
+ "metadata": {},
587
+ "outputs": [],
588
+ "source": [
589
+ "# One more thing!\n",
590
+ "# Let's pickle the training and test dataset so we don't have to execute all this code next time!\n",
591
+ "\n",
592
+ "with open('train.pkl', 'wb') as file:\n",
593
+ " pickle.dump(train, file)\n",
594
+ "\n",
595
+ "with open('test.pkl', 'wb') as file:\n",
596
+ " pickle.dump(test, file)"
597
+ ]
598
+ },
599
+ {
600
+ "cell_type": "markdown",
601
+ "id": "2b58dc61-747f-46f7-b9e0-c205db4f3e5e",
602
+ "metadata": {},
603
+ "source": [
604
+ "## Todos for you:\n",
605
+ "\n",
606
+ "- Investigate the dataset more!\n",
607
+ "- Confirm that the tokenizer tokenizes all 3 digit prices into 1 token"
608
+ ]
609
+ }
610
+ ],
611
+ "metadata": {
612
+ "kernelspec": {
613
+ "display_name": "Python 3 (ipykernel)",
614
+ "language": "python",
615
+ "name": "python3"
616
+ },
617
+ "language_info": {
618
+ "codemirror_mode": {
619
+ "name": "ipython",
620
+ "version": 3
621
+ },
622
+ "file_extension": ".py",
623
+ "mimetype": "text/x-python",
624
+ "name": "python",
625
+ "nbconvert_exporter": "python",
626
+ "pygments_lexer": "ipython3",
627
+ "version": "3.11.11"
628
+ }
629
+ },
630
+ "nbformat": 4,
631
+ "nbformat_minor": 5
632
+ }
week6/.ipynb_checkpoints/day3-checkpoint.ipynb ADDED
@@ -0,0 +1,926 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "db8736a7-ed94-441c-9556-831fa57b5a10",
6
+ "metadata": {},
7
+ "source": [
8
+ "# The Product Pricer Continued\n",
9
+ "\n",
10
+ "A model that can estimate how much something costs, from its description.\n",
11
+ "\n",
12
+ "## Baseline Models\n",
13
+ "\n",
14
+ "Today we work on the simplest models to act as a starting point that we will beat."
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": null,
20
+ "id": "681c717b-4c24-4ac3-a5f3-3c5881d6e70a",
21
+ "metadata": {},
22
+ "outputs": [],
23
+ "source": [
24
+ "# imports\n",
25
+ "\n",
26
+ "import os\n",
27
+ "import math\n",
28
+ "import json\n",
29
+ "import random\n",
30
+ "from dotenv import load_dotenv\n",
31
+ "from huggingface_hub import login\n",
32
+ "import matplotlib.pyplot as plt\n",
33
+ "import numpy as np\n",
34
+ "import pickle\n",
35
+ "from collections import Counter"
36
+ ]
37
+ },
38
+ {
39
+ "cell_type": "code",
40
+ "execution_count": null,
41
+ "id": "933b6e75-3661-4f30-b0b5-c28d04e3748e",
42
+ "metadata": {},
43
+ "outputs": [],
44
+ "source": [
45
+ "# More imports for our traditional machine learning\n",
46
+ "\n",
47
+ "import pandas as pd\n",
48
+ "import numpy as np\n",
49
+ "from sklearn.linear_model import LinearRegression\n",
50
+ "from sklearn.metrics import mean_squared_error, r2_score\n",
51
+ "from sklearn.preprocessing import StandardScaler"
52
+ ]
53
+ },
54
+ {
55
+ "cell_type": "markdown",
56
+ "id": "b3c87c11-8dbe-4b8c-8989-01e3d3a60026",
57
+ "metadata": {},
58
+ "source": [
59
+ "## NLP imports\n",
60
+ "\n",
61
+ "In the next cell, we have more imports for our NLP related machine learning. \n",
62
+ "If the gensim import gives you an error like \"Cannot import name 'triu' from 'scipy.linalg' then please run in another cell: \n",
63
+ "`!pip install \"scipy<1.13\"` \n",
64
+ "As described on StackOverflow [here](https://stackoverflow.com/questions/78279136/importerror-cannot-import-name-triu-from-scipy-linalg-when-importing-gens). \n",
65
+ "Many thanks to students Arnaldo G and Ard V for sorting this."
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "execution_count": null,
71
+ "id": "42cf33b7-7abd-44ba-9780-c156b70473b5",
72
+ "metadata": {},
73
+ "outputs": [],
74
+ "source": [
75
+ "# NLP related imports\n",
76
+ "\n",
77
+ "from sklearn.feature_extraction.text import CountVectorizer\n",
78
+ "from gensim.models import Word2Vec\n",
79
+ "from gensim.utils import simple_preprocess"
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "code",
84
+ "execution_count": null,
85
+ "id": "a1ac3ec0-183c-4a12-920b-b06397f86815",
86
+ "metadata": {},
87
+ "outputs": [],
88
+ "source": [
89
+ "# Finally, more imports for more advanced machine learning\n",
90
+ "\n",
91
+ "from sklearn.svm import LinearSVR\n",
92
+ "from sklearn.ensemble import RandomForestRegressor"
93
+ ]
94
+ },
95
+ {
96
+ "cell_type": "code",
97
+ "execution_count": null,
98
+ "id": "6c01ee5f-c4fc-44fe-9d3a-907e8a0426d2",
99
+ "metadata": {},
100
+ "outputs": [],
101
+ "source": [
102
+ "# Constants - used for printing to stdout in color\n",
103
+ "\n",
104
+ "GREEN = \"\\033[92m\"\n",
105
+ "YELLOW = \"\\033[93m\"\n",
106
+ "RED = \"\\033[91m\"\n",
107
+ "RESET = \"\\033[0m\"\n",
108
+ "COLOR_MAP = {\"red\":RED, \"orange\": YELLOW, \"green\": GREEN}"
109
+ ]
110
+ },
111
+ {
112
+ "cell_type": "code",
113
+ "execution_count": null,
114
+ "id": "36d05bdc-0155-4c72-a7ee-aa4e614ffd3c",
115
+ "metadata": {},
116
+ "outputs": [],
117
+ "source": [
118
+ "# environment\n",
119
+ "\n",
120
+ "load_dotenv(override=True)\n",
121
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
122
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
123
+ "os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
124
+ ]
125
+ },
126
+ {
127
+ "cell_type": "code",
128
+ "execution_count": null,
129
+ "id": "4dd3aad2-6f99-433c-8792-e461d2f06622",
130
+ "metadata": {},
131
+ "outputs": [],
132
+ "source": [
133
+ "# Log in to HuggingFace\n",
134
+ "\n",
135
+ "hf_token = os.environ['HF_TOKEN']\n",
136
+ "login(hf_token, add_to_git_credential=True)"
137
+ ]
138
+ },
139
+ {
140
+ "cell_type": "code",
141
+ "execution_count": null,
142
+ "id": "ff3942d8-b010-46b5-a665-15554eae9776",
143
+ "metadata": {},
144
+ "outputs": [],
145
+ "source": [
146
+ "# One more import after logging in\n",
147
+ "\n",
148
+ "from items import Item"
149
+ ]
150
+ },
151
+ {
152
+ "cell_type": "code",
153
+ "execution_count": null,
154
+ "id": "c830ed3e-24ee-4af6-a07b-a1bfdcd39278",
155
+ "metadata": {},
156
+ "outputs": [],
157
+ "source": [
158
+ "%matplotlib inline"
159
+ ]
160
+ },
161
+ {
162
+ "cell_type": "markdown",
163
+ "id": "5105e13c-bca0-4c70-bfaa-649345f53322",
164
+ "metadata": {},
165
+ "source": [
166
+ "# Loading the pkl files\n",
167
+ "\n",
168
+ "Let's avoid curating all our data again! Load in the pickle files\n",
169
+ "\n",
170
+ "If you didn't already create these in Day 2, you can also download them from my google drive (you'll also find the slides here): \n",
171
+ "https://drive.google.com/drive/folders/1JwNorpRHdnf_pU0GE5yYtfKlyrKC3CoV?usp=sharing\n",
172
+ "\n",
173
+ "But note that the files are quite large - you might need to get a coffee!"
174
+ ]
175
+ },
176
+ {
177
+ "cell_type": "code",
178
+ "execution_count": null,
179
+ "id": "5c9b05f4-c9eb-462c-8d86-de9140a2d985",
180
+ "metadata": {},
181
+ "outputs": [],
182
+ "source": [
183
+ "with open('train.pkl', 'rb') as file:\n",
184
+ " train = pickle.load(file)\n",
185
+ "\n",
186
+ "with open('test.pkl', 'rb') as file:\n",
187
+ " test = pickle.load(file)"
188
+ ]
189
+ },
190
+ {
191
+ "cell_type": "code",
192
+ "execution_count": null,
193
+ "id": "a84638f7-5ff7-4f54-8751-3ef156264aee",
194
+ "metadata": {},
195
+ "outputs": [],
196
+ "source": [
197
+ "# Remind ourselves the training prompt\n",
198
+ "\n",
199
+ "print(train[0].prompt)"
200
+ ]
201
+ },
202
+ {
203
+ "cell_type": "code",
204
+ "execution_count": null,
205
+ "id": "b7619c85-6e9e-48a1-8efe-c6a60471b87c",
206
+ "metadata": {},
207
+ "outputs": [],
208
+ "source": [
209
+ "# Remind a test prompt\n",
210
+ "\n",
211
+ "print(train[0].price)"
212
+ ]
213
+ },
214
+ {
215
+ "cell_type": "markdown",
216
+ "id": "bcccf130-125a-4958-bac3-f46dfcb29b3f",
217
+ "metadata": {},
218
+ "source": [
219
+ "## Unveiling a mighty script that we will use a lot!\n",
220
+ "\n",
221
+ "A rather pleasing Test Harness that will evaluate any model against 250 items from the Test set\n",
222
+ "\n",
223
+ "And show us the results in a visually satisfying way.\n",
224
+ "\n",
225
+ "You write a function of this form:\n",
226
+ "\n",
227
+ "```\n",
228
+ "def my_prediction_function(item):\n",
229
+ " # my code here\n",
230
+ " return my_estimate\n",
231
+ "```\n",
232
+ "\n",
233
+ "And then you call:\n",
234
+ "\n",
235
+ "`Tester.test(my_prediction_function)`\n",
236
+ "\n",
237
+ "To evaluate your model."
238
+ ]
239
+ },
240
+ {
241
+ "cell_type": "code",
242
+ "execution_count": null,
243
+ "id": "b5793f5c-e23e-4a74-9496-1e30dd1e8935",
244
+ "metadata": {},
245
+ "outputs": [],
246
+ "source": [
247
+ "class Tester:\n",
248
+ "\n",
249
+ " def __init__(self, predictor, title=None, data=test, size=250):\n",
250
+ " self.predictor = predictor\n",
251
+ " self.data = data\n",
252
+ " self.title = title or predictor.__name__.replace(\"_\", \" \").title()\n",
253
+ " self.size = size\n",
254
+ " self.guesses = []\n",
255
+ " self.truths = []\n",
256
+ " self.errors = []\n",
257
+ " self.sles = []\n",
258
+ " self.colors = []\n",
259
+ "\n",
260
+ " def color_for(self, error, truth):\n",
261
+ " if error<40 or error/truth < 0.2:\n",
262
+ " return \"green\"\n",
263
+ " elif error<80 or error/truth < 0.4:\n",
264
+ " return \"orange\"\n",
265
+ " else:\n",
266
+ " return \"red\"\n",
267
+ " \n",
268
+ " def run_datapoint(self, i):\n",
269
+ " datapoint = self.data[i]\n",
270
+ " guess = self.predictor(datapoint)\n",
271
+ " truth = datapoint.price\n",
272
+ " error = abs(guess - truth)\n",
273
+ " log_error = math.log(truth+1) - math.log(guess+1)\n",
274
+ " sle = log_error ** 2\n",
275
+ " color = self.color_for(error, truth)\n",
276
+ " title = datapoint.title if len(datapoint.title) <= 40 else datapoint.title[:40]+\"...\"\n",
277
+ " self.guesses.append(guess)\n",
278
+ " self.truths.append(truth)\n",
279
+ " self.errors.append(error)\n",
280
+ " self.sles.append(sle)\n",
281
+ " self.colors.append(color)\n",
282
+ " print(f\"{COLOR_MAP[color]}{i+1}: Guess: ${guess:,.2f} Truth: ${truth:,.2f} Error: ${error:,.2f} SLE: {sle:,.2f} Item: {title}{RESET}\")\n",
283
+ "\n",
284
+ " def chart(self, title):\n",
285
+ " max_error = max(self.errors)\n",
286
+ " plt.figure(figsize=(12, 8))\n",
287
+ " max_val = max(max(self.truths), max(self.guesses))\n",
288
+ " plt.plot([0, max_val], [0, max_val], color='deepskyblue', lw=2, alpha=0.6)\n",
289
+ " plt.scatter(self.truths, self.guesses, s=3, c=self.colors)\n",
290
+ " plt.xlabel('Ground Truth')\n",
291
+ " plt.ylabel('Model Estimate')\n",
292
+ " plt.xlim(0, max_val)\n",
293
+ " plt.ylim(0, max_val)\n",
294
+ " plt.title(title)\n",
295
+ " plt.show()\n",
296
+ "\n",
297
+ " def report(self):\n",
298
+ " average_error = sum(self.errors) / self.size\n",
299
+ " rmsle = math.sqrt(sum(self.sles) / self.size)\n",
300
+ " hits = sum(1 for color in self.colors if color==\"green\")\n",
301
+ " title = f\"{self.title} Error=${average_error:,.2f} RMSLE={rmsle:,.2f} Hits={hits/self.size*100:.1f}%\"\n",
302
+ " self.chart(title)\n",
303
+ "\n",
304
+ " def run(self):\n",
305
+ " self.error = 0\n",
306
+ " for i in range(self.size):\n",
307
+ " self.run_datapoint(i)\n",
308
+ " self.report()\n",
309
+ "\n",
310
+ " @classmethod\n",
311
+ " def test(cls, function):\n",
312
+ " cls(function).run()"
313
+ ]
314
+ },
315
+ {
316
+ "cell_type": "markdown",
317
+ "id": "066fef03-8338-4526-9df3-89b649ad4f0a",
318
+ "metadata": {},
319
+ "source": [
320
+ "# Now for something basic\n",
321
+ "\n",
322
+ "What's the very simplest model you could imagine?\n",
323
+ "\n",
324
+ "Let's start with a random number generator!"
325
+ ]
326
+ },
327
+ {
328
+ "cell_type": "code",
329
+ "execution_count": null,
330
+ "id": "66ea68e8-ab1b-4f0d-aba4-a59574d8f85e",
331
+ "metadata": {},
332
+ "outputs": [],
333
+ "source": [
334
+ "def random_pricer(item):\n",
335
+ " return random.randrange(1,1000)"
336
+ ]
337
+ },
338
+ {
339
+ "cell_type": "code",
340
+ "execution_count": null,
341
+ "id": "53d941cb-5b73-44ea-b893-3a0ce9997066",
342
+ "metadata": {},
343
+ "outputs": [],
344
+ "source": [
345
+ "# Set the random seed\n",
346
+ "\n",
347
+ "random.seed(42)\n",
348
+ "\n",
349
+ "# Run our TestRunner\n",
350
+ "Tester.test(random_pricer)"
351
+ ]
352
+ },
353
+ {
354
+ "cell_type": "code",
355
+ "execution_count": null,
356
+ "id": "97451c73-9c1b-43a8-b3b9-9c41942e48a2",
357
+ "metadata": {},
358
+ "outputs": [],
359
+ "source": [
360
+ "# That was fun!\n",
361
+ "# We can do better - here's another rather trivial model\n",
362
+ "\n",
363
+ "training_prices = [item.price for item in train]\n",
364
+ "training_average = sum(training_prices) / len(training_prices)\n",
365
+ "\n",
366
+ "def constant_pricer(item):\n",
367
+ " return training_average"
368
+ ]
369
+ },
370
+ {
371
+ "cell_type": "code",
372
+ "execution_count": null,
373
+ "id": "8cf384eb-30c2-40d8-b7e5-48942ac6a969",
374
+ "metadata": {},
375
+ "outputs": [],
376
+ "source": [
377
+ "# Run our constant predictor\n",
378
+ "Tester.test(constant_pricer)\n"
379
+ ]
380
+ },
381
+ {
382
+ "cell_type": "code",
383
+ "execution_count": null,
384
+ "id": "6173a4b9-f0b9-407b-a1de-dc8f045adb8f",
385
+ "metadata": {},
386
+ "outputs": [],
387
+ "source": [
388
+ "train[0].details"
389
+ ]
390
+ },
391
+ {
392
+ "cell_type": "code",
393
+ "execution_count": null,
394
+ "id": "ce16eee8-bb34-4914-9aa5-57e30a567842",
395
+ "metadata": {},
396
+ "outputs": [],
397
+ "source": [
398
+ "# Create a new \"features\" field on items, and populate it with json parsed from the details dict\n",
399
+ "\n",
400
+ "for item in train:\n",
401
+ " item.features = json.loads(item.details)\n",
402
+ "for item in test:\n",
403
+ " item.features = json.loads(item.details)\n",
404
+ "\n",
405
+ "# Look at one"
406
+ ]
407
+ },
408
+ {
409
+ "cell_type": "code",
410
+ "execution_count": null,
411
+ "id": "ac702a10-dccb-43d4-887b-6f92a0fb298f",
412
+ "metadata": {},
413
+ "outputs": [],
414
+ "source": [
415
+ "train[0].features.keys()"
416
+ ]
417
+ },
418
+ {
419
+ "cell_type": "code",
420
+ "execution_count": null,
421
+ "id": "fd7a41c5-0c51-41be-a61d-8e80c3e90930",
422
+ "metadata": {},
423
+ "outputs": [],
424
+ "source": [
425
+ "# Look at 20 most common features in training set\n",
426
+ "\n",
427
+ "feature_count = Counter()\n",
428
+ "for item in train:\n",
429
+ " for f in item.features.keys():\n",
430
+ " feature_count[f]+=1\n",
431
+ "\n",
432
+ "feature_count.most_common(40)"
433
+ ]
434
+ },
435
+ {
436
+ "cell_type": "code",
437
+ "execution_count": null,
438
+ "id": "3cef84a9-4932-48fd-9f7a-51cfc06e3216",
439
+ "metadata": {},
440
+ "outputs": [],
441
+ "source": [
442
+ "# Now some janky code to pluck out the Item Weight\n",
443
+ "# Don't worry too much about this: spoiler alert, it's not going to be much use in training!\n",
444
+ "\n",
445
+ "def get_weight(item):\n",
446
+ " weight_str = item.features.get('Item Weight')\n",
447
+ " if weight_str:\n",
448
+ " parts = weight_str.split(' ')\n",
449
+ " amount = float(parts[0])\n",
450
+ " unit = parts[1].lower()\n",
451
+ " if unit==\"pounds\":\n",
452
+ " return amount\n",
453
+ " elif unit==\"ounces\":\n",
454
+ " return amount / 16\n",
455
+ " elif unit==\"grams\":\n",
456
+ " return amount / 453.592\n",
457
+ " elif unit==\"milligrams\":\n",
458
+ " return amount / 453592\n",
459
+ " elif unit==\"kilograms\":\n",
460
+ " return amount / 0.453592\n",
461
+ " elif unit==\"hundredths\" and parts[2].lower()==\"pounds\":\n",
462
+ " return amount / 100\n",
463
+ " else:\n",
464
+ " print(weight_str)\n",
465
+ " return None"
466
+ ]
467
+ },
468
+ {
469
+ "cell_type": "code",
470
+ "execution_count": null,
471
+ "id": "f4848b4a-3c5a-4168-83a5-57a1f3ff270d",
472
+ "metadata": {},
473
+ "outputs": [],
474
+ "source": [
475
+ "weights = [get_weight(t) for t in train]\n",
476
+ "weights = [w for w in weights if w]"
477
+ ]
478
+ },
479
+ {
480
+ "cell_type": "code",
481
+ "execution_count": null,
482
+ "id": "0cd11cc8-f16e-4991-b531-482189ddc4b6",
483
+ "metadata": {},
484
+ "outputs": [],
485
+ "source": [
486
+ "average_weight = sum(weights)/len(weights)\n",
487
+ "average_weight"
488
+ ]
489
+ },
490
+ {
491
+ "cell_type": "code",
492
+ "execution_count": null,
493
+ "id": "efe8ec7f-9777-464f-a809-b06b7033bdb2",
494
+ "metadata": {},
495
+ "outputs": [],
496
+ "source": [
497
+ "def get_weight_with_default(item):\n",
498
+ " weight = get_weight(item)\n",
499
+ " return weight or average_weight"
500
+ ]
501
+ },
502
+ {
503
+ "cell_type": "code",
504
+ "execution_count": null,
505
+ "id": "c2659fef-a455-431a-9a0e-59342b80084b",
506
+ "metadata": {},
507
+ "outputs": [],
508
+ "source": [
509
+ "def get_rank(item):\n",
510
+ " rank_dict = item.features.get(\"Best Sellers Rank\")\n",
511
+ " if rank_dict:\n",
512
+ " ranks = rank_dict.values()\n",
513
+ " return sum(ranks)/len(ranks)\n",
514
+ " return None"
515
+ ]
516
+ },
517
+ {
518
+ "cell_type": "code",
519
+ "execution_count": null,
520
+ "id": "20b9b5be-30bc-4d3a-8492-fbae119421a0",
521
+ "metadata": {},
522
+ "outputs": [],
523
+ "source": [
524
+ "ranks = [get_rank(t) for t in train]\n",
525
+ "ranks = [r for r in ranks if r]\n",
526
+ "average_rank = sum(ranks)/len(ranks)\n",
527
+ "average_rank"
528
+ ]
529
+ },
530
+ {
531
+ "cell_type": "code",
532
+ "execution_count": null,
533
+ "id": "081e646a-ea50-4ec3-9512-6d5f96f8aef6",
534
+ "metadata": {},
535
+ "outputs": [],
536
+ "source": [
537
+ "def get_rank_with_default(item):\n",
538
+ " rank = get_rank(item)\n",
539
+ " return rank or average_rank"
540
+ ]
541
+ },
542
+ {
543
+ "cell_type": "code",
544
+ "execution_count": null,
545
+ "id": "afd5daf7-cb2b-47af-bf17-dd71a9db65d0",
546
+ "metadata": {},
547
+ "outputs": [],
548
+ "source": [
549
+ "def get_text_length(item):\n",
550
+ " return len(item.test_prompt())"
551
+ ]
552
+ },
553
+ {
554
+ "cell_type": "code",
555
+ "execution_count": null,
556
+ "id": "85c89012-a922-401b-8a3b-94af641bf27a",
557
+ "metadata": {},
558
+ "outputs": [],
559
+ "source": [
560
+ "# investigate the brands\n",
561
+ "\n",
562
+ "brands = Counter()\n",
563
+ "for t in train:\n",
564
+ " brand = t.features.get(\"Brand\")\n",
565
+ " if brand:\n",
566
+ " brands[brand]+=1\n",
567
+ "\n",
568
+ "# Look at most common 40 brands\n",
569
+ "\n",
570
+ "brands.most_common(40)"
571
+ ]
572
+ },
573
+ {
574
+ "cell_type": "code",
575
+ "execution_count": null,
576
+ "id": "386dde54-e028-4a6d-b291-cce889ac1fa3",
577
+ "metadata": {},
578
+ "outputs": [],
579
+ "source": [
580
+ "TOP_ELECTRONICS_BRANDS = [\"hp\", \"dell\", \"lenovo\", \"samsung\", \"asus\", \"sony\", \"canon\", \"apple\", \"intel\"]\n",
581
+ "def is_top_electronics_brand(item):\n",
582
+ " brand = item.features.get(\"Brand\")\n",
583
+ " return brand and brand.lower() in TOP_ELECTRONICS_BRANDS"
584
+ ]
585
+ },
586
+ {
587
+ "cell_type": "code",
588
+ "execution_count": null,
589
+ "id": "c31c9c59-9d0d-47a8-a046-f20ed8d38d4c",
590
+ "metadata": {},
591
+ "outputs": [],
592
+ "source": [
593
+ "def get_features(item):\n",
594
+ " return {\n",
595
+ " \"weight\": get_weight_with_default(item),\n",
596
+ " \"rank\": get_rank_with_default(item),\n",
597
+ " \"text_length\": get_text_length(item),\n",
598
+ " \"is_top_electronics_brand\": 1 if is_top_electronics_brand(item) else 0\n",
599
+ " }"
600
+ ]
601
+ },
602
+ {
603
+ "cell_type": "code",
604
+ "execution_count": null,
605
+ "id": "88850855-f5bd-4be2-9d7c-75bf8a21609b",
606
+ "metadata": {},
607
+ "outputs": [],
608
+ "source": [
609
+ "# Look at features in a training item\n",
610
+ "get_features(train[0])"
611
+ ]
612
+ },
613
+ {
614
+ "cell_type": "code",
615
+ "execution_count": null,
616
+ "id": "ee9b5298-68b7-497d-8b2e-875287bb25b2",
617
+ "metadata": {},
618
+ "outputs": [],
619
+ "source": [
620
+ "# A utility function to convert our features into a pandas dataframe\n",
621
+ "\n",
622
+ "def list_to_dataframe(items):\n",
623
+ " features = [get_features(item) for item in items]\n",
624
+ " df = pd.DataFrame(features)\n",
625
+ " df['price'] = [item.price for item in items]\n",
626
+ " return df\n",
627
+ "\n",
628
+ "train_df = list_to_dataframe(train)\n",
629
+ "test_df = list_to_dataframe(test[:250])"
630
+ ]
631
+ },
632
+ {
633
+ "cell_type": "code",
634
+ "execution_count": null,
635
+ "id": "cc1d68e0-ab33-40f4-9334-461d426af25c",
636
+ "metadata": {},
637
+ "outputs": [],
638
+ "source": [
639
+ "# Traditional Linear Regression!\n",
640
+ "\n",
641
+ "np.random.seed(42)\n",
642
+ "\n",
643
+ "# Separate features and target\n",
644
+ "feature_columns = ['weight', 'rank', 'text_length', 'is_top_electronics_brand']\n",
645
+ "\n",
646
+ "X_train = train_df[feature_columns]\n",
647
+ "y_train = train_df['price']\n",
648
+ "X_test = test_df[feature_columns]\n",
649
+ "y_test = test_df['price']\n",
650
+ "\n",
651
+ "# Train a Linear Regression\n",
652
+ "model = LinearRegression()\n",
653
+ "model.fit(X_train, y_train)\n",
654
+ "\n",
655
+ "for feature, coef in zip(feature_columns, model.coef_):\n",
656
+ " print(f\"{feature}: {coef}\")\n",
657
+ "print(f\"Intercept: {model.intercept_}\")\n",
658
+ "\n",
659
+ "# Predict the test set and evaluate\n",
660
+ "y_pred = model.predict(X_test)\n",
661
+ "mse = mean_squared_error(y_test, y_pred)\n",
662
+ "r2 = r2_score(y_test, y_pred)\n",
663
+ "\n",
664
+ "print(f\"Mean Squared Error: {mse}\")\n",
665
+ "print(f\"R-squared Score: {r2}\")"
666
+ ]
667
+ },
668
+ {
669
+ "cell_type": "code",
670
+ "execution_count": null,
671
+ "id": "6561c3c7-ac7f-458b-983c-4a164b9d02c3",
672
+ "metadata": {},
673
+ "outputs": [],
674
+ "source": [
675
+ "# Function to predict price for a new item\n",
676
+ "\n",
677
+ "def linear_regression_pricer(item):\n",
678
+ " features = get_features(item)\n",
679
+ " features_df = pd.DataFrame([features])\n",
680
+ " return model.predict(features_df)[0]"
681
+ ]
682
+ },
683
+ {
684
+ "cell_type": "code",
685
+ "execution_count": null,
686
+ "id": "9bf2caa4-657a-4fc6-9dcb-bed7eaf8dd65",
687
+ "metadata": {},
688
+ "outputs": [],
689
+ "source": [
690
+ "# test it\n",
691
+ "\n",
692
+ "Tester.test(linear_regression_pricer)"
693
+ ]
694
+ },
695
+ {
696
+ "cell_type": "code",
697
+ "execution_count": null,
698
+ "id": "79e1574b-52ef-49cc-bfb5-e97252ed5db8",
699
+ "metadata": {},
700
+ "outputs": [],
701
+ "source": [
702
+ "# For the next few models, we prepare our documents and prices\n",
703
+ "# Note that we use the test prompt for the documents, otherwise we'll reveal the answer!!\n",
704
+ "\n",
705
+ "prices = np.array([float(item.price) for item in train])\n",
706
+ "documents = [item.test_prompt() for item in train]"
707
+ ]
708
+ },
709
+ {
710
+ "cell_type": "code",
711
+ "execution_count": null,
712
+ "id": "e126c22e-53e7-4967-9ebb-6b7dd7fe4ade",
713
+ "metadata": {},
714
+ "outputs": [],
715
+ "source": [
716
+ "# Use the CountVectorizer for a Bag of Words model\n",
717
+ "\n",
718
+ "np.random.seed(42)\n",
719
+ "vectorizer = CountVectorizer(max_features=1000, stop_words='english')\n",
720
+ "X = vectorizer.fit_transform(documents)\n",
721
+ "regressor = LinearRegression()\n",
722
+ "regressor.fit(X, prices)"
723
+ ]
724
+ },
725
+ {
726
+ "cell_type": "code",
727
+ "execution_count": null,
728
+ "id": "4b7148d3-3202-4536-a75c-1627495c51d3",
729
+ "metadata": {},
730
+ "outputs": [],
731
+ "source": [
732
+ "def bow_lr_pricer(item):\n",
733
+ " x = vectorizer.transform([item.test_prompt()])\n",
734
+ " return max(regressor.predict(x)[0], 0)"
735
+ ]
736
+ },
737
+ {
738
+ "cell_type": "code",
739
+ "execution_count": null,
740
+ "id": "38f7f7d0-d22c-4282-92e5-9666a7b8535d",
741
+ "metadata": {},
742
+ "outputs": [],
743
+ "source": [
744
+ "# test it\n",
745
+ "\n",
746
+ "Tester.test(bow_lr_pricer)"
747
+ ]
748
+ },
749
+ {
750
+ "cell_type": "code",
751
+ "execution_count": null,
752
+ "id": "b623079e-54fa-418f-b209-7d54ebbcc23a",
753
+ "metadata": {},
754
+ "outputs": [],
755
+ "source": [
756
+ "# The amazing word2vec model, implemented in gensim NLP library\n",
757
+ "\n",
758
+ "np.random.seed(42)\n",
759
+ "\n",
760
+ "# Preprocess the documents\n",
761
+ "processed_docs = [simple_preprocess(doc) for doc in documents]\n",
762
+ "\n",
763
+ "# Train Word2Vec model\n",
764
+ "w2v_model = Word2Vec(sentences=processed_docs, vector_size=400, window=5, min_count=1, workers=8)"
765
+ ]
766
+ },
767
+ {
768
+ "cell_type": "code",
769
+ "execution_count": null,
770
+ "id": "3de4efc7-68a6-4443-b9fd-70ee9d722362",
771
+ "metadata": {},
772
+ "outputs": [],
773
+ "source": [
774
+ "# This step of averaging vectors across the document is a weakness in our approach\n",
775
+ "\n",
776
+ "def document_vector(doc):\n",
777
+ " doc_words = simple_preprocess(doc)\n",
778
+ " word_vectors = [w2v_model.wv[word] for word in doc_words if word in w2v_model.wv]\n",
779
+ " return np.mean(word_vectors, axis=0) if word_vectors else np.zeros(w2v_model.vector_size)\n",
780
+ "\n",
781
+ "# Create feature matrix\n",
782
+ "X_w2v = np.array([document_vector(doc) for doc in documents])"
783
+ ]
784
+ },
785
+ {
786
+ "cell_type": "code",
787
+ "execution_count": null,
788
+ "id": "9f05eeec-dab8-4007-8e8c-dcf4175b8861",
789
+ "metadata": {},
790
+ "outputs": [],
791
+ "source": [
792
+ "# Run Linear Regression on word2vec\n",
793
+ "\n",
794
+ "word2vec_lr_regressor = LinearRegression()\n",
795
+ "word2vec_lr_regressor.fit(X_w2v, prices)"
796
+ ]
797
+ },
798
+ {
799
+ "cell_type": "code",
800
+ "execution_count": null,
801
+ "id": "e43d3fb9-e013-4573-90bf-9a522132b555",
802
+ "metadata": {},
803
+ "outputs": [],
804
+ "source": [
805
+ "def word2vec_lr_pricer(item):\n",
806
+ " doc = item.test_prompt()\n",
807
+ " doc_vector = document_vector(doc)\n",
808
+ " return max(0, word2vec_lr_regressor.predict([doc_vector])[0])"
809
+ ]
810
+ },
811
+ {
812
+ "cell_type": "code",
813
+ "execution_count": null,
814
+ "id": "6740319d-5c8e-4125-9106-97e2e8ab72c7",
815
+ "metadata": {},
816
+ "outputs": [],
817
+ "source": [
818
+ "Tester.test(word2vec_lr_pricer)"
819
+ ]
820
+ },
821
+ {
822
+ "cell_type": "code",
823
+ "execution_count": null,
824
+ "id": "9d6d3265-37c1-464c-a489-5be4df0a7276",
825
+ "metadata": {},
826
+ "outputs": [],
827
+ "source": [
828
+ "# Support Vector Machines\n",
829
+ "\n",
830
+ "np.random.seed(42)\n",
831
+ "svr_regressor = LinearSVR()\n",
832
+ "\n",
833
+ "svr_regressor.fit(X_w2v, prices)"
834
+ ]
835
+ },
836
+ {
837
+ "cell_type": "code",
838
+ "execution_count": null,
839
+ "id": "fcc289e6-56a1-4119-864f-2fdf8efde643",
840
+ "metadata": {},
841
+ "outputs": [],
842
+ "source": [
843
+ "def svr_pricer(item):\n",
844
+ " np.random.seed(42)\n",
845
+ " doc = item.test_prompt()\n",
846
+ " doc_vector = document_vector(doc)\n",
847
+ " return max(float(svr_regressor.predict([doc_vector])[0]),0)"
848
+ ]
849
+ },
850
+ {
851
+ "cell_type": "code",
852
+ "execution_count": null,
853
+ "id": "80286a48-7cca-40e6-af76-a814a23bb9dc",
854
+ "metadata": {},
855
+ "outputs": [],
856
+ "source": [
857
+ "Tester.test(svr_pricer)"
858
+ ]
859
+ },
860
+ {
861
+ "cell_type": "code",
862
+ "execution_count": null,
863
+ "id": "c6c44fe4-e4d9-4559-a8ed-d8f97e25b69f",
864
+ "metadata": {},
865
+ "outputs": [],
866
+ "source": [
867
+ "# And the powerful Random Forest regression\n",
868
+ "\n",
869
+ "rf_model = RandomForestRegressor(n_estimators=100, random_state=42, n_jobs=8)\n",
870
+ "rf_model.fit(X_w2v, prices)"
871
+ ]
872
+ },
873
+ {
874
+ "cell_type": "code",
875
+ "execution_count": null,
876
+ "id": "a38812d0-913b-400b-804f-51434d895d05",
877
+ "metadata": {},
878
+ "outputs": [],
879
+ "source": [
880
+ "def random_forest_pricer(item):\n",
881
+ " doc = item.test_prompt()\n",
882
+ " doc_vector = document_vector(doc)\n",
883
+ " return max(0, rf_model.predict([doc_vector])[0])"
884
+ ]
885
+ },
886
+ {
887
+ "cell_type": "code",
888
+ "execution_count": null,
889
+ "id": "88b51c01-c791-4fdc-8010-00b2e486b8ce",
890
+ "metadata": {},
891
+ "outputs": [],
892
+ "source": [
893
+ "Tester.test(random_forest_pricer)"
894
+ ]
895
+ },
896
+ {
897
+ "cell_type": "code",
898
+ "execution_count": null,
899
+ "id": "bc85b271-4c92-480c-8843-2d7713b0fa57",
900
+ "metadata": {},
901
+ "outputs": [],
902
+ "source": []
903
+ }
904
+ ],
905
+ "metadata": {
906
+ "kernelspec": {
907
+ "display_name": "Python 3 (ipykernel)",
908
+ "language": "python",
909
+ "name": "python3"
910
+ },
911
+ "language_info": {
912
+ "codemirror_mode": {
913
+ "name": "ipython",
914
+ "version": 3
915
+ },
916
+ "file_extension": ".py",
917
+ "mimetype": "text/x-python",
918
+ "name": "python",
919
+ "nbconvert_exporter": "python",
920
+ "pygments_lexer": "ipython3",
921
+ "version": "3.11.11"
922
+ }
923
+ },
924
+ "nbformat": 4,
925
+ "nbformat_minor": 5
926
+ }
week6/.ipynb_checkpoints/day4-checkpoint.ipynb ADDED
@@ -0,0 +1,406 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "db8736a7-ed94-441c-9556-831fa57b5a10",
6
+ "metadata": {},
7
+ "source": [
8
+ "# The Product Pricer Continued\n",
9
+ "\n",
10
+ "A model that can estimate how much something costs, from its description.\n",
11
+ "\n",
12
+ "## Enter The Frontier!\n",
13
+ "\n",
14
+ "And now - we put Frontier Models to the test.\n",
15
+ "\n",
16
+ "### 2 important points:\n",
17
+ "\n",
18
+ "It's important to appreciate that we aren't Training the frontier models. We're only providing them with the Test dataset to see how they perform. They don't gain the benefit of the 400,000 training examples that we provided to the Traditional ML models.\n",
19
+ "\n",
20
+ "HAVING SAID THAT...\n",
21
+ "\n",
22
+ "It's entirely possible that in their monstrously large training data, they've already been exposed to all the products in the training AND the test set. So there could be test \"contamination\" here which gives them an unfair advantage. We should keep that in mind."
23
+ ]
24
+ },
25
+ {
26
+ "cell_type": "code",
27
+ "execution_count": null,
28
+ "id": "681c717b-4c24-4ac3-a5f3-3c5881d6e70a",
29
+ "metadata": {},
30
+ "outputs": [],
31
+ "source": [
32
+ "# imports\n",
33
+ "\n",
34
+ "import os\n",
35
+ "import re\n",
36
+ "import math\n",
37
+ "import json\n",
38
+ "import random\n",
39
+ "from dotenv import load_dotenv\n",
40
+ "from huggingface_hub import login\n",
41
+ "import matplotlib.pyplot as plt\n",
42
+ "import numpy as np\n",
43
+ "import pickle\n",
44
+ "from collections import Counter\n",
45
+ "from openai import OpenAI\n",
46
+ "from anthropic import Anthropic"
47
+ ]
48
+ },
49
+ {
50
+ "cell_type": "code",
51
+ "execution_count": null,
52
+ "id": "36d05bdc-0155-4c72-a7ee-aa4e614ffd3c",
53
+ "metadata": {},
54
+ "outputs": [],
55
+ "source": [
56
+ "# environment\n",
57
+ "\n",
58
+ "load_dotenv(override=True)\n",
59
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
60
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
61
+ "os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
62
+ ]
63
+ },
64
+ {
65
+ "cell_type": "code",
66
+ "execution_count": null,
67
+ "id": "4dd3aad2-6f99-433c-8792-e461d2f06622",
68
+ "metadata": {},
69
+ "outputs": [],
70
+ "source": [
71
+ "# Log in to HuggingFace\n",
72
+ "\n",
73
+ "hf_token = os.environ['HF_TOKEN']\n",
74
+ "login(hf_token, add_to_git_credential=True)"
75
+ ]
76
+ },
77
+ {
78
+ "cell_type": "code",
79
+ "execution_count": null,
80
+ "id": "6985bdc7-fa45-49a3-ae97-84bdeb9b2083",
81
+ "metadata": {},
82
+ "outputs": [],
83
+ "source": [
84
+ "# moved our Tester into a separate package\n",
85
+ "# call it with Tester.test(function_name, test_dataset)\n",
86
+ "\n",
87
+ "from items import Item\n",
88
+ "from testing import Tester"
89
+ ]
90
+ },
91
+ {
92
+ "cell_type": "code",
93
+ "execution_count": null,
94
+ "id": "b0a6fb86-74a4-403c-ab25-6db2d74e9d2b",
95
+ "metadata": {},
96
+ "outputs": [],
97
+ "source": [
98
+ "openai = OpenAI()\n",
99
+ "claude = Anthropic()"
100
+ ]
101
+ },
102
+ {
103
+ "cell_type": "code",
104
+ "execution_count": null,
105
+ "id": "c830ed3e-24ee-4af6-a07b-a1bfdcd39278",
106
+ "metadata": {},
107
+ "outputs": [],
108
+ "source": [
109
+ "%matplotlib inline"
110
+ ]
111
+ },
112
+ {
113
+ "cell_type": "code",
114
+ "execution_count": null,
115
+ "id": "5c9b05f4-c9eb-462c-8d86-de9140a2d985",
116
+ "metadata": {},
117
+ "outputs": [],
118
+ "source": [
119
+ "# Let's avoid curating all our data again! Load in the pickle files:\n",
120
+ "\n",
121
+ "with open('train.pkl', 'rb') as file:\n",
122
+ " train = pickle.load(file)\n",
123
+ "\n",
124
+ "with open('test.pkl', 'rb') as file:\n",
125
+ " test = pickle.load(file)"
126
+ ]
127
+ },
128
+ {
129
+ "cell_type": "markdown",
130
+ "id": "e5856173-e68c-4975-a769-5f1736e227a5",
131
+ "metadata": {},
132
+ "source": [
133
+ "# Before we look at the Frontier\n",
134
+ "\n",
135
+ "## There is one more model we could consider"
136
+ ]
137
+ },
138
+ {
139
+ "cell_type": "code",
140
+ "execution_count": null,
141
+ "id": "f3e81ee0-828a-4af8-9ccf-177af6c78a0c",
142
+ "metadata": {},
143
+ "outputs": [],
144
+ "source": [
145
+ "# Write the test set to a CSV\n",
146
+ "\n",
147
+ "import csv\n",
148
+ "with open('human_input.csv', 'w', encoding=\"utf-8\") as csvfile:\n",
149
+ " writer = csv.writer(csvfile)\n",
150
+ " for t in test[:250]:\n",
151
+ " writer.writerow([t.test_prompt(), 0])"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "code",
156
+ "execution_count": null,
157
+ "id": "aeafac31-1a10-4029-b190-030378e2fe01",
158
+ "metadata": {},
159
+ "outputs": [],
160
+ "source": [
161
+ "# Read it back in\n",
162
+ "\n",
163
+ "human_predictions = []\n",
164
+ "with open('human_output.csv', 'r', encoding=\"utf-8\") as csvfile:\n",
165
+ " reader = csv.reader(csvfile)\n",
166
+ " for row in reader:\n",
167
+ " human_predictions.append(float(row[1]))"
168
+ ]
169
+ },
170
+ {
171
+ "cell_type": "code",
172
+ "execution_count": null,
173
+ "id": "a9709da2-28f0-419e-af71-4ef6c02246ad",
174
+ "metadata": {},
175
+ "outputs": [],
176
+ "source": [
177
+ "def human_pricer(item):\n",
178
+ " idx = test.index(item)\n",
179
+ " return human_predictions[idx]"
180
+ ]
181
+ },
182
+ {
183
+ "cell_type": "code",
184
+ "execution_count": null,
185
+ "id": "e1ba3b3e-4b08-4f0b-9e51-ebb03a86085d",
186
+ "metadata": {},
187
+ "outputs": [],
188
+ "source": [
189
+ "Tester.test(human_pricer, test)"
190
+ ]
191
+ },
192
+ {
193
+ "cell_type": "markdown",
194
+ "id": "066fef03-8338-4526-9df3-89b649ad4f0a",
195
+ "metadata": {},
196
+ "source": [
197
+ "## First, the humble but mighty GPT-4o-mini\n",
198
+ "\n",
199
+ "It's called mini, but it packs a punch."
200
+ ]
201
+ },
202
+ {
203
+ "cell_type": "code",
204
+ "execution_count": null,
205
+ "id": "66ea68e8-ab1b-4f0d-aba4-a59574d8f85e",
206
+ "metadata": {},
207
+ "outputs": [],
208
+ "source": [
209
+ "# First let's work on a good prompt for a Frontier model\n",
210
+ "# Notice that I'm removing the \" to the nearest dollar\"\n",
211
+ "# When we train our own models, we'll need to make the problem as easy as possible, \n",
212
+ "# but a Frontier model needs no such simplification.\n",
213
+ "\n",
214
+ "def messages_for(item):\n",
215
+ " system_message = \"You estimate prices of items. Reply only with the price, no explanation\"\n",
216
+ " user_prompt = item.test_prompt().replace(\" to the nearest dollar\",\"\").replace(\"\\n\\nPrice is $\",\"\")\n",
217
+ " return [\n",
218
+ " {\"role\": \"system\", \"content\": system_message},\n",
219
+ " {\"role\": \"user\", \"content\": user_prompt},\n",
220
+ " {\"role\": \"assistant\", \"content\": \"Price is $\"}\n",
221
+ " ]"
222
+ ]
223
+ },
224
+ {
225
+ "cell_type": "code",
226
+ "execution_count": null,
227
+ "id": "add7bc0a-71fb-49cc-a49b-9548fd0fe949",
228
+ "metadata": {},
229
+ "outputs": [],
230
+ "source": []
231
+ },
232
+ {
233
+ "cell_type": "code",
234
+ "execution_count": null,
235
+ "id": "4ff92d61-0d27-4b0d-8b32-c9891016509b",
236
+ "metadata": {},
237
+ "outputs": [],
238
+ "source": [
239
+ "# Try this out\n",
240
+ "\n",
241
+ "messages_for(test[0])"
242
+ ]
243
+ },
244
+ {
245
+ "cell_type": "code",
246
+ "execution_count": null,
247
+ "id": "b1af1888-f94a-4106-b0d8-8a70939eec4e",
248
+ "metadata": {},
249
+ "outputs": [],
250
+ "source": [
251
+ "# A utility function to extract the price from a string\n",
252
+ "\n",
253
+ "def get_price(s):\n",
254
+ " s = s.replace('$','').replace(',','')\n",
255
+ " match = re.search(r\"[-+]?\\d*\\.\\d+|\\d+\", s)\n",
256
+ " return float(match.group()) if match else 0"
257
+ ]
258
+ },
259
+ {
260
+ "cell_type": "code",
261
+ "execution_count": null,
262
+ "id": "f138c5b7-bcc1-4085-aced-68dad1bf36b4",
263
+ "metadata": {},
264
+ "outputs": [],
265
+ "source": [
266
+ "get_price(\"The price is roughly $99.99 because blah blah\")"
267
+ ]
268
+ },
269
+ {
270
+ "cell_type": "code",
271
+ "execution_count": null,
272
+ "id": "501a2a7a-69c8-451b-bbc0-398bcb9e1612",
273
+ "metadata": {},
274
+ "outputs": [],
275
+ "source": [
276
+ "# The function for gpt-4o-mini\n",
277
+ "\n",
278
+ "def gpt_4o_mini(item):\n",
279
+ " response = openai.chat.completions.create(\n",
280
+ " model=\"gpt-4o-mini\", \n",
281
+ " messages=messages_for(item),\n",
282
+ " seed=42,\n",
283
+ " max_tokens=5\n",
284
+ " )\n",
285
+ " reply = response.choices[0].message.content\n",
286
+ " return get_price(reply)"
287
+ ]
288
+ },
289
+ {
290
+ "cell_type": "code",
291
+ "execution_count": null,
292
+ "id": "843d88b4-364a-431b-b48b-8a7c1f68b786",
293
+ "metadata": {},
294
+ "outputs": [],
295
+ "source": [
296
+ "test[0].price"
297
+ ]
298
+ },
299
+ {
300
+ "cell_type": "code",
301
+ "execution_count": null,
302
+ "id": "36bdd2c9-1859-4f99-a09f-3ec83b845b30",
303
+ "metadata": {},
304
+ "outputs": [],
305
+ "source": [
306
+ "Tester.test(gpt_4o_mini, test)"
307
+ ]
308
+ },
309
+ {
310
+ "cell_type": "code",
311
+ "execution_count": null,
312
+ "id": "f49e90d6-6749-4eb8-9347-5922b189d379",
313
+ "metadata": {},
314
+ "outputs": [],
315
+ "source": [
316
+ "def gpt_4o_frontier(item):\n",
317
+ " response = openai.chat.completions.create(\n",
318
+ " model=\"gpt-4o-2024-08-06\", \n",
319
+ " messages=messages_for(item),\n",
320
+ " seed=42,\n",
321
+ " max_tokens=5\n",
322
+ " )\n",
323
+ " reply = response.choices[0].message.content\n",
324
+ " return get_price(reply)"
325
+ ]
326
+ },
327
+ {
328
+ "cell_type": "code",
329
+ "execution_count": null,
330
+ "id": "766e697e-55bf-4521-b301-3b07d20045e0",
331
+ "metadata": {},
332
+ "outputs": [],
333
+ "source": [
334
+ "# The function for gpt-4o - the August model\n",
335
+ "# Note that it cost me about 1-2 cents to run this (pricing may vary by region)\n",
336
+ "# You can skip this and look at my results instead\n",
337
+ "\n",
338
+ "Tester.test(gpt_4o_frontier, test)"
339
+ ]
340
+ },
341
+ {
342
+ "cell_type": "code",
343
+ "execution_count": null,
344
+ "id": "53d941cb-5b73-44ea-b893-3a0ce9997066",
345
+ "metadata": {},
346
+ "outputs": [],
347
+ "source": [
348
+ "def claude_3_point_5_sonnet(item):\n",
349
+ " messages = messages_for(item)\n",
350
+ " system_message = messages[0]['content']\n",
351
+ " messages = messages[1:]\n",
352
+ " response = claude.messages.create(\n",
353
+ " model=\"claude-3-5-sonnet-20240620\",\n",
354
+ " max_tokens=5,\n",
355
+ " system=system_message,\n",
356
+ " messages=messages\n",
357
+ " )\n",
358
+ " reply = response.content[0].text\n",
359
+ " return get_price(reply)"
360
+ ]
361
+ },
362
+ {
363
+ "cell_type": "code",
364
+ "execution_count": null,
365
+ "id": "11dba25d-f562-40f9-9855-40b715b7fc86",
366
+ "metadata": {},
367
+ "outputs": [],
368
+ "source": [
369
+ "# The function for Claude 3.5 Sonnet\n",
370
+ "# It also cost me about 1-2 cents to run this (pricing may vary by region)\n",
371
+ "# You can skip this and look at my results instead\n",
372
+ "\n",
373
+ "Tester.test(claude_3_point_5_sonnet, test)"
374
+ ]
375
+ },
376
+ {
377
+ "cell_type": "code",
378
+ "execution_count": null,
379
+ "id": "77428dfb-d8f4-4477-8265-77b4b0badd39",
380
+ "metadata": {},
381
+ "outputs": [],
382
+ "source": []
383
+ }
384
+ ],
385
+ "metadata": {
386
+ "kernelspec": {
387
+ "display_name": "Python 3 (ipykernel)",
388
+ "language": "python",
389
+ "name": "python3"
390
+ },
391
+ "language_info": {
392
+ "codemirror_mode": {
393
+ "name": "ipython",
394
+ "version": 3
395
+ },
396
+ "file_extension": ".py",
397
+ "mimetype": "text/x-python",
398
+ "name": "python",
399
+ "nbconvert_exporter": "python",
400
+ "pygments_lexer": "ipython3",
401
+ "version": "3.11.11"
402
+ }
403
+ },
404
+ "nbformat": 4,
405
+ "nbformat_minor": 5
406
+ }
week6/.ipynb_checkpoints/day4-results-checkpoint.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
week6/.ipynb_checkpoints/day5-checkpoint.ipynb ADDED
@@ -0,0 +1,555 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "db8736a7-ed94-441c-9556-831fa57b5a10",
6
+ "metadata": {},
7
+ "source": [
8
+ "# The Product Pricer Continued\n",
9
+ "\n",
10
+ "A model that can estimate how much something costs, from its description.\n",
11
+ "\n",
12
+ "## AT LAST - it's time for Fine Tuning!\n",
13
+ "\n",
14
+ "After all this data preparation, and old school machine learning, we've finally arrived at the moment you've been waiting for. Fine-tuning a model."
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": null,
20
+ "id": "681c717b-4c24-4ac3-a5f3-3c5881d6e70a",
21
+ "metadata": {},
22
+ "outputs": [],
23
+ "source": [
24
+ "# imports\n",
25
+ "\n",
26
+ "import os\n",
27
+ "import re\n",
28
+ "import math\n",
29
+ "import json\n",
30
+ "import random\n",
31
+ "from dotenv import load_dotenv\n",
32
+ "from huggingface_hub import login\n",
33
+ "import matplotlib.pyplot as plt\n",
34
+ "import numpy as np\n",
35
+ "import pickle\n",
36
+ "from collections import Counter\n",
37
+ "from openai import OpenAI\n",
38
+ "from anthropic import Anthropic"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": null,
44
+ "id": "36d05bdc-0155-4c72-a7ee-aa4e614ffd3c",
45
+ "metadata": {},
46
+ "outputs": [],
47
+ "source": [
48
+ "# environment\n",
49
+ "\n",
50
+ "load_dotenv(override=True)\n",
51
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
52
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
53
+ "os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
54
+ ]
55
+ },
56
+ {
57
+ "cell_type": "code",
58
+ "execution_count": null,
59
+ "id": "4dd3aad2-6f99-433c-8792-e461d2f06622",
60
+ "metadata": {},
61
+ "outputs": [],
62
+ "source": [
63
+ "# Log in to HuggingFace\n",
64
+ "\n",
65
+ "hf_token = os.environ['HF_TOKEN']\n",
66
+ "login(hf_token, add_to_git_credential=True)"
67
+ ]
68
+ },
69
+ {
70
+ "cell_type": "code",
71
+ "execution_count": null,
72
+ "id": "884a50bd-8cae-425e-8e56-f079fc3e65ce",
73
+ "metadata": {},
74
+ "outputs": [],
75
+ "source": [
76
+ "# moved our Tester into a separate package\n",
77
+ "# call it with Tester.test(function_name, test_dataset)\n",
78
+ "\n",
79
+ "from items import Item\n",
80
+ "from testing import Tester"
81
+ ]
82
+ },
83
+ {
84
+ "cell_type": "code",
85
+ "execution_count": null,
86
+ "id": "b0a6fb86-74a4-403c-ab25-6db2d74e9d2b",
87
+ "metadata": {},
88
+ "outputs": [],
89
+ "source": [
90
+ "openai = OpenAI()"
91
+ ]
92
+ },
93
+ {
94
+ "cell_type": "code",
95
+ "execution_count": null,
96
+ "id": "c830ed3e-24ee-4af6-a07b-a1bfdcd39278",
97
+ "metadata": {},
98
+ "outputs": [],
99
+ "source": [
100
+ "%matplotlib inline"
101
+ ]
102
+ },
103
+ {
104
+ "cell_type": "code",
105
+ "execution_count": null,
106
+ "id": "5c9b05f4-c9eb-462c-8d86-de9140a2d985",
107
+ "metadata": {},
108
+ "outputs": [],
109
+ "source": [
110
+ "# Let's avoid curating all our data again! Load in the pickle files:\n",
111
+ "\n",
112
+ "with open('train.pkl', 'rb') as file:\n",
113
+ " train = pickle.load(file)\n",
114
+ "\n",
115
+ "with open('test.pkl', 'rb') as file:\n",
116
+ " test = pickle.load(file)"
117
+ ]
118
+ },
119
+ {
120
+ "cell_type": "code",
121
+ "execution_count": null,
122
+ "id": "e8367135-f40e-43e1-8f3c-09e990ab1194",
123
+ "metadata": {},
124
+ "outputs": [],
125
+ "source": [
126
+ "# OpenAI recommends fine-tuning with populations of 50-100 examples\n",
127
+ "# But as our examples are very small, I'm suggesting we go with 200 examples (and 1 epoch)\n",
128
+ "\n",
129
+ "fine_tune_train = train[:200]\n",
130
+ "fine_tune_validation = train[200:250]"
131
+ ]
132
+ },
133
+ {
134
+ "cell_type": "markdown",
135
+ "id": "8be4a889-81c3-42b1-a2fc-034cdc7321a6",
136
+ "metadata": {},
137
+ "source": [
138
+ "# Step 1\n",
139
+ "\n",
140
+ "Prepare our data for fine-tuning in JSONL (JSON Lines) format and upload to OpenAI"
141
+ ]
142
+ },
143
+ {
144
+ "cell_type": "code",
145
+ "execution_count": null,
146
+ "id": "8ae2fb3c-1cff-4ce3-911e-627c970edd7b",
147
+ "metadata": {},
148
+ "outputs": [],
149
+ "source": [
150
+ "# First let's work on a good prompt for a Frontier model\n",
151
+ "# Notice that I'm removing the \" to the nearest dollar\"\n",
152
+ "# When we train our own models, we'll need to make the problem as easy as possible, \n",
153
+ "# but a Frontier model needs no such simplification.\n",
154
+ "\n",
155
+ "def messages_for(item):\n",
156
+ " system_message = \"You estimate prices of items. Reply only with the price, no explanation\"\n",
157
+ " user_prompt = item.test_prompt().replace(\" to the nearest dollar\",\"\").replace(\"\\n\\nPrice is $\",\"\")\n",
158
+ " return [\n",
159
+ " {\"role\": \"system\", \"content\": system_message},\n",
160
+ " {\"role\": \"user\", \"content\": user_prompt},\n",
161
+ " {\"role\": \"assistant\", \"content\": f\"Price is ${item.price:.2f}\"}\n",
162
+ " ]"
163
+ ]
164
+ },
165
+ {
166
+ "cell_type": "code",
167
+ "execution_count": null,
168
+ "id": "1aa280f6-1227-426a-a2e2-1ce985feba1e",
169
+ "metadata": {},
170
+ "outputs": [],
171
+ "source": [
172
+ "messages_for(train[0])"
173
+ ]
174
+ },
175
+ {
176
+ "cell_type": "code",
177
+ "execution_count": null,
178
+ "id": "c0e5b56c-8a0b-4d8e-a112-ce87efb4e152",
179
+ "metadata": {},
180
+ "outputs": [],
181
+ "source": [
182
+ "# Convert the items into a list of json objects - a \"jsonl\" string\n",
183
+ "# Each row represents a message in the form:\n",
184
+ "# {\"messages\" : [{\"role\": \"system\", \"content\": \"You estimate prices...\n",
185
+ "\n",
186
+ "\n",
187
+ "def make_jsonl(items):\n",
188
+ " result = \"\"\n",
189
+ " for item in items:\n",
190
+ " messages = messages_for(item)\n",
191
+ " messages_str = json.dumps(messages)\n",
192
+ " result += '{\"messages\": ' + messages_str +'}\\n'\n",
193
+ " return result.strip()"
194
+ ]
195
+ },
196
+ {
197
+ "cell_type": "code",
198
+ "execution_count": null,
199
+ "id": "5e72de93-a6a6-4b35-855e-15786b97bf5f",
200
+ "metadata": {},
201
+ "outputs": [],
202
+ "source": [
203
+ "print(make_jsonl(train[:3]))"
204
+ ]
205
+ },
206
+ {
207
+ "cell_type": "code",
208
+ "execution_count": null,
209
+ "id": "7734bff0-95c4-4e67-a87e-7e2254e2c67d",
210
+ "metadata": {},
211
+ "outputs": [],
212
+ "source": [
213
+ "# Convert the items into jsonl and write them to a file\n",
214
+ "\n",
215
+ "def write_jsonl(items, filename):\n",
216
+ " with open(filename, \"w\") as f:\n",
217
+ " jsonl = make_jsonl(items)\n",
218
+ " f.write(jsonl)"
219
+ ]
220
+ },
221
+ {
222
+ "cell_type": "code",
223
+ "execution_count": null,
224
+ "id": "393d3ad8-999a-4f99-8c04-339d9166d604",
225
+ "metadata": {},
226
+ "outputs": [],
227
+ "source": [
228
+ "write_jsonl(fine_tune_train, \"fine_tune_train.jsonl\")"
229
+ ]
230
+ },
231
+ {
232
+ "cell_type": "code",
233
+ "execution_count": null,
234
+ "id": "8e23927f-d73e-4668-ac20-abe6f14a56cb",
235
+ "metadata": {},
236
+ "outputs": [],
237
+ "source": [
238
+ "write_jsonl(fine_tune_validation, \"fine_tune_validation.jsonl\")"
239
+ ]
240
+ },
241
+ {
242
+ "cell_type": "code",
243
+ "execution_count": null,
244
+ "id": "d59ad8d2-c61a-448e-b7ed-232f1606970f",
245
+ "metadata": {},
246
+ "outputs": [],
247
+ "source": [
248
+ "with open(\"fine_tune_train.jsonl\", \"rb\") as f:\n",
249
+ " train_file = openai.files.create(file=f, purpose=\"fine-tune\")"
250
+ ]
251
+ },
252
+ {
253
+ "cell_type": "code",
254
+ "execution_count": null,
255
+ "id": "083fefba-fd54-47ce-9ff3-aabbc200846f",
256
+ "metadata": {},
257
+ "outputs": [],
258
+ "source": [
259
+ "train_file"
260
+ ]
261
+ },
262
+ {
263
+ "cell_type": "code",
264
+ "execution_count": null,
265
+ "id": "97df3360-0760-4422-a556-5f26d23de6dc",
266
+ "metadata": {},
267
+ "outputs": [],
268
+ "source": [
269
+ "with open(\"fine_tune_validation.jsonl\", \"rb\") as f:\n",
270
+ " validation_file = openai.files.create(file=f, purpose=\"fine-tune\")"
271
+ ]
272
+ },
273
+ {
274
+ "cell_type": "code",
275
+ "execution_count": null,
276
+ "id": "a1abb8f3-9e52-4061-970c-fcf399d8ffa3",
277
+ "metadata": {},
278
+ "outputs": [],
279
+ "source": [
280
+ "validation_file"
281
+ ]
282
+ },
283
+ {
284
+ "cell_type": "markdown",
285
+ "id": "466052b9-9fb9-48f6-8cf9-c74e6ddc1394",
286
+ "metadata": {},
287
+ "source": [
288
+ "# Step 2\n",
289
+ "\n",
290
+ "I love Weights and Biases - a beautiful, free platform for monitoring training runs. \n",
291
+ "Weights and Biases is integrated with OpenAI for fine-tuning.\n",
292
+ "\n",
293
+ "First set up your weights & biases free account at:\n",
294
+ "\n",
295
+ "https://wandb.ai\n",
296
+ "\n",
297
+ "From the Avatar >> Settings menu, near the bottom, you can create an API key.\n",
298
+ "\n",
299
+ "Then visit the OpenAI dashboard at:\n",
300
+ "\n",
301
+ "https://platform.openai.com/account/organization\n",
302
+ "\n",
303
+ "In the integrations section, you can add your Weights & Biases key.\n",
304
+ "\n",
305
+ "## And now time to Fine-tune!"
306
+ ]
307
+ },
308
+ {
309
+ "cell_type": "code",
310
+ "execution_count": null,
311
+ "id": "c7add1a7-a746-4d6e-a5f8-e25629b8b527",
312
+ "metadata": {},
313
+ "outputs": [],
314
+ "source": [
315
+ "wandb_integration = {\"type\": \"wandb\", \"wandb\": {\"project\": \"gpt-pricer\"}}"
316
+ ]
317
+ },
318
+ {
319
+ "cell_type": "code",
320
+ "execution_count": null,
321
+ "id": "49801e69-9277-4deb-9f33-99efb6b45ac2",
322
+ "metadata": {},
323
+ "outputs": [],
324
+ "source": [
325
+ "train_file.id"
326
+ ]
327
+ },
328
+ {
329
+ "cell_type": "code",
330
+ "execution_count": null,
331
+ "id": "45421b86-5531-4e42-ab19-d6abbb8f4c13",
332
+ "metadata": {},
333
+ "outputs": [],
334
+ "source": [
335
+ "openai.fine_tuning.jobs.create(\n",
336
+ " training_file=train_file.id,\n",
337
+ " validation_file=validation_file.id,\n",
338
+ " model=\"gpt-4o-mini-2024-07-18\",\n",
339
+ " seed=42,\n",
340
+ " hyperparameters={\"n_epochs\": 1},\n",
341
+ " integrations = [wandb_integration],\n",
342
+ " suffix=\"pricer\"\n",
343
+ ")"
344
+ ]
345
+ },
346
+ {
347
+ "cell_type": "code",
348
+ "execution_count": null,
349
+ "id": "aeb9de2e-542c-4e83-81c7-b6745133e48b",
350
+ "metadata": {},
351
+ "outputs": [],
352
+ "source": [
353
+ "openai.fine_tuning.jobs.list(limit=1)"
354
+ ]
355
+ },
356
+ {
357
+ "cell_type": "code",
358
+ "execution_count": null,
359
+ "id": "40d24873-8ff5-413f-b0d4-8f77c28f18e1",
360
+ "metadata": {},
361
+ "outputs": [],
362
+ "source": [
363
+ "job_id = openai.fine_tuning.jobs.list(limit=1).data[0].id"
364
+ ]
365
+ },
366
+ {
367
+ "cell_type": "code",
368
+ "execution_count": null,
369
+ "id": "a32aef35-4b38-436c-ad00-d082f758efa7",
370
+ "metadata": {},
371
+ "outputs": [],
372
+ "source": [
373
+ "job_id"
374
+ ]
375
+ },
376
+ {
377
+ "cell_type": "code",
378
+ "execution_count": null,
379
+ "id": "a7e01247-c133-48e1-93d3-c79c399e6178",
380
+ "metadata": {},
381
+ "outputs": [],
382
+ "source": [
383
+ "openai.fine_tuning.jobs.retrieve(job_id)"
384
+ ]
385
+ },
386
+ {
387
+ "cell_type": "code",
388
+ "execution_count": null,
389
+ "id": "0f5150e1-b8de-485f-8eba-cf1e5b00c117",
390
+ "metadata": {},
391
+ "outputs": [],
392
+ "source": [
393
+ "openai.fine_tuning.jobs.list_events(fine_tuning_job_id=job_id, limit=10).data"
394
+ ]
395
+ },
396
+ {
397
+ "cell_type": "markdown",
398
+ "id": "066fef03-8338-4526-9df3-89b649ad4f0a",
399
+ "metadata": {},
400
+ "source": [
401
+ "# Step 3\n",
402
+ "\n",
403
+ "Test our fine tuned model"
404
+ ]
405
+ },
406
+ {
407
+ "cell_type": "code",
408
+ "execution_count": null,
409
+ "id": "fa4488cb-3c17-4eda-abd1-53c1c68a491b",
410
+ "metadata": {},
411
+ "outputs": [],
412
+ "source": [
413
+ "fine_tuned_model_name = openai.fine_tuning.jobs.retrieve(job_id).fine_tuned_model"
414
+ ]
415
+ },
416
+ {
417
+ "cell_type": "code",
418
+ "execution_count": null,
419
+ "id": "e9370937-5a6f-4724-8265-b208663b4450",
420
+ "metadata": {},
421
+ "outputs": [],
422
+ "source": [
423
+ "fine_tuned_model_name"
424
+ ]
425
+ },
426
+ {
427
+ "cell_type": "code",
428
+ "execution_count": null,
429
+ "id": "66ea68e8-ab1b-4f0d-aba4-a59574d8f85e",
430
+ "metadata": {},
431
+ "outputs": [],
432
+ "source": [
433
+ "# The prompt\n",
434
+ "\n",
435
+ "def messages_for(item):\n",
436
+ " system_message = \"You estimate prices of items. Reply only with the price, no explanation\"\n",
437
+ " user_prompt = item.test_prompt().replace(\" to the nearest dollar\",\"\").replace(\"\\n\\nPrice is $\",\"\")\n",
438
+ " return [\n",
439
+ " {\"role\": \"system\", \"content\": system_message},\n",
440
+ " {\"role\": \"user\", \"content\": user_prompt},\n",
441
+ " {\"role\": \"assistant\", \"content\": \"Price is $\"}\n",
442
+ " ]"
443
+ ]
444
+ },
445
+ {
446
+ "cell_type": "code",
447
+ "execution_count": null,
448
+ "id": "4ff92d61-0d27-4b0d-8b32-c9891016509b",
449
+ "metadata": {},
450
+ "outputs": [],
451
+ "source": [
452
+ "# Try this out\n",
453
+ "\n",
454
+ "messages_for(test[0])"
455
+ ]
456
+ },
457
+ {
458
+ "cell_type": "code",
459
+ "execution_count": null,
460
+ "id": "b1af1888-f94a-4106-b0d8-8a70939eec4e",
461
+ "metadata": {},
462
+ "outputs": [],
463
+ "source": [
464
+ "# A utility function to extract the price from a string\n",
465
+ "\n",
466
+ "def get_price(s):\n",
467
+ " s = s.replace('$','').replace(',','')\n",
468
+ " match = re.search(r\"[-+]?\\d*\\.\\d+|\\d+\", s)\n",
469
+ " return float(match.group()) if match else 0"
470
+ ]
471
+ },
472
+ {
473
+ "cell_type": "code",
474
+ "execution_count": null,
475
+ "id": "f138c5b7-bcc1-4085-aced-68dad1bf36b4",
476
+ "metadata": {},
477
+ "outputs": [],
478
+ "source": [
479
+ "get_price(\"The price is roughly $99.99 because blah blah\")"
480
+ ]
481
+ },
482
+ {
483
+ "cell_type": "code",
484
+ "execution_count": null,
485
+ "id": "501a2a7a-69c8-451b-bbc0-398bcb9e1612",
486
+ "metadata": {},
487
+ "outputs": [],
488
+ "source": [
489
+ "# The function for gpt-4o-mini\n",
490
+ "\n",
491
+ "def gpt_fine_tuned(item):\n",
492
+ " response = openai.chat.completions.create(\n",
493
+ " model=fine_tuned_model_name, \n",
494
+ " messages=messages_for(item),\n",
495
+ " seed=42,\n",
496
+ " max_tokens=7\n",
497
+ " )\n",
498
+ " reply = response.choices[0].message.content\n",
499
+ " return get_price(reply)"
500
+ ]
501
+ },
502
+ {
503
+ "cell_type": "code",
504
+ "execution_count": null,
505
+ "id": "843d88b4-364a-431b-b48b-8a7c1f68b786",
506
+ "metadata": {},
507
+ "outputs": [],
508
+ "source": [
509
+ "print(test[0].price)\n",
510
+ "print(gpt_fine_tuned(test[0]))"
511
+ ]
512
+ },
513
+ {
514
+ "cell_type": "code",
515
+ "execution_count": null,
516
+ "id": "edd7ada0-15b7-42ec-bbbb-1250e0eb9af1",
517
+ "metadata": {},
518
+ "outputs": [],
519
+ "source": [
520
+ "print(test[0].test_prompt())"
521
+ ]
522
+ },
523
+ {
524
+ "cell_type": "code",
525
+ "execution_count": null,
526
+ "id": "36bdd2c9-1859-4f99-a09f-3ec83b845b30",
527
+ "metadata": {},
528
+ "outputs": [],
529
+ "source": [
530
+ "Tester.test(gpt_fine_tuned, test)"
531
+ ]
532
+ }
533
+ ],
534
+ "metadata": {
535
+ "kernelspec": {
536
+ "display_name": "Python 3 (ipykernel)",
537
+ "language": "python",
538
+ "name": "python3"
539
+ },
540
+ "language_info": {
541
+ "codemirror_mode": {
542
+ "name": "ipython",
543
+ "version": 3
544
+ },
545
+ "file_extension": ".py",
546
+ "mimetype": "text/x-python",
547
+ "name": "python",
548
+ "nbconvert_exporter": "python",
549
+ "pygments_lexer": "ipython3",
550
+ "version": "3.11.11"
551
+ }
552
+ },
553
+ "nbformat": 4,
554
+ "nbformat_minor": 5
555
+ }
week6/.ipynb_checkpoints/day5-results-checkpoint.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
week6/.ipynb_checkpoints/items-checkpoint.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+ from transformers import AutoTokenizer
3
+ import re
4
+
5
+ BASE_MODEL = "meta-llama/Meta-Llama-3.1-8B"
6
+
7
+ MIN_TOKENS = 150 # Any less than this, and we don't have enough useful content
8
+ MAX_TOKENS = 160 # Truncate after this many tokens. Then after adding in prompt text, we will get to around 180 tokens
9
+
10
+ MIN_CHARS = 300
11
+ CEILING_CHARS = MAX_TOKENS * 7
12
+
13
+ class Item:
14
+ """
15
+ An Item is a cleaned, curated datapoint of a Product with a Price
16
+ """
17
+
18
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
19
+ PREFIX = "Price is $"
20
+ QUESTION = "How much does this cost to the nearest dollar?"
21
+ REMOVALS = ['"Batteries Included?": "No"', '"Batteries Included?": "Yes"', '"Batteries Required?": "No"', '"Batteries Required?": "Yes"', "By Manufacturer", "Item", "Date First", "Package", ":", "Number of", "Best Sellers", "Number", "Product "]
22
+
23
+ title: str
24
+ price: float
25
+ category: str
26
+ token_count: int = 0
27
+ details: Optional[str]
28
+ prompt: Optional[str] = None
29
+ include = False
30
+
31
+ def __init__(self, data, price):
32
+ self.title = data['title']
33
+ self.price = price
34
+ self.parse(data)
35
+
36
+ def scrub_details(self):
37
+ """
38
+ Clean up the details string by removing common text that doesn't add value
39
+ """
40
+ details = self.details
41
+ for remove in self.REMOVALS:
42
+ details = details.replace(remove, "")
43
+ return details
44
+
45
+ def scrub(self, stuff):
46
+ """
47
+ Clean up the provided text by removing unnecessary characters and whitespace
48
+ Also remove words that are 7+ chars and contain numbers, as these are likely irrelevant product numbers
49
+ """
50
+ stuff = re.sub(r'[:\[\]"{}【】\s]+', ' ', stuff).strip()
51
+ stuff = stuff.replace(" ,", ",").replace(",,,",",").replace(",,",",")
52
+ words = stuff.split(' ')
53
+ select = [word for word in words if len(word)<7 or not any(char.isdigit() for char in word)]
54
+ return " ".join(select)
55
+
56
+ def parse(self, data):
57
+ """
58
+ Parse this datapoint and if it fits within the allowed Token range,
59
+ then set include to True
60
+ """
61
+ contents = '\n'.join(data['description'])
62
+ if contents:
63
+ contents += '\n'
64
+ features = '\n'.join(data['features'])
65
+ if features:
66
+ contents += features + '\n'
67
+ self.details = data['details']
68
+ if self.details:
69
+ contents += self.scrub_details() + '\n'
70
+ if len(contents) > MIN_CHARS:
71
+ contents = contents[:CEILING_CHARS]
72
+ text = f"{self.scrub(self.title)}\n{self.scrub(contents)}"
73
+ tokens = self.tokenizer.encode(text, add_special_tokens=False)
74
+ if len(tokens) > MIN_TOKENS:
75
+ tokens = tokens[:MAX_TOKENS]
76
+ text = self.tokenizer.decode(tokens)
77
+ self.make_prompt(text)
78
+ self.include = True
79
+
80
+ def make_prompt(self, text):
81
+ """
82
+ Set the prompt instance variable to be a prompt appropriate for training
83
+ """
84
+ self.prompt = f"{self.QUESTION}\n\n{text}\n\n"
85
+ self.prompt += f"{self.PREFIX}{str(round(self.price))}.00"
86
+ self.token_count = len(self.tokenizer.encode(self.prompt, add_special_tokens=False))
87
+
88
+ def test_prompt(self):
89
+ """
90
+ Return a prompt suitable for testing, with the actual price removed
91
+ """
92
+ return self.prompt.split(self.PREFIX)[0] + self.PREFIX
93
+
94
+ def __repr__(self):
95
+ """
96
+ Return a String version of this Item
97
+ """
98
+ return f"<{self.title} = ${self.price}>"
99
+
100
+
101
+
102
+
103
+
week6/.ipynb_checkpoints/lite-checkpoint.ipynb ADDED
@@ -0,0 +1,424 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "28a0673e-96b5-43f2-8a8b-bd033bf851b0",
6
+ "metadata": {},
7
+ "source": [
8
+ "# The Product Pricer Continued\n",
9
+ "\n",
10
+ "A model that can estimate how much something costs, from its description.\n",
11
+ "\n",
12
+ "## Data Curation Part 2\n",
13
+ "\n",
14
+ "Today we'll extend our dataset to a greater coverage, and craft it into an excellent dataset for training.\n",
15
+ "\n",
16
+ "The dataset is here: \n",
17
+ "https://huggingface.co/datasets/McAuley-Lab/Amazon-Reviews-2023\n",
18
+ "\n",
19
+ "And the folder with all the product datasets is here: \n",
20
+ "https://huggingface.co/datasets/McAuley-Lab/Amazon-Reviews-2023/tree/main/raw/meta_categories\n",
21
+ "\n",
22
+ "## The Lite dataset\n",
23
+ "\n",
24
+ "This notebook is an alternative to `day2.ipynb` that creates a smaller dataset for Home Appliances only, to keep training fast and costs low. You may need to update names of future notebooks to reflect that you have built the \"lite\" dataset not the full dataset."
25
+ ]
26
+ },
27
+ {
28
+ "cell_type": "code",
29
+ "execution_count": null,
30
+ "id": "67cedf85-8125-4322-998e-9375fe745597",
31
+ "metadata": {},
32
+ "outputs": [],
33
+ "source": [
34
+ "# imports\n",
35
+ "\n",
36
+ "import os\n",
37
+ "import random\n",
38
+ "from dotenv import load_dotenv\n",
39
+ "from huggingface_hub import login\n",
40
+ "from datasets import load_dataset, Dataset, DatasetDict\n",
41
+ "from items import Item\n",
42
+ "from loaders import ItemLoader\n",
43
+ "import matplotlib.pyplot as plt\n",
44
+ "from collections import Counter, defaultdict\n",
45
+ "import numpy as np\n",
46
+ "import pickle"
47
+ ]
48
+ },
49
+ {
50
+ "cell_type": "code",
51
+ "execution_count": null,
52
+ "id": "7390a6aa-79cb-4dea-b6d7-de7e4b13e472",
53
+ "metadata": {},
54
+ "outputs": [],
55
+ "source": [
56
+ "# environment\n",
57
+ "\n",
58
+ "load_dotenv()\n",
59
+ "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
60
+ "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
61
+ "os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
62
+ ]
63
+ },
64
+ {
65
+ "cell_type": "code",
66
+ "execution_count": null,
67
+ "id": "0732274a-aa6a-44fc-aee2-40dc8a8e4451",
68
+ "metadata": {},
69
+ "outputs": [],
70
+ "source": [
71
+ "# Log in to HuggingFace\n",
72
+ "\n",
73
+ "hf_token = os.environ['HF_TOKEN']\n",
74
+ "login(hf_token, add_to_git_credential=True)"
75
+ ]
76
+ },
77
+ {
78
+ "cell_type": "code",
79
+ "execution_count": null,
80
+ "id": "1adcf323-de9d-4c24-a9c3-d7ae554d06ca",
81
+ "metadata": {},
82
+ "outputs": [],
83
+ "source": [
84
+ "%matplotlib inline"
85
+ ]
86
+ },
87
+ {
88
+ "cell_type": "markdown",
89
+ "id": "01065d69-765c-42c8-9f90-68b8c8754068",
90
+ "metadata": {},
91
+ "source": [
92
+ "## The ItemLoader code\n",
93
+ "\n",
94
+ "Look in loaders.py - there's some useful code to make life easier for us"
95
+ ]
96
+ },
97
+ {
98
+ "cell_type": "markdown",
99
+ "id": "e2b6dc50-ac5c-4cf2-af2e-968ed8ef86d7",
100
+ "metadata": {},
101
+ "source": [
102
+ "## Now to SCALE UP\n",
103
+ "\n",
104
+ "Let's look at all datasets of all the items that you might find in a large home retail store - electrical, electronic, office and related, but not clothes / beauty / books."
105
+ ]
106
+ },
107
+ {
108
+ "cell_type": "code",
109
+ "execution_count": null,
110
+ "id": "d1d06cd3-f3c2-44f0-a9f2-13b54ff8be5c",
111
+ "metadata": {},
112
+ "outputs": [],
113
+ "source": [
114
+ "dataset_names = [\n",
115
+ " # \"Automotive\",\n",
116
+ " # \"Electronics\",\n",
117
+ " # \"Office_Products\",\n",
118
+ " # \"Tools_and_Home_Improvement\",\n",
119
+ " # \"Cell_Phones_and_Accessories\",\n",
120
+ " # \"Toys_and_Games\",\n",
121
+ " \"Appliances\",\n",
122
+ " # \"Musical_Instruments\",\n",
123
+ "]"
124
+ ]
125
+ },
126
+ {
127
+ "cell_type": "code",
128
+ "execution_count": null,
129
+ "id": "aa8fd0f0-509a-4298-8fcc-e499a061e1be",
130
+ "metadata": {},
131
+ "outputs": [],
132
+ "source": [
133
+ "items = []\n",
134
+ "for dataset_name in dataset_names:\n",
135
+ " loader = ItemLoader(dataset_name)\n",
136
+ " items.extend(loader.load())"
137
+ ]
138
+ },
139
+ {
140
+ "cell_type": "code",
141
+ "execution_count": null,
142
+ "id": "3e29a5ab-ca61-41cc-9b33-22d374681b85",
143
+ "metadata": {},
144
+ "outputs": [],
145
+ "source": [
146
+ "print(f\"A grand total of {len(items):,} items\")"
147
+ ]
148
+ },
149
+ {
150
+ "cell_type": "code",
151
+ "execution_count": null,
152
+ "id": "89078cb1-9679-4eb0-b295-599b8586bcd1",
153
+ "metadata": {},
154
+ "outputs": [],
155
+ "source": [
156
+ "# Plot the distribution of token counts again\n",
157
+ "\n",
158
+ "tokens = [item.token_count for item in items]\n",
159
+ "plt.figure(figsize=(15, 6))\n",
160
+ "plt.title(f\"Token counts: Avg {sum(tokens)/len(tokens):,.1f} and highest {max(tokens):,}\\n\")\n",
161
+ "plt.xlabel('Length (tokens)')\n",
162
+ "plt.ylabel('Count')\n",
163
+ "plt.hist(tokens, rwidth=0.7, color=\"skyblue\", bins=range(0, 300, 10))\n",
164
+ "plt.show()"
165
+ ]
166
+ },
167
+ {
168
+ "cell_type": "code",
169
+ "execution_count": null,
170
+ "id": "c38e0c43-9f7a-450e-a911-c94d37d9b9c3",
171
+ "metadata": {},
172
+ "outputs": [],
173
+ "source": [
174
+ "# Plot the distribution of prices\n",
175
+ "\n",
176
+ "prices = [item.price for item in items]\n",
177
+ "plt.figure(figsize=(15, 6))\n",
178
+ "plt.title(f\"Prices: Avg {sum(prices)/len(prices):,.1f} and highest {max(prices):,}\\n\")\n",
179
+ "plt.xlabel('Price ($)')\n",
180
+ "plt.ylabel('Count')\n",
181
+ "plt.hist(prices, rwidth=0.7, color=\"blueviolet\", bins=range(0, 1000, 10))\n",
182
+ "plt.show()"
183
+ ]
184
+ },
185
+ {
186
+ "cell_type": "markdown",
187
+ "id": "ac046cc1-2717-415b-96ad-b73b2950d235",
188
+ "metadata": {},
189
+ "source": [
190
+ "# Dataset Curated!\n",
191
+ "\n",
192
+ "We've crafted an excellent dataset.\n",
193
+ "\n",
194
+ "Let's do some final checks"
195
+ ]
196
+ },
197
+ {
198
+ "cell_type": "code",
199
+ "execution_count": null,
200
+ "id": "70219e99-22cc-4e08-9121-51f9707caef0",
201
+ "metadata": {},
202
+ "outputs": [],
203
+ "source": [
204
+ "# How does the price vary with the character count of the prompt?\n",
205
+ "\n",
206
+ "sample = items\n",
207
+ "\n",
208
+ "sizes = [len(item.prompt) for item in sample]\n",
209
+ "prices = [item.price for item in sample]\n",
210
+ "\n",
211
+ "# Create the scatter plot\n",
212
+ "plt.figure(figsize=(15, 8))\n",
213
+ "plt.scatter(sizes, prices, s=0.2, color=\"red\")\n",
214
+ "\n",
215
+ "# Add labels and title\n",
216
+ "plt.xlabel('Size')\n",
217
+ "plt.ylabel('Price')\n",
218
+ "plt.title('Is there a simple correlation?')\n",
219
+ "\n",
220
+ "# Display the plot\n",
221
+ "plt.show()"
222
+ ]
223
+ },
224
+ {
225
+ "cell_type": "code",
226
+ "execution_count": null,
227
+ "id": "30ae1453-b9fc-40db-8310-65d850c4b1da",
228
+ "metadata": {},
229
+ "outputs": [],
230
+ "source": [
231
+ "def report(item):\n",
232
+ " prompt = item.prompt\n",
233
+ " tokens = Item.tokenizer.encode(item.prompt)\n",
234
+ " print(prompt)\n",
235
+ " print(tokens[-10:])\n",
236
+ " print(Item.tokenizer.batch_decode(tokens[-10:]))"
237
+ ]
238
+ },
239
+ {
240
+ "cell_type": "code",
241
+ "execution_count": null,
242
+ "id": "d9998b8d-d746-4541-9ac2-701108e0e8fb",
243
+ "metadata": {},
244
+ "outputs": [],
245
+ "source": [
246
+ "report(sample[50])"
247
+ ]
248
+ },
249
+ {
250
+ "cell_type": "markdown",
251
+ "id": "7aa0a3fc-d2fe-4e6e-8fdb-96913df2f588",
252
+ "metadata": {},
253
+ "source": [
254
+ "## Observation\n",
255
+ "\n",
256
+ "An interesting thing about the Llama tokenizer is that every number from 1 to 999 gets mapped to 1 token, much as we saw with gpt-4o. The same is not true of qwen2, gemma and phi3, which all map individual digits to tokens. This does turn out to be a bit useful for our project, although it's not an essential requirement."
257
+ ]
258
+ },
259
+ {
260
+ "cell_type": "markdown",
261
+ "id": "0f03c0ee-3103-4603-af5c-b484884a3aa2",
262
+ "metadata": {},
263
+ "source": [
264
+ "# Finally\n",
265
+ "\n",
266
+ "It's time to break down our data into a training, test and validation dataset.\n",
267
+ "\n",
268
+ "It's typical to use 5%-10% of your data for testing purposes, but actually we have far more than we need at this point. We'll take 25,000 points for training, and we'll reserve 2,000 for testing, although we won't use all of them.\n"
269
+ ]
270
+ },
271
+ {
272
+ "cell_type": "code",
273
+ "execution_count": null,
274
+ "id": "3b163ca2-18ef-4c26-8e9d-88eb55f114f6",
275
+ "metadata": {},
276
+ "outputs": [],
277
+ "source": [
278
+ "random.seed(42)\n",
279
+ "random.shuffle(sample)\n",
280
+ "train = sample[:25_000]\n",
281
+ "test = sample[25_000:27_000]\n",
282
+ "print(f\"Divided into a training set of {len(train):,} items and test set of {len(test):,} items\")"
283
+ ]
284
+ },
285
+ {
286
+ "cell_type": "code",
287
+ "execution_count": null,
288
+ "id": "299b9816-8885-4798-829a-69d66d60eb01",
289
+ "metadata": {},
290
+ "outputs": [],
291
+ "source": [
292
+ "print(train[0].prompt)"
293
+ ]
294
+ },
295
+ {
296
+ "cell_type": "code",
297
+ "execution_count": null,
298
+ "id": "97222da3-9f2c-4d15-a5cd-5e5f8dbde6cc",
299
+ "metadata": {},
300
+ "outputs": [],
301
+ "source": [
302
+ "print(test[0].test_prompt())"
303
+ ]
304
+ },
305
+ {
306
+ "cell_type": "code",
307
+ "execution_count": null,
308
+ "id": "7a116369-335a-412b-b70c-2add6675c2e3",
309
+ "metadata": {},
310
+ "outputs": [],
311
+ "source": [
312
+ "# Plot the distribution of prices in the first 250 test points\n",
313
+ "\n",
314
+ "prices = [float(item.price) for item in test[:250]]\n",
315
+ "plt.figure(figsize=(15, 6))\n",
316
+ "plt.title(f\"Avg {sum(prices)/len(prices):.2f} and highest {max(prices):,.2f}\\n\")\n",
317
+ "plt.xlabel('Price ($)')\n",
318
+ "plt.ylabel('Count')\n",
319
+ "plt.hist(prices, rwidth=0.7, color=\"darkblue\", bins=range(0, 1000, 10))\n",
320
+ "plt.show()"
321
+ ]
322
+ },
323
+ {
324
+ "cell_type": "markdown",
325
+ "id": "d522d752-6f66-4786-a4dc-8ef51842558c",
326
+ "metadata": {},
327
+ "source": [
328
+ "# Finally - upload your brand new dataset\n",
329
+ "\n",
330
+ "Convert to prompts and upload to HuggingFace hub"
331
+ ]
332
+ },
333
+ {
334
+ "cell_type": "code",
335
+ "execution_count": null,
336
+ "id": "fa11b3e5-fcf4-4efc-a573-f6f67fec3e73",
337
+ "metadata": {},
338
+ "outputs": [],
339
+ "source": [
340
+ "train_prompts = [item.prompt for item in train]\n",
341
+ "train_prices = [item.price for item in train]\n",
342
+ "test_prompts = [item.test_prompt() for item in test]\n",
343
+ "test_prices = [item.price for item in test]"
344
+ ]
345
+ },
346
+ {
347
+ "cell_type": "code",
348
+ "execution_count": null,
349
+ "id": "b020ab1b-7153-4e5f-b8a3-d5bc2fafb6df",
350
+ "metadata": {},
351
+ "outputs": [],
352
+ "source": [
353
+ "# Create a Dataset from the lists\n",
354
+ "\n",
355
+ "train_dataset = Dataset.from_dict({\"text\": train_prompts, \"price\": train_prices})\n",
356
+ "test_dataset = Dataset.from_dict({\"text\": test_prompts, \"price\": test_prices})\n",
357
+ "dataset = DatasetDict({\n",
358
+ " \"train\": train_dataset,\n",
359
+ " \"test\": test_dataset\n",
360
+ "})"
361
+ ]
362
+ },
363
+ {
364
+ "cell_type": "code",
365
+ "execution_count": null,
366
+ "id": "17639641-fb55-44e2-a463-b0b394d00f32",
367
+ "metadata": {},
368
+ "outputs": [],
369
+ "source": [
370
+ "DATASET_NAME = \"ed-donner/lite-data\"\n",
371
+ "dataset.push_to_hub(DATASET_NAME, private=True)"
372
+ ]
373
+ },
374
+ {
375
+ "cell_type": "code",
376
+ "execution_count": null,
377
+ "id": "b85733ba-d165-4f07-b055-46803543edfe",
378
+ "metadata": {},
379
+ "outputs": [],
380
+ "source": [
381
+ "# One more thing!\n",
382
+ "# Let's pickle the training and test dataset so we don't have to execute all this code next time!\n",
383
+ "\n",
384
+ "with open('train_lite.pkl', 'wb') as file:\n",
385
+ " pickle.dump(train, file)\n",
386
+ "\n",
387
+ "with open('test_lite.pkl', 'wb') as file:\n",
388
+ " pickle.dump(test, file)"
389
+ ]
390
+ },
391
+ {
392
+ "cell_type": "markdown",
393
+ "id": "2b58dc61-747f-46f7-b9e0-c205db4f3e5e",
394
+ "metadata": {},
395
+ "source": [
396
+ "## Todos for you:\n",
397
+ "\n",
398
+ "- Investigate the dataset more!\n",
399
+ "- Confirm that the tokenizer tokenizes all 3 digit prices into 1 token"
400
+ ]
401
+ }
402
+ ],
403
+ "metadata": {
404
+ "kernelspec": {
405
+ "display_name": "Python 3 (ipykernel)",
406
+ "language": "python",
407
+ "name": "python3"
408
+ },
409
+ "language_info": {
410
+ "codemirror_mode": {
411
+ "name": "ipython",
412
+ "version": 3
413
+ },
414
+ "file_extension": ".py",
415
+ "mimetype": "text/x-python",
416
+ "name": "python",
417
+ "nbconvert_exporter": "python",
418
+ "pygments_lexer": "ipython3",
419
+ "version": "3.11.10"
420
+ }
421
+ },
422
+ "nbformat": 4,
423
+ "nbformat_minor": 5
424
+ }
week6/.ipynb_checkpoints/loaders-checkpoint.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datetime import datetime
2
+ from tqdm import tqdm
3
+ from datasets import load_dataset
4
+ from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
5
+ from items import Item
6
+
7
+ CHUNK_SIZE = 1000
8
+ MIN_PRICE = 0.5
9
+ MAX_PRICE = 999.49
10
+
11
+ class ItemLoader:
12
+
13
+
14
+ def __init__(self, name):
15
+ self.name = name
16
+ self.dataset = None
17
+
18
+ def from_datapoint(self, datapoint):
19
+ """
20
+ Try to create an Item from this datapoint
21
+ Return the Item if successful, or None if it shouldn't be included
22
+ """
23
+ try:
24
+ price_str = datapoint['price']
25
+ if price_str:
26
+ price = float(price_str)
27
+ if MIN_PRICE <= price <= MAX_PRICE:
28
+ item = Item(datapoint, price)
29
+ return item if item.include else None
30
+ except ValueError:
31
+ return None
32
+
33
+ def from_chunk(self, chunk):
34
+ """
35
+ Create a list of Items from this chunk of elements from the Dataset
36
+ """
37
+ batch = []
38
+ for datapoint in chunk:
39
+ result = self.from_datapoint(datapoint)
40
+ if result:
41
+ batch.append(result)
42
+ return batch
43
+
44
+ def chunk_generator(self):
45
+ """
46
+ Iterate over the Dataset, yielding chunks of datapoints at a time
47
+ """
48
+ size = len(self.dataset)
49
+ for i in range(0, size, CHUNK_SIZE):
50
+ yield self.dataset.select(range(i, min(i + CHUNK_SIZE, size)))
51
+
52
+ def load_in_parallel(self, workers):
53
+ """
54
+ Use concurrent.futures to farm out the work to process chunks of datapoints -
55
+ This speeds up processing significantly, but will tie up your computer while it's doing so!
56
+ """
57
+ results = []
58
+ chunk_count = (len(self.dataset) // CHUNK_SIZE) + 1
59
+ with ProcessPoolExecutor(max_workers=workers) as pool:
60
+ for batch in tqdm(pool.map(self.from_chunk, self.chunk_generator()), total=chunk_count):
61
+ results.extend(batch)
62
+ for result in results:
63
+ result.category = self.name
64
+ return results
65
+
66
+ def load(self, workers=8):
67
+ """
68
+ Load in this dataset; the workers parameter specifies how many processes
69
+ should work on loading and scrubbing the data
70
+ """
71
+ start = datetime.now()
72
+ print(f"Loading dataset {self.name}", flush=True)
73
+ self.dataset = load_dataset("McAuley-Lab/Amazon-Reviews-2023", f"raw_meta_{self.name}", split="full", trust_remote_code=True)
74
+ results = self.load_in_parallel(workers)
75
+ finish = datetime.now()
76
+ print(f"Completed {self.name} with {len(results):,} datapoints in {(finish-start).total_seconds()/60:.1f} mins", flush=True)
77
+ return results
78
+
79
+
80
+
81
+
week6/.ipynb_checkpoints/similarity search-checkpoint.ipynb ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [],
3
+ "metadata": {},
4
+ "nbformat": 4,
5
+ "nbformat_minor": 5
6
+ }
week6/.ipynb_checkpoints/untitled-checkpoint.txt ADDED
File without changes
week6/Untitled.ipynb ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "dc1bc613-e014-425d-a95e-3f7a0860b1ca",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import ollama"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": 8,
16
+ "id": "f3766e3b-7a0e-4c09-b465-818d0bfa70e3",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "def chat(topic):\n",
21
+ " system = \"You are a therapist, your client is very lonely and addicted\"\n",
22
+ " user = \"Here's the topic on which you have to say a joke: \"\n",
23
+ " user += topic\n",
24
+ " messag = [{'role':'system', 'content':system}, {'role':'user','content':user}]\n",
25
+ " res = ollama.chat(model = 'llama3.2', messages=messag)\n",
26
+ " return res['message']['content']\n",
27
+ " \n",
28
+ " \n",
29
+ " "
30
+ ]
31
+ },
32
+ {
33
+ "cell_type": "code",
34
+ "execution_count": 9,
35
+ "id": "650ff21b-b5c1-4812-838e-f5fc3479d06f",
36
+ "metadata": {},
37
+ "outputs": [
38
+ {
39
+ "name": "stdout",
40
+ "output_type": "stream",
41
+ "text": [
42
+ "* Running on local URL: http://127.0.0.1:7863\n",
43
+ "\n",
44
+ "To create a public link, set `share=True` in `launch()`.\n"
45
+ ]
46
+ },
47
+ {
48
+ "data": {
49
+ "text/html": [
50
+ "<div><iframe src=\"http://127.0.0.1:7863/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
51
+ ],
52
+ "text/plain": [
53
+ "<IPython.core.display.HTML object>"
54
+ ]
55
+ },
56
+ "metadata": {},
57
+ "output_type": "display_data"
58
+ },
59
+ {
60
+ "data": {
61
+ "text/plain": []
62
+ },
63
+ "execution_count": 9,
64
+ "metadata": {},
65
+ "output_type": "execute_result"
66
+ }
67
+ ],
68
+ "source": [
69
+ "import gradio as gr\n",
70
+ "gr.Interface(fn=chat, inputs='textbox',outputs='textbox').launch()"
71
+ ]
72
+ },
73
+ {
74
+ "cell_type": "code",
75
+ "execution_count": null,
76
+ "id": "81c55600-483b-4c12-825d-ab2b58de9daf",
77
+ "metadata": {},
78
+ "outputs": [],
79
+ "source": []
80
+ }
81
+ ],
82
+ "metadata": {
83
+ "kernelspec": {
84
+ "display_name": "Python 3 (ipykernel)",
85
+ "language": "python",
86
+ "name": "python3"
87
+ },
88
+ "language_info": {
89
+ "codemirror_mode": {
90
+ "name": "ipython",
91
+ "version": 3
92
+ },
93
+ "file_extension": ".py",
94
+ "mimetype": "text/x-python",
95
+ "name": "python",
96
+ "nbconvert_exporter": "python",
97
+ "pygments_lexer": "ipython3",
98
+ "version": "3.11.11"
99
+ }
100
+ },
101
+ "nbformat": 4,
102
+ "nbformat_minor": 5
103
+ }
week6/__pycache__/items.cpython-311.pyc ADDED
Binary file (6.35 kB). View file
 
week6/community-contributions/ems_week6_day4_gemini_results.ipynb ADDED
@@ -0,0 +1,313 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "db8736a7-ed94-441c-9556-831fa57b5a10",
6
+ "metadata": {},
7
+ "source": [
8
+ "# The Product Pricer Continued...\n",
9
+ "\n",
10
+ "## Testing Gemini-1.5-pro model"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": null,
16
+ "id": "681c717b-4c24-4ac3-a5f3-3c5881d6e70a",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "import os\n",
21
+ "import re\n",
22
+ "from dotenv import load_dotenv\n",
23
+ "import matplotlib.pyplot as plt\n",
24
+ "import pickle\n",
25
+ "import google.generativeai as google_genai\n",
26
+ "import time"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "execution_count": null,
32
+ "id": "21a3833e-4093-43b0-8f7b-839c50b911ea",
33
+ "metadata": {},
34
+ "outputs": [],
35
+ "source": [
36
+ "from items import Item\n",
37
+ "from testing import Tester "
38
+ ]
39
+ },
40
+ {
41
+ "cell_type": "code",
42
+ "execution_count": null,
43
+ "id": "36d05bdc-0155-4c72-a7ee-aa4e614ffd3c",
44
+ "metadata": {},
45
+ "outputs": [],
46
+ "source": [
47
+ "# environment\n",
48
+ "load_dotenv()\n",
49
+ "os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY', 'your-key-if-not-using-env')"
50
+ ]
51
+ },
52
+ {
53
+ "cell_type": "code",
54
+ "execution_count": null,
55
+ "id": "b0a6fb86-74a4-403c-ab25-6db2d74e9d2b",
56
+ "metadata": {},
57
+ "outputs": [],
58
+ "source": [
59
+ "google_genai.configure()"
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "code",
64
+ "execution_count": null,
65
+ "id": "c830ed3e-24ee-4af6-a07b-a1bfdcd39278",
66
+ "metadata": {},
67
+ "outputs": [],
68
+ "source": [
69
+ "%matplotlib inline"
70
+ ]
71
+ },
72
+ {
73
+ "cell_type": "code",
74
+ "execution_count": null,
75
+ "id": "5c9b05f4-c9eb-462c-8d86-de9140a2d985",
76
+ "metadata": {},
77
+ "outputs": [],
78
+ "source": [
79
+ "# Load in the pickle files that are located in the `pickled_dataset` folder\n",
80
+ "with open('train.pkl', 'rb') as file:\n",
81
+ " train = pickle.load(file)\n",
82
+ "\n",
83
+ "with open('test.pkl', 'rb') as file:\n",
84
+ " test = pickle.load(file)"
85
+ ]
86
+ },
87
+ {
88
+ "cell_type": "code",
89
+ "execution_count": null,
90
+ "id": "fc5c807b-c14c-458e-8cca-32bc0cc5b7c3",
91
+ "metadata": {},
92
+ "outputs": [],
93
+ "source": [
94
+ "# Function to create the messages format required for Gemini 1.5 Pro\n",
95
+ "# This function prepares the system and user messages in the format expected by Gemini models.\n",
96
+ "def gemini_messages_for(item):\n",
97
+ " system_message = \"You estimate prices of items. Reply only with the price, no explanation\"\n",
98
+ " \n",
99
+ " # Modify the test prompt by removing \"to the nearest dollar\" and \"Price is $\"\n",
100
+ " # This ensures that the model receives a cleaner, simpler prompt.\n",
101
+ " user_prompt = item.test_prompt().replace(\" to the nearest dollar\", \"\").replace(\"\\n\\nPrice is $\", \"\")\n",
102
+ "\n",
103
+ " # Reformat messages to Gemini’s expected format: messages = [{'role':'user', 'parts': ['hello']}]\n",
104
+ " return [\n",
105
+ " {\"role\": \"system\", \"parts\": [system_message]}, # System-level instruction\n",
106
+ " {\"role\": \"user\", \"parts\": [user_prompt]}, # User's query\n",
107
+ " {\"role\": \"model\", \"parts\": [\"Price is $\"]} # Assistant's expected prefix for response\n",
108
+ " ]"
109
+ ]
110
+ },
111
+ {
112
+ "cell_type": "code",
113
+ "execution_count": null,
114
+ "id": "d6da66bb-bc4b-49ad-9224-a388470ef20b",
115
+ "metadata": {},
116
+ "outputs": [],
117
+ "source": [
118
+ "# Example usage of the gemini_messages_for function\n",
119
+ "gemini_messages_for(test[0]) # Generate message structure for the first test item"
120
+ ]
121
+ },
122
+ {
123
+ "cell_type": "code",
124
+ "execution_count": null,
125
+ "id": "b1af1888-f94a-4106-b0d8-8a70939eec4e",
126
+ "metadata": {},
127
+ "outputs": [],
128
+ "source": [
129
+ "# Utility function to extract the numerical price from a given string\n",
130
+ "# This function removes currency symbols and commas, then extracts the first number found.\n",
131
+ "def get_price(s):\n",
132
+ " s = s.replace('$', '').replace(',', '') # Remove currency symbols and formatting\n",
133
+ " match = re.search(r\"[-+]?\\d*\\.\\d+|\\d+\", s) # Regular expression to find a number\n",
134
+ " return float(match.group()) if match else 0 # Convert matched value to float, return 0 if no match"
135
+ ]
136
+ },
137
+ {
138
+ "cell_type": "code",
139
+ "execution_count": null,
140
+ "id": "a053c1a9-f86e-427c-a6be-ed8ec7bd63a5",
141
+ "metadata": {},
142
+ "outputs": [],
143
+ "source": [
144
+ "# Example usage of get_price function\n",
145
+ "get_price(\"The price is roughly $99.99 because blah blah\") # Expected output: 99.99"
146
+ ]
147
+ },
148
+ {
149
+ "cell_type": "code",
150
+ "execution_count": null,
151
+ "id": "34a88e34-1719-4d08-adbe-adb69dfe5e83",
152
+ "metadata": {},
153
+ "outputs": [],
154
+ "source": [
155
+ "# Function to get the estimated price using Gemini 1.5 Pro\n",
156
+ "def gemini_1_point_5_pro(item):\n",
157
+ " messages = gemini_messages_for(item) # Generate messages for the model\n",
158
+ " system_message = messages[0]['parts'][0] # Extract system-level instruction\n",
159
+ " user_messages = messages[1:] # Remove system message from messages list\n",
160
+ " \n",
161
+ " # Initialize Gemini 1.5 Pro model with system instruction\n",
162
+ " gemini = google_genai.GenerativeModel(\n",
163
+ " model_name=\"gemini-1.5-pro\",\n",
164
+ " system_instruction=system_message\n",
165
+ " )\n",
166
+ "\n",
167
+ " # Generate response using Gemini API\n",
168
+ " response = gemini.generate_content(\n",
169
+ " contents=user_messages,\n",
170
+ " generation_config=google_genai.GenerationConfig(max_output_tokens=5)\n",
171
+ " )\n",
172
+ "\n",
173
+ " # Extract text response and convert to numerical price\n",
174
+ " return get_price(response.text)"
175
+ ]
176
+ },
177
+ {
178
+ "cell_type": "code",
179
+ "execution_count": null,
180
+ "id": "d89b10bb-8ebb-42ef-9146-f6e64e6849f9",
181
+ "metadata": {},
182
+ "outputs": [],
183
+ "source": [
184
+ "# Example usage:\n",
185
+ "gemini_1_point_5_pro(test[0])"
186
+ ]
187
+ },
188
+ {
189
+ "cell_type": "code",
190
+ "execution_count": null,
191
+ "id": "89ad07e6-a28a-4625-b61e-d2ce12d440fc",
192
+ "metadata": {},
193
+ "outputs": [],
194
+ "source": [
195
+ "# Retrieve the actual price of the test item (for comparison)\n",
196
+ "test[0].price # Output: 374.41"
197
+ ]
198
+ },
199
+ {
200
+ "cell_type": "code",
201
+ "execution_count": null,
202
+ "id": "384f28e5-e51f-4cd3-8d74-30a8275530db",
203
+ "metadata": {},
204
+ "outputs": [],
205
+ "source": [
206
+ "# Test the function for gemini-1.5 pro using the Tester framework\n",
207
+ "Tester.test(gemini_1_point_5_pro, test)"
208
+ ]
209
+ },
210
+ {
211
+ "cell_type": "markdown",
212
+ "id": "9b627291-b02e-48dd-9130-703498135ddf",
213
+ "metadata": {},
214
+ "source": [
215
+ "## Five, Gemini-2.0-flash"
216
+ ]
217
+ },
218
+ {
219
+ "cell_type": "code",
220
+ "execution_count": null,
221
+ "id": "0ee393a9-7afd-404f-92f2-a64bb4d5fb8b",
222
+ "metadata": {},
223
+ "outputs": [],
224
+ "source": [
225
+ "# Function to get the estimated price using Gemini-2.0-flash-exp\n",
226
+ "def gemini_2_point_0_flash_exp(item):\n",
227
+ " messages = gemini_messages_for(item) # Generate messages for the model\n",
228
+ " system_message = messages[0]['parts'][0] # Extract system-level instruction\n",
229
+ " user_messages = messages[1:] # Remove system message from messages list\n",
230
+ " \n",
231
+ " # Initialize Gemini-2.0-flash-exp model with system instruction\n",
232
+ " gemini = google_genai.GenerativeModel(\n",
233
+ " model_name=\"gemini-2.0-flash-exp\",\n",
234
+ " system_instruction=system_message\n",
235
+ " )\n",
236
+ "\n",
237
+ " # Adding a delay to avoid hitting the API rate limit and getting a \"ResourceExhausted: 429\" error\n",
238
+ " time.sleep(5)\n",
239
+ " \n",
240
+ " # Generate response using Gemini API\n",
241
+ " response = gemini.generate_content(\n",
242
+ " contents=user_messages,\n",
243
+ " generation_config=google_genai.GenerationConfig(max_output_tokens=5)\n",
244
+ " )\n",
245
+ "\n",
246
+ " # Extract text response and convert to numerical price\n",
247
+ " return get_price(response.text)"
248
+ ]
249
+ },
250
+ {
251
+ "cell_type": "code",
252
+ "execution_count": null,
253
+ "id": "203dc6f1-309e-46eb-9957-e06eed803cc8",
254
+ "metadata": {},
255
+ "outputs": [],
256
+ "source": [
257
+ "# Example usage:\n",
258
+ "gemini_2_point_0_flash_exp(test[0]) "
259
+ ]
260
+ },
261
+ {
262
+ "cell_type": "code",
263
+ "execution_count": null,
264
+ "id": "a844df09-d347-40b9-bb79-006ec4160aab",
265
+ "metadata": {},
266
+ "outputs": [],
267
+ "source": [
268
+ "# Retrieve the actual price of the test item (for comparison)\n",
269
+ "test[0].price # Output: 374.41"
270
+ ]
271
+ },
272
+ {
273
+ "cell_type": "code",
274
+ "execution_count": null,
275
+ "id": "500b45c7-e5c1-44f2-95c9-1c3c06365339",
276
+ "metadata": {},
277
+ "outputs": [],
278
+ "source": [
279
+ "# Test the function for gemini-2.0-flash-exp using the Tester framework\n",
280
+ "Tester.test(gemini_2_point_0_flash_exp, test)"
281
+ ]
282
+ },
283
+ {
284
+ "cell_type": "code",
285
+ "execution_count": null,
286
+ "id": "746b2d12-ba92-48e2-9065-c9a108d1593b",
287
+ "metadata": {},
288
+ "outputs": [],
289
+ "source": []
290
+ }
291
+ ],
292
+ "metadata": {
293
+ "kernelspec": {
294
+ "display_name": "Python 3 (ipykernel)",
295
+ "language": "python",
296
+ "name": "python3"
297
+ },
298
+ "language_info": {
299
+ "codemirror_mode": {
300
+ "name": "ipython",
301
+ "version": 3
302
+ },
303
+ "file_extension": ".py",
304
+ "mimetype": "text/x-python",
305
+ "name": "python",
306
+ "nbconvert_exporter": "python",
307
+ "pygments_lexer": "ipython3",
308
+ "version": "3.11.11"
309
+ }
310
+ },
311
+ "nbformat": 4,
312
+ "nbformat_minor": 5
313
+ }