Spaces:
Sleeping
Sleeping
new file
Browse files
app.py
CHANGED
|
@@ -5,112 +5,36 @@ from tools import sentiment_analysis_util
|
|
| 5 |
import numpy as np
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import os
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
st.set_page_config(page_title="LangChain Agent", layout="wide")
|
| 10 |
-
load_dotenv()
|
| 11 |
-
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
if "chat_history" not in st.session_state:
|
| 21 |
-
st.session_state["messages"] = [{"role":"system", "content":"""
|
| 22 |
-
You are a sentiment analysis expert. Answer all questions related to the topic. Say I don't know if you don't know.
|
| 23 |
-
"""}]
|
| 24 |
-
|
| 25 |
-
st.image('el_pic.png')
|
| 26 |
-
|
| 27 |
-
sideb=st.sidebar
|
| 28 |
-
with st.sidebar:
|
| 29 |
-
prompt=st.text_input("Enter topic for sentiment analysis: ")
|
| 30 |
-
|
| 31 |
-
check1=sideb.button(f"analyze {prompt}")
|
| 32 |
-
|
| 33 |
-
if check1:
|
| 34 |
-
# Add user message to chat history
|
| 35 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 36 |
-
|
| 37 |
-
with st.chat_message("user"):
|
| 38 |
-
st.markdown(prompt)
|
| 39 |
-
|
| 40 |
-
# ========================== Sentiment analysis
|
| 41 |
-
#Perform sentiment analysis on the cryptocurrency news & predict dominant sentiment along with plotting the sentiment breakdown chart
|
| 42 |
-
# Downloading from reddit
|
| 43 |
-
|
| 44 |
-
# Downloading from alpaca
|
| 45 |
-
if len(prompt.split(' '))<2:
|
| 46 |
-
print('here')
|
| 47 |
-
st.write('I am analyzing Google News ...')
|
| 48 |
-
news_articles = sentiment_analysis_util.fetch_news(str(prompt))
|
| 49 |
-
st.write('Now, I am analyzing Reddit ...')
|
| 50 |
-
reddit_news_articles=sentiment_analysis_util.fetch_reddit_news(prompt)
|
| 51 |
-
analysis_results = []
|
| 52 |
-
|
| 53 |
-
#Perform sentiment analysis for each product review
|
| 54 |
-
if len(prompt.split(' '))<2:
|
| 55 |
-
print('here')
|
| 56 |
-
for article in news_articles:
|
| 57 |
-
if prompt.lower()[0:6] in article['News_Article'].lower():
|
| 58 |
-
sentiment_analysis_result = sentiment_analysis_util.analyze_sentiment(article['News_Article'])
|
| 59 |
-
|
| 60 |
-
# Display sentiment analysis results
|
| 61 |
-
#print(f'News Article: {sentiment_analysis_result["News_Article"]} : Sentiment: {sentiment_analysis_result["Sentiment"]}', '\n')
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
analysis_results.append(result)
|
| 71 |
-
|
| 72 |
-
articles_url=[]
|
| 73 |
-
for article in reddit_news_articles:
|
| 74 |
-
if prompt.lower()[0:6] in article.lower():
|
| 75 |
-
sentiment_analysis_result_reddit = sentiment_analysis_util.analyze_sentiment(article)
|
| 76 |
-
|
| 77 |
-
# Display sentiment analysis results
|
| 78 |
-
#print(f'News Article: {sentiment_analysis_result_reddit["News_Article"]} : Sentiment: {sentiment_analysis_result_reddit["Sentiment"]}', '\n')
|
| 79 |
-
|
| 80 |
-
result = {
|
| 81 |
-
'News_Article': sentiment_analysis_result_reddit["News_Article"],
|
| 82 |
-
'Index':np.round(sentiment_analysis_result_reddit["Sentiment"][0]['score'],2)
|
| 83 |
-
}
|
| 84 |
-
analysis_results.append(np.append(result,np.append(article.split('URL:')[-1:], ((article.split('Date: ')[-1:])[0][0:10]))))
|
| 85 |
-
#pd.DataFrame(analysis_results).to_csv('analysis_results.csv')
|
| 86 |
-
|
| 87 |
-
#Generate summarized message rationalize dominant sentiment
|
| 88 |
-
summary = sentiment_analysis_util.generate_summary_of_sentiment(analysis_results) #, dominant_sentiment)
|
| 89 |
-
st.chat_message("assistant").write((summary))
|
| 90 |
-
st.session_state.messages.append({"role": "assistant", "content": summary})
|
| 91 |
-
#answers=np.append(res["messages"][-1].content,summary)
|
| 92 |
-
|
| 93 |
-
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 94 |
-
|
| 95 |
-
if "openai_model" not in st.session_state:
|
| 96 |
-
st.session_state["openai_model"] = "gpt-3.5-turbo"
|
| 97 |
-
|
| 98 |
-
if prompt := st.chat_input("Any other questions? "):
|
| 99 |
-
# Add user message to chat history
|
| 100 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 101 |
-
# Display user message in chat message container
|
| 102 |
-
with st.chat_message("user"):
|
| 103 |
-
st.markdown(prompt)
|
| 104 |
-
# Display assistant response in chat message container
|
| 105 |
-
with st.chat_message("assistant"):
|
| 106 |
-
stream = client.chat.completions.create(
|
| 107 |
-
model=st.session_state["openai_model"],
|
| 108 |
-
messages=[
|
| 109 |
-
{"role": m["role"], "content": m["content"]}
|
| 110 |
-
for m in st.session_state.messages
|
| 111 |
-
],
|
| 112 |
-
stream=True,
|
| 113 |
-
)
|
| 114 |
-
response = st.write_stream(stream)
|
| 115 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import os
|
| 8 |
+
st.title("💬 Chatbot")
|
| 9 |
+
st.caption("")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
openai_api_key = os.environ["OPENAI_API_KEY"]
|
| 13 |
+
# Initialize session state for storing messages if it doesn't already exist
|
| 14 |
+
if "messages" not in st.session_state:
|
| 15 |
+
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
|
| 16 |
|
| 17 |
+
# Display all previous messages
|
| 18 |
+
for msg in st.session_state.messages:
|
| 19 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
| 20 |
|
| 21 |
+
# Input for new prompts
|
| 22 |
+
prompt = st.chat_input("Enter your question:")
|
| 23 |
+
if prompt:
|
| 24 |
+
if not openai_api_key:
|
| 25 |
+
st.error("No OpenAI API key found. Please set the OPENAI_API_KEY environment variable.")
|
| 26 |
+
st.stop()
|
| 27 |
|
| 28 |
+
# Append the new user message to session state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 30 |
+
st.chat_message("user").write(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Use a spinner to indicate that the model is generating a response
|
| 33 |
+
with st.spinner('Thinking...'):
|
| 34 |
+
client = OpenAI(api_key=openai_api_key)
|
| 35 |
+
response = client.chat.completions.create(model="gpt-3.5-turbo", messages=st.session_state.messages)
|
| 36 |
+
msg = response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Append and display the assistant's response
|
| 39 |
+
st.session_state.messages.append({"role": "assistant", "content": msg})
|
| 40 |
+
st.chat_message("assistant").write(msg)
|