Spaces:
Sleeping
Sleeping
File size: 4,695 Bytes
b2fe391 7ed3b9a b2fe391 7ed3b9a b2fe391 75af226 b2fe391 75af226 b2fe391 75af226 b2fe391 75af226 b2fe391 75af226 b2fe391 75af226 b2fe391 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
import sys
import time
from importlib.metadata import version
import torch
import gradio as gr
from transformers import MBartForConditionalGeneration, AutoTokenizer
# Config
model_name = "/home/user/app/best-unlp"
concurrency_limit = 5
device = "cuda" if torch.cuda.is_available() else "cpu"
# Load the model
model = MBartForConditionalGeneration.from_pretrained(
model_name,
low_cpu_mem_usage=True,
device_map=device,
)
model.eval()
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.src_lang = "uk_UA"
tokenizer.tgt_lang = "uk_UA"
examples = [
"привіт як справі?",
"як твої дела?",
]
title = "Grammar Correction for Ukrainian"
# https://www.tablesgenerator.com/markdown_tables
authors_table = """
## Authors
Follow them on social networks and **contact** if you need any help or have any questions:
| <img src="https://avatars.githubusercontent.com/u/7875085?v=4" width="100"> **Yehor Smoliakov** |
|-------------------------------------------------------------------------------------------------|
| https://t.me/smlkw in Telegram |
| https://x.com/yehor_smoliakov at X |
| https://github.com/egorsmkv at GitHub |
| https://huggingface.co/Yehor at Hugging Face |
| or use [email protected] |
""".strip()
description_head = f"""
# {title}
## Overview
This space uses https://huggingface.co/Pravopysnyk/best-unlp model.
Paste the text you want to enhance.
""".strip()
description_foot = f"""
{authors_table}
""".strip()
normalized_text_value = """
Corrected text will appear here.
Choose **an example** below the Correct button or paste **your text**.
""".strip()
tech_env = f"""
#### Environment
- Python: {sys.version}
""".strip()
tech_libraries = f"""
#### Libraries
- torch: {version('torch')}
- gradio: {version('gradio')}
- transformers: {version('transformers')}
""".strip()
def inference(text, progress=gr.Progress()):
if not text:
raise gr.Error("Please paste your text.")
gr.Info("Starting", duration=2)
progress(0, desc="Correcting...")
results = []
sentences = [
text,
]
for sentence in progress.tqdm(sentences, desc="Correcting...", unit="sentence"):
sentence = sentence.strip()
if len(sentence) == 0:
continue
t0 = time.time()
input_text = sentence
encoded_input = tokenizer(
input_text,
return_tensors="pt",
padding=True,
truncation=True,
max_length=1024,
).to(device)
output_ids = model.generate(
**encoded_input, max_length=1024, num_beams=5, early_stopping=True
)
normalized_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
if not normalized_text:
normalized_text = "-"
elapsed_time = round(time.time() - t0, 2)
normalized_text = normalized_text.strip()
results.append(
{
"sentence": sentence,
"normalized_text": normalized_text,
"elapsed_time": elapsed_time,
}
)
gr.Info("Finished!", duration=2)
result_texts = []
for result in results:
result_texts.append(f'> {result["normalized_text"]}')
result_texts.append("\n")
sum_elapsed_text = sum([result["elapsed_time"] for result in results])
result_texts.append(f"Elapsed time: {sum_elapsed_text} seconds")
return "\n".join(result_texts)
demo = gr.Blocks(
title=title,
analytics_enabled=False,
# theme="huggingface",
theme=gr.themes.Base(),
)
with demo:
gr.Markdown(description_head)
gr.Markdown("## Usage")
with gr.Row():
text = gr.Textbox(label="Text", autofocus=True, max_lines=1)
normalized_text = gr.Textbox(
label="Corrected text",
placeholder=normalized_text_value,
show_copy_button=True,
)
gr.Button("Correct").click(
inference,
concurrency_limit=concurrency_limit,
inputs=text,
outputs=normalized_text,
)
with gr.Row():
gr.Examples(label="Choose an example", inputs=text, examples=examples)
gr.Markdown(description_foot)
gr.Markdown("### Gradio app uses:")
gr.Markdown(tech_env)
gr.Markdown(tech_libraries)
if __name__ == "__main__":
demo.queue()
demo.launch()
|