File size: 2,668 Bytes
b5ec5af 9e28f89 b5ec5af 9e28f89 |
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 |
import gradio as gr
import json
import os
# Optional: Load global state if persistence is required
GLOBAL_STATE_FILE = "global_state.json"
if os.path.exists(GLOBAL_STATE_FILE):
with open(GLOBAL_STATE_FILE, "r") as f:
global_game_state = json.load(f)
else:
global_game_state = {} # initialize an empty state
# Function to handle chat updates
def process_chat(message, local_state):
# Initialize local state if None
local_state = local_state or {}
# Update local state with the latest message (example logic)
local_state["last_message"] = message
# Check for a command to update the global game state
if message.strip().lower() == "update global":
# Update the global state (here simply replace it with local_state)
global global_game_state
global_game_state = local_state.copy()
# Optionally persist the global state to a file
with open(GLOBAL_STATE_FILE, "w") as f:
json.dump(global_game_state, f)
response = "Global game updated!"
else:
response = f"Local game updated with: {message}"
return "", local_state, response
with gr.Blocks() as demo:
gr.Markdown("## Live Game & Chat Interface")
# Create a two-column layout
with gr.Row():
# Left Column: Iframe for the game (takes more space)
with gr.Column(scale=3):
gr.Markdown("### Game")
# The iframe points to your static index.html
gr.HTML(
value="""
<iframe src="https://experiment-sandpack.vercel.app" style="width:100%; height:600px; border:none;" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
"""
)
# Right Column: Chat interface (smaller column)
with gr.Column(scale=1):
gr.Markdown("### Chat")
# Chatbot to display the conversation or responses
chat_output = gr.Textbox(label="Chat Output", interactive=False)
# Textbox for user to input messages
chat_input = gr.Textbox(
placeholder="Type your message here...",
label="Your Message"
)
# A hidden state to keep track of the local game state for the session
local_state = gr.State({})
# When user submits a message, call process_chat
chat_input.submit(
process_chat,
inputs=[chat_input, local_state],
outputs=[chat_input, local_state, chat_output],
)
demo.launch(server_name="0.0.0.0", server_port=7861)
|