import os import streamlit as st from langchain.chat_models import ChatOpenAI from langchain.schema import (AIMessage,HumanMessage,SystemMessage) from transformers import pipeline from streamlit_extras.let_it_rain import rain def get_response(question): st.session_state.sessionMessages.append(HumanMessage(content=question)) assistant_answer = chat(st.session_state.sessionMessages ) st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content)) return assistant_answer.content def get_sentiment(user_input, nlp): result = nlp(user_input) sentiment = "" if (result[0]['label'] == '1 star'): sentiment = 'very negative' elif (result[0]['label'] == '2 stars'): sentiment = 'negative' elif (result[0]['label'] == '3 stars'): sentiment = 'neutral' elif (result[0]['label'] == '4 stars'): sentiment = 'positive' else: sentiment = 'very positive' prob = result[0]['score'] return sentiment, prob # open ai chat = ChatOpenAI(model="gpt-3.5-turbo", temperature=1) # hugging-face model nlp = pipeline(task='sentiment-analysis', model='nlptown/bert-base-multilingual-uncased-sentiment') st.set_page_config(page_title="Home Assistant", page_icon=":robot:") st.header("Knock knock, It's meeee the JOKER!") if "sessionMessages" not in st.session_state: st.session_state.sessionMessages = [SystemMessage(content="You have an evil personality like Joker from Batman")] if "messages" not in st.session_state: st.session_state.messages = [] if user_input := st.chat_input("Say something"): assistant_input = get_response(user_input) sentiment, prob = get_sentiment(user_input, nlp) sentiment_analysis = f" (sentiment:{sentiment},score:{prob})" # add user input to history st.session_state.messages.append({"role": "user", "content": user_input}) # add assistant input to history st.session_state.messages.append({"role": "assistant", "content": assistant_input}) # sentiment analysis if sentiment == "very negative": rain( emoji="🔴", font_size=20, # the size of emoji falling_speed=3, # speed of raining animation_length="infinite", # for how much time the animation will happen ) elif sentiment == "negative": rain( emoji="🔴", font_size=20, # the size of emoji falling_speed=3, # speed of raining animation_length="infinite", # for how much time the animation will happen ) elif sentiment == "neutral": rain( emoji="😐", font_size=20, # the size of emoji falling_speed=3, # speed of raining animation_length="infinite", # for how much time the animation will happen ) elif sentiment == "positive": rain( emoji="🟢", font_size=20, # the size of emoji falling_speed=3, # speed of raining animation_length="infinite", # for how much time the animation will happen ) elif sentiment == "very positive": rain( emoji="🟢", font_size=20, # the size of emoji falling_speed=3, # speed of raining animation_length="infinite", # for how much time the animation will happen ) # display chat history for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"])