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