Created
December 20, 2024 11:21
-
-
Save kevinsimper/98371019c0659a62b42e9f5f59b77739 to your computer and use it in GitHub Desktop.
Build a Gradio app with interactive tabs for ‘Hello’ and ‘Bye,’ dynamically displaying random greetings and farewells using Python’s random module for added fun. This code demonstrates the use of Gradio’s Blocks and TabbedInterface for creating intuitive web-based user interfaces.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gradio as gr | |
import random | |
def create_hello_world_tab(): | |
with gr.Blocks() as hello_world: | |
def greet_on_load(): | |
return f"Random Hello Number: {random.randint(1, 100)}" | |
output = gr.Textbox(label="Greeting Message") | |
gr.Interface( | |
fn=lambda name: "Hello " + name, | |
inputs="text", | |
outputs=output | |
) | |
hello_world.load(greet_on_load, inputs=None, outputs=output) | |
return hello_world | |
def create_bye_world_tab(): | |
with gr.Blocks() as bye_world: | |
def bye_on_load(): | |
return f"Random Bye Number: {random.randint(101, 200)}" | |
output = gr.Textbox(label="Farewell Message") | |
gr.Interface( | |
fn=lambda name: "Bye " + name, | |
inputs="text", | |
outputs=output | |
) | |
bye_world.load(bye_on_load, inputs=None, outputs=output) | |
return bye_world | |
hello_world = create_hello_world_tab() | |
bye_world = create_bye_world_tab() | |
with gr.Blocks() as demo: | |
gr.TabbedInterface( | |
[hello_world, bye_world], | |
["Hello World", "Bye World"] | |
) | |
# Main entry point | |
if __name__ == "__main__": | |
demo.launch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment