vikigitonga11 commited on
Commit
356817f
·
verified ·
1 Parent(s): 7a0884e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -41
app.py CHANGED
@@ -1,53 +1,53 @@
1
  import gradio as gr
2
- from transformers import T5Tokenizer, T5ForConditionalGeneration
 
 
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, num_outputs):
10
- """Generate paraphrased versions 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
- outputs = model.generate(
18
- input_ids,
19
- max_length=max_length,
20
- top_k=50,
21
- top_p=0.95,
22
- num_return_sequences=num_outputs,
23
- do_sample=True
24
  )
25
 
26
- paraphrased_texts = [tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
27
- return paraphrased_texts # Returns a list of paraphrases
28
 
29
  # Define Gradio Interface
30
- description = """
31
- ## ✨ AI Paraphrasing Tool
32
- Enter a sentence and let AI generate multiple paraphrased versions!
33
- - Adjust **max length** for longer outputs.
34
- - Tune **temperature** for more creative results.
35
- - Choose **number of outputs** to generate multiple variations.
36
- """
37
-
38
  demo = gr.Interface(
39
- fn=generate_paraphrase,
40
- inputs=[
41
- gr.Textbox(label="Enter text", placeholder="Type a sentence to paraphrase..."),
42
- gr.Slider(20, 100, value=50, step=5, label="Max Output Length"),
43
- gr.Slider(0.5, 1.5, value=1.0, step=0.1, label="Creativity (Temperature)"),
44
- gr.Dropdown(choices=[1, 2, 3, 4, 5], value=1, label="Number of Outputs")
45
- ],
46
- outputs=gr.Textbox(label="Paraphrased Text", lines=5), # Allows multiple outputs
47
- title="📝 AI Paraphraser",
48
- description=description,
49
- theme="huggingface",
50
- live=True,
51
  )
52
 
53
- demo.launch()
 
 
1
  import gradio as gr
2
+ import re
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
5
 
6
+ # Load T5-small paraphrase model
7
  model_name = "t5-small"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name, torch_dtype=torch.float16) # Use fp16 for speed
10
+
11
+ # Move model to CPU (remove if using GPU)
12
+ model.to("cpu")
13
+
14
+ # Initialize paraphrase pipeline with optimized settings
15
+ paraphrase_pipeline = pipeline(
16
+ "text2text-generation",
17
+ model=model,
18
+ tokenizer=tokenizer,
19
+ truncation=True
20
+ )
21
+
22
+ def split_sentences(text):
23
+ """Split text into sentences using regex (faster than nltk)."""
24
+ return re.split(r'(?<=[.!?])\s+', text.strip())
25
 
26
+ def paraphrase_text(text):
27
+ """Paraphrases input text while maintaining sentence structure."""
28
  if not text.strip():
29
+ return "⚠️ Please enter some text to paraphrase."
30
+
31
+ sentences = split_sentences(text)
32
+
33
+ # Apply T5 paraphrasing to each sentence
34
+ paraphrased_results = paraphrase_pipeline(
35
+ [f"paraphrase: {sentence} </s>" for sentence in sentences if sentence],
36
+ max_length=50, do_sample=True, batch_size=8, num_return_sequences=1 # Faster settings
 
 
 
 
37
  )
38
 
39
+ paraphrased_sentences = [result['generated_text'] for result in paraphrased_results]
40
+ return " ".join(paraphrased_sentences)
41
 
42
  # Define Gradio Interface
 
 
 
 
 
 
 
 
43
  demo = gr.Interface(
44
+ fn=paraphrase_text,
45
+ inputs=gr.Textbox(label="Enter text", placeholder="Type your text to paraphrase...", lines=10),
46
+ outputs=gr.Textbox(label="Paraphrased Text", lines=10),
47
+ title="🚀 Fast & Clean T5-Small Paraphraser",
48
+ description="Enter text and let AI generate a paraphrased version using an optimized T5-small model!",
49
+ theme="huggingface"
 
 
 
 
 
 
50
  )
51
 
52
+ if __name__ == "__main__":
53
+ demo.launch()