Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import gradio as gr
|
|
| 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')
|
|
@@ -85,62 +86,61 @@ with gr.Blocks() as demo:
|
|
| 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 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
|
| 146 |
demo.launch()
|
|
|
|
| 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')
|
|
|
|
| 86 |
stored_paragraphs_1, stored_paragraphs_2 = extract_and_summarize(pdf1, pdf2)
|
| 87 |
updated_dropdown_1 = [f"Paragraph {i+1}: {p[:100]}..." for i, p in enumerate(stored_paragraphs_1)]
|
| 88 |
updated_dropdown_2 = [f"Paragraph {i+1}: {p[:100]}..." for i, p in enumerate(stored_paragraphs_2)]
|
| 89 |
+
return gr.update(choices=updated_dropdown_1), gr.update(choices=updated_dropdown_2)
|
| 90 |
|
| 91 |
b1.click(fn=update_paragraphs, inputs=[pdf1, pdf2], outputs=[paragraph_1_dropdown, paragraph_2_dropdown])
|
| 92 |
|
| 93 |
+
with gr.Row():
|
| 94 |
+
# Process the selected paragraph from PDF 1
|
| 95 |
+
with gr.Column():
|
| 96 |
+
gr.Markdown("### PDF 1 Analysis")
|
| 97 |
+
selected_paragraph_1 = gr.Textbox(label="Selected Paragraph 1 Content", lines=4)
|
| 98 |
+
summarize_btn1 = gr.Button("Summarize Text from PDF 1")
|
| 99 |
+
sentiment_btn1 = gr.Button("Classify Financial Tone from PDF 1")
|
| 100 |
+
summary_textbox_1 = gr.Textbox(label="Summary for PDF 1", lines=4)
|
| 101 |
+
sentiment_textbox_1 = gr.Textbox(label="Classification for PDF 1", lines=4)
|
| 102 |
+
fin_spans_1 = gr.HighlightedText(label="Financial Tone Analysis for PDF 1")
|
| 103 |
+
|
| 104 |
+
def process_paragraph_1(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 |
+
sentiment = text_to_sentiment(selected_paragraph)
|
| 110 |
+
fin_spans = fin_ext(selected_paragraph)
|
| 111 |
+
return selected_paragraph, summary, sentiment, fin_spans
|
| 112 |
+
except (IndexError, ValueError):
|
| 113 |
+
return "Invalid selection", "Error", "Error", []
|
| 114 |
+
|
| 115 |
+
summarize_btn1.click(fn=lambda p: process_paragraph_1(p)[1], inputs=paragraph_1_dropdown, outputs=summary_textbox_1)
|
| 116 |
+
sentiment_btn1.click(fn=lambda p: process_paragraph_1(p)[2], inputs=paragraph_1_dropdown, outputs=sentiment_textbox_1)
|
| 117 |
+
analyze_btn1 = gr.Button("Analyze Financial Tone and FLS")
|
| 118 |
+
analyze_btn1.click(fn=lambda p: process_paragraph_1(p)[3], inputs=paragraph_1_dropdown, outputs=fin_spans_1)
|
| 119 |
+
|
| 120 |
+
# Process the selected paragraph from PDF 2
|
| 121 |
+
with gr.Column():
|
| 122 |
+
gr.Markdown("### PDF 2 Analysis")
|
| 123 |
+
selected_paragraph_2 = gr.Textbox(label="Selected Paragraph 2 Content", lines=4)
|
| 124 |
+
summarize_btn2 = gr.Button("Summarize Text from PDF 2")
|
| 125 |
+
sentiment_btn2 = gr.Button("Classify Financial Tone from PDF 2")
|
| 126 |
+
summary_textbox_2 = gr.Textbox(label="Summary for PDF 2", lines=4)
|
| 127 |
+
sentiment_textbox_2 = gr.Textbox(label="Classification for PDF 2", lines=4)
|
| 128 |
+
fin_spans_2 = gr.HighlightedText(label="Financial Tone Analysis for PDF 2")
|
| 129 |
+
|
| 130 |
+
def process_paragraph_2(paragraph):
|
| 131 |
+
try:
|
| 132 |
+
paragraph_index = int(paragraph.split(':')[0].replace('Paragraph ', '')) - 1
|
| 133 |
+
selected_paragraph = stored_paragraphs_2[paragraph_index]
|
| 134 |
+
summary = summarize_text(selected_paragraph)
|
| 135 |
+
sentiment = text_to_sentiment(selected_paragraph)
|
| 136 |
+
fin_spans = fin_ext(selected_paragraph)
|
| 137 |
+
return selected_paragraph, summary, sentiment, fin_spans
|
| 138 |
+
except (IndexError, ValueError):
|
| 139 |
+
return "Invalid selection", "Error", "Error", []
|
| 140 |
+
|
| 141 |
+
summarize_btn2.click(fn=lambda p: process_paragraph_2(p)[1], inputs=paragraph_2_dropdown, outputs=summary_textbox_2)
|
| 142 |
+
sentiment_btn2.click(fn=lambda p: process_paragraph_2(p)[2], inputs=paragraph_2_dropdown, outputs=sentiment_textbox_2)
|
| 143 |
+
analyze_btn2 = gr.Button("Analyze Financial Tone and FLS")
|
| 144 |
+
analyze_btn2.click(fn=lambda p: process_paragraph_2(p)[3], inputs=paragraph_2_dropdown, outputs=fin_spans_2)
|
|
|
|
| 145 |
|
| 146 |
demo.launch()
|