time-travler / app.py
QuantaScriptor's picture
initial commit
b0127a7 verified
Raw History Blame Contribute Delete
2.18 kB
# This Gradio app simulates a time travel interface, allowing users to input a destination in time and receive a simulated response.
# The app leverages Gradio's latest features, including streaming and dynamic layouts, to create an immersive experience.
import gradio as gr
import numpy as np
import time
# Define the function that simulates time travel
def time_travel(destination, time_machine_state):
# Simulate the time travel process
for i in range(5):
time.sleep(1)
message = f"Traveling through time... {i + 1}/5"
yield message
# Simulate arrival at the destination
time.sleep(2)
arrival_message = f"Arrived at {destination}!"
return arrival_message
# Define the function that handles the time travel process
def handle_time_travel(destination, time_machine_state):
# Initialize the time machine state if it's not set
if time_machine_state is None:
time_machine_state = []
# Append the current destination to the state
time_machine_state.append(destination)
# Simulate the time travel process
for message in time_travel(destination, time_machine_state):
yield message, time_machine_state
# Define the function that handles the reset of the time machine state
def reset_time_machine():
return []
# Create the Gradio interface
with gr.Blocks() as demo:
# Define the input components
destination = gr.Textbox(label="Destination in Time")
time_machine_state = gr.State(value=[])
# Define the output components
travel_log = gr.Textbox(label="Travel Log", interactive=False)
state_display = gr.JSON(label="Time Machine State", interactive=False)
# Define the buttons
travel_btn = gr.Button("Travel Through Time")
reset_btn = gr.Button("Reset Time Machine")
# Define the event listeners
travel_btn.click(
handle_time_travel,
inputs=[destination, time_machine_state],
outputs=[travel_log, state_display],
queue=True
)
reset_btn.click(
reset_time_machine,
outputs=[time_machine_state],
queue=True
)
# Launch the interface
demo.launch(show_error=True)