Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,56 +41,36 @@
|
|
| 41 |
# else:
|
| 42 |
# st.write(f"**Bot:** {message['content']}")
|
| 43 |
import streamlit as st
|
| 44 |
-
from transformers import
|
| 45 |
|
| 46 |
-
st.title("🤖
|
| 47 |
|
| 48 |
-
# Initialize model and tokenizer
|
| 49 |
@st.cache_resource
|
| 50 |
-
def
|
| 51 |
-
|
| 52 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 53 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 54 |
-
return model, tokenizer
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
st.session_state.history = []
|
| 61 |
|
| 62 |
-
# Display
|
| 63 |
-
for
|
| 64 |
-
with st.chat_message(
|
| 65 |
-
st.markdown(
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
st.session_state.history.append({"role": "user", "content": prompt})
|
| 71 |
-
|
| 72 |
-
# Prepare context for the model
|
| 73 |
-
input_ids = tokenizer.encode(
|
| 74 |
-
"\n".join([f"{msg['role']}: {msg['content']}" for msg in st.session_state.history[-5:]]) + "\nassistant:",
|
| 75 |
-
return_tensors="pt"
|
| 76 |
-
)
|
| 77 |
|
| 78 |
# Generate response
|
| 79 |
with st.spinner("Thinking..."):
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
top_p=0.95,
|
| 88 |
-
temperature=0.7
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
response = tokenizer.decode(output[0], skip_special_tokens=True).split("assistant:")[-1].strip()
|
| 92 |
-
|
| 93 |
-
# Add assistant response to history
|
| 94 |
-
st.session_state.history.append({"role": "assistant", "content": response})
|
| 95 |
|
| 96 |
st.rerun()
|
|
|
|
| 41 |
# else:
|
| 42 |
# st.write(f"**Bot:** {message['content']}")
|
| 43 |
import streamlit as st
|
| 44 |
+
from transformers import pipeline
|
| 45 |
|
| 46 |
+
st.title("🤖 Conversational Chatbot")
|
| 47 |
|
|
|
|
| 48 |
@st.cache_resource
|
| 49 |
+
def load_chatbot():
|
| 50 |
+
return pipeline("conversational", model="facebook/blenderbot-400M-distill")
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
chatbot = load_chatbot()
|
| 53 |
|
| 54 |
+
if "conversation" not in st.session_state:
|
| 55 |
+
st.session_state.conversation = []
|
|
|
|
| 56 |
|
| 57 |
+
# Display history
|
| 58 |
+
for msg in st.session_state.conversation:
|
| 59 |
+
with st.chat_message(msg["role"]):
|
| 60 |
+
st.markdown(msg["content"])
|
| 61 |
|
| 62 |
+
if prompt := st.chat_input("Say something"):
|
| 63 |
+
# Add user message
|
| 64 |
+
st.session_state.conversation.append({"role": "user", "content": prompt})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Generate response
|
| 67 |
with st.spinner("Thinking..."):
|
| 68 |
+
result = chatbot(str(st.session_state.conversation))
|
| 69 |
+
|
| 70 |
+
# Extract bot response
|
| 71 |
+
response = result.generated_responses[-1]
|
| 72 |
+
|
| 73 |
+
# Add to conversation
|
| 74 |
+
st.session_state.conversation.append({"role": "assistant", "content": response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
st.rerun()
|