Eaz123 commited on
Commit
658ed6e
·
verified ·
1 Parent(s): da8ccad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -25
app.py CHANGED
@@ -1,30 +1,40 @@
 
 
1
  import gradio as gr
2
- from transformers import T5Tokenizer, T5ForConditionalGeneration
3
 
4
- # Load pretrained T5 model for paraphrasing
5
  model_name = "Vamsi/T5_Paraphrase_Paws"
6
- tokenizer = T5Tokenizer.from_pretrained(model_name)
7
- model = T5ForConditionalGeneration.from_pretrained(model_name)
8
-
9
- def paraphrase_text(input_text):
10
- # Prepare input text
11
- input_ids = tokenizer.encode("paraphrase: " + input_text + " </s>", return_tensors="pt", max_length=512, truncation=True)
12
-
13
- # Generate paraphrase
14
- outputs = model.generate(input_ids, max_length=256, num_return_sequences=1, temperature=1.5)
15
-
16
- # Decode output
17
- paraphrased_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
18
- return paraphrased_text
19
-
20
- # Create Gradio interface
21
- demo = gr.Interface(
22
- fn=paraphrase_text,
23
- inputs=gr.Textbox(lines=5, placeholder="Enter text to paraphrase..."),
24
- outputs="text",
25
- title="AI Paraphraser",
26
- description="Paste any English text to get a paraphrased version using T5 model."
 
 
 
 
 
 
 
 
 
 
27
  )
28
 
29
- # Expose /run/predict endpoint
30
- demo.launch()
 
1
+ # app.py
2
+
3
  import gradio as gr
4
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
 
 
6
  model_name = "Vamsi/T5_Paraphrase_Paws"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
9
+
10
+ def paraphrase(text):
11
+ if not text.strip():
12
+ return "No input provided."
13
+
14
+ input_ids = tokenizer(
15
+ "paraphrase: " + text,
16
+ return_tensors="pt",
17
+ padding="longest",
18
+ truncation=True,
19
+ max_length=256,
20
+ ).input_ids
21
+
22
+ outputs = model.generate(
23
+ input_ids,
24
+ max_length=256,
25
+ num_return_sequences=1,
26
+ num_beams=5,
27
+ temperature=1.5,
28
+ )
29
+
30
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
31
+
32
+ iface = gr.Interface(
33
+ fn=paraphrase,
34
+ inputs=gr.Textbox(lines=4, label="Enter Text to Paraphrase"),
35
+ outputs=gr.Textbox(label="Paraphrased Text"),
36
+ title="Paraphraser Tool",
37
+ allow_flagging="never",
38
  )
39
 
40
+ iface.launch()