Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,27 +2,23 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from parrot import Parrot
|
| 4 |
|
| 5 |
-
#
|
| 6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
print(f"🧠 Using device: {device}")
|
| 8 |
|
| 9 |
-
# Initialize model (ensure it's globally available)
|
| 10 |
parrot = None
|
| 11 |
try:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
use_gpu=torch.cuda.is_available()
|
| 15 |
-
)
|
| 16 |
print("✅ Parrot model loaded successfully.")
|
| 17 |
except Exception as e:
|
| 18 |
-
print(f"❌
|
| 19 |
|
| 20 |
-
#
|
| 21 |
def paraphrase_text(input_text):
|
| 22 |
-
global parrot
|
| 23 |
if not input_text.strip():
|
| 24 |
return "⚠️ Please enter text to paraphrase."
|
| 25 |
-
|
| 26 |
if parrot is None:
|
| 27 |
return "❌ Model not loaded. Please restart the Space."
|
| 28 |
|
|
@@ -32,30 +28,21 @@ def paraphrase_text(input_text):
|
|
| 32 |
diversity_ranker="levenshtein",
|
| 33 |
do_diverse=True
|
| 34 |
)
|
| 35 |
-
|
| 36 |
if not paraphrases:
|
| 37 |
-
return "No
|
| 38 |
|
| 39 |
result = ""
|
| 40 |
for i, para_tuple in enumerate(paraphrases, start=1):
|
| 41 |
result += f"{i}. {para_tuple[0]}\n\n"
|
| 42 |
return result.strip()
|
| 43 |
-
|
| 44 |
except Exception as e:
|
| 45 |
return f"⚠️ Error during paraphrasing: {str(e)}"
|
| 46 |
|
| 47 |
-
#
|
| 48 |
interface = gr.Interface(
|
| 49 |
fn=paraphrase_text,
|
| 50 |
-
inputs=gr.Textbox(
|
| 51 |
-
|
| 52 |
-
placeholder="Enter a short sentence (1–2 sentences max)",
|
| 53 |
-
label="Input Text"
|
| 54 |
-
),
|
| 55 |
-
outputs=gr.Textbox(
|
| 56 |
-
lines=10,
|
| 57 |
-
label="Paraphrased Results"
|
| 58 |
-
),
|
| 59 |
title="🦜 Parrot Paraphraser",
|
| 60 |
description="Generate multiple diverse paraphrases using the Parrot T5 model. Ideal for rewording short sentences or phrases."
|
| 61 |
)
|
|
|
|
| 2 |
import torch
|
| 3 |
from parrot import Parrot
|
| 4 |
|
| 5 |
+
# --- Model initialization ---
|
| 6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
print(f"🧠 Using device: {device}")
|
| 8 |
|
|
|
|
| 9 |
parrot = None
|
| 10 |
try:
|
| 11 |
+
print("🔄 Loading Parrot model...")
|
| 12 |
+
parrot = Parrot(model_tag="prithivida/parrot_paraphraser_on_T5", use_gpu=torch.cuda.is_available())
|
|
|
|
|
|
|
| 13 |
print("✅ Parrot model loaded successfully.")
|
| 14 |
except Exception as e:
|
| 15 |
+
print(f"❌ Model load failed: {str(e)}")
|
| 16 |
|
| 17 |
+
# --- Paraphrasing function ---
|
| 18 |
def paraphrase_text(input_text):
|
| 19 |
+
global parrot
|
| 20 |
if not input_text.strip():
|
| 21 |
return "⚠️ Please enter text to paraphrase."
|
|
|
|
| 22 |
if parrot is None:
|
| 23 |
return "❌ Model not loaded. Please restart the Space."
|
| 24 |
|
|
|
|
| 28 |
diversity_ranker="levenshtein",
|
| 29 |
do_diverse=True
|
| 30 |
)
|
|
|
|
| 31 |
if not paraphrases:
|
| 32 |
+
return "No paraphrase generated. Try another input."
|
| 33 |
|
| 34 |
result = ""
|
| 35 |
for i, para_tuple in enumerate(paraphrases, start=1):
|
| 36 |
result += f"{i}. {para_tuple[0]}\n\n"
|
| 37 |
return result.strip()
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
return f"⚠️ Error during paraphrasing: {str(e)}"
|
| 40 |
|
| 41 |
+
# --- Interface ---
|
| 42 |
interface = gr.Interface(
|
| 43 |
fn=paraphrase_text,
|
| 44 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter 1–2 sentences", label="Input Text"),
|
| 45 |
+
outputs=gr.Textbox(lines=10, label="Paraphrased Results"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
title="🦜 Parrot Paraphraser",
|
| 47 |
description="Generate multiple diverse paraphrases using the Parrot T5 model. Ideal for rewording short sentences or phrases."
|
| 48 |
)
|