himangshu007/Code_Explainer
0
1{2 "nbformat": 4,3 "nbformat_minor": 0,4 "metadata": {5 "colab": {6 "provenance": []7 },8 "kernelspec": {9 "name": "python3",10 "display_name": "Python 3"11 },12 "language_info": {13 "name": "python"14 }15 },16 "cells": [17 {18 "cell_type": "code",19 "execution_count": 1,20 "metadata": {21 "colab": {22 "base_uri": "https://localhost:8080/",23 "height": 56424 },25 "id": "j6uWtED5JbtK",26 "outputId": "2f8bdb19-d923-43c3-cd53-065502595cf6"27 },28 "outputs": [29 {30 "output_type": "stream",31 "name": "stdout",32 "text": [33 "Collecting google-generativeai\n",34 " Downloading google_generativeai-0.2.1-py3-none-any.whl (130 kB)\n",35 "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m130.7/130.7 kB\u001b[0m \u001b[31m1.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",36 "\u001b[?25hCollecting google-ai-generativelanguage==0.3.3 (from google-generativeai)\n",37 " Downloading google_ai_generativelanguage-0.3.3-py3-none-any.whl (267 kB)\n",38 "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m267.9/267.9 kB\u001b[0m \u001b[31m10.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",39 "\u001b[?25hRequirement already satisfied: google-auth in /usr/local/lib/python3.10/dist-packages (from google-generativeai) (2.17.3)\n",40 "Requirement already satisfied: google-api-core in /usr/local/lib/python3.10/dist-packages (from google-generativeai) (2.11.1)\n",41 "Requirement already satisfied: protobuf in /usr/local/lib/python3.10/dist-packages (from google-generativeai) (3.20.3)\n",42 "Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from google-generativeai) (4.66.1)\n",43 "Requirement already satisfied: proto-plus<2.0.0dev,>=1.22.0 in /usr/local/lib/python3.10/dist-packages (from google-ai-generativelanguage==0.3.3->google-generativeai) (1.22.3)\n",44 "Requirement already satisfied: googleapis-common-protos<2.0.dev0,>=1.56.2 in /usr/local/lib/python3.10/dist-packages (from google-api-core->google-generativeai) (1.61.0)\n",45 "Requirement already satisfied: requests<3.0.0.dev0,>=2.18.0 in /usr/local/lib/python3.10/dist-packages (from google-api-core->google-generativeai) (2.31.0)\n",46 "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-auth->google-generativeai) (5.3.1)\n",47 "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth->google-generativeai) (0.3.0)\n",48 "Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from google-auth->google-generativeai) (1.16.0)\n",49 "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth->google-generativeai) (4.9)\n",50 "Requirement already satisfied: grpcio<2.0dev,>=1.33.2 in /usr/local/lib/python3.10/dist-packages (from google-api-core->google-generativeai) (1.59.0)\n",51 "Requirement already satisfied: grpcio-status<2.0.dev0,>=1.33.2 in /usr/local/lib/python3.10/dist-packages (from google-api-core->google-generativeai) (1.48.2)\n",52 "Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in /usr/local/lib/python3.10/dist-packages (from pyasn1-modules>=0.2.1->google-auth->google-generativeai) (0.5.0)\n",53 "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core->google-generativeai) (3.3.0)\n",54 "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core->google-generativeai) (3.4)\n",55 "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core->google-generativeai) (2.0.6)\n",56 "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core->google-generativeai) (2023.7.22)\n",57 "Installing collected packages: google-ai-generativelanguage, google-generativeai\n",58 "Successfully installed google-ai-generativelanguage-0.3.3 google-generativeai-0.2.1\n"59 ]60 },61 {62 "output_type": "display_data",63 "data": {64 "application/vnd.colab-display-data+json": {65 "pip_warning": {66 "packages": [67 "google"68 ]69 }70 }71 },72 "metadata": {}73 }74 ],75 "source": [76 "!pip install google-generativeai"77 ]78 },79 {80 "cell_type": "code",81 "source": [82 "import google.generativeai as palm\n",83 "import os"84 ],85 "metadata": {86 "id": "4UFA8aQlJ32t"87 },88 "execution_count": 1,89 "outputs": []90 },91 {92 "cell_type": "markdown",93 "source": [94 "# Insert PaLM API key here\n",95 "\n",96 "<p>Use the palm.list_models function to find available models<p>\n",97 "PaLM 2 available in 4 sizes: Gecko, Otter, Bison and Unicorn (largest)\n",98 "https://developers.generativeai.google/models/language"99 ],100 "metadata": {101 "id": "OKpyM48OKNKc"102 }103 },104 {105 "cell_type": "code",106 "source": [107 "palm.configure(api_key='AIzaSyDQmcmwtSlNzNwjCnRUzwqowfvxwdGr-Ug')"108 ],109 "metadata": {110 "id": "9W9HpeXxKKUG"111 },112 "execution_count": 37,113 "outputs": []114 },115 {116 "cell_type": "code",117 "source": [118 "models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]\n",119 "model = models[0].name\n",120 "print(model)"121 ],122 "metadata": {123 "colab": {124 "base_uri": "https://localhost:8080/"125 },126 "id": "_5Dtmh_TL-Bd",127 "outputId": "a3014766-652e-4da1-81c9-b6716d70d1d0"128 },129 "execution_count": 39,130 "outputs": [131 {132 "output_type": "stream",133 "name": "stdout",134 "text": [135 "models/text-bison-001\n"136 ]137 }138 ]139 },140 {141 "cell_type": "markdown",142 "source": [143 "# Simplest Chatbot"144 ],145 "metadata": {146 "id": "F6gWQYt0MS_F"147 }148 },149 {150 "cell_type": "code",151 "source": [152 "# Set your input text\n",153 "prompt = \"Why is the sky blue?\"\n",154 "# prompt = \"What is Quantum Computing? Explain like I'm 5.\"\n",155 "\n",156 "completion = palm.generate_text(\n",157 " model=model,\n",158 " prompt=prompt,\n",159 " temperature=0,\n",160 " # temperature=0 >> more deterministic results // temperature=1 >> more randomness\n",161 " max_output_tokens=100\n",162 " # maximum length of response\n",163 ")\n",164 "\n",165 "print(completion.result)"166 ],167 "metadata": {168 "colab": {169 "base_uri": "https://localhost:8080/"170 },171 "id": "SIoorw6PMRLN",172 "outputId": "c1aadc46-dcba-472d-ca75-a173ba8be5e1"173 },174 "execution_count": 40,175 "outputs": [176 {177 "output_type": "stream",178 "name": "stdout",179 "text": [180 "The sky is blue because of a phenomenon called Rayleigh scattering. This is the scattering of light by particles that are smaller than the wavelength of light. In the case of the sky, the particles are molecules of nitrogen and oxygen.\n",181 "\n",182 "When sunlight hits these molecules, the blue light is scattered more than the other colors because it has a shorter wavelength. This is because the blue light waves are more likely to interact with the molecules of nitrogen and oxygen. The other colors of light, such as red and\n"183 ]184 }185 ]186 },187 {188 "cell_type": "markdown",189 "source": [190 "# Custom Application"191 ],192 "metadata": {193 "id": "ESspCyT7M-ss"194 }195 },196 {197 "cell_type": "markdown",198 "source": [199 "## Code Explainer"200 ],201 "metadata": {202 "id": "EvIMYfAtNQbj"203 }204 },205 {206 "cell_type": "code",207 "source": [208 "def get_completion(prompt):\n",209 " completion = palm.generate_text(\n",210 " model=model,\n",211 " prompt=prompt,\n",212 " temperature=0,\n",213 " # The maximum length of the response\n",214 " max_output_tokens=500,\n",215 " )\n",216 " response = completion.result\n",217 " return response"218 ],219 "metadata": {220 "id": "1E-CpnBjNO5P"221 },222 "execution_count": 41,223 "outputs": []224 },225 {226 "cell_type": "markdown",227 "source": [228 "## Simple Prompting"229 ],230 "metadata": {231 "id": "i5xm3hlnNbyq"232 }233 },234 {235 "cell_type": "code",236 "source": [237 "# input_code = f\"\"\"\n",238 "# x = [1, 2, 3, 4, 5]\n",239 "# y = [i**2 for i in x if i%2 == 0]\n",240 "# print(y)\n",241 "# \"\"\"\n",242 "\n",243 "input_code = f\"\"\"\n",244 "def my_func(x):\n",245 " if x > 5:\n",246 " return \"High\"\n",247 " else:\n",248 " return \"Low\"\n",249 "result = my_func(4) + my_func(6) + my_func(4)\n",250 "print(result)\n",251 "\"\"\""252 ],253 "metadata": {254 "id": "lP-PTPmRNkJu"255 },256 "execution_count": 42,257 "outputs": []258 },259 {260 "cell_type": "code",261 "source": [262 "prompt =f\"\"\"\n",263 "Your task is to act as a Python Code Explainer.\n",264 "I'll give you a Code Snippet.\n",265 "Your job is to explain the Code Snippet step-by-step.\n",266 "Also, compute the final output of the code.\n",267 "Code Snippet is shared below, delimited with triple backticks:\n",268 "```\n",269 "{input_code}\n",270 "```\n",271 "\"\"\"\n",272 "\n",273 "print(prompt)"274 ],275 "metadata": {276 "colab": {277 "base_uri": "https://localhost:8080/"278 },279 "id": "U1O-drgdOHPO",280 "outputId": "9e17e016-09cf-48c7-e3fc-e9b76bb72c89"281 },282 "execution_count": 43,283 "outputs": [284 {285 "output_type": "stream",286 "name": "stdout",287 "text": [288 "\n",289 "Your task is to act as a Python Code Explainer.\n",290 "I'll give you a Code Snippet.\n",291 "Your job is to explain the Code Snippet step-by-step.\n",292 "Also, compute the final output of the code.\n",293 "Code Snippet is shared below, delimited with triple backticks:\n",294 "```\n",295 "\n",296 "def my_func(x):\n",297 " if x > 5:\n",298 " return \"High\"\n",299 " else:\n",300 " return \"Low\"\n",301 "result = my_func(4) + my_func(6) + my_func(4)\n",302 "print(result)\n",303 "\n",304 "```\n",305 "\n"306 ]307 }308 ]309 },310 {311 "cell_type": "code",312 "source": [313 "print(get_completion(prompt))"314 ],315 "metadata": {316 "colab": {317 "base_uri": "https://localhost:8080/"318 },319 "id": "BNStH7qDOcLL",320 "outputId": "88e5a622-7149-4d9c-833c-0f7e8b5ce45e"321 },322 "execution_count": 61,323 "outputs": [324 {325 "output_type": "stream",326 "name": "stdout",327 "text": [328 "\n",329 "Steps:\n",330 "1. The func function is defined with an input parameter x.\n",331 "2. The function checks if x is greater than 0.\n",332 "3. If x is greater than 0, the function returns x + func(x-1).\n",333 "4. If x is not greater than 0, the function returns 0.\n",334 "\n",335 "Checkpoints:\n",336 "1. The func function is defined.\n",337 "2. The func function is called with the input parameter 5.\n",338 "3. The func function returns 15.\n",339 "4. The result of the func function is printed, which is 15.\n",340 "\n",341 "Output:\n",342 "15\n"343 ]344 }345 ]346 },347 {348 "cell_type": "markdown",349 "source": [350 "## Effective promting"351 ],352 "metadata": {353 "id": "gn1rdZL7OuPF"354 }355 },356 {357 "cell_type": "code",358 "source": [359 "python_code_examples = f\"\"\"\n",360 "---------------------\n",361 "Example 1: Code Snippet\n",362 "x = 10\n",363 "def foo():\n",364 " global x\n",365 " x = 5\n",366 "foo()\n",367 "print(x)\n",368 "\n",369 "Correct answer: 5\n",370 "Explanation: Inside the foo function, the global keyword is used to modify the global variable x to be 5.\n",371 "So, print(x) outside the function prints the modified value, which is 5.\n",372 "---------------------\n",373 "Example 2: Code Snippet\n",374 "def modify_list(input_list):\n",375 " input_list.append(4)\n",376 " input_list = [1, 2, 3]\n",377 "my_list = [0]\n",378 "modify_list(my_list)\n",379 "print(my_list)\n",380 "\n",381 "Correct answer: [0, 4]\n",382 "Explanation: Inside the modify_list function, an element 4 is appended to input_list.\n",383 "Then, input_list is reassigned to a new list [1, 2, 3], but this change doesn't affect the original list.\n",384 "So, print(my_list) outputs [0, 4].\n",385 "---------------------\n",386 "\"\"\""387 ],388 "metadata": {389 "id": "4tXQxdtoOyrD"390 },391 "execution_count": 45,392 "outputs": []393 },394 {395 "cell_type": "code",396 "source": [397 "# language = \"Python\"\n",398 "\n",399 "# input_code = f\"\"\"\n",400 "# def my_func(x):\n",401 "# if x > 5:\n",402 "# return \"High\"\n",403 "# else:\n",404 "# return \"Low\"\n",405 "# result = my_func(4) + my_func(6) + my_func(4)\n",406 "# print(result)\n",407 "# \"\"\"\n",408 "\n",409 "input_code = f\"\"\"\n",410 "def func(x):\n",411 " if x > 0:\n",412 " return x + func(x-1)\n",413 " return 0\n",414 "\n",415 "result = func(5)\n",416 "print(result)\n",417 "\"\"\""418 ],419 "metadata": {420 "id": "1YvRxRZrO6Nx"421 },422 "execution_count": 53,423 "outputs": []424 },425 {426 "cell_type": "code",427 "source": [428 "prompt = f\"\"\"\n",429 "Your task is to act as a Python Code Explainer.\n",430 "I'll give you a Code Snippet.\n",431 "Your job is to explain the Code Snippet step-by-step.\n",432 "Break down the code into as many steps as possible.\n",433 "Share intermediate checkpoints along with results.\n",434 "State your Steps and Checkpoints in your output.\n",435 "Few good examples of Python code output between #### separator:\n",436 "####\n",437 "{python_code_examples}\n",438 "####\n",439 "Code Snippet is shared below, delimited with triple backticks:\n",440 "```\n",441 "{input_code}\n",442 "```\n",443 "\"\"\"\n",444 "\n",445 "print(prompt)"446 ],447 "metadata": {448 "colab": {449 "base_uri": "https://localhost:8080/"450 },451 "id": "8BKEPulKY-Z3",452 "outputId": "359a14dc-905a-4eaa-dd99-9fcc0fce2e3a"453 },454 "execution_count": 56,455 "outputs": [456 {457 "output_type": "stream",458 "name": "stdout",459 "text": [460 "\n",461 "Your task is to act as a Python Code Explainer.\n",462 "I'll give you a Code Snippet.\n",463 "Your job is to explain the Code Snippet step-by-step.\n",464 "Break down the code into as many steps as possible.\n",465 "Share intermediate checkpoints along with results.\n",466 "State your Steps and Checkpoints in your output.\n",467 "Few good examples of Python code output between #### separator:\n",468 "####\n",469 "\n",470 "---------------------\n",471 "Example 1: Code Snippet\n",472 "x = 10\n",473 "def foo():\n",474 " global x\n",475 " x = 5\n",476 "foo()\n",477 "print(x)\n",478 "\n",479 "Correct answer: 5\n",480 "Explanation: Inside the foo function, the global keyword is used to modify the global variable x to be 5.\n",481 "So, print(x) outside the function prints the modified value, which is 5.\n",482 "---------------------\n",483 "Example 2: Code Snippet\n",484 "def modify_list(input_list):\n",485 " input_list.append(4)\n",486 " input_list = [1, 2, 3]\n",487 "my_list = [0]\n",488 "modify_list(my_list)\n",489 "print(my_list)\n",490 "\n",491 "Correct answer: [0, 4]\n",492 "Explanation: Inside the modify_list function, an element 4 is appended to input_list.\n",493 "Then, input_list is reassigned to a new list [1, 2, 3], but this change doesn't affect the original list.\n",494 "So, print(my_list) outputs [0, 4].\n",495 "---------------------\n",496 "\n",497 "####\n",498 "Code Snippet is shared below, delimited with triple backticks:\n",499 "```\n",500 "\n",501 "def func(x):\n",502 " if x > 0:\n",503 " return x + func(x-1)\n",504 " return 0\n",505 "\n",506 "result = func(5)\n",507 "print(result)\n",508 "\n",509 "```\n",510 "\n"511 ]512 }513 ]514 },515 {516 "cell_type": "code",517 "source": [518 "print(get_completion(prompt))"519 ],520 "metadata": {521 "colab": {522 "base_uri": "https://localhost:8080/"523 },524 "id": "5GWMx5QmZLmL",525 "outputId": "f85abeb1-0630-496e-ca6d-4292456fc7a9"526 },527 "execution_count": 59,528 "outputs": [529 {530 "output_type": "stream",531 "name": "stdout",532 "text": [533 "\n",534 "Steps:\n",535 "1. The func function is defined with an input parameter x.\n",536 "2. The function checks if x is greater than 0.\n",537 "3. If x is greater than 0, the function returns x + func(x-1).\n",538 "4. If x is not greater than 0, the function returns 0.\n",539 "\n",540 "Checkpoints:\n",541 "1. The func function is defined.\n",542 "2. The func function is called with the input parameter 5.\n",543 "3. The func function returns 15.\n",544 "4. The result of the func function is printed, which is 15.\n",545 "\n",546 "Output:\n",547 "15\n"548 ]549 }550 ]551 },552 {553 "cell_type": "markdown",554 "source": [555 "## Code Explainer Tool App"556 ],557 "metadata": {558 "id": "vxxs488vZaJu"559 }560 },561 {562 "cell_type": "code",563 "source": [564 "!pip install gradio"565 ],566 "metadata": {567 "colab": {568 "base_uri": "https://localhost:8080/"569 },570 "id": "QcTVKKK7ZZw8",571 "outputId": "5c940620-fa45-4674-ab43-83df3879e5d4"572 },573 "execution_count": 27,574 "outputs": [575 {576 "output_type": "stream",577 "name": "stdout",578 "text": [579 "Requirement already satisfied: gradio in /usr/local/lib/python3.10/dist-packages (3.50.0)\n",580 "Requirement already satisfied: aiofiles<24.0,>=22.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (23.2.1)\n",581 "Requirement already satisfied: altair<6.0,>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.2.2)\n",582 "Requirement already satisfied: fastapi in /usr/local/lib/python3.10/dist-packages (from gradio) (0.104.0)\n",583 "Requirement already satisfied: ffmpy in /usr/local/lib/python3.10/dist-packages (from gradio) (0.3.1)\n",584 "Requirement already satisfied: gradio-client==0.6.1 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.6.1)\n",585 "Requirement already satisfied: httpx in /usr/local/lib/python3.10/dist-packages (from gradio) (0.25.0)\n",586 "Requirement already satisfied: huggingface-hub>=0.14.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.18.0)\n",587 "Requirement already satisfied: importlib-resources<7.0,>=1.3 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.1.0)\n",588 "Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.1.2)\n",589 "Requirement already satisfied: markupsafe~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.1.3)\n",590 "Requirement already satisfied: matplotlib~=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.7.1)\n",591 "Requirement already satisfied: numpy~=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.23.5)\n",592 "Requirement already satisfied: orjson~=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.9.9)\n",593 "Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from gradio) (23.2)\n",594 "Requirement already satisfied: pandas<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.5.3)\n",595 "Requirement already satisfied: pillow<11.0,>=8.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (9.4.0)\n",596 "Requirement already satisfied: pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,<3.0.0,>=1.7.4 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.10.13)\n",597 "Requirement already satisfied: pydub in /usr/local/lib/python3.10/dist-packages (from gradio) (0.25.1)\n",598 "Requirement already satisfied: python-multipart in /usr/local/lib/python3.10/dist-packages (from gradio) (0.0.6)\n",599 "Requirement already satisfied: pyyaml<7.0,>=5.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.0.1)\n",600 "Requirement already satisfied: requests~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.31.0)\n",601 "Requirement already satisfied: semantic-version~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.10.0)\n",602 "Requirement already satisfied: typing-extensions~=4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.8.0)\n",603 "Requirement already satisfied: uvicorn>=0.14.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.23.2)\n",604 "Requirement already satisfied: websockets<12.0,>=10.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (11.0.3)\n",605 "Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from gradio-client==0.6.1->gradio) (2023.6.0)\n",606 "Requirement already satisfied: entrypoints in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (0.4)\n",607 "Requirement already satisfied: jsonschema>=3.0 in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (4.19.1)\n",608 "Requirement already satisfied: toolz in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (0.12.0)\n",609 "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.14.0->gradio) (3.12.4)\n",610 "Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.14.0->gradio) (4.66.1)\n",611 "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.1.1)\n",612 "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (0.12.1)\n",613 "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (4.43.1)\n",614 "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.4.5)\n",615 "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (3.1.1)\n",616 "Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (2.8.2)\n",617 "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2023.3.post1)\n",618 "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests~=2.0->gradio) (3.3.0)\n",619 "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests~=2.0->gradio) (3.4)\n",620 "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests~=2.0->gradio) (2.0.6)\n",621 "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests~=2.0->gradio) (2023.7.22)\n",622 "Requirement already satisfied: click>=7.0 in /usr/local/lib/python3.10/dist-packages (from uvicorn>=0.14.0->gradio) (8.1.7)\n",623 "Requirement already satisfied: h11>=0.8 in /usr/local/lib/python3.10/dist-packages (from uvicorn>=0.14.0->gradio) (0.14.0)\n",624 "Requirement already satisfied: anyio<4.0.0,>=3.7.1 in /usr/local/lib/python3.10/dist-packages (from fastapi->gradio) (3.7.1)\n",625 "Requirement already satisfied: starlette<0.28.0,>=0.27.0 in /usr/local/lib/python3.10/dist-packages (from fastapi->gradio) (0.27.0)\n",626 "Requirement already satisfied: httpcore<0.19.0,>=0.18.0 in /usr/local/lib/python3.10/dist-packages (from httpx->gradio) (0.18.0)\n",627 "Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from httpx->gradio) (1.3.0)\n",628 "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<4.0.0,>=3.7.1->fastapi->gradio) (1.1.3)\n",629 "Requirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (23.1.0)\n",630 "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (2023.7.1)\n",631 "Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.30.2)\n",632 "Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.10.6)\n",633 "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib~=3.0->gradio) (1.16.0)\n"634 ]635 }636 ]637 },638 {639 "cell_type": "code",640 "source": [641 "#@title Gradio App\n",642 "import gradio as gr\n",643 "import os\n",644 "import google.generativeai as palm\n",645 "\n",646 "# load model\n",647 "# PaLM API Key here\n",648 "palm.configure(api_key='AIzaSyDQmcmwtSlNzNwjCnRUzwqowfvxwdGr-Ug')\n",649 "\n",650 "\n",651 "# define completion function\n",652 "def get_completion(code_snippet):\n",653 "\n",654 " python_code_examples = f\"\"\"\n",655 " ---------------------\n",656 " Example 1: Code Snippet\n",657 " x = 10\n",658 " def foo():\n",659 " global x\n",660 " x = 5\n",661 " foo()\n",662 " print(x)\n",663 " Correct output: 5\n",664 " Code Explanation: Inside the foo function, the global keyword is used to modify the global variable x to be 5.\n",665 " So, print(x) outside the function prints the modified value, which is 5.\n",666 " ---------------------\n",667 " Example 2: Code Snippet\n",668 " def modify_list(input_list):\n",669 " input_list.append(4)\n",670 " input_list = [1, 2, 3]\n",671 " my_list = [0]\n",672 " modify_list(my_list)\n",673 " print(my_list)\n",674 " Correct output: [0, 4]\n",675 " Code Explanation: Inside the modify_list function, an element 4 is appended to input_list.\n",676 " Then, input_list is reassigned to a new list [1, 2, 3], but this change doesn't affect the original list.\n",677 " So, print(my_list) outputs [0, 4].\n",678 " ---------------------\n",679 " \"\"\"\n",680 "\n",681 " prompt = f\"\"\"\n",682 " Your task is to act as a Python Code Explainer.\n",683 " I'll give you a Code Snippet.\n",684 " Your job is to explain the Code Snippet step-by-step.\n",685 " Break down the code into as many steps as possible.\n",686 " Share intermediate checkpoints & steps along with results.\n",687 " Few good examples of Python code output between #### separator:\n",688 " ####\n",689 " {python_code_examples}\n",690 " ####\n",691 " Code Snippet is shared below, delimited with triple backticks:\n",692 " ```\n",693 " {code_snippet}\n",694 " ```\n",695 " \"\"\"\n",696 "\n",697 " completion = palm.generate_text(\n",698 " model=model,\n",699 " prompt=prompt,\n",700 " temperature=0,\n",701 " # The maximum length of the response\n",702 " max_output_tokens=500,\n",703 " )\n",704 " response = completion.result\n",705 " return response\n",706 "\n",707 "# define app UI\n",708 "iface = gr.Interface(fn=get_completion, inputs=[gr.Textbox(label=\"Insert Code Snippet\",lines=5)],\n",709 " outputs=[gr.Textbox(label=\"Explanation Here\",lines=5)],\n",710 " title=\"Python Code Explainer\"\n",711 " )\n",712 "\n",713 "iface.launch(share=True)"714 ],715 "metadata": {716 "colab": {717 "base_uri": "https://localhost:8080/",718 "height": 591719 },720 "id": "5r6l3z6ZZpLQ",721 "outputId": "3535f995-b3cd-42a4-c21a-f9341514ccaf"722 },723 "execution_count": 60,724 "outputs": [725 {726 "output_type": "stream",727 "name": "stdout",728 "text": [729 "Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",730 "Running on public URL: https://ebca3f4c91f02b1545.gradio.live\n",731 "\n",732 "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"733 ]734 },735 {736 "output_type": "display_data",737 "data": {738 "text/plain": [739 "<IPython.core.display.HTML object>"740 ],741 "text/html": [742 "<div><iframe src=\"https://ebca3f4c91f02b1545.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"743 ]744 },745 "metadata": {}746 },747 {748 "output_type": "execute_result",749 "data": {750 "text/plain": []751 },752 "metadata": {},753 "execution_count": 60754 }755 ]756 },757 {758 "cell_type": "code",759 "source": [],760 "metadata": {761 "id": "5ovS39TFbznz"762 },763 "execution_count": null,764 "outputs": []765 }766 ]767}