|
import torch |
|
import gradio as gr |
|
from transformers import pipeline |
|
import logging |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
|
|
|
|
device = 0 if torch.cuda.is_available() else -1 |
|
text_summary = pipeline("summarization", model="facebook/bart-large-cnn", device=device, torch_dtype=torch.bfloat16) |
|
|
|
|
|
def summary(input, summary_type="medium"): |
|
|
|
if not input.strip(): |
|
return "Error: Please provide some text to summarize." |
|
|
|
|
|
input_length = len(input.split()) |
|
logging.info(f"Input length: {input_length} words") |
|
|
|
|
|
if input_length < 10: |
|
return "Error: Input is too short. Please provide at least 10 words." |
|
|
|
|
|
if input_length > 512: |
|
return "Warning: Input exceeds the model's limit of 512 tokens. Please shorten the input text." |
|
|
|
|
|
if summary_type == "short": |
|
max_output_tokens = max(10, input_length // 4) |
|
elif summary_type == "medium": |
|
max_output_tokens = max(20, input_length // 2) |
|
elif summary_type == "long": |
|
max_output_tokens = max(30, (3 * input_length) // 4) |
|
min_output_tokens = max(10, input_length // 6) |
|
|
|
|
|
output = text_summary(input, max_length=max_output_tokens, min_length=min_output_tokens, truncation=True) |
|
return output[0]['summary_text'] |
|
|
|
|
|
def save_output(summary_text): |
|
"""Save the summarized text to a file.""" |
|
with open("summary_output.txt", "w") as file: |
|
file.write(summary_text) |
|
return "Summary saved to 'summary_output.txt'." |
|
|
|
gr.close_all() |
|
|
|
|
|
demo = gr.Interface( |
|
fn=summary, |
|
inputs=[ |
|
gr.Textbox(label="INPUT THE PASSAGE TO SUMMARIZE", lines=15, placeholder="Paste your text here."), |
|
gr.Dropdown(["short", "medium", "long"], label="SUMMARY LENGTH", value="medium") |
|
], |
|
outputs=[ |
|
gr.Textbox(label="SUMMARIZED TEXT", lines=10, placeholder="Your summarized text will appear here."), |
|
gr.Button("Download Summary") |
|
], |
|
title="PAVISHINI @ GenAI Project 1: Text Summarizer", |
|
description=( |
|
"This application summarizes input text. " |
|
"The output length can be short, medium, or long based on your selection." |
|
), |
|
live=True |
|
) |
|
|
|
demo.launch() |
|
|
|
|
|
|