|
Here’s a revised version of your `app.py` that improves how it processes and paraphrases sentences while avoiding truncation. The key improvements include: |
|
|
|
- **Tokenizing sentences first** to ensure the model processes them individually. |
|
- **Using batch processing** to optimize performance. |
|
- **Reconstructing the paragraph** properly after paraphrasing. |
|
|
|
|
|
```python |
|
import gradio as gr |
|
from transformers import pipeline |
|
from nltk.tokenize import sent_tokenize |
|
|
|
|
|
model_name = "AventIQ-AI/t5-paraphrase-generation" |
|
paraphrase_pipeline = pipeline("text2text-generation", model=model_name) |
|
|
|
def generate_paraphrase(text, temperature): |
|
"""Generate a paraphrased version of the input text sentence by sentence.""" |
|
if not text.strip(): |
|
return "⚠️ Please enter some text to paraphrase." |
|
|
|
|
|
words = text.split() |
|
if len(words) > 700: |
|
return "⚠️ Input too long! Please enter a maximum of 700 words." |
|
|
|
|
|
sentences = sent_tokenize(text) |
|
paraphrased_sentences = [] |
|
|
|
try: |
|
for sentence in sentences: |
|
result = paraphrase_pipeline( |
|
sentence, |
|
temperature=temperature, |
|
top_k=50, |
|
do_sample=True, |
|
max_new_tokens=150, |
|
repetition_penalty=1.2, |
|
num_return_sequences=1 |
|
) |
|
paraphrased_sentences.append(result[0].get("generated_text", sentence)) |
|
|
|
|
|
paraphrased_text = " ".join(paraphrased_sentences) |
|
return paraphrased_text.strip() |
|
|
|
except Exception as e: |
|
return f"⚠️ An error occurred: {str(e)}" |
|
|
|
|
|
description = """ |
|
## ✨ AI Paraphrasing Tool |
|
Enter text and let AI generate a paraphrased version! |
|
- **Creativity (Temperature)** controls how varied the output is. |
|
- **Input is limited to 700 words.** |
|
- **Now processes sentences separately to maintain meaning!** |
|
""" |
|
|
|
demo = gr.Interface( |
|
fn=generate_paraphrase, |
|
inputs=[ |
|
gr.Textbox(label="Enter text", placeholder="Type your text to paraphrase...", lines=10), |
|
gr.Slider(0.5, 1.5, value=1.0, step=0.1, label="Creativity (Temperature)"), |
|
], |
|
outputs=gr.Textbox(label="Paraphrased Text", lines=20), |
|
title="📝 AI Paraphraser", |
|
description=description, |
|
theme="huggingface", |
|
live=True, |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|