vikigitonga11 commited on
Commit
364ff78
·
verified ·
1 Parent(s): db15412

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -55
app.py CHANGED
@@ -1,65 +1,51 @@
1
  import gradio as gr
2
- import re
3
- import torch
4
- import asyncio
5
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
6
 
7
- # Load T5 paraphrase model
8
- model_name = "Vamsi/T5_Paraphrase_Paws"
9
- tokenizer = AutoTokenizer.from_pretrained(model_name)
10
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name, torch_dtype=torch.float16)
11
 
12
- # Move model to CPU
13
- model.to("cpu")
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
- # 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,
44
- top_p=0.85,
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)
59
- output_box = gr.Textbox(label="Paraphrased Text", lines=10)
60
- button = gr.Button("Paraphrase")
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
 
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()