tharun1507 commited on
Commit
78b65d9
·
verified ·
1 Parent(s): 91641ff

Update app.py

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