Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,18 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
-
import os
|
4 |
import pandas as pd
|
|
|
5 |
|
6 |
# Load the text summarization pipeline
|
7 |
summarizer = pipeline("summarization", model="astro21/bart-cls_n")
|
8 |
|
9 |
chunk_counter = 0
|
10 |
|
11 |
-
|
12 |
def summarize_text(input_text):
|
13 |
global chunk_counter
|
14 |
-
|
15 |
|
|
|
16 |
max_chunk_size = 1024
|
17 |
chunks = [input_text[i:i + max_chunk_size] for i in range(0, len(input_text), max_chunk_size)]
|
18 |
|
@@ -25,7 +25,6 @@ def summarize_text(input_text):
|
|
25 |
summarized_chunk = summarizer(chunk, max_length=128, min_length=64, do_sample=False)[0]['summary_text']
|
26 |
summarized_chunks.append(f"Chunk {chunk_counter}:\n{summarized_chunk}")
|
27 |
summarized_chunks_only.append(summarized_chunk)
|
28 |
-
|
29 |
chunk_lengths.append(len(chunk))
|
30 |
|
31 |
summarized_text = "\n".join(summarized_chunks)
|
@@ -35,35 +34,19 @@ def summarize_text(input_text):
|
|
35 |
with open("summarized.txt", "w") as output_file:
|
36 |
output_file.write(summarized_text_only)
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
return summarized_text
|
41 |
-
|
42 |
-
|
43 |
-
# def read_file(file):
|
44 |
-
# print(file[0].name)
|
45 |
-
# with open(file[0].name, 'r') as file_:
|
46 |
-
# content = file_.read()
|
47 |
-
# return content
|
48 |
-
|
49 |
-
|
50 |
-
# def summarize_text_file(file):
|
51 |
-
# if file is not None:
|
52 |
-
# content = read_file(file)
|
53 |
-
# return summarize_text(content)
|
54 |
-
|
55 |
-
|
56 |
-
# input_type = gr.inputs.Textbox(label = "textbox" )
|
57 |
-
|
58 |
-
# Name the outputs using the label parameter and provide a download option
|
59 |
-
demo = gr.Interface(fn=summarize_text, inputs=[gr.Textbox(label="Enter Text",placeholder="Ask me anything.",lines=3)],
|
60 |
-
outputs=[gr.Textbox(label="Summarized Text")],
|
61 |
-
title = "Text Summarization",
|
62 |
-
description = "Summarize text using BART",
|
63 |
-
theme = "huggingface",
|
64 |
-
allow_flagging="never",
|
65 |
-
live=True)
|
66 |
|
67 |
-
|
|
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
import pandas as pd
|
4 |
+
import time # Import time to measure duration
|
5 |
|
6 |
# Load the text summarization pipeline
|
7 |
summarizer = pipeline("summarization", model="astro21/bart-cls_n")
|
8 |
|
9 |
chunk_counter = 0
|
10 |
|
|
|
11 |
def summarize_text(input_text):
|
12 |
global chunk_counter
|
13 |
+
start_time = time.time() # Start timer
|
14 |
|
15 |
+
chunk_counter = 0
|
16 |
max_chunk_size = 1024
|
17 |
chunks = [input_text[i:i + max_chunk_size] for i in range(0, len(input_text), max_chunk_size)]
|
18 |
|
|
|
25 |
summarized_chunk = summarizer(chunk, max_length=128, min_length=64, do_sample=False)[0]['summary_text']
|
26 |
summarized_chunks.append(f"Chunk {chunk_counter}:\n{summarized_chunk}")
|
27 |
summarized_chunks_only.append(summarized_chunk)
|
|
|
28 |
chunk_lengths.append(len(chunk))
|
29 |
|
30 |
summarized_text = "\n".join(summarized_chunks)
|
|
|
34 |
with open("summarized.txt", "w") as output_file:
|
35 |
output_file.write(summarized_text_only)
|
36 |
|
37 |
+
end_time = time.time() # End timer
|
38 |
+
duration = end_time - start_time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
result = f"Summarized in {duration:.2f} seconds:\n{summarized_text}"
|
41 |
+
return result
|
42 |
|
43 |
+
# Define the Gradio interface
|
44 |
+
demo = gr.Interface(fn=summarize_text,
|
45 |
+
inputs=gr.Textbox(label="Enter Text", placeholder="Paste your text here.", lines=10),
|
46 |
+
outputs=gr.Textbox(label="Summarized Text"),
|
47 |
+
title="Text Summarization",
|
48 |
+
description="Paste text and click 'Summarize' to see a summarized version using BART.",
|
49 |
+
theme="huggingface",
|
50 |
+
allow_flagging="never")
|
51 |
|
52 |
+
demo.launch(share=True, debug=True)
|