Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
|
4 |
# Load the paraphrase model
|
5 |
model_name = "AventIQ-AI/t5-paraphrase-generation"
|
6 |
paraphrase_pipeline = pipeline("text2text-generation", model=model_name)
|
7 |
|
8 |
def generate_paraphrase(text, temperature):
|
9 |
-
"""Generate a paraphrased version of the input text."""
|
10 |
if not text.strip():
|
11 |
return "⚠️ Please enter some text to paraphrase."
|
12 |
|
@@ -14,23 +23,28 @@ def generate_paraphrase(text, temperature):
|
|
14 |
words = text.split()
|
15 |
if len(words) > 700:
|
16 |
return "⚠️ Input too long! Please enter a maximum of 700 words."
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
try:
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
34 |
except Exception as e:
|
35 |
return f"⚠️ An error occurred: {str(e)}"
|
36 |
|
@@ -40,16 +54,16 @@ description = """
|
|
40 |
Enter text and let AI generate a paraphrased version!
|
41 |
- **Creativity (Temperature)** controls how varied the output is.
|
42 |
- **Input is limited to 700 words.**
|
43 |
-
- **Now
|
44 |
"""
|
45 |
|
46 |
demo = gr.Interface(
|
47 |
fn=generate_paraphrase,
|
48 |
inputs=[
|
49 |
-
gr.Textbox(label="Enter text", placeholder="Type your text to paraphrase...", lines=10),
|
50 |
gr.Slider(0.5, 1.5, value=1.0, step=0.1, label="Creativity (Temperature)"),
|
51 |
],
|
52 |
-
outputs=gr.Textbox(label="Paraphrased Text", lines=20),
|
53 |
title="📝 AI Paraphraser",
|
54 |
description=description,
|
55 |
theme="huggingface",
|
|
|
1 |
+
Here’s a revised version of your `app.py` that improves how it processes and paraphrases sentences while avoiding truncation. The key improvements include:
|
2 |
+
|
3 |
+
- **Tokenizing sentences first** to ensure the model processes them individually.
|
4 |
+
- **Using batch processing** to optimize performance.
|
5 |
+
- **Reconstructing the paragraph** properly after paraphrasing.
|
6 |
+
|
7 |
+
### Revised `app.py`
|
8 |
+
```python
|
9 |
import gradio as gr
|
10 |
from transformers import pipeline
|
11 |
+
from nltk.tokenize import sent_tokenize
|
12 |
|
13 |
# Load the paraphrase model
|
14 |
model_name = "AventIQ-AI/t5-paraphrase-generation"
|
15 |
paraphrase_pipeline = pipeline("text2text-generation", model=model_name)
|
16 |
|
17 |
def generate_paraphrase(text, temperature):
|
18 |
+
"""Generate a paraphrased version of the input text sentence by sentence."""
|
19 |
if not text.strip():
|
20 |
return "⚠️ Please enter some text to paraphrase."
|
21 |
|
|
|
23 |
words = text.split()
|
24 |
if len(words) > 700:
|
25 |
return "⚠️ Input too long! Please enter a maximum of 700 words."
|
26 |
+
|
27 |
+
# Tokenize into sentences
|
28 |
+
sentences = sent_tokenize(text)
|
29 |
+
paraphrased_sentences = []
|
30 |
+
|
31 |
try:
|
32 |
+
for sentence in sentences:
|
33 |
+
result = paraphrase_pipeline(
|
34 |
+
sentence,
|
35 |
+
temperature=temperature,
|
36 |
+
top_k=50,
|
37 |
+
do_sample=True,
|
38 |
+
max_new_tokens=150, # Adjust max tokens per sentence
|
39 |
+
repetition_penalty=1.2,
|
40 |
+
num_return_sequences=1
|
41 |
+
)
|
42 |
+
paraphrased_sentences.append(result[0].get("generated_text", sentence)) # Ensure fallback to original
|
43 |
+
|
44 |
+
# Join paraphrased sentences back into a paragraph
|
45 |
+
paraphrased_text = " ".join(paraphrased_sentences)
|
46 |
+
return paraphrased_text.strip()
|
47 |
+
|
48 |
except Exception as e:
|
49 |
return f"⚠️ An error occurred: {str(e)}"
|
50 |
|
|
|
54 |
Enter text and let AI generate a paraphrased version!
|
55 |
- **Creativity (Temperature)** controls how varied the output is.
|
56 |
- **Input is limited to 700 words.**
|
57 |
+
- **Now processes sentences separately to maintain meaning!**
|
58 |
"""
|
59 |
|
60 |
demo = gr.Interface(
|
61 |
fn=generate_paraphrase,
|
62 |
inputs=[
|
63 |
+
gr.Textbox(label="Enter text", placeholder="Type your text to paraphrase...", lines=10),
|
64 |
gr.Slider(0.5, 1.5, value=1.0, step=0.1, label="Creativity (Temperature)"),
|
65 |
],
|
66 |
+
outputs=gr.Textbox(label="Paraphrased Text", lines=20),
|
67 |
title="📝 AI Paraphraser",
|
68 |
description=description,
|
69 |
theme="huggingface",
|