File size: 2,519 Bytes
f28a407
 
 
 
 
 
 
 
 
91c061d
f28a407
 
 
43ed8d4
91c061d
f28a407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-
"""Code Explainer.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1zjYwVdkZFh1uJRMydEtvhan_1DSWGdAf
"""



#@title Code Explainer
import gradio as gr
import google.generativeai as palm


# load model
# PaLM API Key here
palm.configure(api_key='AIzaSyDa89CpRmIwKA6h8fBO533Si0xK_YvSs7I')
# Use the palm.list_models function to find available models
# PaLM 2 available in 4 sizes: Gecko, Otter, Bison and Unicorn (largest)
models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]
model = models[0].name
# define completion function
def get_completion(code_snippet):

  python_code_examples = f"""
  ---------------------
  Example 1: Code Snippet
  x = 10
  def foo():
      global x
      x = 5
  foo()
  print(x)
  Correct output: 5
  Code Explanation: Inside the foo function, the global keyword is used to modify the global variable x to be 5.
  So, print(x) outside the function prints the modified value, which is 5.
  ---------------------
  Example 2: Code Snippet
  def modify_list(input_list):
      input_list.append(4)
      input_list = [1, 2, 3]
  my_list = [0]
  modify_list(my_list)
  print(my_list)
  Correct output: [0, 4]
  Code Explanation: Inside the modify_list function, an element 4 is appended to input_list.
  Then, input_list is reassigned to a new list [1, 2, 3], but this change doesn't affect the original list.
  So, print(my_list) outputs [0, 4].
  ---------------------
  """

  prompt = f"""
  Your task is to act as any language Code Explainer.
  I'll give you a Code Snippet.
  Your job is to explain the Code Snippet step-by-step.
  Break down the code into as many steps as possible.
  Share intermediate checkpoints & steps along with results.
  Few good examples of Python code output between #### separator:
  ####
  {python_code_examples}
  ####
  Code Snippet is shared below, delimited with triple backticks:
  ```
  {code_snippet}
  ```
  """

  completion = palm.generate_text(
      model=model,
      prompt=prompt,
      temperature=0,
      # The maximum length of the response
      max_output_tokens=500,
      )
  response = completion.result
  return response

# define app UI
iface = gr.Interface(fn=get_completion, inputs=[gr.Textbox(label="Insert Code Snippet",lines=5)],
                    outputs=[gr.Textbox(label="Explanation Here",lines=8)],
                    title="Code Explainer"
                    )

iface.launch()