Upload code_explainer.py
Browse files- code_explainer.py +89 -0
code_explainer.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Code Explainer.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1zjYwVdkZFh1uJRMydEtvhan_1DSWGdAf
|
8 |
+
"""
|
9 |
+
|
10 |
+
!pip install gradio
|
11 |
+
!pip install google-generativeai
|
12 |
+
|
13 |
+
#@title Code Explainer
|
14 |
+
import gradio as gr
|
15 |
+
import google.generativeai as palm
|
16 |
+
|
17 |
+
# load model
|
18 |
+
# PaLM API Key here
|
19 |
+
palm.configure(api_key='AIzaSyDa89CpRmIwKA6h8fBO533Si0xK_YvSs7I')
|
20 |
+
# Use the palm.list_models function to find available models
|
21 |
+
# PaLM 2 available in 4 sizes: Gecko, Otter, Bison and Unicorn (largest)
|
22 |
+
models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]
|
23 |
+
model = models[0].name
|
24 |
+
# define completion function
|
25 |
+
def get_completion(code_snippet):
|
26 |
+
|
27 |
+
python_code_examples = f"""
|
28 |
+
---------------------
|
29 |
+
Example 1: Code Snippet
|
30 |
+
x = 10
|
31 |
+
def foo():
|
32 |
+
global x
|
33 |
+
x = 5
|
34 |
+
foo()
|
35 |
+
print(x)
|
36 |
+
Correct output: 5
|
37 |
+
Code Explanation: Inside the foo function, the global keyword is used to modify the global variable x to be 5.
|
38 |
+
So, print(x) outside the function prints the modified value, which is 5.
|
39 |
+
---------------------
|
40 |
+
Example 2: Code Snippet
|
41 |
+
def modify_list(input_list):
|
42 |
+
input_list.append(4)
|
43 |
+
input_list = [1, 2, 3]
|
44 |
+
my_list = [0]
|
45 |
+
modify_list(my_list)
|
46 |
+
print(my_list)
|
47 |
+
Correct output: [0, 4]
|
48 |
+
Code Explanation: Inside the modify_list function, an element 4 is appended to input_list.
|
49 |
+
Then, input_list is reassigned to a new list [1, 2, 3], but this change doesn't affect the original list.
|
50 |
+
So, print(my_list) outputs [0, 4].
|
51 |
+
---------------------
|
52 |
+
"""
|
53 |
+
|
54 |
+
prompt = f"""
|
55 |
+
Your task is to act as any language Code Explainer.
|
56 |
+
I'll give you a Code Snippet.
|
57 |
+
Your job is to explain the Code Snippet step-by-step.
|
58 |
+
Break down the code into as many steps as possible.
|
59 |
+
Share intermediate checkpoints & steps along with results.
|
60 |
+
Few good examples of Python code output between #### separator:
|
61 |
+
####
|
62 |
+
{python_code_examples}
|
63 |
+
####
|
64 |
+
Code Snippet is shared below, delimited with triple backticks:
|
65 |
+
```
|
66 |
+
{code_snippet}
|
67 |
+
```
|
68 |
+
"""
|
69 |
+
|
70 |
+
completion = palm.generate_text(
|
71 |
+
model=model,
|
72 |
+
prompt=prompt,
|
73 |
+
temperature=0,
|
74 |
+
# The maximum length of the response
|
75 |
+
max_output_tokens=500,
|
76 |
+
)
|
77 |
+
response = completion.result
|
78 |
+
return response
|
79 |
+
|
80 |
+
# define app UI
|
81 |
+
iface = gr.Interface(fn=get_completion, inputs=[gr.Textbox(label="Insert Code Snippet",lines=5)],
|
82 |
+
outputs=[gr.Textbox(label="Explanation Here",lines=8)],
|
83 |
+
title="Code Explainer"
|
84 |
+
)
|
85 |
+
|
86 |
+
iface.launch()
|
87 |
+
|
88 |
+
|
89 |
+
|