Spaces:
Runtime error
Runtime error
File size: 3,330 Bytes
d6ede21 f14d463 d6ede21 f14d463 23dbcc2 a87ae0d 23dbcc2 a87ae0d 23dbcc2 d6ede21 22bf1ee a1d1228 d6ede21 23dbcc2 dbce2c4 23dbcc2 d6ede21 23dbcc2 f14d463 |
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 |
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain
from langchain.document_loaders.csv_loader import CSVLoader
from langchain.vectorstores import FAISS
import tempfile
from streamlit_chat import message
import streamlit as st
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
def get_sentiment(text):
sid = SentimentIntensityAnalyzer()
sentiment_scores = sid.polarity_scores(text)
compound_score = sentiment_scores['compound']
if compound_score >= 0.05:
return 'positive'
elif compound_score <= -0.05:
return 'negative'
else:
return 'neutral'
def add_sentiment_emoji(text, sentiment):
emoji_mapping = {
'positive': 'π',
'negative': 'π',
'neutral': 'π'
}
emoji = emoji_mapping.get(sentiment, '')
return f"{text} {emoji}"
import os
import sys
import pandas as pd
def conversational_chat(query):
result = chain({"question": query,
"chat_history": st.session_state['history']})
st.session_state['history'].append((query, result["answer"]))
return result["answer"]
user_api_key = st.sidebar.text_input(
label="#### Your OpenAI API key π",
placeholder="Paste your openAI API key, sk-",
type="password")
if user_api_key is not None and user_api_key.strip() != "":
os.environ["OPENAI_API_KEY"] =user_api_key
file_path='./personality_less.csv'
loader = CSVLoader(file_path=file_path, encoding="utf-8", csv_args={
'delimiter': ','})
data = loader.load()
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_documents(data, embeddings)
chain = ConversationalRetrievalChain.from_llm(
llm = ChatOpenAI(temperature=0.0,model_name='gpt-3.5-turbo'),
retriever=vectorstore.as_retriever())
if 'history' not in st.session_state:
st.session_state['history'] = []
if 'generated' not in st.session_state:
st.session_state['generated'] = ["Hello ! Ask me anything about " + " π€"]
if 'past' not in st.session_state:
st.session_state['past'] = ["Hey ! π"]
#container for the chat history
response_container = st.container()
#container for the user's text input
container = st.container()
with container:
with st.form(key='my_form', clear_on_submit=True):
user_input = st.text_input("Query:", placeholder="Talk about your csv data here (:", key='input')
submit_button = st.form_submit_button(label='Send')
if submit_button and user_input:
output = conversational_chat(user_input)
st.session_state['past'].append(user_input)
st.session_state['generated'].append(output)
if st.session_state['generated']:
with response_container:
for i in range(len(st.session_state['generated'])):
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")
else:
st.text("Please enter your OpenAI API key above.") |