CoolFace
Apppublic

Harini14/codeReviewer

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py92 linesDownload Raw Back to root
1# -*- coding: utf-8 -*-2"""Copy of Code Reviewer.ipynb3 4Automatically generated by Colaboratory.5 6Original file is located at7    https://colab.research.google.com/drive/1VAskAHWz5-Glpa-tiIG0DMPyVy7px3sb8"""9 10 11 12#@title Code Explainer13import gradio as gr14 15import google.generativeai as palm16 17# load model18# PaLM API Key here19palm.configure(api_key='AIzaSyDZS__5FCSykuAODAhxF-UyRBUlHS9Cehs')20# Use the palm.list_models function to find available models21# PaLM 2 available in 4 sizes: Gecko, Otter, Bison and Unicorn (largest)22models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]23model = models[0].name24 25# define completion function26 27def get_completion(code_snippet):28 29  python_code_examples = f"""30  ---------------------31  Example 1: Code Snippet32 def calculate_average(numbers):33  total = 034  for number in numbers:35    total += number36  average = total / len(numbers)37  return average38 39  Code Review: Consider using the sum() function to calculate the total sum of the numbers40   instead of manually iterating over the list.41   This would make the code more concise and efficient.42  ---------------------43  Example 2: Code Snippet44  def find_largest_number(numbers):45  largest_number = numbers[0]46  for number in numbers:47    if number > largest_number:48      largest_number = number49  return largest_number50 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 areas62   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 response79      max_output_tokens=500,80      )81  response = completion.result82  return response83 84# define app UI85iface = 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 90iface.launch()91 92