File size: 1,709 Bytes
945bbd0
 
8a30555
155660f
 
 
1b33daa
 
 
 
e417927
a162763
 
 
 
 
a77cc2d
a162763
a77cc2d
 
 
a162763
 
a77cc2d
 
 
 
1b33daa
155660f
 
 
 
 
 
 
 
 
 
 
c165e9b
155660f
 
36f3185
 
155660f
d7ea8b9
155660f
 
6154d1b
155660f
1b33daa
945bbd0
1b33daa
 
945bbd0
1b33daa
 
945bbd0
 
1b33daa
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import gradio as gr
from transformers import pipeline
import re
from transformers import AutoTokenizer
from transformers import AutoModelForSeq2SeqLM


# Load the model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("kriton/greek-text-summarization")
model = AutoModelForSeq2SeqLM.from_pretrained("kriton/greek-text-summarization")
generator = pipeline("summarization", model="kriton/greek-text-summarization")



#remove incomplete sentences from the output
def remove_incomplete_sentence(text):
    sentence_endings = r'[.!?;]'
    
    sentences = re.split(r'(?<=[.!?;])\s+', text.strip())
    
    if len(sentences) == 0 or len(sentences) == 1:
        return text.strip()

    if re.match(f'.*[{sentence_endings}]$', sentences[-1]):
        return text.strip()  
    return ' '.join(sentences[:-1]).strip()

# Define the summary generation function
def genarate_summary(article):
    inputs = tokenizer(
        'summarize: ' + article, 
        return_tensors="pt", 
        max_length=1024, 
        truncation=True,
        padding="max_length",
    )

    outputs = model.generate(
        inputs["input_ids"], 
        max_length=1024, 
        min_length=130, 
        length_penalty=3.0, 
        num_beams=8, 
        early_stopping=True,
        repetition_penalty=3.0,
        no_repeat_ngram_size=3
    )

    return remove_incomplete_sentence(tokenizer.decode(outputs[0], skip_special_tokens=True))

# Set up Gradio Interface
iface = gr.Interface(
    fn=genarate_summary, 
    inputs="text", 
    outputs="text",
    title="Greek Text Summarizer",
    description="Enter an article in Greek, and this tool will generate a summary."
)

# Launch the Gradio Interface
iface.launch()