Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,43 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
-
import
|
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 |
-
description="Paste your Python code below. This tool provides basic optimization suggestions and error reporting. Note: This is a simplified demonstration and does not replace a full debugger.",
|
41 |
)
|
42 |
|
43 |
-
|
|
|
|
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 Generator API
|
11 |
+
def generate_code(instructions, language, generation_type):
|
12 |
+
api_url = 'https://api.codepal.ai/v1/code-generator/query'
|
13 |
+
headers = {'Authorization': f'Bearer {api_key}'}
|
14 |
+
data = {
|
15 |
+
'instructions': instructions,
|
16 |
+
'language': language,
|
17 |
+
'generation_type': generation_type
|
18 |
+
}
|
19 |
+
response = requests.post(api_url, headers=headers, data=data)
|
20 |
+
if response.status_code == 201:
|
21 |
+
return response.json().get('result', 'No code generated.')
|
22 |
+
elif response.status_code == 401:
|
23 |
+
return 'Unauthorized: Invalid or missing API key.'
|
24 |
+
else:
|
25 |
+
return f'Error {response.status_code}: {response.text}'
|
26 |
+
|
27 |
+
# Gradio interface components
|
28 |
+
instructions_input = gr.Textbox(label='Function Description', placeholder='Describe the function to generate...')
|
29 |
+
language_input = gr.Dropdown(label='Programming Language', choices=['python', 'javascript', 'go', 'java', 'csharp'])
|
30 |
+
generation_type_input = gr.Radio(label='Generation Type', choices=['minimal', 'standard', 'documented'], value='standard')
|
31 |
+
output_display = gr.Code(label='Generated Code')
|
32 |
+
|
33 |
+
# Create Gradio interface
|
34 |
+
interface = gr.Interface(
|
35 |
+
fn=generate_code,
|
36 |
+
inputs=[instructions_input, language_input, generation_type_input],
|
37 |
+
outputs=output_display,
|
38 |
+
title='Code Generator Interface',
|
39 |
+
description='Generate code snippets using CodePal\'s Code Generator API.'
|
|
|
40 |
)
|
41 |
|
42 |
+
# Launch the interface
|
43 |
+
interface.launch()
|