Spaces:
Sleeping
Sleeping
| 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) | |