Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -41,36 +41,55 @@
|
|
41 |
# else:
|
42 |
# st.write(f"**Bot:** {message['content']}")
|
43 |
import streamlit as st
|
44 |
-
from transformers import pipeline
|
45 |
|
46 |
-
st.title("🤖
|
47 |
|
48 |
@st.cache_resource
|
49 |
-
def
|
50 |
-
|
|
|
|
|
|
|
51 |
|
52 |
-
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("
|
63 |
# Add user message
|
64 |
st.session_state.conversation.append({"role": "user", "content": prompt})
|
65 |
|
66 |
-
#
|
67 |
-
|
68 |
-
result = chatbot(str(st.session_state.conversation))
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
st.session_state.conversation.append({"role": "assistant", "content": response})
|
75 |
-
|
76 |
st.rerun()
|
|
|
41 |
# else:
|
42 |
# st.write(f"**Bot:** {message['content']}")
|
43 |
import streamlit as st
|
44 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
45 |
|
46 |
+
st.title("🤖 Smart Chatbot")
|
47 |
|
48 |
@st.cache_resource
|
49 |
+
def load_model():
|
50 |
+
model_name = "facebook/blenderbot-400M-distill"
|
51 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
52 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
53 |
+
return pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
54 |
|
55 |
+
chatbot = load_model()
|
56 |
|
57 |
if "conversation" not in st.session_state:
|
58 |
st.session_state.conversation = []
|
59 |
|
60 |
+
# Display chat history
|
61 |
for msg in st.session_state.conversation:
|
62 |
with st.chat_message(msg["role"]):
|
63 |
st.markdown(msg["content"])
|
64 |
|
65 |
+
if prompt := st.chat_input("Ask me anything"):
|
66 |
# Add user message
|
67 |
st.session_state.conversation.append({"role": "user", "content": prompt})
|
68 |
|
69 |
+
# Format context
|
70 |
+
context = "\n".join([f"{msg['role']}: {msg['content']}" for msg in st.session_state.conversation[-3:]])
|
|
|
71 |
|
72 |
+
try:
|
73 |
+
with st.spinner("Thinking..."):
|
74 |
+
response = chatbot(
|
75 |
+
context,
|
76 |
+
max_length=200,
|
77 |
+
temperature=0.9,
|
78 |
+
top_k=60,
|
79 |
+
top_p=0.9,
|
80 |
+
num_beams=5,
|
81 |
+
no_repeat_ngram_size=3
|
82 |
+
)[0]['generated_text']
|
83 |
+
|
84 |
+
# Clean response
|
85 |
+
response = response.split("assistant:")[-1].strip()
|
86 |
+
|
87 |
+
# Ensure meaningful response
|
88 |
+
if not response or response.lower() in ["i don't know", "i'm not sure"]:
|
89 |
+
response = "I need to learn more about that. Could you clarify?"
|
90 |
+
|
91 |
+
except Exception as e:
|
92 |
+
response = "Let me check my knowledge sources and get back to you on that."
|
93 |
+
|
94 |
st.session_state.conversation.append({"role": "assistant", "content": response})
|
|
|
95 |
st.rerun()
|