williambr commited on
Commit
ad9082d
1 Parent(s): d58474b

Create new file

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ #from streamlit_chat import message as st_message
3
+ from streamlit_chat import message as st_message
4
+ from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
5
+
6
+ st.title("Chatbot Blenderbot Streamlit")
7
+
8
+ if "history" not in st.session_state:
9
+ st.session_state.history = []
10
+
11
+ def get_models():
12
+ tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
13
+ model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill")
14
+ return tokenizer, model
15
+
16
+ def generate_answer():
17
+ tokenizer, model = get_models()
18
+ user_message = st.session_state.input_text
19
+ inputs = tokenizer(st.session_state.input_text, return_tensors="pt")
20
+ result = model.generate(**inputs)
21
+ message_bot = tokenizer.decode(result[0], skip_special_tokens=True) # .replace("<s>", "").replace("</s>", "")
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("Response", key="input_text", on_change=generate_answer)
26
+
27
+ for chat in st.session_state.history:
28
+ st_message(**chat)