File size: 1,356 Bytes
7fbd2c2
 
9c7c7dd
7fbd2c2
 
4cf87c9
7fbd2c2
9c7c7dd
 
7fbd2c2
9c7c7dd
 
 
 
 
 
 
 
 
 
 
 
30e894a
9c7c7dd
 
 
 
 
30e894a
 
 
 
 
 
 
 
 
9c7c7dd
 
30e894a
7fbd2c2
30e894a
1dc4ba4
9c7c7dd
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
import gradio as gr
from transformers import pipeline
import os

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

chunk_counter = 0

def summarize_text(input_text):
    global chunk_counter  # Use a global variable to keep track of the chunk number
    chunk_counter = 0  # Initialize the chunk counter

    # Split the input text into chunks with a maximum size of 512
    max_chunk_size = 512
    chunks = [input_text[i:i+max_chunk_size] for i in range(0, len(input_text), max_chunk_size)]

    summarized_chunks = []
    for chunk in chunks:
        chunk_counter += 1
        # Summarize each chunk
        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}")

    # Concatenate the summaries
    summarized_text = "\n".join(summarized_chunks)
    return summarized_text

def read_file(file_path):
    with open(file_path, 'r') as file:
        content = file.read()
    return content


def summarize_text_file(file):
    if file is not None:
        content = read_file(file.name)
        return summarize_text(content)

input_type = gr.inputs.File("text")

demo = gr.Interface(fn=summarize_text_file, inputs=input_type, outputs="text", live=True)

demo.launch()