Spaces:
Sleeping
Sleeping
# 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") | |
os.environ["OPENAI_API_KEY"] = openai_api_key | |
openai.api_key = openai_api_key | |
# 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() | |