File size: 5,076 Bytes
0bf43ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
from openai import OpenAI
import streamlit as st
import utils as u
from langchain_openai import ChatOpenAI
from tools import sentiment_analysis_util
import functools
from typing import Annotated
import operator
from typing import Sequence, TypedDict
import numpy as np
import pandas as pd
from dotenv import load_dotenv
import os
import functools
from typing import Annotated
import operator

st.set_page_config(page_title="LangChain Agent", layout="wide")
load_dotenv()
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]

llm = ChatOpenAI(model="gpt-3.5-turbo")

from langchain_core.runnables import RunnableConfig

st.title("💬 ExpressMood")

@st.cache_resource
def initialize_session_state():
    if "chat_history" not in st.session_state:
        st.session_state["messages"] = [{"role":"system", "content":"""
You are a sentiment analysis expert. Answer all questions related to cryptocurrency investment reccommendations. Say I don't know if you don't know.
"""}]

initialize_session_state()

sideb=st.sidebar
with st.sidebar:
    prompt=st.text_input("Enter topic for sentiment analysis: ")

check1=sideb.button(f"analyze {prompt}")

if check1:
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})
    # Display user message in chat message container
    with st.chat_message("user"):
        st.markdown(prompt)

    # ========================== Sentiment analysis
    #Perform sentiment analysis on the cryptocurrency news & predict dominant sentiment along with plotting the sentiment breakdown chart
    # Downloading from reddit

    # Downloading from alpaca
    if len(prompt.split(' '))<2:
        print('here')
        st.write('I am analyzing Google News ...')
        news_articles = sentiment_analysis_util.fetch_news(str(prompt))
    st.write('Now, I am analyzing Reddit ...')
    reddit_news_articles=sentiment_analysis_util.fetch_reddit_news(prompt)
    analysis_results = []

    #Perform sentiment analysis for each product review
    if len(prompt.split(' '))<2:
        print('here')
        for article in news_articles:
            if prompt.lower()[0:6] in article['News_Article'].lower():
                sentiment_analysis_result = sentiment_analysis_util.analyze_sentiment(article['News_Article'])

                # Display sentiment analysis results
                #print(f'News Article: {sentiment_analysis_result["News_Article"]} : Sentiment: {sentiment_analysis_result["Sentiment"]}', '\n')

                result = {
                            'News_Article': sentiment_analysis_result["News_Article"],
                            'Sentiment': sentiment_analysis_result["Sentiment"][0]['label'],
                            'Index': sentiment_analysis_result["Sentiment"][0]['score'],
                            'URL': article['URL']
                        }
                
                analysis_results.append(result)

    articles_url=[]
    for article in reddit_news_articles:
        if prompt.lower()[0:6] in article.lower():
            sentiment_analysis_result_reddit = sentiment_analysis_util.analyze_sentiment(article)

            # Display sentiment analysis results
            #print(f'News Article: {sentiment_analysis_result_reddit["News_Article"]} : Sentiment: {sentiment_analysis_result_reddit["Sentiment"]}', '\n')

            result = {
                        'News_Article': sentiment_analysis_result_reddit["News_Article"],
                        'Index':np.round(sentiment_analysis_result_reddit["Sentiment"][0]['score'],2)
                    }
            analysis_results.append(np.append(result,np.append(article.split('URL:')[-1:], ((article.split('Date: ')[-1:])[0][0:10]))))
    #pd.DataFrame(analysis_results).to_csv('analysis_results.csv')

    #Generate summarized message rationalize dominant sentiment
    summary = sentiment_analysis_util.generate_summary_of_sentiment(analysis_results) #, dominant_sentiment)
    st.chat_message("assistant").write((summary))
    st.session_state.messages.append({"role": "assistant", "content": summary})
    #answers=np.append(res["messages"][-1].content,summary)

client = OpenAI(api_key=OPENAI_API_KEY)

if "openai_model" not in st.session_state:
    st.session_state["openai_model"] = "gpt-3.5-turbo"

if prompt := st.chat_input("Any other questions? "):
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})
    # Display user message in chat message container
    with st.chat_message("user"):
        st.markdown(prompt)
    # Display assistant response in chat message container
    with st.chat_message("assistant"):
        stream = client.chat.completions.create(
            model=st.session_state["openai_model"],
            messages=[
                {"role": m["role"], "content": m["content"]}
                for m in st.session_state.messages
            ],
            stream=True,
        )
        response = st.write_stream(stream)
    st.session_state.messages.append({"role": "assistant", "content": response})