Sibinraj's picture
Update app.py
132b757 verified
raw
history blame contribute delete
No virus
5.02 kB
import torch
import gradio as gr
from transformers import T5ForConditionalGeneration, T5Tokenizer
import fitz # PyMuPDF for extracting text from PDF
# Load the model and tokenizer
model_path = 'Sibinraj/T5-finetuned-dialogue_sumxx'
model = T5ForConditionalGeneration.from_pretrained(model_path)
tokenizer = T5Tokenizer.from_pretrained(model_path)
def extract_text_from_pdf(pdf_path):
text = ""
with fitz.open(pdf_path) as doc:
for page in doc:
text += page.get_text()
return text
def summarize_text(text, max_length, show_length):
inputs = tokenizer.encode(
"summarize: " + text,
return_tensors='pt',
max_length=512,
truncation=True,
padding='max_length'
)
summary_ids = model.generate(
inputs,
max_length=max_length + 20, # Allow some buffer
min_length=10, # Set a reasonable minimum length
num_beams=5,
no_repeat_ngram_size=2,
early_stopping=True
)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
summary_words = summary.split()
print(summary_words)
if len(summary_words) > max_length:
summary = ' '.join(summary_words[:max_length])
elif len(summary_words) < max_length:
additional_tokens = model.generate(
tokenizer.encode(" ".join(summary_words), return_tensors='pt'),
max_length=max_length - len(summary_words) + len(summary_words),
min_length=max_length - len(summary_words) + len(summary_words),
num_beams=5,
no_repeat_ngram_size=2,
early_stopping=True
)
additional_summary = tokenizer.decode(additional_tokens[0], skip_special_tokens=True)
summary += ' ' + ' '.join(additional_summary.split()[len(summary_words):max_length])
if show_length:
summary_length = len(summary.split())
summary = f"{summary}\n\n(Summary length: {summary_length} words)"
return summary
def handle_input(input_type, text, pdf, max_length, show_length):
if input_type == 'Text':
return summarize_text(text, max_length, show_length)
elif input_type == 'PDF':
extracted_text = extract_text_from_pdf(pdf.name)
return summarize_text(extracted_text, max_length, show_length)
# Define examples for text input
examples_text = [
['Text', 'The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.', None, 50, 50],
['Text', '#Person1#: Is this the workshop to prepare for an interview? #Person2#: This is the interview class. Welcome to our class. #Person1#: I am really excited to be taking this workshop so that I can get ready for my interview next week. #Person2#: We are all learning things that will help us in our interview. What do you think are some important considerations going into your interview? #Person1#: I think that we should dress neatly and appropriately. #Person2#: Yes. Second, as you can imagine, attitude and friendliness go a long way. #Person1#: Yes, and I always feel much better when I am friendly. #Person2#: Believe it or not, the interviewers are as interested in your questions as they are in your answers. #Person1#: Any more hints as to what I should do in an interview? #Person2#: Always be honest with your answers. The interviewers really do want to know if you will be a good fit for them.', None, 50, 50]
]
# Define the Gradio interface
interface = gr.Interface(
fn=handle_input,
inputs=[
gr.Radio(['Text', 'PDF'], label='Input Type', type='value'),
gr.Textbox(lines=10, placeholder='Enter Text Here...', label='Input Text', visible=True),
gr.File(label='Upload PDF', type='filepath', visible=True),
gr.Slider(minimum=10, maximum=150, step=1, label='Max Length'),
gr.Checkbox(label='Show summary length', value=False)
],
outputs=gr.Textbox(label='Summarized Text'),
title='Text or PDF Summarizer using T5-finetuned-dialogue_sumxx',
examples=examples_text
)
# Launch the Gradio interface
interface.launch()