Update app.py
Browse files
app.py
CHANGED
@@ -25,15 +25,19 @@ def split_sentences(text):
|
|
25 |
return re.split(r'(?<=[.!?])\s+', text.strip())
|
26 |
|
27 |
async def paraphrase_text(text):
|
28 |
-
"""Paraphrases input text 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 |
max_length=80,
|
38 |
do_sample=True,
|
39 |
temperature=0.7,
|
@@ -41,14 +45,14 @@ async def paraphrase_text(text):
|
|
41 |
top_k=50,
|
42 |
repetition_penalty=1.2,
|
43 |
num_return_sequences=1,
|
44 |
-
batch_size=8
|
45 |
)
|
46 |
|
47 |
# Extract and join paraphrased sentences
|
48 |
-
paraphrased_sentences = [result[
|
49 |
return " ".join(paraphrased_sentences)
|
50 |
|
51 |
-
# Define Gradio Interface
|
52 |
with gr.Blocks() as demo:
|
53 |
gr.Markdown("# 🚀 Fast & Parallel T5 Paraphraser")
|
54 |
input_box = gr.Textbox(label="Enter text", placeholder="Type your text to paraphrase...", lines=10)
|
@@ -57,5 +61,5 @@ with gr.Blocks() as demo:
|
|
57 |
|
58 |
button.click(paraphrase_text, inputs=input_box, outputs=output_box)
|
59 |
|
60 |
-
# ✅ Fix queue issue
|
61 |
-
demo.queue(
|
|
|
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 |
+
# Ensure batch processing
|
35 |
+
formatted_input = [f"paraphrase: {sentence} </s>" for sentence in sentences if sentence]
|
36 |
+
|
37 |
+
# Run in async mode to handle multiple requests in parallel
|
38 |
+
paraphrased_results = await asyncio.to_thread(
|
39 |
+
paraphrase_pipeline,
|
40 |
+
formatted_input,
|
41 |
max_length=80,
|
42 |
do_sample=True,
|
43 |
temperature=0.7,
|
|
|
45 |
top_k=50,
|
46 |
repetition_penalty=1.2,
|
47 |
num_return_sequences=1,
|
48 |
+
batch_size=8 # Batch processing enabled ✅
|
49 |
)
|
50 |
|
51 |
# Extract and join paraphrased sentences
|
52 |
+
paraphrased_sentences = [result["generated_text"] for result in paraphrased_results]
|
53 |
return " ".join(paraphrased_sentences)
|
54 |
|
55 |
+
# Define Gradio Interface with Non-Blocking Requests
|
56 |
with gr.Blocks() as demo:
|
57 |
gr.Markdown("# 🚀 Fast & Parallel T5 Paraphraser")
|
58 |
input_box = gr.Textbox(label="Enter text", placeholder="Type your text to paraphrase...", lines=10)
|
|
|
61 |
|
62 |
button.click(paraphrase_text, inputs=input_box, outputs=output_box)
|
63 |
|
64 |
+
# ✅ Fix queue issue & enable concurrent users
|
65 |
+
demo.queue(concurrency_count=10).launch(share=True) # Allows 10 users to paraphrase simultaneously
|