Spaces:
Sleeping
Sleeping
File size: 8,724 Bytes
6e19f63 46e69b0 cb42996 46e69b0 07aeb52 cb42996 1f2f75f cb42996 b867a1d 1f2f75f b867a1d cb42996 b867a1d f9b48f4 b867a1d f9b48f4 b867a1d f9b48f4 b867a1d f9b48f4 b867a1d f9b48f4 b867a1d cb42996 f9b48f4 cb42996 e1c280b cb42996 46e69b0 cb42996 e1c280b 46e69b0 b54eb41 cb42996 |
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
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(
"""<div style="display:flex; justify-content:center; align-items:center; height:400px; border:1px dashed #ccc; border-radius:8px;">
<div style="text-align:center;">
<h3>No App Generated Yet</h3>
<p>Enter a description and click "Generate & Run App"</p>
</div>
</div>"""
)
# Example button functions
def set_prompt(text):
return text
hello_btn.click(
lambda: set_prompt("A simple hello world app that greets the user by name"),
inputs=None,
outputs=prompt
)
calc_btn.click(
lambda: set_prompt("A calculator app that can add, subtract, multiply and divide two numbers"),
inputs=None,
outputs=prompt
)
img_btn.click(
lambda: set_prompt("An image filter app that can apply grayscale, invert, and sepia filters to images"),
inputs=None,
outputs=prompt
)
# Main function to generate and run the app
def generate_and_run_app(prompt_text):
if not prompt_text:
return (
"Please enter a description of the app you want to generate.",
"",
"""<div style="text-align:center; padding:20px;">
<h3>No App Generated</h3>
<p>Please enter a description first.</p>
</div>"""
)
# Get code from LLM (simulated)
code, error = simulate_llm_response(prompt_text)
if error:
return (
code,
f"Error generating code: {error}",
"""<div style="text-align:center; color:red; padding:20px;">
<h3>Error</h3>
<p>{0}</p>
</div>""".format(error)
)
# Execute the code
demo_obj, exec_error, stdout, stderr = execute_code(code)
log_output = f"STDOUT:\n{stdout}\n\nSTDERR:\n{stderr}"
if exec_error:
return (
code,
f"Error: {exec_error}",
"""<div style="text-align:center; color:red; padding:20px;">
<h3>Execution Error</h3>
<p>{0}</p>
</div>""".format(exec_error)
)
try:
# Render the demo to HTML
rendered_html = demo_obj.render().replace('window.gradio_mode = "app";', '')
return code, f"✅ App generated and running!", rendered_html
except Exception as e:
import traceback
return (
code,
f"Error rendering app: {str(e)}",
"""<div style="text-align:center; color:red; padding:20px;">
<h3>Rendering Error</h3>
<p>{0}</p>
</div>""".format(traceback.format_exc())
)
# Connect the generate button
generate_btn.click(
generate_and_run_app,
inputs=prompt,
outputs=[code_display, log_output, app_container]
)
# Launch the app
if __name__ == "__main__":
app.launch(server_name="0.0.0.0", server_port=7860) |