Make a streamlit app that uses the following logic to chat with chatbot from…
- User
Make a streamlit app that uses the following logic to chat with chatbot 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 full chat history
def chat_with_model(inputs, top_p=1.0, temperature=1.0, chat_counter=0): """ Interact with the Gradio chatbot while maintaining memory.
Args: inputs (str): User input message to the chatbot. top_p (float): Top-p (nucleus sampling) parameter for response creativity (default = 1.0). temperature (float): Temperature for randomness in response (default = 1.0). chat_counter (int): The number of interactions so far (used internally). Returns: tuple: (response_text, new_chat_counter, updated_chatbot_history) - response_text (str): The assistant's most recent reply. - new_chat_counter (int): Updated chat count. - updated_chatbot_history (list): Updated conversation history. """ global chatbot_history # Modify the global chatbot history within the function # Call Gradio API endpoint /predict with history result = client.predict( inputs, # User's input top_p, # Sampling nucleus parameter temperature, # Model sampling temperature chat_counter, # Current step count of chat chatbot_history, # Previous conversation history api_name="/predict" # Gradio endpoint's function name ) # Parse Gradio-provided outputs updated_history = result[0] # Chat history list of tuples [(user, bot), ...] new_chat_counter = result[1] # Updated chat counter status_code_from_server = result[2] # Status from OpenAI's server (optional) response_textbox_value = result[3] # String output of the assistant's reply # Update the persistent history chatbot_history = updated_history # Extract the latest assistant response from the updated chat history if updated_history: response_text = updated_history[-1][1] # Final assistant's message else: response_text = "No response received." return response_text, new_chat_counter, updated_historyExample interactive chat loop
if name == "main": print("Starting a chat with GPT-4 on Hugging Face Spaces! Type 'exit' to end the chat.")
chat_counter = 0 # Start at 0 interactions while True: user_input = input("You: ") if user_input.lower() in ["exit", "quit"]: print("Ending chat. Goodbye!") break # Call the chat function assistant_reply, chat_counter, chatbot_history = chat_with_model( inputs=user_input, top_p=1.0, # Default settings; adjust for creativity! temperature=0.7, # Slightly less randomness chat_counter=chat_counter ) # Print the assistant's response print(f"Assistant: {assistant_reply}") - Assistant
- User
you don't have to show the history, but enable to chatbot history to work
- Assistant
- User
I want you to create a gradio app that has nice chat bot ui
- Assistant
- User
clearing the chat isn't working correctlt
- Assistant
- User
history isn't clearing correctly because you have added it to the top of the code. Fix this problem
- Assistant
- User
it is clearing in message, but it isn't clearing in ui.
- Assistant
- User
- Assistant
- User
Just clear chatbot
- Assistant