nakas's picture
Update app.py
57a2c17 verified
raw
history blame
7.64 kB
import os
import importlib.util
import sys
import tempfile
import re
import ast
import gradio as gr
import openai
import requests
import json
# Extract code blocks from text
def extract_code_blocks(text):
pattern = r'```(?:python)?\s*([\s\S]*?)```'
matches = re.findall(pattern, text)
if not matches and text.strip():
if re.search(r'import\s+\w+|def\s+\w+\(|class\s+\w+:|if\s+__name__\s*==\s*[\'"]__main__[\'"]:', text):
return [text.strip()]
return [match.strip() for match in matches]
# Validate that the code is safe to run
def validate_gradio_code(code):
try:
tree = ast.parse(code)
# Check imports
allowed_modules = ['gradio', 'numpy', 'pandas', 'matplotlib', 'PIL', 'os', 'io', 'base64',
'time', 'datetime', 'json', 'random', 'math', 'sys', 're', 'pathlib',
'collections', 'typing', 'warnings']
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for name in node.names:
if name.name not in allowed_modules:
return False, f"Unauthorized import: {name.name}"
elif isinstance(node, ast.ImportFrom):
if node.module not in allowed_modules and node.module is not None:
return False, f"Unauthorized import from: {node.module}"
# Check for harmful operations
code_str = code.lower()
harmful_ops = ['subprocess', 'system(', 'popen(', 'execve(', 'chmod(', 'rmdir(',
'remove(', 'unlink(', 'rmtree(', 'socket', 'eval(', 'exec(']
for op in harmful_ops:
if op in code_str:
return False, f"Potentially harmful operation detected: {op}"
return True, None
except SyntaxError as e:
return False, f"Syntax error in the code: {str(e)}"
except Exception as e:
return False, f"Error validating code: {str(e)}"
# Generate app using direct API call
def generate_gradio_app(api_key, prompt):
if not api_key or len(api_key) < 20:
return None, "Please provide a valid OpenAI API key"
try:
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"model": "gpt-4o",
"messages": [
{"role": "system", "content": """You are an expert Gradio developer.
Create a standalone Gradio application based on the user's prompt.
Your response should ONLY include Python code without any explanation.
The code must:
1. Import all necessary libraries
2. Define a complete, functional Gradio interface
3. DO NOT include a launch command or if __name__ == "__main__" block
4. The interface should be assigned to a variable named 'demo'
5. Handle errors gracefully
6. Be completely self-contained in a single script
"""
},
{"role": "user", "content": prompt}
],
"temperature": 0.2,
"max_tokens": 4000
}
response = requests.post(
"https://api.openai.com/v1/chat/completions",
headers=headers,
json=data
)
if response.status_code != 200:
return None, f"API Error: {response.status_code} - {response.text}"
result = response.json()
generated_code = result["choices"][0]["message"]["content"]
code_blocks = extract_code_blocks(generated_code)
if code_blocks:
return code_blocks[0], None
else:
return generated_code, None
except Exception as e:
return None, str(e)
# Create the main UI
with gr.Blocks(title="AI Gradio App Generator") as demo:
gr.Markdown("# 🤖 AI Gradio App Generator")
gr.Markdown("Describe the app you want, and I'll generate it for you using OpenAI's API.")
with gr.Row():
with gr.Column():
api_key = gr.Textbox(
label="OpenAI API Key",
placeholder="sk-...",
type="password",
info="Your key is used for this session only and not stored"
)
prompt = gr.Textbox(
label="App Description",
placeholder="Describe the Gradio app you want to create...",
lines=5
)
with gr.Row():
submit_btn = gr.Button("Generate App", variant="primary")
clear_btn = gr.Button("Clear", variant="secondary")
with gr.Accordion("Generated Code", open=False):
code_output = gr.Code(language="python", label="Generated Code")
status_output = gr.Markdown("")
app_container = gr.HTML(visible=False)
generated_app_container = gr.Group(visible=False)
def on_submit(api_key_input, prompt_text):
# Generate code
code, error = generate_gradio_app(api_key_input, prompt_text)
if error:
return None, f"⚠️ Error generating code: {error}", gr.update(visible=False), gr.update(visible=False)
# Validate code
is_valid, error_msg = validate_gradio_code(code)
if not is_valid:
return code, f"⚠️ Error validating code: {error_msg}", gr.update(visible=False), gr.update(visible=False)
# Save code to temp file
with tempfile.NamedTemporaryFile(suffix='.py', delete=False) as f:
f.write(code.encode('utf-8'))
temp_file = f.name
try:
# Import module
module_name = os.path.basename(temp_file).replace('.py', '')
spec = importlib.util.spec_from_file_location(module_name, temp_file)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
# Get the Gradio interface
if hasattr(module, 'demo'):
generated_app = module.demo
# Clear the container and add the new app
return (
code,
"✅ App generated successfully!",
gr.update(visible=False),
gr.update(visible=True, value=generated_app)
)
else:
return code, "⚠️ No 'demo' variable found in the generated code", gr.update(visible=False), gr.update(visible=False)
except Exception as e:
return code, f"⚠️ Error executing the generated code: {str(e)}", gr.update(visible=False), gr.update(visible=False)
finally:
try:
os.unlink(temp_file)
except:
pass
def on_clear():
return "", "", gr.update(visible=False), gr.update(visible=False)
submit_btn.click(
on_submit,
inputs=[api_key, prompt],
outputs=[code_output, status_output, app_container, generated_app_container]
)
clear_btn.click(
on_clear,
inputs=[],
outputs=[prompt, status_output, app_container, generated_app_container]
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)