File size: 1,420 Bytes
675db3d
 
 
8f5ec2e
675db3d
34f7b43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
675db3d
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import torch
import sys

st.title("Dialog Summarizer App")

# User input
user_input = st.text_area("Enter the dialog:")

# Add "Summarize" and "Clear" buttons
summarize_button = st.button("Summarize")
clear_button = st.button("Clear")

# If "Clear" button is clicked, clear the user input
if clear_button:
    user_input = ""

# "Summarize" button and user input, generate and display summary
if summarize_button and user_input:
    # Load pre-trained Pegasus model and tokenizer
    model_name = "ale-dp/pegasus-finetuned-dialog-summarizer"
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForSeq2SeqLM.from_pretrained(model_name)

    # Generate summary
    summary = generate_summary(model, tokenizer, user_input)

    # Display the generated summary
    st.subheader("Generated Summary:")
    st.write(summary)

def generate_summary(model, tokenizer, dialogue):
    # Tokenize input dialogue
    inputs = tokenizer(dialogue, return_tensors="pt", max_length=1024, truncation=True)

    # Generate summary
    with torch.no_grad():
        summary_ids = model.generate(inputs["input_ids"], max_length=150, length_penalty=0.8, num_beams=4)

    # Decode and return the summary
    summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
    return summary