Eaz123 commited on
Commit
a3beb44
·
verified ·
1 Parent(s): 18be27e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -35
app.py CHANGED
@@ -1,43 +1,63 @@
1
  import gradio as gr
2
- import nltk
3
- nltk.download('punkt') # Download necessary data
 
4
 
5
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
 
 
 
 
 
 
 
6
 
7
- # Load model and tokenizer
8
- model_name = "Vamsi/T5_Paraphrase_Paws"
9
- tokenizer = AutoTokenizer.from_pretrained(model_name)
10
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
11
 
12
- def paraphrase(text):
13
  if not text.strip():
14
  return "Please enter some text to paraphrase."
15
- input_text = f"paraphrase: {text} </s>"
16
- encoding = tokenizer.encode_plus(
17
- input_text,
18
- max_length=256,
19
- padding="max_length",
20
- return_tensors="pt",
21
- truncation=True
22
- )
23
- input_ids, attention_mask = encoding["input_ids"], encoding["attention_mask"]
24
- outputs = model.generate(
25
- input_ids=input_ids,
26
- attention_mask=attention_mask,
27
- max_length=256,
28
- num_beams=5,
29
- num_return_sequences=1,
30
- temperature=1.5
31
- )
32
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
33
-
34
- # Gradio Interface
35
- demo = gr.Interface(
36
- fn=paraphrase,
37
- inputs=gr.Textbox(label="Enter your text to paraphrase"),
38
- outputs=gr.Textbox(label="Paraphrased text"),
39
- title="AI Paraphrasing Tool",
40
- description="Enter your sentence or paragraph, and the model will return a paraphrased version."
41
- )
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  demo.launch()
 
1
  import gradio as gr
2
+ from parrot import Parrot
3
+ import torch
4
+ import language_tool_python
5
 
6
+ # Initialize Parrot model
7
+ parrot = Parrot(
8
+ model_tag="prithivida/parrot_paraphraser_on_T5",
9
+ use_gpu=torch.cuda.is_available()
10
+ )
11
+
12
+ # Grammar checker
13
+ tool = language_tool_python.LanguageTool('en-US')
14
 
15
+ def grammar_correct(text):
16
+ """Correct grammar using LanguageTool."""
17
+ matches = tool.check(text)
18
+ return language_tool_python.utils.correct(text, matches)
19
 
20
+ def generate_paraphrase(text: str, do_grammar=True) -> str:
21
  if not text.strip():
22
  return "Please enter some text to paraphrase."
23
+
24
+ try:
25
+ # Generate a paraphrase using Parrot
26
+ paras = parrot.augment(
27
+ input_phrase=text,
28
+ max_return_phrases=1,
29
+ adequacy_threshold=0.9,
30
+ fluency_threshold=0.9,
31
+ do_diverse=False
32
+ )
33
+ if paras and paras[0]:
34
+ output = paras[0][0]
35
+ else:
36
+ output = "Could not generate a meaningful paraphrase."
37
+
38
+ # Apply grammar correction if selected
39
+ if do_grammar:
40
+ output = grammar_correct(output)
41
+
42
+ return output
43
+
44
+ except Exception as e:
45
+ return f"Error during paraphrasing: {str(e)}"
46
+
47
+ # Gradio UI
48
+ with gr.Blocks(css=".gradio-container { max-width: 800px !important; margin: auto; }") as demo:
49
+ gr.Markdown("# ✨ Paraphrasing Tool\nWrite better, clearer, and smarter!")
50
+
51
+ with gr.Row():
52
+ input_text = gr.Textbox(label="Enter your text", placeholder="Type or paste your sentence here...", lines=4)
53
+
54
+ with gr.Row():
55
+ do_grammar = gr.Checkbox(label="Apply grammar correction", value=True)
56
+ run_btn = gr.Button("Paraphrase")
57
+
58
+ with gr.Row():
59
+ output_text = gr.Textbox(label="Paraphrased Output", lines=4)
60
+
61
+ run_btn.click(fn=generate_paraphrase, inputs=[input_text, do_grammar], outputs=output_text)
62
 
63
  demo.launch()