S-Dreamer commited on
Commit
d4ab35d
·
verified ·
1 Parent(s): e567067

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -36
app.py CHANGED
@@ -3,16 +3,32 @@ 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:
@@ -21,35 +37,33 @@ def review_code(code):
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()
 
 
 
3
  import requests
4
 
5
  # Retrieve the API key from the environment variable
6
+ def get_api_key():
7
+ """Retrieve the API key from the environment variable."""
8
+ api_key = os.getenv("CODEPAL_API_KEY")
9
+ if not api_key:
10
+ raise ValueError("API key not found. Please set the CODEPAL_API_KEY environment variable.")
11
+ return api_key
12
 
13
  # Function to call CodePal's Code Reviewer API
14
  def review_code(code):
15
+ """
16
+ Sends the provided code to the CodePal Code Reviewer API and returns the review results.
17
+
18
+ Parameters:
19
+ code (str): The code to be reviewed.
20
+
21
+ Returns:
22
+ str: The result of the code review, or an error message if the API request fails.
23
+ """
24
  api_url = "https://api.codepal.ai/v1/code-reviewer/query"
25
+ headers = {"Authorization": f"Bearer {get_api_key()}"}
26
+ response = requests.post(api_url, headers=headers, json={"code": code})
27
+
28
+ return handle_api_response(response)
29
+
30
+ def handle_api_response(response):
31
+ """Handle the API response and return the appropriate message."""
32
  if response.status_code == 201:
33
  return response.json().get("result", "No review available.")
34
  elif response.status_code == 401:
 
37
  return f"Error {response.status_code}: {response.text}"
38
 
39
  # Build the Gradio interface using Blocks for better layout control
40
+ def create_interface():
41
+ """Create the Gradio interface for the code reviewer."""
42
+ with gr.Blocks() as demo:
43
+ gr.Markdown("# Code Reviewer Interface\nGet a professional code review for any piece of code.")
44
+ gr.LoginButton()
45
 
46
+ with gr.Row():
47
+ with gr.Column():
48
+ code_input = gr.Textbox(label="Code to Review", placeholder="Paste your code here...", lines=15)
49
+ review_button = gr.Button("Review Code")
50
+ clear_button = gr.Button("Clear")
51
+ with gr.Column():
52
+ review_output = gr.Code(label="Code Review", language="markdown")
53
+
54
+ gr.Examples(
55
+ examples=[
56
+ ["def factorial(n):\n if n == 0:\n return 1\n else:\n return n * factorial(n-1)\n\nprint(factorial(5))"]
57
+ ],
58
+ inputs=code_input
59
+ )
60
+
61
+ review_button.click(review_code, inputs=code_input, outputs=review_output)
62
+ clear_button.click(lambda: ("", ""), outputs=[code_input, review_output])
63
+
64
+ return demo
 
 
 
 
 
 
 
65
 
66
+ # Launch the Gradio interface
67
+ if __name__ == "__main__":
68
+ demo = create_interface()
69
+ demo.launch()