Spaces:
Sleeping
Sleeping
File size: 3,859 Bytes
5becb6b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
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",
)
|