sakethsai67 commited on
Commit
374821f
·
verified ·
1 Parent(s): 5947c1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -76
app.py CHANGED
@@ -1,91 +1,84 @@
1
  # -*- coding: utf-8 -*-
2
- """Copy of Code Reviewer.ipynb
3
 
4
- Automatically generated by Colaboratory.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/1FzNbYw4i5ZFSKsnc7p9qygg8ks_mfQY0
8
- """
9
-
10
-
11
-
12
- #@title Code Explainer
13
  import gradio as gr
14
  import os
15
  import google.generativeai as palm
16
 
17
- # load model
18
- # PaLM API Key here
19
  palm.configure(api_key='AIzaSyDA0uqrEshIOwKv69DLRWz2QXq8lb8SvLA')
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
-
25
- # define completion function
26
-
27
- def get_completion(code_snippet):
28
-
29
- python_code_examples = f"""
30
- ---------------------
31
- Example 1: Code Snippet
32
- def calculate_average(numbers):
33
- total = 0
34
- for number in numbers:
35
- total += number
36
- average = total / len(numbers)
37
- return average
38
-
39
- Code Review: Consider using the sum() function to calculate the total sum of the numbers
40
- instead of manually iterating over the list.
41
- This would make the code more concise and efficient.
42
- ---------------------
43
- Example 2: Code Snippet
44
- def find_largest_number(numbers):
45
- largest_number = numbers[0]
46
- for number in numbers:
47
- if number > largest_number:
48
- largest_number = number
49
- return largest_number
50
 
 
 
51
 
52
- Code Review: Refactor the code using the max() function to find the largest number in the list.
53
- This would simplify the code and improve its readability.
54
- ---------------------
55
- """
56
 
 
 
57
 
58
- prompt = f"""
59
- I will provide you with code snippets,
60
- and you will review them for potential issues and suggest improvements.
61
- Please focus on providing concise and actionable feedback, highlighting areas
62
- that could benefit from refactoring, optimization, or bug fixes.
63
- Your feedback should be constructive and aim to enhance the overall quality and maintainability of the code.
64
- Please avoid providing explanations for your suggestions unless specifically requested. Instead, focus on clearly identifying areas for improvement and suggesting alternative approaches or solutions.
65
- Few good examples of Python code output between #### separator:
66
- ####
67
- {python_code_examples}
68
- ####
69
- Code Snippet is shared below, delimited with triple backticks:
70
- ```
71
- {code_snippet}
72
- ```
73
- """
74
- completion = palm.generate_text(
75
- model=model,
76
- prompt=prompt,
77
- temperature=0,
78
- # The maximum length of the response
79
- max_output_tokens=500,
80
- )
81
- response = completion.result
82
- return response
83
 
84
- # define app UI
85
- iface = gr.Interface(fn=get_completion, inputs=[gr.Textbox(label="Insert Code Snippet",lines=5)],
86
- outputs=[gr.Textbox(label="Review Here",lines=8)],
87
- title="Code Reviewer"
88
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  iface.launch()
91
 
 
 
 
1
  # -*- coding: utf-8 -*-
2
+ """Code Reviewer App using Google PaLM"""
3
 
 
 
 
 
 
 
 
 
 
4
  import gradio as gr
5
  import os
6
  import google.generativeai as palm
7
 
8
+ # Configure the PaLM API Key
 
9
  palm.configure(api_key='AIzaSyDA0uqrEshIOwKv69DLRWz2QXq8lb8SvLA')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ # List and filter models
12
+ models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]
13
 
14
+ # Check if models list is not empty
15
+ if not models:
16
+ raise Exception("No models found that support 'generateText'. Please check your API key or access rights.")
 
17
 
18
+ # Use the first available model
19
+ model = models[0].name
20
 
21
+ # Define the function to get a code review
22
+ def get_completion(code_snippet):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ python_code_examples = """
25
+ ---------------------
26
+ Example 1: Code Snippet
27
+ def calculate_average(numbers):
28
+ total = 0
29
+ for number in numbers:
30
+ total += number
31
+ average = total / len(numbers)
32
+ return average
33
+ Code Review: Consider using the sum() function to calculate the total sum of the numbers
34
+ instead of manually iterating over the list. This would make the code more concise and efficient.
35
+ ---------------------
36
+ Example 2: Code Snippet
37
+ def find_largest_number(numbers):
38
+ largest_number = numbers[0]
39
+ for number in numbers:
40
+ if number > largest_number:
41
+ largest_number = number
42
+ return largest_number
43
+ Code Review: Refactor the code using the max() function to find the largest number in the list.
44
+ This would simplify the code and improve its readability.
45
+ ---------------------
46
+ """
47
+
48
+ prompt = f"""
49
+ I will provide you with code snippets,
50
+ and you will review them for potential issues and suggest improvements.
51
+ Please focus on providing concise and actionable feedback, highlighting areas
52
+ that could benefit from refactoring, optimization, or bug fixes.
53
+ Avoid explanations unless specifically requested.
54
+
55
+ Few good examples of Python code output between #### separator:
56
+ ####
57
+ {python_code_examples}
58
+ ####
59
+ Code Snippet is shared below, delimited with triple backticks:
60
+ ```
61
+ {code_snippet}
62
+ ```
63
+ """
64
+
65
+ completion = palm.generate_text(
66
+ model=model,
67
+ prompt=prompt,
68
+ temperature=0,
69
+ max_output_tokens=500,
70
+ )
71
+ return completion.result
72
+
73
+ # Define Gradio Interface
74
+ iface = gr.Interface(
75
+ fn=get_completion,
76
+ inputs=[gr.Textbox(label="Insert Code Snippet", lines=5)],
77
+ outputs=[gr.Textbox(label="Review Here", lines=8)],
78
+ title="Code Reviewer"
79
+ )
80
 
81
  iface.launch()
82
 
83
+
84
+