File size: 2,179 Bytes
b0127a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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)