Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,336 +1,37 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import json
|
4 |
-
import importlib
|
5 |
-
import sys
|
6 |
-
from contextlib import redirect_stdout, redirect_stderr
|
7 |
-
import io
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
# Example app implementations
|
13 |
-
APP_REGISTRY = {
|
14 |
-
"hello_world": {
|
15 |
-
"title": "Hello World",
|
16 |
-
"description": "A simple greeting app",
|
17 |
-
"function": None # will be populated
|
18 |
-
},
|
19 |
-
"calculator": {
|
20 |
-
"title": "Calculator",
|
21 |
-
"description": "A basic arithmetic calculator",
|
22 |
-
"function": None # will be populated
|
23 |
-
},
|
24 |
-
"image_filter": {
|
25 |
-
"title": "Image Filter",
|
26 |
-
"description": "Apply visual effects to images",
|
27 |
-
"function": None # will be populated
|
28 |
-
}
|
29 |
-
}
|
30 |
-
|
31 |
-
# Hello World implementation
|
32 |
-
def create_hello_world():
|
33 |
-
def greet(name):
|
34 |
-
return f"Hello, {name}!"
|
35 |
-
|
36 |
-
demo = gr.Interface(
|
37 |
-
fn=greet,
|
38 |
-
inputs=gr.Textbox(label="Your Name"),
|
39 |
-
outputs=gr.Textbox(label="Greeting"),
|
40 |
-
title="Hello World App",
|
41 |
-
description="A simple greeting app",
|
42 |
-
examples=[["World"], ["Friend"], ["Gradio"]]
|
43 |
-
)
|
44 |
-
|
45 |
-
return demo
|
46 |
-
|
47 |
-
# Calculator implementation
|
48 |
-
def create_calculator():
|
49 |
-
def calculate(num1, num2, operation):
|
50 |
-
if operation == "Add":
|
51 |
-
return num1 + num2
|
52 |
-
elif operation == "Subtract":
|
53 |
-
return num1 - num2
|
54 |
-
elif operation == "Multiply":
|
55 |
-
return num1 * num2
|
56 |
-
elif operation == "Divide":
|
57 |
-
if num2 == 0:
|
58 |
-
return "Error: Division by zero"
|
59 |
-
return num1 / num2
|
60 |
-
|
61 |
-
demo = gr.Interface(
|
62 |
-
fn=calculate,
|
63 |
-
inputs=[
|
64 |
-
gr.Number(label="First Number"),
|
65 |
-
gr.Number(label="Second Number"),
|
66 |
-
gr.Radio(["Add", "Subtract", "Multiply", "Divide"], label="Operation")
|
67 |
-
],
|
68 |
-
outputs=gr.Textbox(label="Result"),
|
69 |
-
title="Calculator App",
|
70 |
-
description="Perform basic arithmetic operations",
|
71 |
-
examples=[
|
72 |
-
[5, 3, "Add"],
|
73 |
-
[10, 4, "Subtract"],
|
74 |
-
[6, 7, "Multiply"],
|
75 |
-
[20, 4, "Divide"]
|
76 |
-
]
|
77 |
-
)
|
78 |
-
|
79 |
-
return demo
|
80 |
-
|
81 |
-
# Image Filter implementation
|
82 |
-
def create_image_filter():
|
83 |
-
def apply_filter(image, filter_type):
|
84 |
-
if image is None:
|
85 |
-
return None
|
86 |
-
|
87 |
-
import numpy as np
|
88 |
-
from PIL import Image
|
89 |
-
|
90 |
-
img_array = np.array(image)
|
91 |
-
|
92 |
-
if filter_type == "Grayscale":
|
93 |
-
result = np.mean(img_array, axis=2).astype(np.uint8)
|
94 |
-
return Image.fromarray(result)
|
95 |
-
elif filter_type == "Invert":
|
96 |
-
result = 255 - img_array
|
97 |
-
return Image.fromarray(result)
|
98 |
-
elif filter_type == "Sepia":
|
99 |
-
sepia = np.array([[0.393, 0.769, 0.189],
|
100 |
-
[0.349, 0.686, 0.168],
|
101 |
-
[0.272, 0.534, 0.131]])
|
102 |
-
sepia_img = img_array.dot(sepia.T)
|
103 |
-
sepia_img[sepia_img > 255] = 255
|
104 |
-
return Image.fromarray(sepia_img.astype(np.uint8))
|
105 |
-
return image
|
106 |
-
|
107 |
-
demo = gr.Interface(
|
108 |
-
fn=apply_filter,
|
109 |
-
inputs=[
|
110 |
-
gr.Image(type="pil"),
|
111 |
-
gr.Radio(["Grayscale", "Invert", "Sepia"], label="Filter")
|
112 |
-
],
|
113 |
-
outputs=gr.Image(type="pil"),
|
114 |
-
title="Image Filter App",
|
115 |
-
description="Apply different filters to your images",
|
116 |
-
examples=[
|
117 |
-
["https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=500", "Grayscale"],
|
118 |
-
["https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=500", "Invert"],
|
119 |
-
["https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=500", "Sepia"]
|
120 |
-
]
|
121 |
-
)
|
122 |
-
|
123 |
-
return demo
|
124 |
-
|
125 |
-
# Register demo creation functions
|
126 |
-
APP_REGISTRY["hello_world"]["function"] = create_hello_world
|
127 |
-
APP_REGISTRY["calculator"]["function"] = create_calculator
|
128 |
-
APP_REGISTRY["image_filter"]["function"] = create_image_filter
|
129 |
-
|
130 |
-
# Example of code for each app as a string (for display purposes)
|
131 |
-
APP_CODES = {
|
132 |
-
"hello_world": """
|
133 |
import gradio as gr
|
134 |
|
135 |
def greet(name):
|
136 |
return f"Hello, {name}!"
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
outputs=gr.Textbox(label="Greeting"),
|
142 |
-
title="Hello World App",
|
143 |
-
description="A simple greeting app",
|
144 |
-
examples=[["World"], ["Friend"], ["Gradio"]]
|
145 |
-
)
|
146 |
-
""",
|
147 |
-
|
148 |
-
"calculator": """
|
149 |
-
import gradio as gr
|
150 |
-
|
151 |
-
def calculate(num1, num2, operation):
|
152 |
-
if operation == "Add":
|
153 |
-
return num1 + num2
|
154 |
-
elif operation == "Subtract":
|
155 |
-
return num1 - num2
|
156 |
-
elif operation == "Multiply":
|
157 |
-
return num1 * num2
|
158 |
-
elif operation == "Divide":
|
159 |
-
if num2 == 0:
|
160 |
-
return "Error: Division by zero"
|
161 |
-
return num1 / num2
|
162 |
-
|
163 |
-
demo = gr.Interface(
|
164 |
-
fn=calculate,
|
165 |
-
inputs=[
|
166 |
-
gr.Number(label="First Number"),
|
167 |
-
gr.Number(label="Second Number"),
|
168 |
-
gr.Radio(["Add", "Subtract", "Multiply", "Divide"], label="Operation")
|
169 |
-
],
|
170 |
-
outputs=gr.Textbox(label="Result"),
|
171 |
-
title="Calculator App",
|
172 |
-
description="Perform basic arithmetic operations",
|
173 |
-
examples=[
|
174 |
-
[5, 3, "Add"],
|
175 |
-
[10, 4, "Subtract"],
|
176 |
-
[6, 7, "Multiply"],
|
177 |
-
[20, 4, "Divide"]
|
178 |
-
]
|
179 |
-
)
|
180 |
-
""",
|
181 |
-
|
182 |
-
"image_filter": """
|
183 |
-
import gradio as gr
|
184 |
-
import numpy as np
|
185 |
-
from PIL import Image
|
186 |
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
if filter_type == "Grayscale":
|
194 |
-
result = np.mean(img_array, axis=2).astype(np.uint8)
|
195 |
-
return Image.fromarray(result)
|
196 |
-
elif filter_type == "Invert":
|
197 |
-
result = 255 - img_array
|
198 |
-
return Image.fromarray(result)
|
199 |
-
elif filter_type == "Sepia":
|
200 |
-
sepia = np.array([[0.393, 0.769, 0.189],
|
201 |
-
[0.349, 0.686, 0.168],
|
202 |
-
[0.272, 0.534, 0.131]])
|
203 |
-
sepia_img = img_array.dot(sepia.T)
|
204 |
-
sepia_img[sepia_img > 255] = 255
|
205 |
-
return Image.fromarray(sepia_img.astype(np.uint8))
|
206 |
-
return image
|
207 |
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
gr.Radio(["Grayscale", "Invert", "Sepia"], label="Filter")
|
213 |
-
],
|
214 |
-
outputs=gr.Image(type="pil"),
|
215 |
-
title="Image Filter App",
|
216 |
-
description="Apply different filters to your images",
|
217 |
-
examples=[
|
218 |
-
["https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=500", "Grayscale"],
|
219 |
-
["https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=500", "Invert"],
|
220 |
-
["https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=500", "Sepia"]
|
221 |
-
]
|
222 |
-
)
|
223 |
-
"""
|
224 |
-
}
|
225 |
|
226 |
-
#
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
# App selection and display state
|
233 |
-
current_app = gr.State("")
|
234 |
-
|
235 |
-
with gr.Row():
|
236 |
-
with gr.Column(scale=1):
|
237 |
-
# App selection area
|
238 |
-
gr.Markdown("### Choose an App Template")
|
239 |
-
app_selection = gr.Radio(
|
240 |
-
choices=list(APP_REGISTRY.keys()),
|
241 |
-
value="hello_world",
|
242 |
-
label="Available Templates",
|
243 |
-
info="Select a template to view and run it",
|
244 |
-
interactive=True
|
245 |
-
)
|
246 |
-
|
247 |
-
# Description display
|
248 |
-
app_description = gr.Markdown(f"**{APP_REGISTRY['hello_world']['title']}**: {APP_REGISTRY['hello_world']['description']}")
|
249 |
-
|
250 |
-
# Load button
|
251 |
-
load_btn = gr.Button("Load Selected App", variant="primary")
|
252 |
-
|
253 |
-
# Code display
|
254 |
-
with gr.Accordion("View Code", open=False):
|
255 |
-
code_display = gr.Code(
|
256 |
-
language="python",
|
257 |
-
value=APP_CODES["hello_world"],
|
258 |
-
label="App Code"
|
259 |
-
)
|
260 |
-
|
261 |
-
# Status message
|
262 |
-
status_msg = gr.Markdown("Select an app and click 'Load Selected App'")
|
263 |
-
|
264 |
-
with gr.Column(scale=2, elem_id="app-container"):
|
265 |
-
# This HTML component will hold the iframe
|
266 |
-
app_iframe = gr.HTML(
|
267 |
-
value="<p>No app loaded yet. Select an app and click 'Load Selected App'.</p>",
|
268 |
-
label="App Preview"
|
269 |
-
)
|
270 |
-
|
271 |
-
# Update description and code when selection changes
|
272 |
-
def update_description_and_code(selection):
|
273 |
-
return (
|
274 |
-
f"**{APP_REGISTRY[selection]['title']}**: {APP_REGISTRY[selection]['description']}",
|
275 |
-
APP_CODES[selection]
|
276 |
-
)
|
277 |
-
|
278 |
-
app_selection.change(
|
279 |
-
update_description_and_code,
|
280 |
-
inputs=app_selection,
|
281 |
-
outputs=[app_description, code_display]
|
282 |
-
)
|
283 |
-
|
284 |
-
# Load the selected app using an iframe
|
285 |
-
def load_app_in_iframe(app_name, previous_app):
|
286 |
-
if app_name not in APP_REGISTRY:
|
287 |
-
return (
|
288 |
-
f"Error: App '{app_name}' not found",
|
289 |
-
"<p>App not found!</p>",
|
290 |
-
previous_app
|
291 |
-
)
|
292 |
-
|
293 |
-
# If the same app is already loaded, just notify the user
|
294 |
-
if app_name == previous_app:
|
295 |
-
return (
|
296 |
-
f"{APP_REGISTRY[app_name]['title']} is already loaded",
|
297 |
-
f'<iframe id="app-iframe" src="/{app_name}"></iframe>',
|
298 |
-
app_name
|
299 |
-
)
|
300 |
-
|
301 |
-
# Create iframe HTML
|
302 |
-
iframe_html = f'<iframe id="app-iframe" src="/{app_name}"></iframe>'
|
303 |
-
|
304 |
-
return (
|
305 |
-
f"Successfully loaded {APP_REGISTRY[app_name]['title']}",
|
306 |
-
iframe_html,
|
307 |
-
app_name
|
308 |
-
)
|
309 |
-
|
310 |
-
# Connect the button to the load function
|
311 |
-
load_btn.click(
|
312 |
-
load_app_in_iframe,
|
313 |
-
inputs=[app_selection, current_app],
|
314 |
-
outputs=[status_msg, app_iframe, current_app]
|
315 |
-
)
|
316 |
-
|
317 |
-
return demo
|
318 |
|
319 |
-
|
320 |
-
def create_app():
|
321 |
-
# Create the main selector app
|
322 |
-
main_app = create_main_app()
|
323 |
-
|
324 |
-
# Create all the individual apps and mount them at their respective paths
|
325 |
-
for app_name, app_info in APP_REGISTRY.items():
|
326 |
-
app_func = app_info["function"]
|
327 |
-
if app_func:
|
328 |
-
app_instance = app_func()
|
329 |
-
main_app = gr.mount_gradio_app(main_app, app_instance, f"/{app_name}")
|
330 |
-
|
331 |
-
return main_app
|
332 |
|
333 |
-
#
|
334 |
if __name__ == "__main__":
|
335 |
-
|
336 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
1 |
import gradio as gr
|
2 |
+
import threading
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Hello World Gradio app as a string
|
5 |
+
hello_world_code = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
import gradio as gr
|
7 |
|
8 |
def greet(name):
|
9 |
return f"Hello, {name}!"
|
10 |
|
11 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
12 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
13 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Function to execute the Gradio app dynamically
|
16 |
+
def run_gradio_app():
|
17 |
+
try:
|
18 |
+
exec(hello_world_code, {"gr": gr})
|
19 |
+
except Exception as e:
|
20 |
+
print(f"Error executing generated code: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
# Function triggered on button click
|
23 |
+
def launch_app():
|
24 |
+
threading.Thread(target=run_gradio_app, daemon=True).start()
|
25 |
+
return "Hello World Gradio app launched! Refresh to see it."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
# Gradio UI
|
28 |
+
with gr.Blocks() as ui:
|
29 |
+
gr.Markdown("### Basic Dynamic Gradio Loader")
|
30 |
+
launch_button = gr.Button("Launch Hello World App")
|
31 |
+
output_text = gr.Textbox(label="Status")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
launch_button.click(launch_app, outputs=output_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
# Start the initial UI
|
36 |
if __name__ == "__main__":
|
37 |
+
ui.launch(server_name="0.0.0.0", server_port=7860)
|
|