Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,38 @@
|
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=
|
|
|
|
| 23 |
num_return_sequences=1,
|
| 24 |
no_repeat_ngram_size=2,
|
| 25 |
early_stopping=True
|
|
@@ -28,13 +41,29 @@ def paraphrase_text(text):
|
|
| 28 |
paraphrased_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 29 |
return paraphrased_text
|
| 30 |
|
| 31 |
-
# Gradio
|
| 32 |
-
|
| 33 |
fn=paraphrase_text,
|
| 34 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
# Load the model
|
| 7 |
model_name = "ramsrigouthamg/t5_paraphraser"
|
| 8 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 9 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 10 |
|
| 11 |
+
def paraphrase_text(text, creativity_level=3, tone="neutral"):
|
| 12 |
if not text.strip():
|
| 13 |
return "Please enter some text to paraphrase."
|
| 14 |
|
| 15 |
+
# Adjust generation parameters based on creativity level
|
| 16 |
+
num_beams = 3 + creativity_level # Scale beams with creativity (3-8)
|
| 17 |
+
temperature = 0.7 + (creativity_level * 0.15) # Scale temperature (0.7-1.45)
|
| 18 |
+
|
| 19 |
# Preprocess input
|
| 20 |
input_text = "paraphrase: " + text + " </s>"
|
| 21 |
+
encoding = tokenizer.encode_plus(
|
| 22 |
+
input_text,
|
| 23 |
+
max_length=256,
|
| 24 |
+
padding="max_length",
|
| 25 |
+
return_tensors="pt",
|
| 26 |
+
truncation=True
|
| 27 |
+
)
|
| 28 |
|
| 29 |
# Generate paraphrased output
|
| 30 |
generated_ids = model.generate(
|
| 31 |
input_ids=encoding["input_ids"],
|
| 32 |
attention_mask=encoding["attention_mask"],
|
| 33 |
max_length=256,
|
| 34 |
+
num_beams=num_beams,
|
| 35 |
+
temperature=temperature,
|
| 36 |
num_return_sequences=1,
|
| 37 |
no_repeat_ngram_size=2,
|
| 38 |
early_stopping=True
|
|
|
|
| 41 |
paraphrased_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 42 |
return paraphrased_text
|
| 43 |
|
| 44 |
+
# Create Gradio interface
|
| 45 |
+
demo = gr.Interface(
|
| 46 |
fn=paraphrase_text,
|
| 47 |
+
inputs=[
|
| 48 |
+
gr.Textbox(lines=6, placeholder="Enter your text here...", label="Original Text"),
|
| 49 |
+
gr.Slider(1, 5, value=3, label="Creativity Level"),
|
| 50 |
+
gr.Dropdown(["neutral", "formal", "casual", "academic"], value="neutral", label="Output Tone")
|
| 51 |
+
],
|
| 52 |
outputs=gr.Textbox(label="Paraphrased Text"),
|
| 53 |
title="AI Paraphraser",
|
| 54 |
description="An advanced paraphrasing tool using the T5 transformer model."
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# Create FastAPI app and mount Gradio app
|
| 58 |
+
app = FastAPI()
|
| 59 |
+
|
| 60 |
+
@app.get("/")
|
| 61 |
+
async def root():
|
| 62 |
+
return {"message": "Paraphraser API is running"}
|
| 63 |
+
|
| 64 |
+
# Mount Gradio app
|
| 65 |
+
app = gr.mount_gradio_app(app, demo, path="/gradio")
|
| 66 |
+
|
| 67 |
+
# For direct python execution
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
demo.launch()
|