All_in_one / pages /chat.py
Pippoz's picture
Merge branch 'main' of https://huggingface.co/spaces/Pippoz/All_in_one into main
025d45a
import streamlit as st
from streamlit_chat import message as st_message
from transformers import BlenderbotTokenizer
from transformers import BlenderbotForConditionalGeneration
def app():
@st.experimental_singleton(show_spinner=False, suppress_st_warning=True)
def get_models():
# it may be necessary for other frameworks to cache the model
# seems pytorch keeps an internal state of the conversation
model_name = "facebook/blenderbot-400M-distill"
tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
model = BlenderbotForConditionalGeneration.from_pretrained(model_name)
return tokenizer, model
if "history" not in st.session_state:
st.session_state.history = []
st.title("IoT Chat Robot")
st.write('Just chat with a friendly and smart AI developed by Meta')
st.markdown('## ')
def generate_answer():
tokenizer, model = get_models()
user_message = st.session_state.input_text
inputs = tokenizer(st.session_state.input_text, return_tensors="pt")
result = model.generate(**inputs)
message_bot = tokenizer.decode(
result[0], skip_special_tokens=True
) # .replace("<s>", "").replace("</s>", "")
st.session_state.history.append({"message": user_message, "is_user": True})
st.session_state.history.append({"message": message_bot, "is_user": False})
st.text_input("Enter your prompt...", key="input_text", on_change=generate_answer)
for chat in st.session_state.history:
st_message(**chat) # unpacking