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()