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71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "05432987-80bc-4aa5-8c05-277861e19307",
   "metadata": {},
   "source": [
    "## Adds docstrings/comments to code and generates code summary"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e706f175-1e83-4d2c-8613-056b2e532624",
   "metadata": {},
   "source": [
    "### Model Usage  \n",
    "\n",
    "- **Open Source Models:**\n",
    "\n",
    "  - Deployed via Endpoint: Hosted on a server and accessed remotely (Qwen 1.5-7)\n",
    "  - Run Locally on Machine: Executed directly on a local device (Ollama running Llama 3.2-1B)\n",
    "\n",
    "- **Closed Source Models:**  \n",
    "  - Accessed through API key authentication: (OpenAI, Anthropic).  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ed667df-6660-4ba3-80c5-4c1c8f7e63f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# imports\n",
    "\n",
    "import os\n",
    "import io\n",
    "import sys \n",
    "import json\n",
    "import requests\n",
    "from dotenv import load_dotenv\n",
    "from openai import OpenAI\n",
    "import google.generativeai\n",
    "import anthropic\n",
    "import ollama\n",
    "from IPython.display import Markdown, display, update_display\n",
    "import gradio as gr\n",
    "from huggingface_hub import login, InferenceClient\n",
    "from transformers import AutoTokenizer, pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9dd4bf1-48cf-44dc-9d04-0ec6e8189a3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# environment\n",
    "\n",
    "load_dotenv()\n",
    "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')\n",
    "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY')\n",
    "CODE_QWEN_URL = os.environ['CODE_QWEN_URL'] \n",
    "BIGBIRD_PEGASUS_URL = os.environ['BIGBIRD_PEGASUS_URL']\n",
    "HF_TOKEN = os.environ['HF_TOKEN']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71f671d6-50a7-43cf-9e04-52a159d67dab",
   "metadata": {},
   "outputs": [],
   "source": [
    "!ollama pull llama3.2:1b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e6f8f35-477d-4014-8fe9-874b5aee0061",
   "metadata": {},
   "outputs": [],
   "source": [
    "openai = OpenAI()\n",
    "claude = anthropic.Anthropic()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae34b79c-425a-4f04-821a-8f1d9868b146",
   "metadata": {},
   "outputs": [],
   "source": [
    "OPENAI_MODEL = \"gpt-4o-mini\"\n",
    "CLAUDE_MODEL = \"claude-3-haiku-20240307\"\n",
    "LLAMA_MODEL = \"llama3.2:1b\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80e6d920-3c94-48c4-afd8-518f415ab777",
   "metadata": {},
   "outputs": [],
   "source": [
    "code_qwen = \"Qwen/CodeQwen1.5-7B-Chat\"\n",
    "bigbird_pegasus = \"google/bigbird-pegasus-large-arxiv\"\n",
    "login(HF_TOKEN, add_to_git_credential=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "314cd8e3-2c10-4149-9818-4e6b0c05b871",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Uses Llama to Check Which Language the Code is Written In\n",
    "system_message_comments = \"You are an assistant designed to add docstrings and helpful comments to code for documentation purposes.\"\n",
    "system_message_comments += \"Respond back with properly formatted code, including docstrings and comments. Keep comments concise. \"\n",
    "system_message_comments += \"Do not respond with greetings, or any such extra output\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66fa09e4-1b79-4f53-9bb7-904d515b2f26",
   "metadata": {},
   "outputs": [],
   "source": [
    "system_message_summary = \"You are an assistant designed to summarise code for documentation purposes. You are not to display code again.\"\n",
    "system_message_summary += \"Respond back with a properly crafted summary, mentioning key details regarding to the code, such as workflow, code language.\"\n",
    "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.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea405820-f9d1-4cf1-b465-9ae5cd9016f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def user_prompt_for(code):\n",
    "    user_prompt = \"Rewrite this code to include helpful comments and docstrings. \"\n",
    "    user_prompt += \"Respond only with code.\\n\"\n",
    "    user_prompt += code\n",
    "    return user_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26c9be56-1d4f-43e5-9bc4-eb5b76da8071",
   "metadata": {},
   "outputs": [],
   "source": [
    "def user_prompt_for_summary(code):\n",
    "    user_prompt = \"Return the summary of the code.\\n\"\n",
    "    user_prompt += code\n",
    "    return user_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0ac22cb-dc96-4ae1-b00d-2747572f6945",
   "metadata": {},
   "outputs": [],
   "source": [
    "def messages_for(code):\n",
    "    messages = [\n",
    "        {\"role\": \"system\", \"content\": system_message_comments},\n",
    "        {\"role\":\"user\", \"content\" : user_prompt_for(code)}\n",
    "    ]\n",
    "    return messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eae1a8b4-68a8-4cd5-849e-0ecabd166a0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def messages_for_summary(code):\n",
    "    messages = [\n",
    "        {\"role\": \"system\", \"content\": system_message_summary},\n",
    "        {\"role\":\"user\", \"content\" : user_prompt_for_summary(code)}\n",
    "    ]\n",
    "    return messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5eb726dd-e09e-4011-8eb6-4d20f2830ff5",
   "metadata": {},
   "outputs": [],
   "source": [
    "func = \"\"\"\n",
    "import time\n",
    "\n",
    "def calculate(iterations, param1, param2):\n",
    "    result = 1.0\n",
    "    for i in range(1, iterations+1):\n",
    "        j = i * param1 - param2\n",
    "        result -= (1/j)\n",
    "        j = i * param1 + param2\n",
    "        result += (1/j)\n",
    "    return result\n",
    "\n",
    "start_time = time.time()\n",
    "result = calculate(100_000_000, 4, 1) * 4\n",
    "end_time = time.time()\n",
    "\n",
    "print(f\"Result: {result:.12f}\")\n",
    "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f61943b2-c939-4910-a670-58abaf464bb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def call_llama(code):\n",
    "    # commented code\n",
    "    messages = messages_for(code)\n",
    "    response1 = ollama.chat(model=LLAMA_MODEL, messages=messages)\n",
    "\n",
    "    # summary\n",
    "    messages = messages_for_summary(code)\n",
    "    response2 = ollama.chat(model=LLAMA_MODEL, messages=messages)\n",
    "    \n",
    "    return response1['message']['content'],response2['message']['content']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "696fb97e-807e-40ed-b0e1-beb82d1108a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def call_claude(code):\n",
    "    # commented code\n",
    "    message1 = claude.messages.create(\n",
    "        model=CLAUDE_MODEL,\n",
    "        system=system_message_comments,\n",
    "        messages=([{\"role\": \"user\", \"content\":user_prompt_for(code)}]),\n",
    "        max_tokens=500\n",
    "    )\n",
    "\n",
    "    # summary\n",
    "    message2 = claude.messages.create(\n",
    "        model=CLAUDE_MODEL,\n",
    "        system=system_message_summary,\n",
    "        messages=([{\"role\": \"user\", \"content\":user_prompt_for_summary(code)}]),\n",
    "        max_tokens=500\n",
    "    )\n",
    "    \n",
    "    return message1.content[0].text,message2.content[0].text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bf1db64-86fa-42a1-98dd-3df74607f8db",
   "metadata": {},
   "outputs": [],
   "source": [
    "def call_gpt(code):\n",
    "    # commented code\n",
    "    completion1 = openai.chat.completions.create(\n",
    "        model=OPENAI_MODEL,\n",
    "        messages=messages_for(code),\n",
    "    )\n",
    "\n",
    "    #summary\n",
    "    completion2 = openai.chat.completions.create(\n",
    "        model=OPENAI_MODEL,\n",
    "        messages=messages_for_summary(code),\n",
    "    )\n",
    "    \n",
    "    return completion1.choices[0].message.content,completion2.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6863dc42-cbcd-4a95-8b0a-cfbcbfed0764",
   "metadata": {},
   "outputs": [],
   "source": [
    "def call_codeqwen(code):\n",
    "    # commented code\n",
    "    tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
    "    messages = messages_for(code)\n",
    "    text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
    "    client = InferenceClient(CODE_QWEN_URL, token=HF_TOKEN)\n",
    "    response1 = client.text_generation(text, details=True, max_new_tokens=1000)\n",
    "\n",
    "    # summary\n",
    "    tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
    "    messages = messages_for_summary(code)\n",
    "    text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
    "    client = InferenceClient(CODE_QWEN_URL, token=HF_TOKEN)\n",
    "    response2 = client.text_generation(text, details=True, max_new_tokens=1000)\n",
    "    \n",
    "    return response1.generated_text ,response2.generated_text "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06d05c02-45e4-47da-b70b-cf433dfaca4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_docs(code,model):\n",
    "    if model == \"Llama\":\n",
    "        comments,summary = call_llama(code)\n",
    "    elif model == \"Claude\":\n",
    "        comments,summary = call_claude(code)\n",
    "    elif model == \"GPT\":\n",
    "        comments,summary = call_gpt(code)\n",
    "    elif model == \"CodeQwen\":\n",
    "        comments,summary = call_codeqwen(code)\n",
    "    else:\n",
    "        raise ValueError(\"Unknown Model\")\n",
    "    return comments,summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b4ea289-5da9-4b0e-b4d4-f8f01e466839",
   "metadata": {},
   "outputs": [],
   "source": [
    "css = \"\"\"\n",
    ".comments {background-color: #00599C;}\n",
    ".summary {background-color: #008B8B;}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89ad7c7b-b881-45d3-aadc-d7206af578fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "with gr.Blocks(css=css) as ui:\n",
    "    gr.Markdown(\"### Code Documentation and Formatting\")\n",
    "    with gr.Row():\n",
    "        code = gr.Textbox(label=\"Input Code: \", value=func, lines=10)\n",
    "    with gr.Row():\n",
    "        model = gr.Dropdown([\"GPT\",\"Claude\",\"Llama\",\"CodeQwen\"],label=\"Select model\",value=\"GPT\")\n",
    "    with gr.Row():\n",
    "        docs = gr.Button(\"Add Comments and Sumarise Code\")\n",
    "    with gr.Row():\n",
    "        commented_code = gr.Textbox(label= \"Formatted Code\", lines=10,elem_classes=[\"comments\"])\n",
    "        code_summary = gr.Textbox(label = \"Code Summary\", lines=10,elem_classes=[\"summary\"])\n",
    "    docs.click(create_docs,inputs=[code,model],outputs=[commented_code,code_summary]),"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a9e3b1c-bfe6-4b71-aac8-fa36a491c157",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "ui.launch(inbrowser=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac895aa9-e044-4598-b715-d96d1c158656",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a96877c-22b7-4ad5-b235-1cf8f8b200a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(call_llama(func))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f11de1a2-52c0-41c7-ad88-01ef5f8bc628",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}