Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,20 +5,25 @@ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
|
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, num_beams, temperature, top_p):
|
9 |
inputs = tokenizer.encode("grammar: " + text, return_tensors="pt")
|
10 |
|
11 |
-
if max_new_tokens > 0:
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
22 |
else:
|
23 |
outputs = model.generate(
|
24 |
inputs,
|
@@ -27,19 +32,13 @@ def correct_text(text, max_length, min_length, max_new_tokens, num_beams, temper
|
|
27 |
num_beams=num_beams,
|
28 |
temperature=temperature,
|
29 |
top_p=top_p,
|
30 |
-
early_stopping=True
|
|
|
31 |
)
|
32 |
|
33 |
corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
34 |
yield corrected_text
|
35 |
|
36 |
-
def respond(prompt, max_length, min_length, max_new_tokens, num_beams, temperature, top_p):
|
37 |
-
# Your response generation logic here
|
38 |
-
#response = correct_text(message, max_length, max_new_tokens, min_length, num_beams, temperature, top_p)
|
39 |
-
#yield response
|
40 |
-
#return f"System message: {system_message}, Max Length: {max_length}, Min Length: {min_length}, Max new tokens: {max_new_tokens}, Num Beams: {num_beams}, Temperature: {temperature}, Top-p: {top_p}"
|
41 |
-
#response = correct_text(prompt, max_length, max_new_tokens, min_length, num_beams, temperature, top_p)
|
42 |
-
return prompt
|
43 |
|
44 |
def update_prompt(prompt):
|
45 |
return prompt
|
@@ -68,14 +67,14 @@ with gr.Blocks() as demo:
|
|
68 |
with gr.Accordion("Generation Parameters:", open=False):
|
69 |
max_length = gr.Slider(minimum=1, maximum=256, value=80, step=1, label="Max Length")
|
70 |
min_length = gr.Slider(minimum=1, maximum=256, value=0, step=1, label="Min Length")
|
71 |
-
max_tokens = gr.Slider(minimum=0, maximum=256, value=0, step=1, label="Max
|
|
|
72 |
num_beams = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Num Beams")
|
73 |
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
74 |
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
75 |
|
76 |
|
77 |
-
|
78 |
-
submitBtn.click(correct_text, [prompt_box, max_length, min_length, max_tokens, num_beams, temperature, top_p], output_box)
|
79 |
|
80 |
|
81 |
|
|
|
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,
|
|
|
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 |
|
43 |
def update_prompt(prompt):
|
44 |
return prompt
|
|
|
67 |
with gr.Accordion("Generation Parameters:", open=False):
|
68 |
max_length = gr.Slider(minimum=1, maximum=256, value=80, step=1, label="Max Length")
|
69 |
min_length = gr.Slider(minimum=1, maximum=256, value=0, step=1, label="Min Length")
|
70 |
+
max_tokens = gr.Slider(minimum=0, maximum=256, value=0, step=1, label="Max New Tokens")
|
71 |
+
min_tokens = gr.Slider(minimum=0, maximum=256, value=0, step=1, label="Min New Tokens")
|
72 |
num_beams = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Num Beams")
|
73 |
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
74 |
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
75 |
|
76 |
|
77 |
+
submitBtn.click(correct_text, [prompt_box, max_length, min_length, max_tokens, min_tokens, num_beams, temperature, top_p], output_box)
|
|
|
78 |
|
79 |
|
80 |
|