vikigitonga11 commited on
Commit
db15412
·
verified ·
1 Parent(s): 27c456c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -10
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 asynchronously."""
29
  if not text.strip():
30
  return "⚠️ Please enter some text to paraphrase."
31
 
32
  sentences = split_sentences(text)
33
-
34
- # Apply T5 paraphrasing with optimized settings
35
- paraphrased_results = await asyncio.to_thread(paraphrase_pipeline,
36
- [f"paraphrase: {sentence} </s>" for sentence in sentences if sentence],
 
 
 
 
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['generated_text'] for result in paraphrased_results]
49
  return " ".join(paraphrased_sentences)
50
 
51
- # Define Gradio Interface (Disable queueing)
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(max_size=None).launch(share=True) # Corrected way to disable queueing
 
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