JohnKouf's picture
Update app.py
e417927 verified
raw
history blame
1.71 kB
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()