vikigitonga11's picture
Update app.py
3a56540 verified
raw
history blame
1.89 kB
import gradio as gr
import re
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
# Load FLAN-T5 Large model
model_name = "google/flan-t5-large"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name, torch_dtype=torch.float16) # Use float16 for efficiency
# Move model to CPU (Change to "cuda" if using GPU)
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
# Initialize paraphrase pipeline
paraphrase_pipeline = pipeline(
"text2text-generation",
model=model,
tokenizer=tokenizer,
truncation=True,
device=0 if device == "cuda" else -1 # Use GPU if available
)
def split_sentences(text):
"""Split text into sentences using regex."""
return re.split(r'(?<=[.!?])\s+', text.strip())
def paraphrase_text(text):
"""Paraphrases input text while maintaining sentence structure."""
if not text.strip():
return "⚠️ Please enter some text to paraphrase."
sentences = split_sentences(text)
# Apply FLAN-T5 paraphrasing to each sentence
paraphrased_results = paraphrase_pipeline(
[f"Rephrase this sentence: {sentence}" for sentence in sentences if sentence],
max_length=50, do_sample=True, batch_size=8, num_return_sequences=1
)
paraphrased_sentences = [result['generated_text'] for result in paraphrased_results]
return " ".join(paraphrased_sentences)
# Define Gradio Interface
demo = gr.Interface(
fn=paraphrase_text,
inputs=gr.Textbox(label="Enter text", placeholder="Type your text to paraphrase...", lines=10),
outputs=gr.Textbox(label="Paraphrased Text", lines=10),
title="🚀 FLAN-T5 Paraphraser",
description="Enter text and let AI generate a paraphrased version using FLAN-T5-Large!",
theme="huggingface"
)
if __name__ == "__main__":
demo.launch()