tharun1507 commited on
Commit
c43ee0e
·
verified ·
1 Parent(s): 366fbd9

Create app.py

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