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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -2,7 +2,6 @@ import os
2
  import gradio as gr
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")
@@ -10,33 +9,38 @@ def get_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:
35
- return "Unauthorized: Invalid or missing API key."
36
- else:
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:
@@ -63,7 +67,6 @@ def create_interface():
63
 
64
  return demo
65
 
66
- # Launch the Gradio interface
67
  if __name__ == "__main__":
68
  demo = create_interface()
69
- demo.launch()
 
2
  import gradio as gr
3
  import requests
4
 
 
5
  def get_api_key():
6
  """Retrieve the API key from the environment variable."""
7
  api_key = os.getenv("CODEPAL_API_KEY")
 
9
  raise ValueError("API key not found. Please set the CODEPAL_API_KEY environment variable.")
10
  return api_key
11
 
12
+ def handle_api_response(response):
13
+ """Handle the API response and return the appropriate message."""
14
+ if response.status_code == 201:
15
+ return response.json().get("result", "No review available.")
16
+ elif response.status_code == 401:
17
+ return "Unauthorized: Invalid or missing API key."
18
+ else:
19
+ return f"Error {response.status_code}: {response.text}"
20
+
21
  def review_code(code):
22
  """
23
  Sends the provided code to the CodePal Code Reviewer API and returns the review results.
24
 
25
  Parameters:
26
+ code (str): The code to be reviewed.
27
 
28
  Returns:
29
+ str: The result of the code review, or an error message if the API request fails.
30
  """
31
+ if not code.strip():
32
+ return "Error: The code input is empty. Please provide code for review."
33
+
34
  api_url = "https://api.codepal.ai/v1/code-reviewer/query"
35
  headers = {"Authorization": f"Bearer {get_api_key()}"}
36
+ payload = {"code": code}
 
 
37
 
38
+ try:
39
+ response = requests.post(api_url, headers=headers, json=payload)
40
+ return handle_api_response(response)
41
+ except requests.RequestException as e:
42
+ return f"An error occurred while making the API request: {e}"
 
 
 
43
 
 
44
  def create_interface():
45
  """Create the Gradio interface for the code reviewer."""
46
  with gr.Blocks() as demo:
 
67
 
68
  return demo
69
 
 
70
  if __name__ == "__main__":
71
  demo = create_interface()
72
+ demo.launch()