|
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 |
|
|
|
|
|
|
|
|
|
chat = ChatOpenAI(model="gpt-3.5-turbo", temperature=1) |
|
|
|
|
|
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})" |
|
|
|
|
|
st.session_state.messages.append({"role": "user", "content": user_input}) |
|
|
|
|
|
st.session_state.messages.append({"role": "assistant", "content": assistant_input}) |
|
|
|
|
|
|
|
|
|
|
|
if sentiment == "very negative": |
|
rain( |
|
emoji="β", |
|
font_size=20, |
|
falling_speed=3, |
|
animation_length="infinite", |
|
) |
|
elif sentiment == "negative": |
|
rain( |
|
emoji="β", |
|
font_size=20, |
|
falling_speed=3, |
|
animation_length="infinite", |
|
) |
|
elif sentiment == "neutral": |
|
rain( |
|
emoji="π", |
|
font_size=20, |
|
falling_speed=3, |
|
animation_length="infinite", |
|
) |
|
elif sentiment == "positive": |
|
rain( |
|
emoji="π’", |
|
font_size=20, |
|
falling_speed=3, |
|
animation_length="infinite", |
|
) |
|
elif sentiment == "very positive": |
|
rain( |
|
emoji="π’", |
|
font_size=20, |
|
falling_speed=3, |
|
animation_length="infinite", |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message["role"]): |
|
st.markdown(message["content"]) |