File size: 1,675 Bytes
9163e4e
be43a82
 
 
 
9163e4e
 
 
be43a82
9163e4e
 
be43a82
 
9163e4e
be43a82
 
 
 
 
 
 
 
 
 
9163e4e
 
be43a82
 
9163e4e
 
be43a82
 
9163e4e
 
 
 
be43a82
 
9163e4e
be43a82
 
 
 
 
 
 
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
46
import os
import io
from IPython.display import Image, display, HTML
from PIL import Image
import base64
from transformers import pipeline, AutoTokenizer

# Initialize summarizer and tokenizer
summarizer = pipeline ("summarization", model="sshleifer/distilbart-cnn-12-6", tokenizer="sshleifer/distilbart-cnn-12-6")
tokenizer = AutoTokenizer.from_pretrained("sshleifer/distilbart-cnn-12-6")

import json

def summarize_text(input_text):
    """Summarizes the given input text.

    Args:
        input_text (str): The text to be summarized.

    Returns:
        dict: A dictionary containing the summary under the 'summary' key.
    """

    # Tokenize and truncate input if necessary
    max_length = tokenizer.model_max_length
    inputs = tokenizer(input_text, truncation=True, max_length=max_length, return_tensors="pt")

    # Generate summary
    summary_ids = summarizer.model.generate(inputs.input_ids, max_length=50, min_length=10, do_sample=False)
    summary_text = tokenizer.decode(summary_ids[0], skip_special_tokens=True)

    # Return summary as a dictionary
    return {"summary": summary_text}

def generate_summary(input):
    output = summarize_text(input)
    return output
    
gr.close_all()
demo = gr.Interface(fn=generate_summary, 
                        inputs=[gr.Textbox(label="Text to summarize", lines=6)], 
                        outputs=[gr.Textbox(label="Summary", lines=3)],
                        title="Text Summarization",
                        description="Summarize text using the 'shleifer/distilbart-cnn-12-6' language model.",                    
                    )
demo.launch(share=True, server_port=int(os.environ['PORT']))