Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# streamlit_app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Initialize the model
|
| 6 |
+
pipe = pipeline("text2text-generation", model="facebook/blenderbot-400M-distill")
|
| 7 |
+
|
| 8 |
+
# Initialize session state for conversation history
|
| 9 |
+
if 'conversation_history' not in st.session_state:
|
| 10 |
+
st.session_state.conversation_history = ""
|
| 11 |
+
|
| 12 |
+
def converse(user_message):
|
| 13 |
+
# Update the conversation history
|
| 14 |
+
st.session_state.conversation_history += f"User: {user_message}\n"
|
| 15 |
+
result = pipe(st.session_state.conversation_history)[0]['generated_text']
|
| 16 |
+
st.session_state.conversation_history += f"Bot: {result}\n"
|
| 17 |
+
return result
|
| 18 |
+
|
| 19 |
+
# Streamlit app
|
| 20 |
+
st.title("AI Chatbot")
|
| 21 |
+
st.write("Chat with the AI bot!")
|
| 22 |
+
|
| 23 |
+
# Text input from the user
|
| 24 |
+
user_message = st.text_input("Enter Your message:")
|
| 25 |
+
|
| 26 |
+
# Checkbox to toggle conversation history display
|
| 27 |
+
show_history = st.checkbox("Show conversation history", value=True)
|
| 28 |
+
|
| 29 |
+
# Button to trigger response generation
|
| 30 |
+
if st.button("Send"):
|
| 31 |
+
if user_message:
|
| 32 |
+
# Get response from the chatbot
|
| 33 |
+
bot_response = converse(user_message)
|
| 34 |
+
|
| 35 |
+
if show_history:
|
| 36 |
+
# Display conversation history if checkbox is checked
|
| 37 |
+
st.write("### Conversation History")
|
| 38 |
+
st.text(st.session_state.conversation_history)
|
| 39 |
+
else:
|
| 40 |
+
# Show a warning if no message is provided
|
| 41 |
+
st.warning("Please enter a message before sending.")
|