Mollymo's picture
Update app.py
fcdbd45 verified
raw
history blame
1.69 kB
import gradio as gr
import torch
from parrot import Parrot
# --- Model initialization ---
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"🧠 Using device: {device}")
parrot = None
try:
print("🔄 Loading Parrot model...")
parrot = Parrot(model_tag="prithivida/parrot_paraphraser_on_T5", use_gpu=torch.cuda.is_available())
print("✅ Parrot model loaded successfully.")
except Exception as e:
print(f"❌ Model load failed: {str(e)}")
# --- Paraphrasing function ---
def paraphrase_text(input_text):
global parrot
if not input_text.strip():
return "⚠️ Please enter text to paraphrase."
if parrot is None:
return "❌ Model not loaded. Please restart the Space."
try:
paraphrases = parrot.augment(
input_phrase=input_text,
diversity_ranker="levenshtein",
do_diverse=True
)
if not paraphrases:
return "No paraphrase generated. Try another input."
result = ""
for i, para_tuple in enumerate(paraphrases, start=1):
result += f"{i}. {para_tuple[0]}\n\n"
return result.strip()
except Exception as e:
return f"⚠️ Error during paraphrasing: {str(e)}"
# --- Interface ---
interface = gr.Interface(
fn=paraphrase_text,
inputs=gr.Textbox(lines=3, placeholder="Enter 1–2 sentences", label="Input Text"),
outputs=gr.Textbox(lines=10, label="Paraphrased Results"),
title="🦜 Parrot Paraphraser",
description="Generate multiple diverse paraphrases using the Parrot T5 model. Ideal for rewording short sentences or phrases."
)
if __name__ == "__main__":
interface.launch()