Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from streamlit_chat import message as st_message
|
3 |
+
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
4 |
+
|
5 |
+
def get_models():
|
6 |
+
tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
|
7 |
+
model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill")
|
8 |
+
return tokenizer, model
|
9 |
+
|
10 |
+
if "history" not in st.session_state:
|
11 |
+
st.session_state.history = []
|
12 |
+
|
13 |
+
st.title("Chatbot Four")
|
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 |
+
|
22 |
+
st.session_state.history.append({"message": user_message, "is_user": True})
|
23 |
+
st.session_state.history.append({"message": message_bot, "is_user": False})
|
24 |
+
|
25 |
+
st.text_input("Your text message", key="input_text", on_change=generate_answer)
|
26 |
+
|
27 |
+
for chat in st.session_state.history:
|
28 |
+
st_message(**chat) # unpacking
|