Spaces:
Sleeping
Sleeping
Commit
·
890ed5a
1
Parent(s):
46cde23
Upload copy_of_code_reviewer.py
Browse files- copy_of_code_reviewer.py +92 -0
copy_of_code_reviewer.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
!pip install gradio
|
11 |
+
!pip install google-generativeai
|
12 |
+
|
13 |
+
#@title Code Explainer
|
14 |
+
import gradio as gr
|
15 |
+
import os
|
16 |
+
import google.generativeai as palm
|
17 |
+
|
18 |
+
# load model
|
19 |
+
# PaLM API Key here
|
20 |
+
palm.configure(api_key='AIzaSyDA0uqrEshIOwKv69DLRWz2QXq8lb8SvLA')
|
21 |
+
# Use the palm.list_models function to find available models
|
22 |
+
# PaLM 2 available in 4 sizes: Gecko, Otter, Bison and Unicorn (largest)
|
23 |
+
models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]
|
24 |
+
model = models[0].name
|
25 |
+
|
26 |
+
# define completion function
|
27 |
+
|
28 |
+
def get_completion(code_snippet):
|
29 |
+
|
30 |
+
python_code_examples = f"""
|
31 |
+
---------------------
|
32 |
+
Example 1: Code Snippet
|
33 |
+
def calculate_average(numbers):
|
34 |
+
total = 0
|
35 |
+
for number in numbers:
|
36 |
+
total += number
|
37 |
+
average = total / len(numbers)
|
38 |
+
return average
|
39 |
+
|
40 |
+
Code Review: Consider using the sum() function to calculate the total sum of the numbers
|
41 |
+
instead of manually iterating over the list.
|
42 |
+
This would make the code more concise and efficient.
|
43 |
+
---------------------
|
44 |
+
Example 2: Code Snippet
|
45 |
+
def find_largest_number(numbers):
|
46 |
+
largest_number = numbers[0]
|
47 |
+
for number in numbers:
|
48 |
+
if number > largest_number:
|
49 |
+
largest_number = number
|
50 |
+
return largest_number
|
51 |
+
|
52 |
+
|
53 |
+
Code Review: Refactor the code using the max() function to find the largest number in the list.
|
54 |
+
This would simplify the code and improve its readability.
|
55 |
+
---------------------
|
56 |
+
"""
|
57 |
+
|
58 |
+
|
59 |
+
prompt = f"""
|
60 |
+
I will provide you with code snippets,
|
61 |
+
and you will review them for potential issues and suggest improvements.
|
62 |
+
Please focus on providing concise and actionable feedback, highlighting areas
|
63 |
+
that could benefit from refactoring, optimization, or bug fixes.
|
64 |
+
Your feedback should be constructive and aim to enhance the overall quality and maintainability of the code.
|
65 |
+
Please avoid providing explanations for your suggestions unless specifically requested. Instead, focus on clearly identifying areas for improvement and suggesting alternative approaches or solutions.
|
66 |
+
Few good examples of Python code output between #### separator:
|
67 |
+
####
|
68 |
+
{python_code_examples}
|
69 |
+
####
|
70 |
+
Code Snippet is shared below, delimited with triple backticks:
|
71 |
+
```
|
72 |
+
{code_snippet}
|
73 |
+
```
|
74 |
+
"""
|
75 |
+
completion = palm.generate_text(
|
76 |
+
model=model,
|
77 |
+
prompt=prompt,
|
78 |
+
temperature=0,
|
79 |
+
# The maximum length of the response
|
80 |
+
max_output_tokens=500,
|
81 |
+
)
|
82 |
+
response = completion.result
|
83 |
+
return response
|
84 |
+
|
85 |
+
# define app UI
|
86 |
+
iface = gr.Interface(fn=get_completion, inputs=[gr.Textbox(label="Insert Code Snippet",lines=5)],
|
87 |
+
outputs=[gr.Textbox(label="Review Here",lines=8)],
|
88 |
+
title="Code Reviewer"
|
89 |
+
)
|
90 |
+
|
91 |
+
iface.launch()
|
92 |
+
|