awacke1's picture
Update app.py
8bb59aa verified
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import time
# 1. πŸš€ Interface - The launchpad of Gradio greatness!
def hello_world(name):
return f"Hello, {name}! Welcome to the Gradio circus! πŸŽͺ"
# 2. 🧱 Blocks - Building blocks so fun, even LEGO is jealous!
with gr.Blocks() as demo:
gr.Markdown("# 🎭 Gradio's 28 Ring Circus!")
# 3. πŸ“ Textbox - Where words come to party!
name_input = gr.Textbox(label="Enter your name, circus-goer!")
greeting_output = gr.Textbox(label="Ringmaster's Welcome")
gr.Interface(fn=hello_world, inputs=name_input, outputs=greeting_output)
# 4. πŸ”’ Number - Counting clowns has never been easier!
def square_number(num):
return f"Squared faster than a juggler's hands: {num**2}"
gr.Interface(fn=square_number, inputs=gr.Number(label="Enter a number to square"), outputs="text")
# 5. 🎚️ Slider - Slide into DMs? Nah, slide into data!
def slider_shenanigans(value):
return f"Slider set to {value}! That's {value/10} in clown shoes!"
gr.Interface(fn=slider_shenanigans, inputs=gr.Slider(0, 100), outputs="text")
# 6. βœ… Checkbox - To check or not to check, that is the question!
def checkbox_challenge(checked):
return "You've unleashed the confetti cannon!" if checked else "Confetti cannon: sad and unused 😒"
gr.Interface(fn=checkbox_challenge, inputs=gr.Checkbox(label="Release the confetti?"), outputs="text")
# 7. πŸ“» Radio - Tune into the frequency of fun!
def radio_racket(choice):
sounds = {"Lion": "🦁 ROAR!", "Elephant": "🐘 TRUMPET!", "Monkey": "πŸ’ OOH OOH AH AH!"}
return f"You tuned into: {sounds.get(choice, 'Static... must be the clowns interfering')}"
gr.Interface(fn=radio_racket, inputs=gr.Radio(["Lion", "Elephant", "Monkey"], label="Choose your circus animal"), outputs="text")
# 8. πŸ—³οΈ Dropdown - Drop it like it's hot (but it's a select menu)!
def dropdown_delight(choice):
return f"You chose {choice}! A person of exquisite taste in circus snacks! 🍿"
gr.Interface(fn=dropdown_delight, inputs=gr.Dropdown(["Popcorn", "Cotton Candy", "Peanuts"], label="Pick your circus treat"), outputs="text")
# 9. πŸ–ΌοΈ Image - Worth a thousand words, or one really good meme!
def image_inverter(img):
return Image.fromarray(255 - np.array(img))
gr.Interface(fn=image_inverter, inputs=gr.Image(label="Upload an image to invert"), outputs="image")
# 10. 🎬 Video - Moving pictures, like magic but with more pixels!
def video_info(video):
return f"Video info: {video.name}. Warning: May contain clowns!"
gr.Interface(fn=video_info, inputs=gr.Video(label="Upload a video"), outputs="text")
# 11. 🎡 Audio - Making waves, literally!
def audio_echo(audio):
return (audio[1], 22050) # Return the audio with same sample rate
gr.Interface(fn=audio_echo, inputs=gr.Audio(sources=["microphone"], type="numpy"), outputs="audio")
# 12. πŸ“‚ File - Where digital hoarders unite!
def file_fun(file):
return f"File uploaded: {file.name}. I hope it's not your tax returns!"
gr.Interface(fn=file_fun, inputs=gr.File(label="Upload a file"), outputs="text")
# 13. 🏷️ Label - Sticking labels on things since kindergarten!
def label_laughs(text):
return "Funny" if "laugh" in text.lower() else "Not funny (try again, clown)"
gr.Interface(fn=label_laughs, inputs="text", outputs=gr.Label())
# 14-15-16. πŸ–ΌοΈπŸŽ¬πŸŽ΅ Image, Video, Audio Output - The holy trinity of media outputs!
def media_madness(choice):
if choice == "Image":
return Image.new('RGB', (100, 100), color = 'red')
elif choice == "Video":
return gr.Video.update(visible=True, value=None)
else:
return gr.Audio.update(visible=True, value=None)
gr.Interface(
fn=media_madness,
inputs=gr.Radio(["Image", "Video", "Audio"]),
outputs=[gr.Image(), gr.Video(visible=False), gr.Audio(visible=False)]
)
# 17. πŸ“Š Plot - Making data look pretty since matplotlib was a twinkle in its creator's eye!
def plot_partay():
fig, ax = plt.subplots()
x = np.linspace(0, 10, 100)
ax.plot(x, np.sin(x))
ax.set_title("Sine Wave (AKA The Rollercoaster of Emotions)")
return fig
gr.Interface(fn=plot_partay, inputs=None, outputs="plot")
# 18. πŸ“‹ JSON - Because sometimes life needs to be structured!
def json_jester():
return {"joke": "Why don't scientists trust atoms? Because they make up everything!"}
gr.Interface(fn=json_jester, inputs=None, outputs="json")
# 19. πŸš€ Launch - We have liftoff! (Hopefully not like that one rocket...)
def prepare_for_launch():
return "Initiating launch sequence... Don't forget your space helmet!"
launch_interface = gr.Interface(fn=prepare_for_launch, inputs=None, outputs="text")
# 20. πŸšͺ Close - All good things must come to an end (like this increasingly long demo)
def time_to_close():
time.sleep(2) # Pretend we're doing important closing stuff
return "Show's over, folks! Time to pack up the circus tent!"
gr.Interface(fn=time_to_close, inputs=None, outputs="text")
# 21. πŸ’Ύ Load - Because sometimes you just need to pick up where you left off
# Note: This is a placeholder. You'll need to replace it with an actual saved model.
# saved_interface = gr.Interface.load("models/saved_model")
saved_interface = gr.Interface(fn=lambda x: f"Loaded model says: {x}", inputs="text", outputs="text")
# 22. πŸ“‘ TabbedInterface - For when you can't decide which interface to use!
with gr.TabbedInterface([launch_interface, saved_interface], ["New Launch", "Saved Launch"]):
gr.Markdown("Choose your launch adventure!")
# 25. 🎟️ Queueing - Like waiting in line for a rollercoaster, but for data!
def long_process(x):
time.sleep(5) # Simulate a long process
return f"Processed: {x}"
gr.Interface(fn=long_process, inputs="text", outputs="text").queue()
# 26. πŸ” Auth - Keep the riffraff out (and by riffraff, we mean anyone without a password)
def secret_function():
return "You've accessed the secret function! The password is 'password123'. Don't tell anyone!"
gr.Interface(fn=secret_function, inputs=None, outputs="text").launch(auth=("user", "pass"))
# 27. 🎨 Theme - Because even data needs a good outfit
gr.themes.Base() # Using a built-in theme as custom themes might not be available
# 28. πŸ“Š Analytics - Keeping track of all the fun (and the not-so-fun)
# Note: analytics_enabled is typically set when launching the interface, not here
# Let the show begin!
demo.launch()