S-Dreamer commited on
Commit
2d03412
·
verified ·
1 Parent(s): 8884e79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -40
app.py CHANGED
@@ -1,43 +1,43 @@
1
-
2
  import gradio as gr
3
- import ast
4
- import traceback
5
-
6
- def optimize_and_debug(code):
7
- """Attempts to optimize and debug the given Python code."""
8
- try:
9
- tree = ast.parse(code)
10
- # Rudimentary optimization: This is a placeholder. Real optimization is complex.
11
- # Here we just check for unnecessary nested loops (a very simple example).
12
- nested_loops = False
13
- for node in ast.walk(tree):
14
- if isinstance(node, ast.For) and any(isinstance(subnode, ast.For) for subnode in ast.walk(node)):
15
- nested_loops = True
16
- break
17
- optimization_suggestions = []
18
- if nested_loops:
19
- optimization_suggestions.append("Consider optimizing nested loops. They can significantly impact performance.")
20
-
21
-
22
- # Execution and error handling
23
- exec(code, {}) # Execute the code (Caution: security implications for untrusted input)
24
- return "Code executed successfully.", optimization_suggestions, ""
25
-
26
- except Exception as e:
27
- error_message = traceback.format_exc()
28
- return "Code execution failed.", [], error_message
29
-
30
-
31
- iface = gr.Interface(
32
- fn=optimize_and_debug,
33
- inputs=gr.Textbox(lines=10, label="Enter your Python code here:"),
34
- outputs=[
35
- gr.Textbox(label="Result"),
36
- gr.Textbox(label="Optimization Suggestions"),
37
- gr.Textbox(label="Error Messages (if any)"),
38
- ],
39
- title="Python Code Optimizer & Debugger (Simplified)",
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
- iface.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 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()