Spaces:
Running
Running
File size: 2,731 Bytes
ab8e90d 1b6fe24 ab8e90d 1b6fe24 ab8e90d 1b6fe24 ab8e90d 1b6fe24 ab8e90d 1b6fe24 ab8e90d |
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import gradio as gr
import json
import os
from smol import ChatAgent
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
def process_chat(message, chat_history):
if chat_history is None:
chat_history = []
chat_history.append(("User", message))
response = ChatAgent(message)
chat_history.append(("AI", response))
return "", chat_history
def chat_function(user_prompt, history):
# Initialize history if empty
if history is None:
history = []
# Append the user's message as a dictionary
history.append({"role": "user", "content": user_prompt})
# Process the prompt using your ChatAgent to get the AI response
ai_response = ChatAgent(user_prompt)
print(f"AI RESPONSE: {ai_response}")
print("HISTORY: ", history)
# Append the AI's response as a dictionary
history.append({"role": "assistant", "content": ai_response})
# Clear the input and return the updated history
return "", history
with gr.Blocks() as demo:
gr.Markdown("## Live Game & Chat Interface")
with gr.Row():
# Left Column: Iframe for the game
with gr.Column(scale=3):
gr.Markdown("### Game")
# The iframe points to your static Game
gr.HTML(
value="""
<iframe src="https://experiment-sandpack.vercel.app" style="width:100%; height:600px; border:none;"></iframe>
"""
)
# Right Column: Chat interface (smaller column)
with gr.Column(scale=1):
gr.Markdown("### Chat")
chatbot = gr.Chatbot(type="messages",label="Conversation")
# Textbox to receive user prompt
txt_input = gr.Textbox(placeholder="Type your prompt here...", label="Your Message")
# State to hold the conversation history
state = gr.State([])
# When the user submits a message, update the chat
txt_input.submit(chat_function, inputs=[txt_input, state], outputs=[txt_input, chatbot])
# chat_output = gr.Chatbot(label="Chat Output", type="messages", interactive=False)
# chat_input = gr.Textbox(
# placeholder="Type your message here...",
# label="Your Message"
# )
# chat_history = gr.State([])
# chat_input.submit(
# process_chat,
# inputs=[chat_input, chat_history],
# outputs=[chat_input, chat_output],
# )
demo.launch()
|