# Import packages import numpy as np import pandas as pd import matplotlib.pyplot as plt import re import tensorflow as tf import pickle import gradio as gr import yake import spacy from spacy import displacy import streamlit as st import spacy_streamlit import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification # Load NLP model nlp = spacy.load('en_core_web_sm') # Load your custom tokenizer and model tokenizer = AutoTokenizer.from_pretrained("paragon-analytics/bert_resil") model = AutoModelForSequenceClassification.from_pretrained("paragon-analytics/bert_resil") # Initialize YAKE keyword extractor kw_extractor = yake.KeywordExtractor() custom_kw_extractor = yake.KeywordExtractor(lan="en", n=2, dedupLim=0.2, top=10, features=None) # Initialize the transformers interpret explainer from transformers_interpret import SequenceClassificationExplainer cls_explainer = SequenceClassificationExplainer(model, tokenizer) def process_final_text(text): X_test = str(text).lower() # Encode the input and get model output encoded_input = tokenizer(X_test, return_tensors='pt') output = model(**encoded_input) scores = output[0][0].detach().numpy() scores = tf.nn.softmax(scores) # Get Keywords keywords = custom_kw_extractor.extract_keywords(X_test) letter = [] score = [] for i in keywords: if i[1] > 0.4: a = "+++" elif (i[1] <= 0.4) and (i[1] > 0.1): a = "++" elif (i[1] <= 0.1) and (i[1] > 0.01): a = "+" else: a = "NA" letter.append(i[0]) score.append(a) keywords = [(letter[i], score[i]) for i in range(len(letter))] # Get NER doc = nlp(text) sp_html = displacy.render(doc, style="ent", page=True, jupyter=False) NER = sp_html # Transformer Interpret word_attributions = cls_explainer(X_test) letter = [] score = [] for i in word_attributions: if i[1] > 0.5: a = "++" elif (i[1] <= 0.5) and (i[1] > 0.1): a = "+" elif (i[1] >= -0.5) and (i[1] < -0.1): a = "-" elif i[1] < -0.5: a = "--" else: a = "NA" letter.append(i[0]) score.append(a) word_attributions = [(letter[i], score[i]) for i in range(len(letter))] return {"Inspirational": float(scores.numpy()[1]), "Uninspiring": float(scores.numpy()[0])}, keywords, NER, word_attributions def main(prob1): text = str(prob1) obj = process_final_text(text) return obj[0], obj[1], obj[2], obj[3] title = "Welcome to **HopeHarbor**! ✨" description1 = """ By leveraging a powerful large language model, this app evaluates your message for resilience and hope, enhancing your ability to inspire during challenging times.""" with gr.Blocks(title=title) as demo: gr.Markdown(f"## {title}") gr.Markdown(description1) gr.Markdown("""---""") prob1 = gr.Textbox(label="Enter Your Text Here:", lines=2, placeholder="Type it here ...") submit_btn = gr.Button("Analyze") with gr.Column(visible=True) as output_col: label = gr.Label(label="Predicted Label") impplot = gr.HighlightedText( label="Important Words", combine_adjacent=False, color_map={"+++": "royalblue", "++": "cornflowerblue", "+": "lightsteelblue", "NA": "white"} ) NER = gr.HTML(label='NER:') intp = gr.HighlightedText( label="Word Scores", combine_adjacent=False, color_map={"++": "darkgreen", "+": "green", "--": "darkred", "-": "red", "NA": "white"} ) submit_btn.click( main, [prob1], [label, impplot, NER, intp], api_name="ResText" ) gr.Markdown("### Click on any of the examples below to see to what extent they contain resilience messaging:") gr.Examples( examples=[ ["Please stay at home and avoid unnecessary trips."], ["Please stay at home and avoid unnecessary trips. We will survive this."], ["We will survive this."], ["Watch today’s news briefing with the latest updates on COVID-19 in Connecticut."], ["So let's keep doing what we know works. Let's stay strong, and let's beat this virus. I know we can, and I know we can come out stronger on the other side."], ["It is really wonderful how much resilience there is in human nature. Let any obstructing cause, no matter what, be removed in any way, even by death, and we fly back to first principles of hope and enjoyment."], ["Resilience is accepting your new reality, even if it’s less good than the one you had before. You can fight it, you can do nothing but scream about what you’ve lost, or you can accept that and try to put together something that’s good."], ["You survived all of the days you thought you couldn't, never underestimate your resilience."], ["Like tiny seeds with potent power to push through tough ground and become mighty trees, we hold innate reserves of unimaginable strength. We are resilient."] ], inputs=[prob1], outputs=[label, impplot, NER, intp], fn=main, cache_examples=True ) demo.launch()