Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,40 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
-
from transformers import
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
if not text.strip():
|
| 12 |
-
return "
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
outputs = model.generate(
|
| 23 |
-
input_ids,
|
| 24 |
max_length=256,
|
| 25 |
-
num_return_sequences=1,
|
| 26 |
num_beams=5,
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
|
|
|
|
| 32 |
iface = gr.Interface(
|
| 33 |
-
fn=
|
| 34 |
-
inputs=gr.Textbox(lines=
|
| 35 |
outputs=gr.Textbox(label="Paraphrased Text"),
|
| 36 |
-
title="Paraphraser
|
| 37 |
-
|
| 38 |
)
|
| 39 |
|
| 40 |
iface.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
| 3 |
|
| 4 |
+
# Load the model
|
| 5 |
+
model_name = "ramsrigouthamg/t5_paraphraser"
|
| 6 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 7 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
|
| 9 |
+
def paraphrase_text(text):
|
| 10 |
if not text.strip():
|
| 11 |
+
return "Please enter some text to paraphrase."
|
| 12 |
+
|
| 13 |
+
# Preprocess input
|
| 14 |
+
input_text = "paraphrase: " + text + " </s>"
|
| 15 |
+
encoding = tokenizer.encode_plus(input_text, max_length=256, padding="max_length", return_tensors="pt", truncation=True)
|
| 16 |
+
|
| 17 |
+
# Generate paraphrased output
|
| 18 |
+
generated_ids = model.generate(
|
| 19 |
+
input_ids=encoding["input_ids"],
|
| 20 |
+
attention_mask=encoding["attention_mask"],
|
|
|
|
|
|
|
| 21 |
max_length=256,
|
|
|
|
| 22 |
num_beams=5,
|
| 23 |
+
num_return_sequences=1,
|
| 24 |
+
no_repeat_ngram_size=2,
|
| 25 |
+
early_stopping=True
|
| 26 |
)
|
| 27 |
|
| 28 |
+
paraphrased_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 29 |
+
return paraphrased_text
|
| 30 |
|
| 31 |
+
# Gradio Interface
|
| 32 |
iface = gr.Interface(
|
| 33 |
+
fn=paraphrase_text,
|
| 34 |
+
inputs=gr.Textbox(lines=6, placeholder="Enter your text here...", label="Original Text"),
|
| 35 |
outputs=gr.Textbox(label="Paraphrased Text"),
|
| 36 |
+
title="AI Paraphraser",
|
| 37 |
+
description="An advanced paraphrasing tool using the T5 transformer model."
|
| 38 |
)
|
| 39 |
|
| 40 |
iface.launch()
|