reviewer / app.py
RAMREDDY9391's picture
Rename code_reviewer.py to app.py
4761c5b
# -*- coding: utf-8 -*-
"""Code Reviewer.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1BRI4Pc9jOR8yvCwn-01ca1-WNKxAC03h
"""
#@title Code Explainer
import gradio as gr
import os
import google.generativeai as palm
# load model
# PaLM API Key here
palm.configure(api_key='INSERT_API_KEY_HERE')
# Use the palm.list_models function to find available models
# PaLM 2 available in 4 sizes: Gecko, Otter, Bison and Unicorn (largest)
models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]
model = models[0].name
# define completion function
def get_completion(code_snippet):
python_code_examples = f"""
---------------------
Example 1: Code Snippet
def calculate_average(numbers):
total = 0
for number in numbers:
total += number
average = total / len(numbers)
return average
Code Review: Consider using the sum() function to calculate the total sum of the numbers
instead of manually iterating over the list.
This would make the code more concise and efficient.
---------------------
Example 2: Code Snippet
def find_largest_number(numbers):
largest_number = numbers[0]
for number in numbers:
if number > largest_number:
largest_number = number
return largest_number
Code Review: Refactor the code using the max() function to find the largest number in the list.
This would simplify the code and improve its readability.
---------------------
"""
prompt = f"""
I will provide you with code snippets,
and you will review them for potential issues and suggest improvements.
Please focus on providing concise and actionable feedback, highlighting areas
that could benefit from refactoring, optimization, or bug fixes.
Your feedback should be constructive and aim to enhance the overall quality and maintainability of the code.
Please avoid providing explanations for your suggestions unless specifically requested. Instead, focus on clearly identifying areas for improvement and suggesting alternative approaches or solutions.
Few good examples of Python code output between #### separator:
####
{python_code_examples}
####
Code Snippet is shared below, delimited with triple backticks:
```
{code_snippet}
```
"""
completion = palm.generate_text(
model=model,
prompt=prompt,
temperature=0,
# The maximum length of the response
max_output_tokens=500,
)
response = completion.result
return response
# define app UI
iface = gr.Interface(fn=get_completion, inputs=[gr.Textbox(label="Insert Code Snippet",lines=5)],
outputs=[gr.Textbox(label="Review Here",lines=8)],
title="Code Reviewer"
)
iface.launch()