Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,51 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import torch
|
4 |
-
import asyncio
|
5 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
6 |
|
7 |
-
# Load T5
|
8 |
-
model_name = "
|
9 |
-
tokenizer =
|
10 |
-
model =
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
# Initialize paraphrase pipeline
|
16 |
-
paraphrase_pipeline = pipeline(
|
17 |
-
"text2text-generation",
|
18 |
-
model=model,
|
19 |
-
tokenizer=tokenizer,
|
20 |
-
truncation=True
|
21 |
-
)
|
22 |
-
|
23 |
-
def split_sentences(text):
|
24 |
-
"""Split text into sentences using regex (faster than nltk)."""
|
25 |
-
return re.split(r'(?<=[.!?])\s+', text.strip())
|
26 |
-
|
27 |
-
async def paraphrase_text(text):
|
28 |
-
"""Paraphrases input text asynchronously while maintaining sentence structure."""
|
29 |
if not text.strip():
|
30 |
return "⚠️ Please enter some text to paraphrase."
|
31 |
-
|
32 |
-
sentences = split_sentences(text)
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
formatted_input,
|
41 |
-
max_length=80,
|
42 |
-
do_sample=True,
|
43 |
-
temperature=0.7,
|
44 |
-
top_p=0.85,
|
45 |
top_k=50,
|
46 |
-
|
47 |
-
num_return_sequences=1,
|
48 |
-
|
49 |
)
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
|
65 |
-
demo.queue(concurrency_count=10).launch(share=True) # Allows 10 users to paraphrase simultaneously
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration, pipeline
|
|
|
|
|
|
|
3 |
|
4 |
+
# Load T5-small model and tokenizer
|
5 |
+
model_name = "t5-small"
|
6 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
7 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
8 |
|
9 |
+
def generate_paraphrase(text, max_length, temperature):
|
10 |
+
"""Generate a paraphrased version of the input text using T5-small."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
if not text.strip():
|
12 |
return "⚠️ Please enter some text to paraphrase."
|
|
|
|
|
13 |
|
14 |
+
input_text = f"paraphrase: {text} </s>"
|
15 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
16 |
+
|
17 |
+
output = model.generate(
|
18 |
+
input_ids,
|
19 |
+
max_length=max_length,
|
|
|
|
|
|
|
|
|
|
|
20 |
top_k=50,
|
21 |
+
top_p=0.95,
|
22 |
+
num_return_sequences=1,
|
23 |
+
do_sample=True
|
24 |
)
|
25 |
|
26 |
+
paraphrased_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
27 |
+
return paraphrased_text
|
28 |
+
|
29 |
+
# Define Gradio Interface
|
30 |
+
description = """
|
31 |
+
## ✨ AI Paraphrasing Tool
|
32 |
+
Enter a sentence and let AI generate a paraphrased version!
|
33 |
+
- Adjust **max length** for longer outputs.
|
34 |
+
- Tune **temperature** for more creative results.
|
35 |
+
"""
|
36 |
+
|
37 |
+
demo = gr.Interface(
|
38 |
+
fn=generate_paraphrase,
|
39 |
+
inputs=[
|
40 |
+
gr.Textbox(label="Enter text", placeholder="Type a sentence to paraphrase..."),
|
41 |
+
gr.Slider(20, 100, value=50, step=5, label="Max Output Length"),
|
42 |
+
gr.Slider(0.5, 1.5, value=1.0, step=0.1, label="Creativity (Temperature)"),
|
43 |
+
],
|
44 |
+
outputs=gr.Textbox(label="Paraphrased Text"),
|
45 |
+
title="📝 AI Paraphraser",
|
46 |
+
description=description,
|
47 |
+
theme="huggingface",
|
48 |
+
live=True,
|
49 |
+
)
|
50 |
|
51 |
+
demo.launch()
|
|