nukopy
Add audio files with Git LFS support
5becb6b
import random
import time
import gradio as gr
import numpy as np
def store_message(message: str, history: list[str]):
output = {"Current messages": message, "Previous messages": history[::-1]}
history.append(message)
return output, history
def store_message_ui():
gr.Interface(
fn=store_message,
inputs=["textbox", gr.State(value=[])],
outputs=["json", gr.State()],
title="Session State Management",
)
def fake_gan():
time.sleep(1)
images = [
"https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80",
"https://images.unsplash.com/photo-1554151228-14d9def656e4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=386&q=80",
"https://images.unsplash.com/photo-1542909168-82c3e7fdca5c?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8aHVtYW4lMjBmYWNlfGVufDB8fDB8fA%3D%3D&w=1000&q=80",
]
return images
def fake_gan_ui():
gr.Interface(
fn=fake_gan,
inputs=None,
outputs=gr.Gallery(label="Generated Images", columns=2),
title="FD-GAN",
description="This is a fake demo of a GAN. In reality, the images are randomly chosen from Unsplash.",
)
def fake_diffusion(steps):
rng = np.random.default_rng()
for i in range(steps):
time.sleep(1)
image = rng.random(size=(600, 600, 3))
yield image
image = np.ones((1000, 1000, 3), np.uint8)
image[:] = [255, 124, 0]
yield image
def fake_diffusion_ui():
gr.Interface(
fake_diffusion,
inputs=gr.Slider(1, 10, 3, step=1, label="Steps"),
outputs="image",
title="Fake Diffusion",
description="This is a fake demo of a diffusion model. In reality, the images are randomly chosen from Unsplash.",
)
def fake_chat_ui():
with gr.Blocks(title="Fake Chat"):
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
def user(user_message: str, history: list[list[str]]):
return "", history + [[user_message, None]]
def bot(history):
bot_message = random.choice(
["How are you?", "I love you", "I'm very hungry"]
)
time.sleep(1)
user_message = history[-1][0]
history[-1][1] = f"reply to: {user_message}\n{bot_message}"
return history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(lambda: None, None, chatbot, queue=False)
def keep_repeating(audio_file):
for _ in range(10):
time.sleep(0.5)
print(audio_file)
yield audio_file
def keep_repeating_ui():
gr.Interface(
keep_repeating,
gr.Audio(sources=["microphone"], type="filepath"),
gr.Audio(streaming=True, autoplay=True),
title="Keep Repeating",
description="Keep repeating the audio from the microphone",
)
def flip(im):
return np.flipud(im)
def streaming_cam_ui():
gr.Interface(
flip,
gr.Image(sources=["webcam"], streaming=True),
"image",
live=True,
title="Streaming Camera",
)
def main():
# fake GAN
fake_gan_ui()
# keep repeating
keep_repeating_ui()
# fake chat
# fake_chat_ui()
# fake diffusion
fake_diffusion_ui()
# session state management
store_message_ui()
# streaming camera
streaming_cam_ui()
# static audio
gr.Interface(
fn=lambda x: x,
inputs=gr.Audio(sources=["microphone"]),
outputs=gr.Audio(sources=["microphone"]),
title="Static Audio",
description="Play the audio from the microphone",
)