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=""" """ ) # 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()