Spaces:
Sleeping
Sleeping
Initial commit
Browse files- app.py +61 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
GLOBAL_STATE_FILE = "global_state.json"
|
| 6 |
+
if os.path.exists(GLOBAL_STATE_FILE):
|
| 7 |
+
with open(GLOBAL_STATE_FILE, "r") as f:
|
| 8 |
+
global_game_state = json.load(f)
|
| 9 |
+
else:
|
| 10 |
+
global_game_state = {} # initialize an empty state
|
| 11 |
+
|
| 12 |
+
# TODO: Function to handle chat updates.
|
| 13 |
+
def process_chat(message, local_state):
|
| 14 |
+
# Initialize local state if None
|
| 15 |
+
local_state = local_state or {}
|
| 16 |
+
# Update local state with the latest message
|
| 17 |
+
local_state["last_message"] = message
|
| 18 |
+
|
| 19 |
+
# Check for a command to update the global game state
|
| 20 |
+
if message.strip().lower() == "update global":
|
| 21 |
+
# Update the global state
|
| 22 |
+
global global_game_state
|
| 23 |
+
global_game_state = local_state.copy()
|
| 24 |
+
# Optionally persist the global state to a file
|
| 25 |
+
with open(GLOBAL_STATE_FILE, "w") as f:
|
| 26 |
+
json.dump(global_game_state, f)
|
| 27 |
+
response = "Global game updated!"
|
| 28 |
+
else:
|
| 29 |
+
response = f"Local game updated with: {message}"
|
| 30 |
+
return "", local_state, response
|
| 31 |
+
|
| 32 |
+
with gr.Blocks() as demo:
|
| 33 |
+
gr.Markdown("## Live Game & Chat Interface")
|
| 34 |
+
|
| 35 |
+
with gr.Row():
|
| 36 |
+
# Left Column: Iframe for the game
|
| 37 |
+
with gr.Column(scale=3):
|
| 38 |
+
gr.Markdown("### Game")
|
| 39 |
+
# The iframe points to your static Game
|
| 40 |
+
gr.HTML(
|
| 41 |
+
value="""
|
| 42 |
+
<iframe src="https://experiment-sandpack.vercel.app" style="width:100%; height:600px; border:none;"></iframe>
|
| 43 |
+
"""
|
| 44 |
+
)
|
| 45 |
+
# Right Column: Chat interface (smaller column)
|
| 46 |
+
with gr.Column(scale=1):
|
| 47 |
+
gr.Markdown("### Chat")
|
| 48 |
+
chat_output = gr.Textbox(label="Chat Output", interactive=False)
|
| 49 |
+
chat_input = gr.Textbox(
|
| 50 |
+
placeholder="Type your message here...",
|
| 51 |
+
label="Your Message"
|
| 52 |
+
)
|
| 53 |
+
local_state = gr.State({})
|
| 54 |
+
|
| 55 |
+
chat_input.submit(
|
| 56 |
+
process_chat,
|
| 57 |
+
inputs=[chat_input, local_state],
|
| 58 |
+
outputs=[chat_input, local_state, chat_output],
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.25.2
|
| 2 |
+
gradio
|