JagadishR commited on
Commit
c6b049a
·
verified ·
1 Parent(s): 357589c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #@title Gradio App
2
+ import gradio as gr
3
+ import os
4
+ import google.generativeai as palm
5
+
6
+ # load model
7
+ # PaLM API Key here
8
+ palm.configure(api_key='INSERT_KEY_HERE')
9
+
10
+
11
+ # define completion function
12
+ def get_completion(code_snippet):
13
+
14
+ python_code_examples = f"""
15
+ ---------------------
16
+ Example 1: Code Snippet
17
+ x = 10
18
+ def foo():
19
+ global x
20
+ x = 5
21
+ foo()
22
+ print(x)
23
+ Correct output: 5
24
+ Code Explanation: Inside the foo function, the global keyword is used to modify the global variable x to be 5.
25
+ So, print(x) outside the function prints the modified value, which is 5.
26
+ ---------------------
27
+ Example 2: Code Snippet
28
+ def modify_list(input_list):
29
+ input_list.append(4)
30
+ input_list = [1, 2, 3]
31
+ my_list = [0]
32
+ modify_list(my_list)
33
+ print(my_list)
34
+ Correct output: [0, 4]
35
+ Code Explanation: Inside the modify_list function, an element 4 is appended to input_list.
36
+ Then, input_list is reassigned to a new list [1, 2, 3], but this change doesn't affect the original list.
37
+ So, print(my_list) outputs [0, 4].
38
+ ---------------------
39
+ """
40
+
41
+ prompt = f"""
42
+ Your task is to act as a Python Code Explainer.
43
+ I'll give you a Code Snippet.
44
+ Your job is to explain the Code Snippet step-by-step.
45
+ Break down the code into as many steps as possible.
46
+ Share intermediate checkpoints & steps along with results.
47
+ Few good examples of Python code output between #### separator:
48
+ ####
49
+ {python_code_examples}
50
+ ####
51
+ Code Snippet is shared below, delimited with triple backticks:
52
+ ```
53
+ {code_snippet}
54
+ ```
55
+ """
56
+
57
+ completion = palm.generate_text(
58
+ model=model,
59
+ prompt=prompt,
60
+ temperature=0,
61
+ # The maximum length of the response
62
+ max_output_tokens=500,
63
+ )
64
+ response = completion.result
65
+ return response
66
+
67
+ # define app UI
68
+ iface = gr.Interface(fn=get_completion, inputs=[gr.Textbox(label="Insert Code Snippet",lines=5)],
69
+ outputs=[gr.Textbox(label="Explanation Here",lines=5)],
70
+ title="Python Code Explainer"
71
+ )
72
+
73
+ iface.launch(share=True)