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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -36
app.py CHANGED
@@ -1,51 +1,55 @@
 
1
  import gradio as gr
2
  import requests
3
- import os
4
 
5
- # Function to call CodePal's Code Generator API
6
- def generate_code(instructions, language, generation_type):
7
- api_url = 'https://api.codepal.ai/v1/code-generator/query'
8
- api_key = os.getenv('CODEPAL_API_KEY')
9
- headers = {'Authorization': f'Bearer {api_key}'}
10
- data = {
11
- 'instructions': instructions,
12
- 'language': language,
13
- 'generation_type': generation_type
14
- }
15
  response = requests.post(api_url, headers=headers, data=data)
16
  if response.status_code == 201:
17
- return response.json().get('result', 'No code generated.')
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
- # Gradio interface components
24
  with gr.Blocks() as demo:
25
- gr.Markdown("# Code Generator Interface\nGenerate code snippets using CodePal's Code Generator API.")
 
 
 
 
26
  with gr.Row():
27
  with gr.Column():
28
- login_button = gr.LoginButton()
29
- instructions_input = gr.Textbox(label='Function Description', placeholder='Describe the function to generate...')
30
- language_input = gr.Dropdown(label='Programming Language', choices=['python', 'javascript', 'go', 'java', 'csharp'])
31
- generation_type_input = gr.Radio(label='Generation Type', choices=['minimal', 'standard', 'documented'], value='standard')
32
- generate_button = gr.Button(value='Generate Code')
33
- clear_button = gr.Button(value='Clear')
34
- error_output = gr.Textbox(label='Error Message', visible=False)
35
  with gr.Column():
36
- output_display = gr.Code(label='Generated Code')
37
-
38
- # Example inputs
39
- examples = [
40
- ["Function to add two numbers", "python", "standard"],
41
- ["Class definition for a linked list", "java", "documented"],
42
- ["Script to fetch data from an API", "javascript", "minimal"]
43
- ]
44
- gr.Examples(examples=examples, inputs=[instructions_input, language_input, generation_type_input])
45
-
46
- # Define button actions
47
- generate_button.click(generate_code, inputs=[instructions_input, language_input, generation_type_input], outputs=[output_display, error_output])
48
- clear_button.click(lambda: ("", "python", "standard", "", ""), outputs=[instructions_input, language_input, generation_type_input, output_display, error_output])
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()