File size: 8,585 Bytes
ad85bde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# import streamlit as st
# import pandas as pd
# import numpy as np
# import matplotlib.pyplot as plt

# # Placeholder for loading models
# def load_models():
#     # In a real scenario, you would load your pre-trained models here.
#     return {"model_placeholder": "Loaded Model"}

# # Placeholder function to classify news as ESG-related
# def classify_esg(text, models, api_key):
#     # Simulate ESG classification logic
#     # This is where you would use your model to classify the text.
#     return np.random.choice(["Yes", "No"])

# # Placeholder function to determine sentiment
# def determine_sentiment(text, models, api_key):
#     # Simulate sentiment analysis logic
#     # This is where you would use your model to determine the sentiment.
#     return np.random.choice(["Positive", "Neutral", "Negative"])

# # Placeholder function to run Alphalens analysis
# def run_alphalens_analysis(data, models, api_key):
#     # Simulate some metrics
#     metrics = {"alpha": np.random.rand(), "beta": np.random.rand()}
    
#     # Generate a simple plot
#     fig, ax = plt.subplots()
#     ax.plot([1, 2, 3], [1, 2, 3], 'r')  # Example plot
#     ax.set_title('Example Plot')
    
#     return metrics, [fig]

# # Streamlit app code
# models = load_models()

# st.title('NLP Project: ESG News Analysis and Financial Impact')

# api_key = st.sidebar.text_input("OpenAI API Key", type="password")

# uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
# if uploaded_file is not None:
#     data = pd.read_csv(uploaded_file)
#     st.write("Uploaded News Data:")
#     st.dataframe(data)

#     if st.button('Classify News as ESG-related'):
#         data['ESG'] = data['news'].apply(lambda x: classify_esg(x, models, api_key))
#         st.write("News with ESG Classification:")
#         st.dataframe(data)

#     if st.button('Determine Sentiment'):
#         data['Sentiment'] = data['news'].apply(lambda x: determine_sentiment(x, models, api_key))
#         st.write("News with Sentiment Analysis:")
#         st.dataframe(data)

#     if st.button('Run Alphalens Analysis'):
#         metrics, plots = run_alphalens_analysis(data, models, api_key)
#         st.write("Alphalens Analysis Metrics:")
#         st.json(metrics)
        
#         st.write("Alphalens Analysis Plots:")
#         for plot in plots:
#             st.pyplot(plot)


import streamlit as st
import pandas as pd
import numpy as np
import os
import openai
import json
from getpass import getpass
from tqdm import tqdm
import matplotlib.pyplot as plt

def get_sentiment_gpt(company, SASB, news, max_retries=5, model = 'gpt-4-turbo-2024-04-09'):
    system_prompt = """
    As a specialist in ESG analytics, 
    You possess a deep understanding of evaluating environmental, social, and governance factors in the context of corporate news. 
    Your expertise lies in discerning the underlying sentiment of news segments that pertain to a company's ESG practices, 
    determining whether the coverage reflects a positive, negative, or neutral stance.
    """

    allowed_sentiments = ['Negative', 'Positive', 'Neutral']
    attempt = 0

    while attempt < max_retries:
        main_prompt = f"""
        Classify the sentiment (Only options: Positive, Negative, Neutral) of the following news: {news} |
        The sentiment classification should be about the sections of the news talking about the company {company}. | 
        The ESG part of the news should be around topics within the following SASB topics {SASB}

        The output should be a structured JSON object with the key: "sentiment".

        Here is the format I expect for the JSON object:

        {{
          "sentiment": "Enter 'Positive', 'Neutral', or 'Negative'",
        }}

        Do not return any additional text or information outside of this JSON structure.
        """

        messages = [
          {"role": "system", "content": system_prompt},
          {"role": "user", "content": main_prompt}
        ]

        response = openai.chat.completions.create(
        model=model,
        messages=messages,
        response_format={"type": "json_object"}  # Enable JSON mode
        )

        response_json = json.loads(response.choices[0].message.content)
        json_sentiment = response_json.get('sentiment')

        if json_sentiment in allowed_sentiments:
            return json_sentiment

        attempt += 1

    # After max retries, if no valid sentiment is found, handle as needed (e.g., return a default sentiment)
    print("Failed to obtain a valid sentiment after maximum retries. Defaulting to 'Neutral'.")
    return 'Neutral'  # Default return value if no valid sentiment is obtained


def update_dataset_with_gpt_sentiment(df, model, column_name='GPT_based_sentiment'):
    # Initialize the new column to store GPT-based sentiment
    df['GPT_based_sentiment'] = None
    
    # Use tqdm to show a progress bar for the operation
    for index, row in tqdm(df.iterrows(), total=len(df), desc="Processing rows"):
        # Extract necessary information for each row
        company = row['Company']  # Make sure this matches your DataFrame's column name
        SASB = row['SASB']  # Make sure this matches your DataFrame's column name
        news = row['title & content']  # Make sure this matches your DataFrame's column name
        
        # Call the function to get the sentiment
        sentiment = get_sentiment_gpt(company, SASB, news, model=model)
        
        # Update the DataFrame with the obtained sentiment
        df.at[index, column_name] = sentiment  # Now correctly assigns the sentiment
        
    return df

def app_layout():
    st.set_page_config(page_title="NLP ESG Project", page_icon="πŸ“ˆ")

    # Custom styles
    st.markdown(
        """
        <style>
        .streamlit-container {
            background-color: #F5F5F5;
        }
        .stButton>button {
            width: 100%;
            border-radius: 10px;
            border: none;
            margin: 10px 0;
            padding: 15px 20px;
            background-color: #79AEC8;
            color: white;
            font-size: 18px;
        }
        .stButton>button:hover {
            background-color: #6699CC;
        }
        </style>
        """,
        unsafe_allow_html=True,
    )

    # Header section
    st.write("# NLP Project: ESG News Analysis and Financial Impact")
    st.sidebar.write("## Configuration")

    # API Key input
    openai_api_key = st.sidebar.text_input("Enter your OpenAI API key", type="password")

    # File Upload
    st.sidebar.write("## Upload Data")
    uploaded_file = st.sidebar.file_uploader("", type="csv")

    # Investment Strategy Slider
    st.sidebar.markdown("### Investment Strategy")
    investment_strategy = st.sidebar.slider(
        "Investment Strategy",
        min_value=0.0, max_value=1.0, value=0.5, step=0.01,
        format="",
        help="0 is Conservative, 1 is Aggressive",
        label_visibility="collapsed"
    )
    st.sidebar.text(f"Current Strategy: {'Conservative' if investment_strategy <= 0.5 else 'Aggressive'}")

    # Main container
    if uploaded_file is not None:
        # Displaying the file
        data = pd.read_csv(uploaded_file)
        st.write("### Uploaded News Data:")
        st.dataframe(data, use_container_width=True)

        if st.button("πŸ” Classify ESG"):
            st.write("Classifying ESG-related news...")
            # Placeholder - replace with actual ESG classification code
            data['ESG'] = "Yes"  # placeholder

        if st.button("😊 Determine Sentiment"):
            st.write("Determining sentiment using GPT...")
            # Run sentiment analysis with GPT
            try:
                with st.spinner("Analyzing sentiment..."):
                    # Assume you have your API key set and a function defined to handle sentiment analysis
                    updated_data = update_dataset_with_gpt_sentiment(data, model='gpt-4-turbo-2024-04-09')
                    st.write("News with GPT-based Sentiment Analysis:")
                    st.dataframe(updated_data, use_container_width=True)
            except Exception as e:
                st.error(f"An error occurred: {e}")

        if st.button("πŸ“Š Alphalens Analysis"):
            st.write("Alphalens analysis will be here")  # placeholder

        # Expander for advanced settings
        with st.expander("Advanced Settings"):
            st.write("Any advanced settings and configurations will go here.")

def main():
    app_layout()

if __name__ == "__main__":
    main()