Spaces:
Runtime error
Runtime error
Commit
•
1145324
1
Parent(s):
f4f3b62
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from streamlit_chat import message as st_message
|
3 |
+
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
4 |
+
|
5 |
+
st.title("Streamlit Blenderbot Chatbot")
|
6 |
+
|
7 |
+
if "history" not in st.session_state:
|
8 |
+
st.session_state.history = []
|
9 |
+
|
10 |
+
def get_models():
|
11 |
+
tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
|
12 |
+
model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill")
|
13 |
+
return tokenizer, model
|
14 |
+
|
15 |
+
def generate_answer():
|
16 |
+
tokenizer, model = get_models()
|
17 |
+
user_message = st.session_state.input_text
|
18 |
+
inputs = tokenizer(st.session_state.input_text, return_tensors="pt")
|
19 |
+
result = model.generate(**inputs)
|
20 |
+
message_bot = tokenizer.decode(result[0], skip_special_tokens=True) # .replace("<s>", "").replace("</s>", "")
|
21 |
+
st.session_state.history.append({"message": user_message, "is_user": True})
|
22 |
+
st.session_state.history.append({"message": message_bot, "is_user": False})
|
23 |
+
|
24 |
+
st.text_input("Response", key="input_text", on_change=generate_answer)
|
25 |
+
|
26 |
+
for chat in st.session_state.history:
|
27 |
+
st_message(**chat)
|