|
import gradio as gr |
|
import json |
|
import os |
|
|
|
|
|
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 = {} |
|
|
|
|
|
def process_chat(message, local_state): |
|
|
|
local_state = local_state or {} |
|
|
|
local_state["last_message"] = message |
|
|
|
|
|
if message.strip().lower() == "update global": |
|
|
|
global global_game_state |
|
global_game_state = local_state.copy() |
|
|
|
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") |
|
|
|
|
|
with gr.Row(): |
|
|
|
with gr.Column(scale=3): |
|
gr.Markdown("### Game") |
|
|
|
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> |
|
""" |
|
) |
|
|
|
with gr.Column(scale=1): |
|
gr.Markdown("### Chat") |
|
|
|
chat_output = gr.Textbox(label="Chat Output", interactive=False) |
|
|
|
chat_input = gr.Textbox( |
|
placeholder="Type your message here...", |
|
label="Your Message" |
|
) |
|
|
|
local_state = gr.State({}) |
|
|
|
|
|
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) |
|
|