File size: 12,110 Bytes
6e19f63
46e69b0
b5fe8ca
 
 
 
 
 
 
 
cb42996
 
46e69b0
07aeb52
b5fe8ca
 
 
 
 
 
 
cb42996
 
 
 
 
1f2f75f
 
 
cb42996
 
 
 
 
 
 
b5fe8ca
 
cb42996
 
 
 
 
b867a1d
 
 
 
 
 
 
 
1f2f75f
 
b867a1d
 
cb42996
 
 
 
 
 
 
 
 
 
 
b5fe8ca
 
cb42996
 
 
 
 
 
 
b867a1d
 
 
 
f9b48f4
 
b867a1d
f9b48f4
 
b867a1d
f9b48f4
 
b867a1d
 
 
 
f9b48f4
b867a1d
f9b48f4
b867a1d
 
cb42996
 
 
 
 
 
 
 
b5fe8ca
 
cb42996
b5fe8ca
 
cb42996
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5fe8ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb42996
b5fe8ca
 
cb42996
b5fe8ca
 
 
 
 
 
 
 
 
cb42996
b5fe8ca
 
 
 
 
cb42996
b5fe8ca
 
cb42996
b5fe8ca
 
 
 
 
 
 
 
 
 
 
 
 
cb42996
 
b5fe8ca
cb42996
b5fe8ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb42996
 
 
 
b5fe8ca
 
 
cb42996
b5fe8ca
cb42996
 
 
 
 
 
 
 
 
b5fe8ca
 
 
 
 
cb42996
 
b5fe8ca
 
 
cb42996
b5fe8ca
 
 
cb42996
b5fe8ca
 
cb42996
b5fe8ca
cb42996
b5fe8ca
 
 
cb42996
b5fe8ca
 
cb42996
 
 
 
 
b5fe8ca
 
cb42996
 
b5fe8ca
cb42996
 
 
 
 
b5fe8ca
cb42996
 
 
f9b48f4
b5fe8ca
 
cb42996
 
 
 
b5fe8ca
 
cb42996
b5fe8ca
 
 
 
 
 
 
 
cb42996
 
 
 
b5fe8ca
e1c280b
b5fe8ca
 
 
 
46e69b0
b5fe8ca
 
 
 
 
 
cb42996
b5fe8ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb42996
b5fe8ca
 
 
cb42996
 
 
b5fe8ca
 
 
 
 
 
 
 
 
 
cb42996
e1c280b
b5fe8ca
b54eb41
b5fe8ca
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import gradio as gr
import os
import tempfile
import time
import subprocess
import threading
import signal
import json
import random
import string

# Set fixed MPLCONFIGDIR to avoid permission issues
os.environ['MPLCONFIGDIR'] = '/tmp'

# Create tmp directory if it doesn't exist
TEMP_DIR = os.path.join(tempfile.gettempdir(), "gradio_apps")
os.makedirs(TEMP_DIR, exist_ok=True)

# Track running processes
processes = {}

# 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"
)

demo.launch(server_name="0.0.0.0", server_port=PORT)
""",
    
    "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"
)

demo.launch(server_name="0.0.0.0", server_port=PORT)
""",
    
    "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",
    allow_flagging=False
)

demo.launch(server_name="0.0.0.0", server_port=PORT)
"""
}

# 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

# Find an available port
def find_available_port(start_port=7870):
    """Find an available port starting from start_port"""
    import socket
    from contextlib import closing
    
    def is_port_available(port):
        with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
            return sock.connect_ex(('localhost', port)) != 0
    
    port = start_port
    while not is_port_available(port):
        port += 1
    
    return port

# Generate a random string
def random_string(length=8):
    """Generate a random string of fixed length"""
    letters = string.ascii_lowercase
    return ''.join(random.choice(letters) for i in range(length))

# Function to run a Gradio app as a subprocess
def run_gradio_app(code, app_id=None):
    """Run a Gradio app as a subprocess and return the port"""
    global processes
    
    # Clean up any previous process with the same ID
    if app_id in processes and processes[app_id]["process"].poll() is None:
        processes[app_id]["process"].terminate()
        try:
            processes[app_id]["process"].wait(timeout=5)
        except:
            processes[app_id]["process"].kill()
        
        # Remove the file
        try:
            os.unlink(processes[app_id]["file"])
        except:
            pass
    
    # Generate a unique ID if not provided
    if app_id is None:
        app_id = random_string()
    
    # Find an available port
    port = find_available_port()
    
    # Replace PORT in the code with the actual port
    code = code.replace("PORT", str(port))
    
    # Create a temporary file
    with tempfile.NamedTemporaryFile(suffix='.py', dir=TEMP_DIR, delete=False) as f:
        f.write(code.encode('utf-8'))
        file_path = f.name
    
    # Run the app as a subprocess
    try:
        process = subprocess.Popen(
            [sys.executable, file_path],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE
        )
        
        # Wait a moment for the app to start
        time.sleep(3)
        
        # Check if the process is still running
        if process.poll() is not None:
            stdout, stderr = process.communicate()
            return None, f"App failed to start (exit code: {process.returncode})\nStdout: {stdout.decode('utf-8')}\nStderr: {stderr.decode('utf-8')}"
        
        # Store the process and file path
        processes[app_id] = {
            "process": process,
            "file": file_path,
            "port": port
        }
        
        return port, None
    except Exception as e:
        import traceback
        return None, f"Error starting app: {str(e)}\n{traceback.format_exc()}"

# Function to stop a running app
def stop_gradio_app(app_id):
    """Stop a running Gradio app"""
    global processes
    
    if app_id in processes:
        process = processes[app_id]["process"]
        file_path = processes[app_id]["file"]
        
        if process.poll() is None:
            process.terminate()
            try:
                process.wait(timeout=5)
            except:
                process.kill()
        
        # Remove the file
        try:
            os.unlink(file_path)
        except:
            pass
        
        del processes[app_id]
        return True
    
    return False

# Clean up on exit
def cleanup():
    """Clean up all running processes and temporary files"""
    for app_id in list(processes.keys()):
        stop_gradio_app(app_id)

import atexit
atexit.register(cleanup)

# Import sys after we've defined all the functions
import sys

# Main Gradio interface
with gr.Blocks(title="LLM Gradio App Generator") as demo:
    # Header
    gr.Markdown("# 🤖 LLM Gradio App Generator")
    gr.Markdown("Generate and run Gradio apps dynamically!")
    
    # App ID for tracking the current app
    app_id = gr.State("")
    
    with gr.Row():
        # Left column (input)
        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("### Try These Examples:")
            with gr.Row():
                hello_btn = gr.Button("Hello World")
                calc_btn = gr.Button("Calculator")
                image_btn = gr.Button("Image Filter")
            
            # Generate button
            with gr.Row():
                generate_btn = gr.Button("Generate & Run App", variant="primary")
                stop_btn = gr.Button("Stop App", variant="stop")
            
            # Display the generated code
            with gr.Accordion("Generated Code", open=False):
                code_output = gr.Code(language="python", label="Python Code")
            
            # Status message
            status_output = gr.Markdown("Enter a description and click 'Generate & Run App'")
        
        # Right column (output)
        with gr.Column(scale=2):
            # Frame to display the running app
            app_frame = gr.HTML(
                """<div style="display:flex; justify-content:center; align-items:center; height:600px; border:1px dashed #ccc; border-radius:8px;">
                    <div style="text-align:center;">
                        <h3>App Preview</h3>
                        <p>Generate an app to see it here</p>
                    </div>
                </div>"""
            )
    
    # Example button functions
    def use_example(example_text):
        return example_text
    
    hello_btn.click(
        lambda: use_example("A simple hello world app that greets the user by name"),
        inputs=None,
        outputs=prompt
    )
    
    calc_btn.click(
        lambda: use_example("A calculator app that can add, subtract, multiply and divide two numbers"),
        inputs=None,
        outputs=prompt
    )
    
    image_btn.click(
        lambda: use_example("An image filter app that can apply grayscale, invert, and sepia filters to images"),
        inputs=None,
        outputs=prompt
    )
    
    # Generate and run the app
    def on_generate_click(prompt_text, current_app_id):
        if not prompt_text:
            return current_app_id, "", "Please enter a description of the app you want to generate.", app_frame.value
        
        # Stop the current app if running
        if current_app_id:
            stop_gradio_app(current_app_id)
        
        # Generate a new app ID
        new_app_id = random_string()
        
        # Get code from LLM (simulated)
        code, error = simulate_llm_response(prompt_text)
        if error:
            return current_app_id, "", f"Error generating code: {error}", app_frame.value
        
        # Run the app
        port, run_error = run_gradio_app(code, new_app_id)
        if run_error:
            return current_app_id, code, f"Error running app: {run_error}", app_frame.value
        
        # Create an iframe to display the app
        iframe_html = f"""
        <div style="height:600px; border:1px solid #ddd; border-radius:8px; overflow:hidden;">
            <iframe src="http://localhost:{port}" width="100%" height="100%" frameborder="0"></iframe>
        </div>
        """
        
        return new_app_id, code, f"✅ App running on port {port}", iframe_html
    
    # Stop the app
    def on_stop_click(current_app_id):
        if not current_app_id:
            return "", "No app is currently running"
        
        stopped = stop_gradio_app(current_app_id)
        
        if stopped:
            # Reset the frame
            iframe_html = """
            <div style="display:flex; justify-content:center; align-items:center; height:600px; border:1px dashed #ccc; border-radius:8px;">
                <div style="text-align:center;">
                    <h3>App Stopped</h3>
                    <p>Generate a new app to see it here</p>
                </div>
            </div>
            """
            
            return "", f"✅ App stopped successfully", iframe_html
        else:
            return current_app_id, "Failed to stop the app", app_frame.value
    
    # Connect the generate button
    generate_btn.click(
        on_generate_click,
        inputs=[prompt, app_id],
        outputs=[app_id, code_output, status_output, app_frame]
    )
    
    # Connect the stop button
    stop_btn.click(
        on_stop_click,
        inputs=[app_id],
        outputs=[app_id, status_output, app_frame]
    )

# Launch the main app
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)