import gradio as gr import time # For a more "natural" typing simulation def echo_chat_function(message, history): """ Responds to a user's message in a chat context. 'message' is the latest user input. 'history' is a list of previous [user_message, bot_message] pairs. """ if not message: # In a chat, if the user sends an empty message, # you might want to prompt them or do nothing. return "Agent: Hmm, did you mean to send something?" # This is where you would put your agent's logic (e.g., LLM calls). # For now, we'll just echo with a prefix. response_prefix = "Agent Echo: " full_response = f"{response_prefix}{message}" # Simulate the agent "typing" for a better chat feel # gr.ChatInterface handles streaming if you yield partial responses partial_response = "" for char in full_response: partial_response += char time.sleep(0.03) # Small delay for each character yield partial_response # Yield intermediate parts of the response # Create the Gradio Chat Interface chat_iface = gr.ChatInterface( fn=echo_chat_function, # The Python function to call for responses chatbot=gr.Chatbot( height=400, # The deprecation warning you saw earlier is for the underlying data format. # When using ChatInterface, it generally handles this for you. # If you were manually populating a Chatbot, you'd use: # value=[{"role": "user", "content": "Hi"}], type="messages" # But ChatInterface handles this internally. ), textbox=gr.Textbox(placeholder="Type your message here and press Enter...", container=False, scale=7), # Input textbox configuration title="Simple Echo Chat Agent ", description=""" This is a simple chat interface for the Agents-MCP-Hackathon. Type a message, and the 'agent' will echo it back. This version includes specific button configurations suitable for newer Gradio versions. """, examples=[["Hello!"], ["How are you today?"]], # Example prompts cache_examples=False, # Don't cache examples for dynamic functions usually submit_btn="Send" # Text for the submit button (optional, Enter also works) ) # Launch the app if __name__ == "__main__": chat_iface.launch()