Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
from PIL import Image
|
4 |
-
|
5 |
-
# Simple function to set matplotlib config dir
|
6 |
import os
|
|
|
|
|
|
|
|
|
7 |
os.environ['MPLCONFIGDIR'] = '/tmp'
|
8 |
|
9 |
-
#
|
|
|
|
|
|
|
|
|
10 |
def greet(name):
|
11 |
return f"Hello, {name}!"
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def calculate(num1, num2, operation):
|
15 |
if operation == "Add":
|
16 |
return num1 + num2
|
@@ -23,7 +38,24 @@ def calculate(num1, num2, operation):
|
|
23 |
return "Error: Division by zero"
|
24 |
return num1 / num2
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
def apply_filter(image, filter_type):
|
28 |
if image is None:
|
29 |
return None
|
@@ -45,57 +77,191 @@ def apply_filter(image, filter_type):
|
|
45 |
return Image.fromarray(sepia_img.astype(np.uint8))
|
46 |
return image
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
gr.Markdown("Perform basic arithmetic operations between two numbers.")
|
70 |
-
|
71 |
-
num1 = gr.Number(label="First Number", value=5)
|
72 |
-
num2 = gr.Number(label="Second Number", value=3)
|
73 |
-
operation = gr.Radio(
|
74 |
-
["Add", "Subtract", "Multiply", "Divide"],
|
75 |
-
label="Operation",
|
76 |
-
value="Add"
|
77 |
-
)
|
78 |
-
calc_btn = gr.Button("Calculate")
|
79 |
-
result = gr.Textbox(label="Result")
|
80 |
-
|
81 |
-
calc_btn.click(fn=calculate, inputs=[num1, num2, operation], outputs=result)
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
# Launch the app
|
100 |
if __name__ == "__main__":
|
101 |
-
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
+
import io
|
4 |
+
from contextlib import redirect_stdout, redirect_stderr
|
5 |
+
|
6 |
+
# Set fixed MPLCONFIGDIR to avoid permission issues
|
7 |
os.environ['MPLCONFIGDIR'] = '/tmp'
|
8 |
|
9 |
+
# Sample code for different Gradio apps
|
10 |
+
EXAMPLE_CODES = {
|
11 |
+
"hello_world": """
|
12 |
+
import gradio as gr
|
13 |
+
|
14 |
def greet(name):
|
15 |
return f"Hello, {name}!"
|
16 |
|
17 |
+
demo = gr.Interface(
|
18 |
+
fn=greet,
|
19 |
+
inputs=gr.Textbox(label="Your Name"),
|
20 |
+
outputs=gr.Textbox(label="Greeting"),
|
21 |
+
title="Hello World",
|
22 |
+
description="A simple greeting app"
|
23 |
+
)
|
24 |
+
""",
|
25 |
+
|
26 |
+
"calculator": """
|
27 |
+
import gradio as gr
|
28 |
+
|
29 |
def calculate(num1, num2, operation):
|
30 |
if operation == "Add":
|
31 |
return num1 + num2
|
|
|
38 |
return "Error: Division by zero"
|
39 |
return num1 / num2
|
40 |
|
41 |
+
demo = gr.Interface(
|
42 |
+
fn=calculate,
|
43 |
+
inputs=[
|
44 |
+
gr.Number(label="First Number"),
|
45 |
+
gr.Number(label="Second Number"),
|
46 |
+
gr.Radio(["Add", "Subtract", "Multiply", "Divide"], label="Operation")
|
47 |
+
],
|
48 |
+
outputs=gr.Textbox(label="Result"),
|
49 |
+
title="Calculator",
|
50 |
+
description="Perform basic arithmetic operations"
|
51 |
+
)
|
52 |
+
""",
|
53 |
+
|
54 |
+
"image_filter": """
|
55 |
+
import gradio as gr
|
56 |
+
import numpy as np
|
57 |
+
from PIL import Image
|
58 |
+
|
59 |
def apply_filter(image, filter_type):
|
60 |
if image is None:
|
61 |
return None
|
|
|
77 |
return Image.fromarray(sepia_img.astype(np.uint8))
|
78 |
return image
|
79 |
|
80 |
+
demo = gr.Interface(
|
81 |
+
fn=apply_filter,
|
82 |
+
inputs=[
|
83 |
+
gr.Image(type="pil"),
|
84 |
+
gr.Radio(["Grayscale", "Invert", "Sepia"], label="Filter")
|
85 |
+
],
|
86 |
+
outputs=gr.Image(type="pil"),
|
87 |
+
title="Image Filter",
|
88 |
+
description="Apply various filters to images"
|
89 |
+
)
|
90 |
+
"""
|
91 |
+
}
|
92 |
+
|
93 |
+
# Function to simulate LLM API call
|
94 |
+
def simulate_llm_response(prompt):
|
95 |
+
"""Simulate an LLM response based on the prompt"""
|
96 |
+
prompt_lower = prompt.lower()
|
97 |
+
|
98 |
+
if "hello" in prompt_lower or "greet" in prompt_lower:
|
99 |
+
return EXAMPLE_CODES["hello_world"], None
|
100 |
+
elif "calculat" in prompt_lower or "math" in prompt_lower or "arithmetic" in prompt_lower:
|
101 |
+
return EXAMPLE_CODES["calculator"], None
|
102 |
+
elif "image" in prompt_lower or "filter" in prompt_lower or "photo" in prompt_lower:
|
103 |
+
return EXAMPLE_CODES["image_filter"], None
|
104 |
+
else:
|
105 |
+
# Default to hello world
|
106 |
+
return EXAMPLE_CODES["hello_world"], None
|
107 |
+
|
108 |
+
# Function to execute code and extract the demo object
|
109 |
+
def execute_code(code):
|
110 |
+
"""Execute the code and return the demo object"""
|
111 |
+
# Create a clean namespace
|
112 |
+
namespace = {}
|
113 |
+
|
114 |
+
# Capture stdout and stderr
|
115 |
+
stdout_buffer = io.StringIO()
|
116 |
+
stderr_buffer = io.StringIO()
|
117 |
+
|
118 |
+
try:
|
119 |
+
with redirect_stdout(stdout_buffer), redirect_stderr(stderr_buffer):
|
120 |
+
# Execute the code in the namespace
|
121 |
+
exec(code, namespace)
|
122 |
+
|
123 |
+
# Check if 'demo' exists in the namespace
|
124 |
+
if 'demo' not in namespace:
|
125 |
+
return None, "No 'demo' object found in the code", stdout_buffer.getvalue(), stderr_buffer.getvalue()
|
126 |
+
|
127 |
+
# Return the demo object
|
128 |
+
return namespace['demo'], None, stdout_buffer.getvalue(), stderr_buffer.getvalue()
|
129 |
+
|
130 |
+
except Exception as e:
|
131 |
+
import traceback
|
132 |
+
return None, f"Error executing code: {str(e)}\n{traceback.format_exc()}", stdout_buffer.getvalue(), stderr_buffer.getvalue()
|
133 |
+
|
134 |
+
# Main app
|
135 |
+
with gr.Blocks(title="LLM Gradio App Generator") as app:
|
136 |
+
# Header
|
137 |
+
gr.Markdown("# 🤖 LLM Gradio App Generator")
|
138 |
+
gr.Markdown("Generate and run Gradio apps dynamically!")
|
139 |
+
|
140 |
+
with gr.Row():
|
141 |
+
# Input column
|
142 |
+
with gr.Column(scale=1):
|
143 |
+
# App description input
|
144 |
+
prompt = gr.Textbox(
|
145 |
+
label="Describe the app you want",
|
146 |
+
placeholder="e.g., A calculator app that can perform basic arithmetic",
|
147 |
+
lines=3
|
148 |
+
)
|
149 |
+
|
150 |
+
# Example buttons
|
151 |
+
gr.Markdown("### Example Prompts")
|
152 |
+
hello_btn = gr.Button("Hello World App")
|
153 |
+
calc_btn = gr.Button("Calculator App")
|
154 |
+
img_btn = gr.Button("Image Filter App")
|
155 |
+
|
156 |
+
# Generate button
|
157 |
+
generate_btn = gr.Button("Generate & Run App", variant="primary")
|
158 |
+
|
159 |
+
# Code display
|
160 |
+
gr.Markdown("### Generated Code")
|
161 |
+
code_display = gr.Code(language="python")
|
162 |
+
|
163 |
+
# Log output
|
164 |
+
with gr.Accordion("Execution Log", open=False):
|
165 |
+
log_output = gr.Textbox(label="Output Log", lines=5)
|
166 |
+
|
167 |
+
# Output column
|
168 |
+
with gr.Column(scale=2):
|
169 |
+
# Status message
|
170 |
+
status_msg = gr.Markdown("### Generated App Will Appear Here")
|
171 |
+
|
172 |
+
# Container for the dynamically generated app
|
173 |
+
app_container = gr.HTML(
|
174 |
+
"""<div style="display:flex; justify-content:center; align-items:center; height:400px; border:1px dashed #ccc; border-radius:8px;">
|
175 |
+
<div style="text-align:center;">
|
176 |
+
<h3>No App Generated Yet</h3>
|
177 |
+
<p>Enter a description and click "Generate & Run App"</p>
|
178 |
+
</div>
|
179 |
+
</div>"""
|
180 |
+
)
|
181 |
+
|
182 |
+
# Example button functions
|
183 |
+
def set_prompt(text):
|
184 |
+
return text
|
185 |
+
|
186 |
+
hello_btn.click(
|
187 |
+
lambda: set_prompt("A simple hello world app that greets the user by name"),
|
188 |
+
inputs=None,
|
189 |
+
outputs=prompt
|
190 |
+
)
|
191 |
+
|
192 |
+
calc_btn.click(
|
193 |
+
lambda: set_prompt("A calculator app that can add, subtract, multiply and divide two numbers"),
|
194 |
+
inputs=None,
|
195 |
+
outputs=prompt
|
196 |
+
)
|
197 |
|
198 |
+
img_btn.click(
|
199 |
+
lambda: set_prompt("An image filter app that can apply grayscale, invert, and sepia filters to images"),
|
200 |
+
inputs=None,
|
201 |
+
outputs=prompt
|
202 |
+
)
|
203 |
+
|
204 |
+
# Main function to generate and run the app
|
205 |
+
def generate_and_run_app(prompt_text):
|
206 |
+
if not prompt_text:
|
207 |
+
return (
|
208 |
+
"Please enter a description of the app you want to generate.",
|
209 |
+
"",
|
210 |
+
"""<div style="text-align:center; padding:20px;">
|
211 |
+
<h3>No App Generated</h3>
|
212 |
+
<p>Please enter a description first.</p>
|
213 |
+
</div>"""
|
214 |
+
)
|
215 |
+
|
216 |
+
# Get code from LLM (simulated)
|
217 |
+
code, error = simulate_llm_response(prompt_text)
|
218 |
+
if error:
|
219 |
+
return (
|
220 |
+
code,
|
221 |
+
f"Error generating code: {error}",
|
222 |
+
"""<div style="text-align:center; color:red; padding:20px;">
|
223 |
+
<h3>Error</h3>
|
224 |
+
<p>{0}</p>
|
225 |
+
</div>""".format(error)
|
226 |
+
)
|
227 |
|
228 |
+
# Execute the code
|
229 |
+
demo_obj, exec_error, stdout, stderr = execute_code(code)
|
230 |
+
log_output = f"STDOUT:\n{stdout}\n\nSTDERR:\n{stderr}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
|
232 |
+
if exec_error:
|
233 |
+
return (
|
234 |
+
code,
|
235 |
+
f"Error: {exec_error}",
|
236 |
+
"""<div style="text-align:center; color:red; padding:20px;">
|
237 |
+
<h3>Execution Error</h3>
|
238 |
+
<p>{0}</p>
|
239 |
+
</div>""".format(exec_error)
|
240 |
+
)
|
241 |
+
|
242 |
+
try:
|
243 |
+
# Render the demo to HTML
|
244 |
+
rendered_html = demo_obj.render().replace('window.gradio_mode = "app";', '')
|
245 |
+
|
246 |
+
return code, f"✅ App generated and running!", rendered_html
|
247 |
+
except Exception as e:
|
248 |
+
import traceback
|
249 |
+
return (
|
250 |
+
code,
|
251 |
+
f"Error rendering app: {str(e)}",
|
252 |
+
"""<div style="text-align:center; color:red; padding:20px;">
|
253 |
+
<h3>Rendering Error</h3>
|
254 |
+
<p>{0}</p>
|
255 |
+
</div>""".format(traceback.format_exc())
|
256 |
+
)
|
257 |
+
|
258 |
+
# Connect the generate button
|
259 |
+
generate_btn.click(
|
260 |
+
generate_and_run_app,
|
261 |
+
inputs=prompt,
|
262 |
+
outputs=[code_display, log_output, app_container]
|
263 |
+
)
|
264 |
|
265 |
# Launch the app
|
266 |
if __name__ == "__main__":
|
267 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|