Spaces:
Runtime error
Runtime error
| # -*- coding: utf-8 -*- | |
| """Code Reviewer App using Google PaLM""" | |
| import gradio as gr | |
| import os | |
| import google.generativeai as palm | |
| # Configure the PaLM API Key | |
| palm.configure(api_key='AIzaSyDA0uqrEshIOwKv69DLRWz2QXq8lb8SvLA') | |
| # List and filter models | |
| models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods] | |
| # Check if models list is not empty | |
| if not models: | |
| raise Exception("No models found that support 'generateText'. Please check your API key or access rights.") | |
| # Use the first available model | |
| model = models[0].name | |
| # Define the function to get a code review | |
| def get_completion(code_snippet): | |
| python_code_examples = """ | |
| --------------------- | |
| 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. | |
| Avoid explanations unless specifically requested. | |
| 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, | |
| max_output_tokens=500, | |
| ) | |
| return completion.result | |
| # Define Gradio Interface | |
| 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() | |