Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,6 @@ import gradio as gr
|
|
3 |
from transformers import pipeline
|
4 |
import spacy
|
5 |
import lib.read_pdf
|
6 |
-
|
7 |
# Initialize spaCy model
|
8 |
nlp = spacy.load('en_core_web_sm')
|
9 |
nlp.add_pipe('sentencizer')
|
@@ -71,72 +70,77 @@ with gr.Blocks() as demo:
|
|
71 |
gr.Markdown("## Financial Report Paragraph Selection and Analysis")
|
72 |
|
73 |
with gr.Row():
|
74 |
-
#
|
75 |
with gr.Column():
|
76 |
-
gr.Markdown("### PDF 1 Analysis")
|
77 |
pdf1 = gr.Dropdown(choices=get_pdf_files(PDF_FOLDER), label="Select PDF 1")
|
|
|
|
|
|
|
78 |
b1 = gr.Button("Extract and Display Paragraphs")
|
79 |
paragraph_1_dropdown = gr.Dropdown(label="Select Paragraph from PDF 1")
|
80 |
-
|
81 |
-
summary_textbox_1 = gr.Textbox(label="Summary for PDF 1", lines=4)
|
82 |
-
sentiment_textbox_1 = gr.Textbox(label="Classification for PDF 1", lines=4)
|
83 |
-
fin_spans_1 = gr.HighlightedText(label="Financial Tone Analysis for PDF 1")
|
84 |
|
85 |
def update_paragraphs(pdf1, pdf2):
|
86 |
global stored_paragraphs_1, stored_paragraphs_2
|
87 |
stored_paragraphs_1, stored_paragraphs_2 = extract_and_summarize(pdf1, pdf2)
|
88 |
updated_dropdown_1 = [f"Paragraph {i+1}: {p[:100]}..." for i, p in enumerate(stored_paragraphs_1)]
|
89 |
updated_dropdown_2 = [f"Paragraph {i+1}: {p[:100]}..." for i, p in enumerate(stored_paragraphs_2)]
|
90 |
-
return gr.update(choices=updated_dropdown_1),
|
91 |
-
|
92 |
-
def process_paragraph_1(paragraph):
|
93 |
-
try:
|
94 |
-
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
95 |
-
selected_paragraph = stored_paragraphs_1[paragraph_index]
|
96 |
-
summary = summarize_text(selected_paragraph)
|
97 |
-
sentiment = text_to_sentiment(selected_paragraph)
|
98 |
-
fin_spans = fin_ext(selected_paragraph)
|
99 |
-
return selected_paragraph, summary, sentiment, fin_spans
|
100 |
-
except (IndexError, ValueError):
|
101 |
-
return "Invalid selection", "Error", "Error", []
|
102 |
|
103 |
b1.click(fn=update_paragraphs, inputs=[pdf1, pdf2], outputs=[paragraph_1_dropdown, paragraph_2_dropdown])
|
104 |
-
summarize_btn1 = gr.Button("Summarize Text from PDF 1")
|
105 |
-
sentiment_btn1 = gr.Button("Classify Financial Tone from PDF 1")
|
106 |
-
analyze_btn1 = gr.Button("Analyze Financial Tone and FLS")
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
-
with gr.Column():
|
113 |
-
gr.Markdown("### PDF 2 Analysis")
|
114 |
-
pdf2 = gr.Dropdown(choices=get_pdf_files(PDF_FOLDER), label="Select PDF 2")
|
115 |
-
b2 = gr.Button("Extract and Display Paragraphs")
|
116 |
-
paragraph_2_dropdown = gr.Dropdown(label="Select Paragraph from PDF 2")
|
117 |
-
selected_paragraph_2 = gr.Textbox(label="Selected Paragraph 2 Content", lines=4)
|
118 |
-
summary_textbox_2 = gr.Textbox(label="Summary for PDF 2", lines=4)
|
119 |
-
sentiment_textbox_2 = gr.Textbox(label="Classification for PDF 2", lines=4)
|
120 |
-
fin_spans_2 = gr.HighlightedText(label="Financial Tone Analysis for PDF 2")
|
121 |
-
|
122 |
-
def process_paragraph_2(paragraph):
|
123 |
-
try:
|
124 |
-
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
125 |
-
selected_paragraph = stored_paragraphs_2[paragraph_index]
|
126 |
-
summary = summarize_text(selected_paragraph)
|
127 |
-
sentiment = text_to_sentiment(selected_paragraph)
|
128 |
-
fin_spans = fin_ext(selected_paragraph)
|
129 |
-
return selected_paragraph, summary, sentiment, fin_spans
|
130 |
-
except (IndexError, ValueError):
|
131 |
-
return "Invalid selection", "Error", "Error", []
|
132 |
-
|
133 |
-
b2.click(fn=update_paragraphs, inputs=[pdf1, pdf2], outputs=[paragraph_1_dropdown, paragraph_2_dropdown])
|
134 |
-
summarize_btn2 = gr.Button("Summarize Text from PDF 2")
|
135 |
-
sentiment_btn2 = gr.Button("Classify Financial Tone from PDF 2")
|
136 |
-
analyze_btn2 = gr.Button("Analyze Financial Tone and FLS")
|
137 |
-
|
138 |
-
summarize_btn2.click(fn=lambda p: process_paragraph_2(p)[1], inputs=paragraph_2_dropdown, outputs=summary_textbox_2)
|
139 |
-
sentiment_btn2.click(fn=lambda p: process_paragraph_2(p)[2], inputs=paragraph_2_dropdown, outputs=sentiment_textbox_2)
|
140 |
-
analyze_btn2.click(fn=lambda p: process_paragraph_2(p)[3], inputs=paragraph_2_dropdown, outputs=fin_spans_2)
|
141 |
|
142 |
demo.launch()
|
|
|
3 |
from transformers import pipeline
|
4 |
import spacy
|
5 |
import lib.read_pdf
|
|
|
6 |
# Initialize spaCy model
|
7 |
nlp = spacy.load('en_core_web_sm')
|
8 |
nlp.add_pipe('sentencizer')
|
|
|
70 |
gr.Markdown("## Financial Report Paragraph Selection and Analysis")
|
71 |
|
72 |
with gr.Row():
|
73 |
+
# Upload PDFs
|
74 |
with gr.Column():
|
|
|
75 |
pdf1 = gr.Dropdown(choices=get_pdf_files(PDF_FOLDER), label="Select PDF 1")
|
76 |
+
pdf2 = gr.Dropdown(choices=get_pdf_files(PDF_FOLDER), label="Select PDF 2")
|
77 |
+
|
78 |
+
with gr.Column():
|
79 |
b1 = gr.Button("Extract and Display Paragraphs")
|
80 |
paragraph_1_dropdown = gr.Dropdown(label="Select Paragraph from PDF 1")
|
81 |
+
paragraph_2_dropdown = gr.Dropdown(label="Select Paragraph from PDF 2")
|
|
|
|
|
|
|
82 |
|
83 |
def update_paragraphs(pdf1, pdf2):
|
84 |
global stored_paragraphs_1, stored_paragraphs_2
|
85 |
stored_paragraphs_1, stored_paragraphs_2 = extract_and_summarize(pdf1, pdf2)
|
86 |
updated_dropdown_1 = [f"Paragraph {i+1}: {p[:100]}..." for i, p in enumerate(stored_paragraphs_1)]
|
87 |
updated_dropdown_2 = [f"Paragraph {i+1}: {p[:100]}..." for i, p in enumerate(stored_paragraphs_2)]
|
88 |
+
return gr.update(choices=updated_dropdown_1),gr.update(choices=updated_dropdown_2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
b1.click(fn=update_paragraphs, inputs=[pdf1, pdf2], outputs=[paragraph_1_dropdown, paragraph_2_dropdown])
|
|
|
|
|
|
|
91 |
|
92 |
+
with gr.Row():
|
93 |
+
# Process the selected paragraph from PDF 1
|
94 |
+
with gr.Column():
|
95 |
+
gr.Markdown("### PDF 1 Analysis")
|
96 |
+
selected_paragraph_1 = gr.Textbox(label="Selected Paragraph 1 Content", lines=4)
|
97 |
+
summarize_btn1 = gr.Button("Summarize Text from PDF 1")
|
98 |
+
sentiment_btn1 = gr.Button("Classify Financial Tone from PDF 1")
|
99 |
+
summary_textbox_1 = gr.Textbox(label="Summary for PDF 1", lines=4)
|
100 |
+
sentiment_textbox_1 = gr.Textbox(label="Classification for PDF 1", lines=4)
|
101 |
+
fin_spans_1 = gr.HighlightedText(label="Financial Tone Analysis for PDF 1")
|
102 |
+
|
103 |
+
def process_paragraph_1(paragraph):
|
104 |
+
try:
|
105 |
+
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
106 |
+
selected_paragraph = stored_paragraphs_1[paragraph_index]
|
107 |
+
summary = summarize_text(selected_paragraph)
|
108 |
+
sentiment = text_to_sentiment(selected_paragraph)
|
109 |
+
fin_spans = fin_ext(selected_paragraph)
|
110 |
+
return selected_paragraph, summary, sentiment, fin_spans
|
111 |
+
except (IndexError, ValueError):
|
112 |
+
return "Invalid selection", "Error", "Error", []
|
113 |
+
|
114 |
+
summarize_btn1.click(fn=lambda p: process_paragraph_1(p)[1], inputs=paragraph_1_dropdown, outputs=summary_textbox_1)
|
115 |
+
sentiment_btn1.click(fn=lambda p: process_paragraph_1(p)[2], inputs=paragraph_1_dropdown, outputs=sentiment_textbox_1)
|
116 |
+
analyze_btn1 = gr.Button("Analyze Financial Tone and FLS")
|
117 |
+
analyze_btn1.click(fn=lambda p: process_paragraph_1(p)[3], inputs=paragraph_1_dropdown, outputs=fin_spans_1)
|
118 |
+
|
119 |
+
# Process the selected paragraph from PDF 2
|
120 |
+
with gr.Column():
|
121 |
+
gr.Markdown("### PDF 2 Analysis")
|
122 |
+
selected_paragraph_2 = gr.Textbox(label="Selected Paragraph 2 Content", lines=4)
|
123 |
+
summarize_btn2 = gr.Button("Summarize Text from PDF 2")
|
124 |
+
sentiment_btn2 = gr.Button("Classify Financial Tone from PDF 2")
|
125 |
+
summary_textbox_2 = gr.Textbox(label="Summary for PDF 2", lines=4)
|
126 |
+
sentiment_textbox_2 = gr.Textbox(label="Classification for PDF 2", lines=4)
|
127 |
+
fin_spans_2 = gr.HighlightedText(label="Financial Tone Analysis for PDF 2")
|
128 |
+
|
129 |
+
def process_paragraph_2(paragraph):
|
130 |
+
try:
|
131 |
+
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
132 |
+
selected_paragraph = stored_paragraphs_2[paragraph_index]
|
133 |
+
summary = summarize_text(selected_paragraph)
|
134 |
+
sentiment = text_to_sentiment(selected_paragraph)
|
135 |
+
fin_spans = fin_ext(selected_paragraph)
|
136 |
+
return selected_paragraph, summary, sentiment, fin_spans
|
137 |
+
except (IndexError, ValueError):
|
138 |
+
return "Invalid selection", "Error", "Error", []
|
139 |
+
|
140 |
+
summarize_btn2.click(fn=lambda p: process_paragraph_2(p)[1], inputs=paragraph_2_dropdown, outputs=summary_textbox_2)
|
141 |
+
sentiment_btn2.click(fn=lambda p: process_paragraph_2(p)[2], inputs=paragraph_2_dropdown, outputs=sentiment_textbox_2)
|
142 |
+
analyze_btn2 = gr.Button("Analyze Financial Tone and FLS")
|
143 |
+
analyze_btn2.click(fn=lambda p: process_paragraph_2(p)[3], inputs=paragraph_2_dropdown, outputs=fin_spans_2)
|
144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
demo.launch()
|