import os os.system("python -m pip install --upgrade pip") os.system("pip install transformers torch spacy") os.system("pip install gradio==3.0.18") os.system("python -m spacy download en_core_web_sm") from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification import gradio as gr import spacy nlp = spacy.load('en_core_web_sm') nlp.add_pipe('sentencizer') def split_in_sentences(text): doc = nlp(text) return [str(sent).strip() for sent in doc.sents] def make_spans(text,results): results_list = [] for i in range(len(results)): results_list.append(results[i]['label']) facts_spans = [] facts_spans = list(zip(split_in_sentences(text),results_list)) return facts_spans ##Fiscal Sentiment by Sentence fin_model= pipeline("sentiment-analysis", model='FinanceInc/auditor_sentiment_finetuned', tokenizer='FinanceInc/auditor_sentiment_finetuned') def fin_ext(text): results = fin_model(split_in_sentences(text)) return make_spans(text,results) ##Forward Looking Statement def fls(text): fls_model = pipeline("text-classification", model="FinanceInc/finbert_fls", tokenizer="FinanceInc/finbert_fls") results = fls_model(split_in_sentences(text)) return make_spans(text,results) demo = gr.Blocks() with demo: gr.Markdown("## HS Financial Analyst AI") gr.Markdown("This project applies AI trained by our finetuned FinBERT model to analyze earning calls and other financial documents.") with gr.Row(): with gr.Column(): with gr.Row(): text = gr.Textbox(value=" This quarter we earned about 3M. We beat our foecast by 30%. Next quarter we expect to earn 4M") with gr.Row(): b5 = gr.Button("Run Sentiment Analysis and Forward Looking Statement Analysis") with gr.Column(): with gr.Row(): fin_spans = gr.HighlightedText() with gr.Row(): fls_spans = gr.HighlightedText() b5.click(fin_ext, inputs=text, outputs=fin_spans) b5.click(fls, inputs=text, outputs=fls_spans) demo.launch()