rishika18/code-review
1
1# -*- coding: utf-8 -*-2"""Code Reviewer.ipynb3 4Automatically generated by Colaboratory.5 6Original file is located at7 https://colab.research.google.com/drive/1BRI4Pc9jOR8yvCwn-01ca1-WNKxAC03h8"""9 10#@title Code Explainer11import gradio as gr12 13import google.generativeai as palm14 15# load model16# PaLM API Key here17palm.configure(api_key='AIzaSyDa89CpRmIwKA6h8fBO533Si0xK_YvSs7I')18# Use the palm.list_models function to find available models19# PaLM 2 available in 4 sizes: Gecko, Otter, Bison and Unicorn (largest)20models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]21model = models[0].name22 23# define completion function24 25def get_completion(code_snippet):26 27 python_code_examples = f"""28 ---------------------29 Example 1: Code Snippet30 def calculate_average(numbers):31 total = 032 for number in numbers:33 total += number34 average = total / len(numbers)35 return average36 37 Code Review: Consider using the sum() function to calculate the total sum of the numbers38 instead of manually iterating over the list.39 This would make the code more concise and efficient.40 ---------------------41 Example 2: Code Snippet42 def find_largest_number(numbers):43 largest_number = numbers[0]44 for number in numbers:45 if number > largest_number:46 largest_number = number47 return largest_number48 49 50 Code Review: Refactor the code using the max() function to find the largest number in the list.51 This would simplify the code and improve its readability.52 ---------------------53 """54 55 56 prompt = f"""57 I will provide you with code snippets,58 and you will review them for potential issues and suggest improvements.59 Please focus on providing concise and actionable feedback, highlighting areas60 that could benefit from refactoring, optimization, or bug fixes.61 Your feedback should be constructive and aim to enhance the overall quality and maintainability of the code.62 Please avoid providing explanations for your suggestions unless specifically requested. Instead, focus on clearly identifying areas for improvement and suggesting alternative approaches or solutions.63 Few good examples of Python code output between #### separator:64 ####65 {python_code_examples}66 ####67 Code Snippet is shared below, delimited with triple backticks:68 ```69 {code_snippet}70 ```71 """72 completion = palm.generate_text(73 model=model,74 prompt=prompt,75 temperature=0,76 # The maximum length of the response77 max_output_tokens=500,78 )79 response = completion.result80 return response81 82# define app UI83iface = gr.Interface(fn=get_completion, inputs=[gr.Textbox(label="Insert Code Snippet",lines=5)],84 outputs=[gr.Textbox(label="Review Here",lines=8)],85 title="Code Reviewer"86 )87 88iface.launch()89 90 