Please make the clearing chat work and make the chat list so that user can see previous…
- User
Please make the clearing chat work and make the chat list so that user can see previous chattings. import gradio as gr from gradio_client import Client
Initialize the client by pointing to the Gradio Space
client = Client("yuntian-deng/ChatGPT4")
Persistent conversation history
chatbot_history = [] # Stores chat history as a list of tuples (user, assistant)
def clear_chat(): """ Clears the global chatbot history and resets the Chatbot UI. """ global chatbot_history chatbot_history = [] # Reset global chatbot history return [] # Clear the Chatbot UI
def chat_with_model(user_input, existing_history): """ Interact with the chatbot while maintaining conversation context.
codeArgs: user_input (str): User's most recent message. existing_history (list of tuples): [(user1, bot1), (user2, bot2), ...], conversation context. Returns: updated_history (list of tuples): Chat history after receiving the assistant's response. """ global chatbot_history # Use global chatbot history # Update the global chatbot history chatbot_history = existing_history if user_input.strip(): # Check if the input is not empty # Call the Gradio backend Space result = client.predict( user_input, # User message 1.0, # Top-p value for creativity 0.7, # Temperature for randomness len(chatbot_history), # Chat counter chatbot_history, # Conversation history context api_name="/predict" # Gradio Space API endpoint ) # Extract updated history updated_history = result[0] # Gradio Space response (chat history) chatbot_history = updated_history # Update global chatbot history return chatbot_history else: return chatbot_history # If no user input, just return the existing history - Assistant