CoolFace
Apppublic

shrut27/ESG_Report_Analysis

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py86 linesDownload Raw Back to root
1import streamlit as st2from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline3import spacy4from tika import parser5import requests6import pandas as pd7 8# Loading spaCy model outside the streamlit cache9nlp = spacy.load("en_core_web_sm")10 11@st.cache_resource()12def load_environmental_model():13    name_env = "ESGBERT/EnvironmentalBERT-environmental"14    tokenizer_env = AutoTokenizer.from_pretrained(name_env)15    model_env = AutoModelForSequenceClassification.from_pretrained(name_env)16    return pipeline("text-classification", model=model_env, tokenizer=tokenizer_env)17 18@st.cache_resource()19def load_social_model():20    name_soc = "ESGBERT/SocialBERT-social"21    tokenizer_soc = AutoTokenizer.from_pretrained(name_soc)22    model_soc = AutoModelForSequenceClassification.from_pretrained(name_soc)23    return pipeline("text-classification", model=model_soc, tokenizer=tokenizer_soc)24 25@st.cache_resource()26def load_governance_model():27    name_gov = "ESGBERT/GovernanceBERT-governance"28    tokenizer_gov = AutoTokenizer.from_pretrained(name_gov)29    model_gov = AutoModelForSequenceClassification.from_pretrained(name_gov)30    return pipeline("text-classification", model=model_gov, tokenizer=tokenizer_gov)31 32@st.cache_resource()33def load_sentiment_model():34    model_name = "climatebert/distilroberta-base-climate-sentiment"35    model = AutoModelForSequenceClassification.from_pretrained(model_name)36    tokenizer = AutoTokenizer.from_pretrained(model_name, max_len=512)37    return pipeline("text-classification", model=model, tokenizer=tokenizer)38 39# Streamlit App40st.title("ESG Report Classification using Natural Language Processing")41 42# Get report URL from user input43url = st.text_input("Enter the URL of the report (PDF):")44 45# Model selection dropdown46st.write("Environmental Model, Social Model, Governance Model would give the percentage denoting the parameter chosen.")47st.write("Sentiment Model shows if the company is a risk or opportunity based on all 3 parameters.")48selected_model = st.selectbox("Select Model", ["Environmental Model", "Social Model", "Governance Model", "Sentiment Model"])49 50if url:51    # Download PDF content from the URL52    response = requests.get(url, stream=True)53 54    if response.status_code == 200:55        # Parse PDF and extract text56        raw_text = parser.from_buffer(response.content)['content']57        # Extract sentences using spaCy58        doc = nlp(raw_text)59        sentences = [sent.text for sent in doc.sents]60        # Filtering and preprocessing sentences61        sequences = list(map(str, sentences))62        sentences = [x.replace("\n", "") for x in sequences]63        sentences = [x for x in sentences if x != ""]64        sentences = [x for x in sentences if x[0].isupper()]65        sub_sentences = sentences[:100]  66        # Classification using different models based on user selection67        if selected_model == "Environmental Model":68            pipe_model = load_environmental_model()69        elif selected_model == "Social Model":70            pipe_model = load_social_model()71        elif selected_model == "Governance Model":72            pipe_model = load_governance_model()73        else:74            pipe_model = load_sentiment_model()75 76        # Get predictions for the selected model77        model_results = pipe_model(sub_sentences, padding=True, truncation=True)78        model_labels = [x["label"] for x in model_results]79 80        # Display count of sentences labeled as the selected model81        st.subheader(f"{selected_model} Sentences Count")82        st.write(pd.DataFrame({"sentence": sub_sentences, selected_model: model_labels}).groupby(selected_model).count())83 84    else:85        st.error("Error fetching PDF content from the provided URL. Please check the URL and try again.")86