Spaces:
No application file
No application file
Update Text_Summary_app.py
Browse files- Text_Summary_app.py +30 -14
Text_Summary_app.py
CHANGED
|
@@ -1,30 +1,46 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from transformers import pipeline, AutoTokenizer
|
| 4 |
|
| 5 |
# Initialize summarizer and tokenizer
|
| 6 |
-
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", tokenizer="sshleifer/distilbart-cnn-12-6")
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained("sshleifer/distilbart-cnn-12-6")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
def summarize_text(input_text):
|
| 10 |
-
"""Summarizes the given input text.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
max_length = tokenizer.model_max_length
|
| 12 |
inputs = tokenizer(input_text, truncation=True, max_length=max_length, return_tensors="pt")
|
|
|
|
|
|
|
| 13 |
summary_ids = summarizer.model.generate(inputs.input_ids, max_length=50, min_length=10, do_sample=False)
|
| 14 |
summary_text = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 15 |
return {"summary": summary_text}
|
| 16 |
|
| 17 |
def generate_summary(input):
|
| 18 |
output = summarize_text(input)
|
| 19 |
-
return output
|
| 20 |
-
|
| 21 |
gr.close_all()
|
| 22 |
-
demo = gr.Interface(
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import io
|
| 3 |
+
from IPython.display import Image, display, HTML
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import base64
|
| 6 |
from transformers import pipeline, AutoTokenizer
|
| 7 |
|
| 8 |
# Initialize summarizer and tokenizer
|
| 9 |
+
summarizer = pipeline ("summarization", model="sshleifer/distilbart-cnn-12-6", tokenizer="sshleifer/distilbart-cnn-12-6")
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained("sshleifer/distilbart-cnn-12-6")
|
| 11 |
|
| 12 |
+
import json
|
| 13 |
+
|
| 14 |
def summarize_text(input_text):
|
| 15 |
+
"""Summarizes the given input text.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
input_text (str): The text to be summarized.
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
dict: A dictionary containing the summary under the 'summary' key.
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
# Tokenize and truncate input if necessary
|
| 25 |
max_length = tokenizer.model_max_length
|
| 26 |
inputs = tokenizer(input_text, truncation=True, max_length=max_length, return_tensors="pt")
|
| 27 |
+
|
| 28 |
+
# Generate summary
|
| 29 |
summary_ids = summarizer.model.generate(inputs.input_ids, max_length=50, min_length=10, do_sample=False)
|
| 30 |
summary_text = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 31 |
+
|
| 32 |
+
# Return summary as a dictionary
|
| 33 |
return {"summary": summary_text}
|
| 34 |
|
| 35 |
def generate_summary(input):
|
| 36 |
output = summarize_text(input)
|
| 37 |
+
return output
|
| 38 |
+
|
| 39 |
gr.close_all()
|
| 40 |
+
demo = gr.Interface(fn=generate_summary,
|
| 41 |
+
inputs=[gr.Textbox(label="Text to summarize", lines=6)],
|
| 42 |
+
outputs=[gr.Textbox(label="Summary", lines=3)],
|
| 43 |
+
title="Text Summarization",
|
| 44 |
+
description="Summarize text using the 'shleifer/distilbart-cnn-12-6' language model.",
|
| 45 |
+
)
|
| 46 |
+
demo.launch(share=True, server_port=int(os.environ['PORT']))
|
|
|
|
|
|