Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,6 +20,7 @@ def make_spans(text, results):
|
|
| 20 |
# Initialize pipelines
|
| 21 |
summarizer = pipeline("summarization", model="human-centered-summarization/financial-summarization-pegasus")
|
| 22 |
fin_model = pipeline("sentiment-analysis", model='yiyanghkust/finbert-tone', tokenizer='yiyanghkust/finbert-tone')
|
|
|
|
| 23 |
|
| 24 |
def summarize_text(text):
|
| 25 |
resp = summarizer(text)
|
|
@@ -32,6 +33,9 @@ def text_to_sentiment(text):
|
|
| 32 |
def fin_ext(text):
|
| 33 |
results = fin_model(split_in_sentences(text))
|
| 34 |
return make_spans(text, results)
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def extract_and_summarize(pdf1, pdf2):
|
| 37 |
if not pdf1 or not pdf2:
|
|
@@ -97,51 +101,103 @@ with gr.Blocks() as demo:
|
|
| 97 |
# Process the selected paragraph from PDF 1
|
| 98 |
with gr.Column():
|
| 99 |
gr.Markdown("### PDF 1 Analysis")
|
| 100 |
-
def
|
| 101 |
try:
|
| 102 |
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
| 103 |
selected_paragraph = stored_paragraphs_1[paragraph_index]
|
| 104 |
summary = summarize_text(selected_paragraph)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
sentiment = text_to_sentiment(selected_paragraph)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
fin_spans = fin_ext(selected_paragraph)
|
| 107 |
-
return
|
| 108 |
except (IndexError, ValueError):
|
| 109 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
selected_paragraph_1 = gr.Textbox(label="Selected Paragraph 1 Content", lines=4)
|
| 111 |
selected_paragraph_1.change(show, paragraph_1_dropdown, selected_paragraph_1)
|
| 112 |
summarize_btn1 = gr.Button("Summarize Text from PDF 1")
|
| 113 |
summary_textbox_1 = gr.Textbox(label="Summary for PDF 1", lines=2)
|
| 114 |
-
summarize_btn1.click(fn=lambda p:
|
| 115 |
sentiment_btn1 = gr.Button("Classify Financial Tone from PDF 1")
|
| 116 |
sentiment_textbox_1 = gr.Textbox(label="Classification for PDF 1", lines=1)
|
| 117 |
-
sentiment_btn1.click(fn=lambda p:
|
| 118 |
-
analyze_btn1 = gr.Button("Analyze Financial Tone on each sentence")
|
| 119 |
fin_spans_1 = gr.HighlightedText(label="Financial Tone Analysis for PDF 1")
|
| 120 |
-
analyze_btn1.click(fn=lambda p:
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
# Process the selected paragraph from PDF 2
|
| 123 |
with gr.Column():
|
| 124 |
gr.Markdown("### PDF 2 Analysis")
|
| 125 |
-
def
|
| 126 |
try:
|
| 127 |
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
| 128 |
-
selected_paragraph =
|
| 129 |
summary = summarize_text(selected_paragraph)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
sentiment = text_to_sentiment(selected_paragraph)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
fin_spans = fin_ext(selected_paragraph)
|
| 132 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
except (IndexError, ValueError):
|
| 134 |
-
return
|
| 135 |
selected_paragraph_2 = gr.Textbox(label="Selected Paragraph 2 Content", lines=4)
|
| 136 |
selected_paragraph_2.change(show, paragraph_2_dropdown, selected_paragraph_2)
|
| 137 |
summarize_btn2 = gr.Button("Summarize Text from PDF 2")
|
| 138 |
summary_textbox_2 = gr.Textbox(label="Summary for PDF 2", lines=2)
|
| 139 |
-
summarize_btn2.click(fn=lambda p:
|
| 140 |
sentiment_btn2 = gr.Button("Classify Financial Tone from PDF 2")
|
| 141 |
sentiment_textbox_2 = gr.Textbox(label="Classification for PDF 2", lines=1)
|
| 142 |
-
sentiment_btn2.click(fn=lambda p:
|
| 143 |
-
analyze_btn2 = gr.Button("Analyze Financial Tone on each sentence")
|
| 144 |
fin_spans_2 = gr.HighlightedText(label="Financial Tone Analysis for PDF 2")
|
| 145 |
-
analyze_btn2.click(fn=lambda p:
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
demo.launch()
|
|
|
|
| 20 |
# Initialize pipelines
|
| 21 |
summarizer = pipeline("summarization", model="human-centered-summarization/financial-summarization-pegasus")
|
| 22 |
fin_model = pipeline("sentiment-analysis", model='yiyanghkust/finbert-tone', tokenizer='yiyanghkust/finbert-tone')
|
| 23 |
+
fin_model_bis = pipeline("sentiment-analysis", model='ProsusAI/finbert', tokenizer='ProsusAI/finbert')
|
| 24 |
|
| 25 |
def summarize_text(text):
|
| 26 |
resp = summarizer(text)
|
|
|
|
| 33 |
def fin_ext(text):
|
| 34 |
results = fin_model(split_in_sentences(text))
|
| 35 |
return make_spans(text, results)
|
| 36 |
+
def fin_ext_bis(text):
|
| 37 |
+
results = fin_model_bis(split_in_sentences(text))
|
| 38 |
+
return make_spans(text, results)
|
| 39 |
|
| 40 |
def extract_and_summarize(pdf1, pdf2):
|
| 41 |
if not pdf1 or not pdf2:
|
|
|
|
| 101 |
# Process the selected paragraph from PDF 1
|
| 102 |
with gr.Column():
|
| 103 |
gr.Markdown("### PDF 1 Analysis")
|
| 104 |
+
def process_paragraph_1_sum(paragraph):
|
| 105 |
try:
|
| 106 |
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
| 107 |
selected_paragraph = stored_paragraphs_1[paragraph_index]
|
| 108 |
summary = summarize_text(selected_paragraph)
|
| 109 |
+
return summary
|
| 110 |
+
except (IndexError, ValueError):
|
| 111 |
+
return "Error"
|
| 112 |
+
def process_paragraph_1_sent(paragraph):
|
| 113 |
+
try:
|
| 114 |
+
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
| 115 |
+
selected_paragraph = stored_paragraphs_1[paragraph_index]
|
| 116 |
sentiment = text_to_sentiment(selected_paragraph)
|
| 117 |
+
|
| 118 |
+
return sentiment
|
| 119 |
+
except (IndexError, ValueError):
|
| 120 |
+
return "Error"
|
| 121 |
+
def process_paragraph_1_sent_tone(paragraph):
|
| 122 |
+
try:
|
| 123 |
+
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
| 124 |
+
selected_paragraph = stored_paragraphs_1[paragraph_index]
|
| 125 |
fin_spans = fin_ext(selected_paragraph)
|
| 126 |
+
return fin_spans
|
| 127 |
except (IndexError, ValueError):
|
| 128 |
+
return []
|
| 129 |
+
def process_paragraph_1_sent_tone_bis(paragraph):
|
| 130 |
+
try:
|
| 131 |
+
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
| 132 |
+
selected_paragraph = stored_paragraphs_1[paragraph_index]
|
| 133 |
+
fin_spans = fin_ext_bis(selected_paragraph)
|
| 134 |
+
return fin_spans
|
| 135 |
+
except (IndexError, ValueError):
|
| 136 |
+
return []
|
| 137 |
selected_paragraph_1 = gr.Textbox(label="Selected Paragraph 1 Content", lines=4)
|
| 138 |
selected_paragraph_1.change(show, paragraph_1_dropdown, selected_paragraph_1)
|
| 139 |
summarize_btn1 = gr.Button("Summarize Text from PDF 1")
|
| 140 |
summary_textbox_1 = gr.Textbox(label="Summary for PDF 1", lines=2)
|
| 141 |
+
summarize_btn1.click(fn=lambda p: process_paragraph_1_sum(p), inputs=paragraph_1_dropdown, outputs=summary_textbox_1)
|
| 142 |
sentiment_btn1 = gr.Button("Classify Financial Tone from PDF 1")
|
| 143 |
sentiment_textbox_1 = gr.Textbox(label="Classification for PDF 1", lines=1)
|
| 144 |
+
sentiment_btn1.click(fn=lambda p: process_paragraph_1_sent(p), inputs=paragraph_1_dropdown, outputs=sentiment_textbox_1)
|
| 145 |
+
analyze_btn1 = gr.Button("Analyze Financial Tone on each sentence with yiyanghkust/finbert-tone")
|
| 146 |
fin_spans_1 = gr.HighlightedText(label="Financial Tone Analysis for PDF 1")
|
| 147 |
+
analyze_btn1.click(fn=lambda p: process_paragraph_1_sent_tone(p), inputs=paragraph_1_dropdown, outputs=fin_spans_1)
|
| 148 |
+
analyze_btn1_ = gr.Button("Analyze Financial Tone on each sentence with ProsusAI/finbert")
|
| 149 |
+
fin_spans_1_ = gr.HighlightedText(label="Financial Tone Analysis for PDF 1 bis")
|
| 150 |
+
analyze_btn1_.click(fn=lambda p: process_paragraph_1_sent_tone_bis(p)[3], inputs=paragraph_1_dropdown, outputs=fin_spans_1_)
|
| 151 |
|
| 152 |
# Process the selected paragraph from PDF 2
|
| 153 |
with gr.Column():
|
| 154 |
gr.Markdown("### PDF 2 Analysis")
|
| 155 |
+
def process_paragraph_2_sum(paragraph):
|
| 156 |
try:
|
| 157 |
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
| 158 |
+
selected_paragraph = stored_paragraphs_1[paragraph_index]
|
| 159 |
summary = summarize_text(selected_paragraph)
|
| 160 |
+
return summary
|
| 161 |
+
except (IndexError, ValueError):
|
| 162 |
+
return "Error"
|
| 163 |
+
def process_paragraph_2_sent(paragraph):
|
| 164 |
+
try:
|
| 165 |
+
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
| 166 |
+
selected_paragraph = stored_paragraphs_1[paragraph_index]
|
| 167 |
sentiment = text_to_sentiment(selected_paragraph)
|
| 168 |
+
|
| 169 |
+
return sentiment
|
| 170 |
+
except (IndexError, ValueError):
|
| 171 |
+
return "Error"
|
| 172 |
+
def process_paragraph_2_sent_tone(paragraph):
|
| 173 |
+
try:
|
| 174 |
+
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
| 175 |
+
selected_paragraph = stored_paragraphs_1[paragraph_index]
|
| 176 |
fin_spans = fin_ext(selected_paragraph)
|
| 177 |
+
return fin_spans
|
| 178 |
+
except (IndexError, ValueError):
|
| 179 |
+
return []
|
| 180 |
+
def process_paragraph_2_sent_tone_bis(paragraph):
|
| 181 |
+
try:
|
| 182 |
+
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
| 183 |
+
selected_paragraph = stored_paragraphs_2[paragraph_index]
|
| 184 |
+
fin_spans = fin_ext_bis(selected_paragraph)
|
| 185 |
+
return fin_spans
|
| 186 |
except (IndexError, ValueError):
|
| 187 |
+
return []
|
| 188 |
selected_paragraph_2 = gr.Textbox(label="Selected Paragraph 2 Content", lines=4)
|
| 189 |
selected_paragraph_2.change(show, paragraph_2_dropdown, selected_paragraph_2)
|
| 190 |
summarize_btn2 = gr.Button("Summarize Text from PDF 2")
|
| 191 |
summary_textbox_2 = gr.Textbox(label="Summary for PDF 2", lines=2)
|
| 192 |
+
summarize_btn2.click(fn=lambda p: process_paragraph_2_sum(p), inputs=paragraph_2_dropdown, outputs=summary_textbox_2)
|
| 193 |
sentiment_btn2 = gr.Button("Classify Financial Tone from PDF 2")
|
| 194 |
sentiment_textbox_2 = gr.Textbox(label="Classification for PDF 2", lines=1)
|
| 195 |
+
sentiment_btn2.click(fn=lambda p: process_paragraph_2_sent(p), inputs=paragraph_2_dropdown, outputs=sentiment_textbox_2)
|
| 196 |
+
analyze_btn2 = gr.Button("Analyze Financial Tone on each sentence with yiyanghkust/finbert-tone")
|
| 197 |
fin_spans_2 = gr.HighlightedText(label="Financial Tone Analysis for PDF 2")
|
| 198 |
+
analyze_btn2.click(fn=lambda p: process_paragraph_2_sent_tone(p), inputs=paragraph_2_dropdown, outputs=fin_spans_2)
|
| 199 |
+
analyze_btn2_ = gr.Button("Analyze Financial Tone on each sentence with ProsusAI/finbert")
|
| 200 |
+
fin_spans_2_ = gr.HighlightedText(label="Financial Tone Analysis for PDF 2 bis")
|
| 201 |
+
analyze_btn2_.click(fn=lambda p: process_paragraph_2_sent_tone_bis(p)[3], inputs=paragraph_2_dropdown, outputs=fin_spans_2_)
|
| 202 |
|
| 203 |
demo.launch()
|