Spaces:
Sleeping
Sleeping
File size: 3,526 Bytes
fe5911b a35c180 fe5911b a4d1b31 e48dfae a4d1b31 fe5911b a4d1b31 fe5911b d75d406 fe5911b a4d1b31 fe5911b 0359207 fe5911b 66e8623 fe5911b c3f9b5c fe5911b c3f9b5c fe5911b af71ddd fe5911b a4d1b31 c3f9b5c a4d1b31 9c315e1 a4d1b31 9c315e1 a4d1b31 9c315e1 a4d1b31 9c315e1 a4d1b31 fe5911b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
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="HomeX 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"]) |