File size: 1,867 Bytes
7fbd2c2
 
272d571
de6eaa0
7fbd2c2
 
19eaa8f
7fbd2c2
9c7c7dd
 
7fbd2c2
272d571
de6eaa0
9c7c7dd
de6eaa0
272d571
 
9c7c7dd
 
272d571
 
 
9c7c7dd
 
 
30e894a
272d571
 
9c7c7dd
 
272d571
 
 
 
 
 
de6eaa0
 
1dc4ba4
de6eaa0
 
8dfca02
de6eaa0
 
 
 
 
 
 
 
8dfca02
de6eaa0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import gradio as gr
from transformers import pipeline
import pandas as pd
import time  # Import time to measure duration

# Load the text summarization pipeline
summarizer = pipeline("summarization", model="astro21/bart-cls_n")

chunk_counter = 0

def summarize_text(input_text):
    global chunk_counter
    start_time = time.time()  # Start timer

    chunk_counter = 0
    max_chunk_size = 1024
    chunks = [input_text[i:i + max_chunk_size] for i in range(0, len(input_text), max_chunk_size)]

    summarized_chunks = []
    chunk_lengths = []
    summarized_chunks_only = []

    for chunk in chunks:
        chunk_counter += 1
        summarized_chunk = summarizer(chunk, max_length=128, min_length=64, do_sample=False)[0]['summary_text']
        summarized_chunks.append(f"Chunk {chunk_counter}:\n{summarized_chunk}")
        summarized_chunks_only.append(summarized_chunk)
        chunk_lengths.append(len(chunk))

    summarized_text = "\n".join(summarized_chunks)
    summarized_text_only = "\n".join(summarized_chunks_only)

    # Save the merged summary to a file
    with open("summarized.txt", "w") as output_file:
        output_file.write(summarized_text_only)

    end_time = time.time()  # End timer
    duration = end_time - start_time

    result = f"Summarized in {duration:.2f} seconds:\n{summarized_text}"
    return result

# Define the Gradio interface
demo = gr.Interface(fn=summarize_text, 
                    inputs=gr.Textbox(label="Enter Text", placeholder="Paste your text here.", lines=10),
                    outputs=gr.Textbox(label="Summarized Text"),
                    title="Text Summarization",
                    description="Paste text and click 'Summarize' to see a summarized version using BART.",
                    theme="huggingface",
                    allow_flagging="never")

demo.launch(share=True, debug=True)