Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from PIL import Image
|
5 |
+
import time
|
6 |
+
|
7 |
+
# 1. π Interface - The launchpad of Gradio greatness!
|
8 |
+
def hello_world(name):
|
9 |
+
return f"Hello, {name}! Welcome to the Gradio circus! πͺ"
|
10 |
+
|
11 |
+
# 2. π§± Blocks - Building blocks so fun, even LEGO is jealous!
|
12 |
+
with gr.Blocks() as demo:
|
13 |
+
gr.Markdown("# π Gradio's 28 Ring Circus!")
|
14 |
+
|
15 |
+
# 3. π Textbox - Where words come to party!
|
16 |
+
name_input = gr.Textbox(label="Enter your name, circus-goer!")
|
17 |
+
greeting_output = gr.Textbox(label="Ringmaster's Welcome")
|
18 |
+
gr.Interface(fn=hello_world, inputs=name_input, outputs=greeting_output)
|
19 |
+
|
20 |
+
# 4. π’ Number - Counting clowns has never been easier!
|
21 |
+
def square_number(num):
|
22 |
+
return f"Squared faster than a juggler's hands: {num**2}"
|
23 |
+
|
24 |
+
gr.Interface(fn=square_number, inputs=gr.Number(label="Enter a number to square"), outputs="text")
|
25 |
+
|
26 |
+
# 5. ποΈ Slider - Slide into DMs? Nah, slide into data!
|
27 |
+
def slider_shenanigans(value):
|
28 |
+
return f"Slider set to {value}! That's {value/10} in clown shoes!"
|
29 |
+
|
30 |
+
gr.Interface(fn=slider_shenanigans, inputs=gr.Slider(0, 100), outputs="text")
|
31 |
+
|
32 |
+
# 6. β
Checkbox - To check or not to check, that is the question!
|
33 |
+
def checkbox_challenge(checked):
|
34 |
+
return "You've unleashed the confetti cannon!" if checked else "Confetti cannon: sad and unused π’"
|
35 |
+
|
36 |
+
gr.Interface(fn=checkbox_challenge, inputs=gr.Checkbox(label="Release the confetti?"), outputs="text")
|
37 |
+
|
38 |
+
# 7. π» Radio - Tune into the frequency of fun!
|
39 |
+
def radio_racket(choice):
|
40 |
+
sounds = {"Lion": "π¦ ROAR!", "Elephant": "π TRUMPET!", "Monkey": "π OOH OOH AH AH!"}
|
41 |
+
return f"You tuned into: {sounds.get(choice, 'Static... must be the clowns interfering')}"
|
42 |
+
|
43 |
+
gr.Interface(fn=radio_racket, inputs=gr.Radio(["Lion", "Elephant", "Monkey"], label="Choose your circus animal"), outputs="text")
|
44 |
+
|
45 |
+
# 8. π³οΈ Dropdown - Drop it like it's hot (but it's a select menu)!
|
46 |
+
def dropdown_delight(choice):
|
47 |
+
return f"You chose {choice}! A person of exquisite taste in circus snacks! πΏ"
|
48 |
+
|
49 |
+
gr.Interface(fn=dropdown_delight, inputs=gr.Dropdown(["Popcorn", "Cotton Candy", "Peanuts"], label="Pick your circus treat"), outputs="text")
|
50 |
+
|
51 |
+
# 9. πΌοΈ Image - Worth a thousand words, or one really good meme!
|
52 |
+
def image_inverter(img):
|
53 |
+
return Image.fromarray(255 - np.array(img))
|
54 |
+
|
55 |
+
gr.Interface(fn=image_inverter, inputs=gr.Image(label="Upload an image to invert"), outputs="image")
|
56 |
+
|
57 |
+
# 10. π¬ Video - Moving pictures, like magic but with more pixels!
|
58 |
+
def video_info(video):
|
59 |
+
return f"Video info: {video.name}. Warning: May contain clowns!"
|
60 |
+
|
61 |
+
gr.Interface(fn=video_info, inputs=gr.Video(label="Upload a video"), outputs="text")
|
62 |
+
|
63 |
+
# 11. π΅ Audio - Making waves, literally!
|
64 |
+
def audio_echo(audio):
|
65 |
+
return (audio[1], 22050) # Return the audio with same sample rate
|
66 |
+
|
67 |
+
gr.Interface(fn=audio_echo, inputs=gr.Audio(source="microphone", type="numpy"), outputs="audio")
|
68 |
+
|
69 |
+
# 12. π File - Where digital hoarders unite!
|
70 |
+
def file_fun(file):
|
71 |
+
return f"File uploaded: {file.name}. I hope it's not your tax returns!"
|
72 |
+
|
73 |
+
gr.Interface(fn=file_fun, inputs=gr.File(label="Upload a file"), outputs="text")
|
74 |
+
|
75 |
+
# 13. π·οΈ Label - Sticking labels on things since kindergarten!
|
76 |
+
def label_laughs(text):
|
77 |
+
return "Funny" if "laugh" in text.lower() else "Not funny (try again, clown)"
|
78 |
+
|
79 |
+
gr.Interface(fn=label_laughs, inputs="text", outputs=gr.Label())
|
80 |
+
|
81 |
+
# 14-15-16. πΌοΈπ¬π΅ Image, Video, Audio Output - The holy trinity of media outputs!
|
82 |
+
def media_madness(choice):
|
83 |
+
if choice == "Image":
|
84 |
+
return Image.new('RGB', (100, 100), color = 'red')
|
85 |
+
elif choice == "Video":
|
86 |
+
return gr.Video.update(visible=True, value="path_to_video.mp4")
|
87 |
+
else:
|
88 |
+
return gr.Audio.update(visible=True, value="path_to_audio.mp3")
|
89 |
+
|
90 |
+
gr.Interface(
|
91 |
+
fn=media_madness,
|
92 |
+
inputs=gr.Radio(["Image", "Video", "Audio"]),
|
93 |
+
outputs=[gr.Image(), gr.Video(visible=False), gr.Audio(visible=False)]
|
94 |
+
)
|
95 |
+
|
96 |
+
# 17. π Plot - Making data look pretty since matplotlib was a twinkle in its creator's eye!
|
97 |
+
def plot_partay():
|
98 |
+
fig, ax = plt.subplots()
|
99 |
+
x = np.linspace(0, 10, 100)
|
100 |
+
ax.plot(x, np.sin(x))
|
101 |
+
ax.set_title("Sine Wave (AKA The Rollercoaster of Emotions)")
|
102 |
+
return fig
|
103 |
+
|
104 |
+
gr.Interface(fn=plot_partay, inputs=None, outputs="plot")
|
105 |
+
|
106 |
+
# 18. π JSON - Because sometimes life needs to be structured!
|
107 |
+
def json_jester():
|
108 |
+
return {"joke": "Why don't scientists trust atoms? Because they make up everything!"}
|
109 |
+
|
110 |
+
gr.Interface(fn=json_jester, inputs=None, outputs="json")
|
111 |
+
|
112 |
+
# 19. π Launch - We have liftoff! (Hopefully not like that one rocket...)
|
113 |
+
def prepare_for_launch():
|
114 |
+
return "Initiating launch sequence... Don't forget your space helmet!"
|
115 |
+
|
116 |
+
launch_interface = gr.Interface(fn=prepare_for_launch, inputs=None, outputs="text")
|
117 |
+
launch_interface.launch()
|
118 |
+
|
119 |
+
# 20. πͺ Close - All good things must come to an end (like this increasingly long demo)
|
120 |
+
def time_to_close():
|
121 |
+
time.sleep(2) # Pretend we're doing important closing stuff
|
122 |
+
return "Show's over, folks! Time to pack up the circus tent!"
|
123 |
+
|
124 |
+
gr.Interface(fn=time_to_close, inputs=None, outputs="text")
|
125 |
+
|
126 |
+
# 21. πΎ Load - Because sometimes you just need to pick up where you left off
|
127 |
+
saved_interface = gr.Interface.load("models/saved_model")
|
128 |
+
|
129 |
+
# 22. π TabbedInterface - For when you can't decide which interface to use!
|
130 |
+
with gr.TabbedInterface([launch_interface, saved_interface], ["New Launch", "Saved Launch"]):
|
131 |
+
gr.Markdown("Choose your launch adventure!")
|
132 |
+
|
133 |
+
# 23. π Series - Like a good book series, but with more data processing
|
134 |
+
def step1(x):
|
135 |
+
return x + 1
|
136 |
+
|
137 |
+
def step2(x):
|
138 |
+
return x * 2
|
139 |
+
|
140 |
+
gr.Series(
|
141 |
+
gr.Interface(fn=step1, inputs="number", outputs="number"),
|
142 |
+
gr.Interface(fn=step2, inputs="number", outputs="number")
|
143 |
+
)
|
144 |
+
|
145 |
+
# 24. ποΈ Parallel - Multitasking: the art of messing up several things simultaneously
|
146 |
+
gr.Parallel(
|
147 |
+
gr.Interface(fn=lambda x: f"Path A: {x.upper()}", inputs="text", outputs="text"),
|
148 |
+
gr.Interface(fn=lambda x: f"Path B: {x.lower()}", inputs="text", outputs="text")
|
149 |
+
)
|
150 |
+
|
151 |
+
# 25. ποΈ Queueing - Like waiting in line for a rollercoaster, but for data!
|
152 |
+
def long_process(x):
|
153 |
+
time.sleep(5) # Simulate a long process
|
154 |
+
return f"Processed: {x}"
|
155 |
+
|
156 |
+
gr.Interface(fn=long_process, inputs="text", outputs="text").queue()
|
157 |
+
|
158 |
+
# 26. π Auth - Keep the riffraff out (and by riffraff, we mean anyone without a password)
|
159 |
+
def secret_function():
|
160 |
+
return "You've accessed the secret function! The password is 'password123'. Don't tell anyone!"
|
161 |
+
|
162 |
+
gr.Interface(fn=secret_function, inputs=None, outputs="text").launch(auth=("user", "pass"))
|
163 |
+
|
164 |
+
# 27. π¨ Theme - Because even data needs a good outfit
|
165 |
+
gr.Theme.from_hub("freddyaboulton/dracula_revamped")
|
166 |
+
|
167 |
+
# 28. π Analytics - Keeping track of all the fun (and the not-so-fun)
|
168 |
+
gr.analytics_enabled = True
|
169 |
+
|
170 |
+
# Let the show begin!
|
171 |
+
demo.launch()
|