import gradio as gr import os import io from contextlib import redirect_stdout, redirect_stderr # Set fixed MPLCONFIGDIR to avoid permission issues os.environ['MPLCONFIGDIR'] = '/tmp' # Sample code for different Gradio apps EXAMPLE_CODES = { "hello_world": """ import gradio as gr def greet(name): return f"Hello, {name}!" demo = gr.Interface( fn=greet, inputs=gr.Textbox(label="Your Name"), outputs=gr.Textbox(label="Greeting"), title="Hello World", description="A simple greeting app" ) """, "calculator": """ import gradio as gr def calculate(num1, num2, operation): if operation == "Add": return num1 + num2 elif operation == "Subtract": return num1 - num2 elif operation == "Multiply": return num1 * num2 elif operation == "Divide": if num2 == 0: return "Error: Division by zero" return num1 / num2 demo = gr.Interface( fn=calculate, inputs=[ gr.Number(label="First Number"), gr.Number(label="Second Number"), gr.Radio(["Add", "Subtract", "Multiply", "Divide"], label="Operation") ], outputs=gr.Textbox(label="Result"), title="Calculator", description="Perform basic arithmetic operations" ) """, "image_filter": """ import gradio as gr import numpy as np from PIL import Image def apply_filter(image, filter_type): if image is None: return None img_array = np.array(image) if filter_type == "Grayscale": result = np.mean(img_array, axis=2).astype(np.uint8) return Image.fromarray(result) elif filter_type == "Invert": result = 255 - img_array return Image.fromarray(result) elif filter_type == "Sepia": sepia = np.array([[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]]) sepia_img = img_array.dot(sepia.T) sepia_img[sepia_img > 255] = 255 return Image.fromarray(sepia_img.astype(np.uint8)) return image demo = gr.Interface( fn=apply_filter, inputs=[ gr.Image(type="pil"), gr.Radio(["Grayscale", "Invert", "Sepia"], label="Filter") ], outputs=gr.Image(type="pil"), title="Image Filter", description="Apply various filters to images" ) """ } # Function to simulate LLM API call def simulate_llm_response(prompt): """Simulate an LLM response based on the prompt""" prompt_lower = prompt.lower() if "hello" in prompt_lower or "greet" in prompt_lower: return EXAMPLE_CODES["hello_world"], None elif "calculat" in prompt_lower or "math" in prompt_lower or "arithmetic" in prompt_lower: return EXAMPLE_CODES["calculator"], None elif "image" in prompt_lower or "filter" in prompt_lower or "photo" in prompt_lower: return EXAMPLE_CODES["image_filter"], None else: # Default to hello world return EXAMPLE_CODES["hello_world"], None # Function to execute code and extract the demo object def execute_code(code): """Execute the code and return the demo object""" # Create a clean namespace namespace = {} # Capture stdout and stderr stdout_buffer = io.StringIO() stderr_buffer = io.StringIO() try: with redirect_stdout(stdout_buffer), redirect_stderr(stderr_buffer): # Execute the code in the namespace exec(code, namespace) # Check if 'demo' exists in the namespace if 'demo' not in namespace: return None, "No 'demo' object found in the code", stdout_buffer.getvalue(), stderr_buffer.getvalue() # Return the demo object return namespace['demo'], None, stdout_buffer.getvalue(), stderr_buffer.getvalue() except Exception as e: import traceback return None, f"Error executing code: {str(e)}\n{traceback.format_exc()}", stdout_buffer.getvalue(), stderr_buffer.getvalue() # Main app with gr.Blocks(title="LLM Gradio App Generator") as app: # Header gr.Markdown("# 🤖 LLM Gradio App Generator") gr.Markdown("Generate and run Gradio apps dynamically!") with gr.Row(): # Input column with gr.Column(scale=1): # App description input prompt = gr.Textbox( label="Describe the app you want", placeholder="e.g., A calculator app that can perform basic arithmetic", lines=3 ) # Example buttons gr.Markdown("### Example Prompts") hello_btn = gr.Button("Hello World App") calc_btn = gr.Button("Calculator App") img_btn = gr.Button("Image Filter App") # Generate button generate_btn = gr.Button("Generate & Run App", variant="primary") # Code display gr.Markdown("### Generated Code") code_display = gr.Code(language="python") # Log output with gr.Accordion("Execution Log", open=False): log_output = gr.Textbox(label="Output Log", lines=5) # Output column with gr.Column(scale=2): # Status message status_msg = gr.Markdown("### Generated App Will Appear Here") # Container for the dynamically generated app app_container = gr.HTML( """
Enter a description and click "Generate & Run App"
Please enter a description first.
{0}
{0}
{0}