File size: 1,686 Bytes
0e2fb0b
b37e595
20793f6
0e2fb0b
fcdbd45
20793f6
22d2933
0e2fb0b
22d2933
20793f6
fcdbd45
 
20793f6
 
fcdbd45
20793f6
fcdbd45
b37e595
fcdbd45
20793f6
 
22d2933
 
 
20793f6
 
 
 
 
 
 
fcdbd45
20793f6
5af7486
20793f6
 
5af7486
20793f6
 
 
fcdbd45
b37e595
 
fcdbd45
 
20793f6
 
b37e595
 
 
20793f6
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
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()