Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,41 +5,8 @@ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, GenerationConfig
|
|
| 5 |
model = AutoModelForSeq2SeqLM.from_pretrained("vennify/t5-base-grammar-correction")
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained("vennify/t5-base-grammar-correction")
|
| 7 |
|
| 8 |
-
def correct_text(text, max_length, min_length, max_new_tokens, min_new_tokens, num_beams, temperature, top_p):
|
| 9 |
-
inputs = tokenizer.encode("grammar: " + text, return_tensors="pt")
|
| 10 |
-
|
| 11 |
-
if max_new_tokens > 0 or min_new_tokens > 0:
|
| 12 |
-
if max_new_tokens > 0 and min_new_tokens > 0:
|
| 13 |
-
outputs = model.generate(
|
| 14 |
-
inputs,
|
| 15 |
-
max_new_tokens=max_new_tokens,
|
| 16 |
-
min_new_tokens=min_new_tokens,
|
| 17 |
-
num_beams=num_beams,
|
| 18 |
-
temperature=temperature,
|
| 19 |
-
top_p=top_p,
|
| 20 |
-
early_stopping=True,
|
| 21 |
-
do_sample=True
|
| 22 |
-
)
|
| 23 |
-
elif max_new_tokens > 0:
|
| 24 |
-
outputs = model.generate(inputs, max_new_tokens=max_new_tokens, min_length=min_length, num_beams=num_beams, temperature=temperature, top_p=top_p, early_stopping=True, do_sample=True)
|
| 25 |
-
else:
|
| 26 |
-
outputs = model.generate(inputs, max_length=max_length, min_new_tokens=min_new_tokens, num_beams=num_beams, temperature=temperature, top_p=top_p, early_stopping=True, do_sample=True)
|
| 27 |
-
else:
|
| 28 |
-
outputs = model.generate(
|
| 29 |
-
inputs,
|
| 30 |
-
max_length=max_length,
|
| 31 |
-
min_length=min_length,
|
| 32 |
-
num_beams=num_beams,
|
| 33 |
-
temperature=temperature,
|
| 34 |
-
top_p=top_p,
|
| 35 |
-
early_stopping=True,
|
| 36 |
-
do_sample=True
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 40 |
-
yield corrected_text
|
| 41 |
|
| 42 |
-
def
|
| 43 |
inputs = tokenizer.encode("grammar: " + text, return_tensors="pt")
|
| 44 |
outputs = model.generate(inputs, **genConfig.to_dict())
|
| 45 |
|
|
@@ -64,11 +31,9 @@ def respond(text, max_length, min_length, max_new_tokens, min_new_tokens, num_be
|
|
| 64 |
if min_new_tokens > 0:
|
| 65 |
config.min_new_tokens = min_new_tokens
|
| 66 |
|
| 67 |
-
corrected =
|
| 68 |
yield corrected
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
def update_prompt(prompt):
|
| 73 |
return prompt
|
| 74 |
|
|
@@ -87,7 +52,6 @@ with gr.Blocks() as demo:
|
|
| 87 |
samp1.click(update_prompt, samp1, prompt_box)
|
| 88 |
samp2.click(update_prompt, samp2, prompt_box)
|
| 89 |
samp3.click(update_prompt, samp3, prompt_box)
|
| 90 |
-
|
| 91 |
submitBtn = gr.Button("Submit")
|
| 92 |
|
| 93 |
with gr.Accordion("Generation Parameters:", open=False):
|
|
@@ -102,6 +66,6 @@ with gr.Blocks() as demo:
|
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
-
submitBtn.click(respond, [prompt_box, max_length, min_length, max_tokens, min_tokens, num_beams, temperature, top_p], output_box)
|
| 106 |
|
| 107 |
demo.launch()
|
|
|
|
| 5 |
model = AutoModelForSeq2SeqLM.from_pretrained("vennify/t5-base-grammar-correction")
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained("vennify/t5-base-grammar-correction")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
def correct_text(text, genConfig):
|
| 10 |
inputs = tokenizer.encode("grammar: " + text, return_tensors="pt")
|
| 11 |
outputs = model.generate(inputs, **genConfig.to_dict())
|
| 12 |
|
|
|
|
| 31 |
if min_new_tokens > 0:
|
| 32 |
config.min_new_tokens = min_new_tokens
|
| 33 |
|
| 34 |
+
corrected = correct_text(text, config)
|
| 35 |
yield corrected
|
| 36 |
|
|
|
|
|
|
|
| 37 |
def update_prompt(prompt):
|
| 38 |
return prompt
|
| 39 |
|
|
|
|
| 52 |
samp1.click(update_prompt, samp1, prompt_box)
|
| 53 |
samp2.click(update_prompt, samp2, prompt_box)
|
| 54 |
samp3.click(update_prompt, samp3, prompt_box)
|
|
|
|
| 55 |
submitBtn = gr.Button("Submit")
|
| 56 |
|
| 57 |
with gr.Accordion("Generation Parameters:", open=False):
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
+
submitBtn.click(respond, [prompt_box, max_length, min_length, max_tokens, min_tokens, num_beams, temperature, top_k, top_p], output_box)
|
| 70 |
|
| 71 |
demo.launch()
|