Spaces:
Running
Running
File size: 2,038 Bytes
e567067 6db3950 2d03412 e567067 2d03412 e567067 2d03412 e567067 2d03412 e567067 2d03412 e567067 4d58d83 e567067 4d58d83 e567067 4d58d83 e567067 6db3950 2d03412 4d58d83 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import os
import gradio as gr
import requests
# Retrieve the API key from the environment variable
api_key = os.getenv("CODEPAL_API_KEY")
if not api_key:
raise ValueError("API key not found. Please set the CODEPAL_API_KEY environment variable.")
# Function to call CodePal's Code Reviewer API
def review_code(code):
api_url = "https://api.codepal.ai/v1/code-reviewer/query"
headers = {"Authorization": f"Bearer {api_key}"}
data = {"code": code}
response = requests.post(api_url, headers=headers, data=data)
if response.status_code == 201:
return response.json().get("result", "No review available.")
elif response.status_code == 401:
return "Unauthorized: Invalid or missing API key."
else:
return f"Error {response.status_code}: {response.text}"
# Build the Gradio interface using Blocks for better layout control
with gr.Blocks() as demo:
gr.Markdown("# Code Reviewer Interface\nGet a professional code review for any piece of code.")
# Hugging Face Login Button (requires `hf_oauth: true` in your README metadata)
login_button = gr.LoginButton()
with gr.Row():
with gr.Column():
code_input = gr.Textbox(
label="Code to Review",
placeholder="Paste your code here...",
lines=15
)
review_button = gr.Button("Review Code")
clear_button = gr.Button("Clear")
with gr.Column():
review_output = gr.Code(label="Code Review", language="markdown")
# Examples to guide users
gr.Examples(
examples=[
["def factorial(n):\n if n == 0:\n return 1\n else:\n return n * factorial(n-1)\n\nprint(factorial(5))"]
],
inputs=code_input
)
# Define the actions for the buttons
review_button.click(review_code, inputs=code_input, outputs=review_output)
clear_button.click(lambda: ("", ""), outputs=[code_input, review_output])
# Launch the interface
demo.launch() |