File size: 1,715 Bytes
2d03412
6db3950
2d03412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6db3950
 
2d03412
 
1
2
3
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
41
42
43
import os
import gradio as gr
import requests

# Retrieve the API key from the environment variable
api_key = os.getenv('CODEPAL_API_KEY')
if not api_key:
    raise ValueError("API key not found. Please set the CODEPAL_API_KEY environment variable.")

# Function to call CodePal's Code Generator API
def generate_code(instructions, language, generation_type):
    api_url = 'https://api.codepal.ai/v1/code-generator/query'
    headers = {'Authorization': f'Bearer {api_key}'}
    data = {
        'instructions': instructions,
        'language': language,
        'generation_type': generation_type
    }
    response = requests.post(api_url, headers=headers, data=data)
    if response.status_code == 201:
        return response.json().get('result', 'No code generated.')
    elif response.status_code == 401:
        return 'Unauthorized: Invalid or missing API key.'
    else:
        return f'Error {response.status_code}: {response.text}'

# Gradio interface components
instructions_input = gr.Textbox(label='Function Description', placeholder='Describe the function to generate...')
language_input = gr.Dropdown(label='Programming Language', choices=['python', 'javascript', 'go', 'java', 'csharp'])
generation_type_input = gr.Radio(label='Generation Type', choices=['minimal', 'standard', 'documented'], value='standard')
output_display = gr.Code(label='Generated Code')

# Create Gradio interface
interface = gr.Interface(
    fn=generate_code,
    inputs=[instructions_input, language_input, generation_type_input],
    outputs=output_display,
    title='Code Generator Interface',
    description='Generate code snippets using CodePal\'s Code Generator API.'
)

# Launch the interface
interface.launch()