Spaces:
Sleeping
Sleeping
| # streamlit_app.py | |
| import streamlit as st | |
| from transformers import pipeline | |
| # Initialize the model | |
| pipe = pipeline("text2text-generation", model="facebook/blenderbot-400M-distill") | |
| # Initialize session state for conversation history | |
| if 'conversation_history' not in st.session_state: | |
| st.session_state.conversation_history = "" | |
| def converse(user_message): | |
| # Update the conversation history | |
| st.session_state.conversation_history += f"User: {user_message}\n" | |
| result = pipe(st.session_state.conversation_history)[0]['generated_text'] | |
| st.session_state.conversation_history += f"Bot: {result}\n" | |
| return result | |
| # Streamlit app | |
| st.title("AI Chatbot") | |
| st.write("Chat with the AI bot!") | |
| # Text input from the user | |
| user_message = st.text_input("Enter Your message:") | |
| # Checkbox to toggle conversation history display | |
| show_history = st.checkbox("Show conversation history", value=True) | |
| # Button to trigger response generation | |
| if st.button("Send"): | |
| if user_message: | |
| # Get response from the chatbot | |
| bot_response = converse(user_message) | |
| if show_history: | |
| # Display conversation history if checkbox is checked | |
| st.write("### Conversation History") | |
| st.text(st.session_state.conversation_history) | |
| else: | |
| # Show a warning if no message is provided | |
| st.warning("Please enter a message before sending.") | |