Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,72 @@
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Use a pipeline as a high-level helper
|
6 |
device = 0 if torch.cuda.is_available() else -1
|
7 |
-
text_summary = pipeline("summarization", model="
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
# Calculate the number of tokens based on input length
|
11 |
input_length = len(input.split())
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
14 |
|
15 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
output = text_summary(input, max_length=max_output_tokens, min_length=min_output_tokens, truncation=True)
|
17 |
return output[0]['summary_text']
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
gr.close_all()
|
20 |
|
21 |
# Create the Gradio interface
|
22 |
demo = gr.Interface(
|
23 |
fn=summary,
|
24 |
-
inputs=[
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
title="PAVISHINI @ GenAI Project 1: Text Summarizer",
|
27 |
-
description=
|
|
|
|
|
|
|
|
|
28 |
)
|
29 |
|
30 |
demo.launch()
|
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
from transformers import pipeline
|
4 |
+
import logging
|
5 |
+
|
6 |
+
# Set up logging
|
7 |
+
logging.basicConfig(level=logging.INFO)
|
8 |
|
9 |
# Use a pipeline as a high-level helper
|
10 |
device = 0 if torch.cuda.is_available() else -1
|
11 |
+
text_summary = pipeline("summarization", model="facebook/bart-large-cnn", device=device, torch_dtype=torch.bfloat16)
|
12 |
+
|
13 |
+
# Function for summarization with enhancements
|
14 |
+
def summary(input, summary_type="medium"):
|
15 |
+
# Check for empty input
|
16 |
+
if not input.strip():
|
17 |
+
return "Error: Please provide some text to summarize."
|
18 |
|
19 |
+
# Calculate input length
|
|
|
20 |
input_length = len(input.split())
|
21 |
+
logging.info(f"Input length: {input_length} words")
|
22 |
+
|
23 |
+
# Handle input that's too short
|
24 |
+
if input_length < 10:
|
25 |
+
return "Error: Input is too short. Please provide at least 10 words."
|
26 |
|
27 |
+
# Handle input that's too long for the model
|
28 |
+
if input_length > 512:
|
29 |
+
return "Warning: Input exceeds the model's limit of 512 tokens. Please shorten the input text."
|
30 |
+
|
31 |
+
# Adjust max/min lengths based on the summary type
|
32 |
+
if summary_type == "short":
|
33 |
+
max_output_tokens = max(10, input_length // 4)
|
34 |
+
elif summary_type == "medium":
|
35 |
+
max_output_tokens = max(20, input_length // 2)
|
36 |
+
elif summary_type == "long":
|
37 |
+
max_output_tokens = max(30, (3 * input_length) // 4)
|
38 |
+
min_output_tokens = max(10, input_length // 6)
|
39 |
+
|
40 |
+
# Generate summary
|
41 |
output = text_summary(input, max_length=max_output_tokens, min_length=min_output_tokens, truncation=True)
|
42 |
return output[0]['summary_text']
|
43 |
|
44 |
+
# Gradio interface
|
45 |
+
def save_output(summary_text):
|
46 |
+
"""Save the summarized text to a file."""
|
47 |
+
with open("summary_output.txt", "w") as file:
|
48 |
+
file.write(summary_text)
|
49 |
+
return "Summary saved to 'summary_output.txt'."
|
50 |
+
|
51 |
gr.close_all()
|
52 |
|
53 |
# Create the Gradio interface
|
54 |
demo = gr.Interface(
|
55 |
fn=summary,
|
56 |
+
inputs=[
|
57 |
+
gr.Textbox(label="INPUT THE PASSAGE TO SUMMARIZE", lines=15, placeholder="Paste your text here."),
|
58 |
+
gr.Dropdown(["short", "medium", "long"], label="SUMMARY LENGTH", value="medium")
|
59 |
+
],
|
60 |
+
outputs=[
|
61 |
+
gr.Textbox(label="SUMMARIZED TEXT", lines=10, placeholder="Your summarized text will appear here."),
|
62 |
+
gr.Button("Download Summary")
|
63 |
+
],
|
64 |
title="PAVISHINI @ GenAI Project 1: Text Summarizer",
|
65 |
+
description=(
|
66 |
+
"This application summarizes input text. "
|
67 |
+
"The output length can be short, medium, or long based on your selection."
|
68 |
+
),
|
69 |
+
live=True
|
70 |
)
|
71 |
|
72 |
demo.launch()
|