Spaces:
Running
on
Zero
Running
on
Zero
File size: 12,262 Bytes
af58cc7 7653a75 af58cc7 770c54a af58cc7 770c54a 37b63ef af58cc7 26a7c02 770c54a 2200921 770c54a af58cc7 770c54a 7653a75 770c54a 7653a75 770c54a 7653a75 770c54a 7653a75 af58cc7 7370121 af58cc7 2200921 770c54a af58cc7 55e8ee9 af58cc7 e0101eb 7653a75 af58cc7 770c54a 2200921 70c32dd 770c54a 70c32dd 770c54a 26a7c02 770c54a 47390d1 af58cc7 770c54a af58cc7 47390d1 af58cc7 7d5720e af58cc7 b0eb535 770c54a 483a346 c53a7a8 af58cc7 770c54a b0eb535 770c54a b0eb535 af58cc7 770c54a 37b63ef 5f9ec7e 770c54a af58cc7 7bdd58e af58cc7 770c54a af58cc7 2e99c9d af58cc7 770c54a af58cc7 37b63ef 770c54a 3a9d25c 54f0752 770c54a 2200921 770c54a 2200921 770c54a 2200921 770c54a 2200921 770c54a 2200921 770c54a 2200921 f12f606 770c54a 2200921 770c54a 2200921 770c54a 37b63ef af58cc7 6ba5eff af58cc7 37b63ef af58cc7 770c54a af58cc7 770c54a af58cc7 020def9 770c54a 26a7c02 020def9 770c54a 6ba5eff 020def9 af58cc7 770c54a 37b63ef 770c54a 54f0752 770c54a af58cc7 770c54a 54f0752 770c54a 5e36c08 770c54a 37b63ef 770c54a 7806982 770c54a 54f0752 770c54a af58cc7 770c54a 2200921 020def9 770c54a 020def9 770c54a b0eb535 020def9 770c54a af58cc7 770c54a af58cc7 |
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
import sys
import time
try:
import spaces
except ImportError:
print("ZeroGPU is not available, skipping...")
import torch
import torchaudio
import gradio as gr
import torchaudio.transforms as T
import polars as pl
from importlib.metadata import version
from gradio.utils import is_zero_gpu_space
from gradio.themes import Base
from paddleocr import PaddleOCR
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
AutoModelForCTC,
Wav2Vec2BertProcessor,
)
use_zero_gpu = is_zero_gpu_space()
use_cuda = torch.cuda.is_available()
if use_zero_gpu:
spaces_version = version("spaces")
print("ZeroGPU is available, changing inference call.")
else:
spaces_version = "N/A"
print("ZeroGPU is not available, skipping...")
print(f"Spaces version: {spaces_version}")
if use_cuda:
print("CUDA is available, setting correct `device` variable.")
device = "cuda"
torch_dtype = torch.bfloat16
else:
device = "cpu"
torch_dtype = torch.bfloat16
# Config
model_name = "Yehor/kulyk-uk-en"
concurrency_limit = 5
min_duration = 0.5
max_duration = 60
current_theme = Base()
# Load the model
model = AutoModelForCausalLM.from_pretrained(
model_name,
device_map=device,
torch_dtype=torch_dtype,
)
model.eval()
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Load ASR
audio_model = AutoModelForCTC.from_pretrained(
"Yehor/w2v-bert-uk-v2.1-bf16", torch_dtype=torch_dtype, device_map=device
)
processor = Wav2Vec2BertProcessor.from_pretrained("Yehor/w2v-bert-uk-v2.1-bf16")
# Load OCR
ocr_model = PaddleOCR(
lang="uk",
use_doc_orientation_classify=False,
use_doc_unwarping=False,
use_textline_orientation=False,
)
# Examples
examples_text = [
"WP: F-16 навряд чи суттєво змінять ситуацію на полі бою",
"Над Україною збито ракету та 7 із 8 «Шахедів»",
"Олімпійські ігри 2024. Розклад змагань українських спортсменів на 28 липня",
"Кампанія Гарріс зібрала понад 200 мільйонів доларів менш ніж за тиждень",
"За тиждень НБУ продав майже 800 мільйонів доларів на міжбанківському ринку",
"Париж 2024. День 2: Текстова трансляція",
]
examples_audio = [
"example_1.wav",
"example_2.wav",
"example_3.wav",
"example_4.wav",
"example_5.wav",
"example_6.wav",
]
examples_image = [
"example_1.jpg",
"example_2.jpg",
"example_3.jpg",
"example_4.jpg",
"example_5.jpg",
"example_6.jpg",
]
title = "UK-EN Translator"
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 egorsmkv@gmail.com |
""".strip()
description_head = f"""
# {title}
This space translates your text, audio, image from Ukrainian to English using [kulyk-uk-en](https://huggingface.co/Yehor/kulyk-uk-en) model. Also, check [EN-UK Translator](https://huggingface.co/spaces/Yehor/en-uk-translator) out.
""".strip()
tech_env = f"""
#### Environment
- Python: {sys.version}
- Torch device: {device}
- Torch dtype: {torch_dtype}
#### Models
- [kulyk-uk-en](https://huggingface.co/Yehor/kulyk-en-uk)
- [wav2vec2-bert](https://huggingface.co/Yehor/w2v-bert-uk-v2.1-bf16)
- [PaddleOCR](https://huggingface.co/PaddlePaddle/eslav_PP-OCRv5_mobile_rec)
""".strip()
tech_libraries = f"""
#### Libraries
- torch: {version("torch")}
- torchaudio: {version("torchaudio")}
- transformers: {version("transformers")}
- accelerate: {version("accelerate")}
- gradio: {version("gradio")}
""".strip()
def translate(text: str) -> str:
prompt = "Translate the text to Ukrainian:\n" + text
input_ids = tokenizer.apply_chat_template(
[{"role": "user", "content": prompt}],
add_generation_prompt=True,
return_tensors="pt",
tokenize=True,
).to(model.device)
output = model.generate(
input_ids,
max_new_tokens=2048,
# Greedy Search
do_sample=False,
repetition_penalty=1.05,
# Sampling
# do_sample=True,
# temperature=0.1,
# # top_k=1,
# min_p=0.9,
# repetition_penalty=1.05,
)
prompt_len = input_ids.shape[1]
generated_tokens = output[:, prompt_len:]
translated_text = tokenizer.batch_decode(
generated_tokens, skip_special_tokens=True
)[0]
return translated_text.strip()
@spaces.GPU
def inference_text(text, progress=gr.Progress()):
if not text:
raise gr.Error("Please paste your text.")
progress(0, desc="Translating...")
results = []
sentences = text.split("\n")
non_empty_sentences = []
for sentence in sentences:
s = sentence.strip()
if len(s) != 0:
non_empty_sentences.append(s)
for sentence in progress.tqdm(
non_empty_sentences, desc="Translating...", unit="sentence"
):
t0 = time.time()
translated_text = translate(sentence)
elapsed_time = round(time.time() - t0, 2)
results.append(
{
"sentence": sentence,
"translated_text": translated_text,
"elapsed_time": elapsed_time,
}
)
gr.Info("Finished!", duration=2)
return pl.DataFrame(results)
@spaces.GPU
def inference_audio(audio, progress=gr.Progress()):
if not audio:
raise gr.Error("Please paste your audio file.")
progress(0, desc="Translating...")
meta = torchaudio.info(audio)
duration = meta.num_frames / meta.sample_rate
if duration < min_duration:
raise gr.Error(
f"The duration of the file is less than {min_duration} seconds, it is {round(duration, 2)} seconds."
)
if duration > max_duration:
raise gr.Error(f"The duration of the file exceeds {max_duration} seconds.")
audio_input, sr = torchaudio.load(audio)
if meta.num_channels > 1:
audio_input = torch.mean(audio_input, dim=0, keepdim=True)
if meta.sample_rate != 16_000:
resampler = T.Resample(sr, 16_000, dtype=audio_input.dtype)
audio_input = resampler(audio_input)
audio_input = audio_input.squeeze().numpy()
features = processor([audio_input], sampling_rate=16_000).input_features
features = torch.tensor(features).to(device, dtype=torch_dtype)
with torch.inference_mode():
logits = audio_model(features).logits
predicted_ids = torch.argmax(logits, dim=-1)
predictions = processor.batch_decode(predicted_ids)
print("Predictions:", predictions)
if not predictions:
text = "-"
else:
text = "\n".join(predictions)
print("Text:", text)
results = []
sentences = text.split("\n")
non_empty_sentences = []
for sentence in sentences:
s = sentence.strip()
if len(s) != 0:
non_empty_sentences.append(s)
for sentence in progress.tqdm(
non_empty_sentences, desc="Translating...", unit="sentence"
):
t0 = time.time()
translated_text = translate(sentence)
elapsed_time = round(time.time() - t0, 2)
results.append(
{
"sentence": sentence,
"translated_text": translated_text,
"elapsed_time": elapsed_time,
}
)
gr.Info("Finished!", duration=2)
return pl.DataFrame(results)
@spaces.GPU
def inference_image(image, progress=gr.Progress()):
if not image:
raise gr.Error("Please paste your image file.")
progress(0, desc="Translating...")
if not isinstance(image, str):
raise gr.Error("Please paste your image file.")
predictions = ocr_model.predict(image)
results = []
for prediction in predictions:
results.append(' '.join(prediction['rec_texts']))
text = " ".join(results)
print("Text:", text)
results = []
sentences = [text]
for sentence in progress.tqdm(sentences, desc="Translating...", unit="sentence"):
t0 = time.time()
translated_text = translate(sentence)
elapsed_time = round(time.time() - t0, 2)
results.append(
{
"sentence": sentence,
"translated_text": translated_text,
"elapsed_time": elapsed_time,
}
)
gr.Info("Finished!", duration=2)
return pl.DataFrame(results)
def create_app():
tab = gr.Blocks(
title=title,
analytics_enabled=False,
theme=current_theme,
)
with tab:
gr.Markdown(description_head)
gr.Markdown("## Usage")
translated_text = gr.DataFrame(
label="Translated text",
)
text = gr.Textbox(label="Text", autofocus=True, lines=5)
gr.Button("Translate").click(
inference_text,
concurrency_limit=concurrency_limit,
inputs=text,
outputs=translated_text,
)
with gr.Row():
gr.Examples(label="Choose an example", inputs=text, examples=examples_text)
return tab
def create_audio_app():
with gr.Blocks(theme=current_theme) as tab:
gr.Markdown(description_head)
gr.Markdown("## Usage")
translated_text = gr.DataFrame(
label="Translated text",
)
audio = gr.Audio(label="Audio file", sources="upload", type="filepath")
gr.Button("Translate").click(
inference_audio,
concurrency_limit=concurrency_limit,
inputs=audio,
outputs=translated_text,
)
with gr.Row():
gr.Examples(
label="Choose an example", inputs=audio, examples=examples_audio
)
gr.Markdown(
f"> Due to resource limitations, audio duration **must not** exceed **{max_duration}** seconds."
)
return tab
def create_image_app():
with gr.Blocks(theme=current_theme) as tab:
gr.Markdown(description_head)
gr.Markdown("## Usage")
translated_text = gr.DataFrame(
label="Translated text",
)
image = gr.Image(label="Image file", sources="upload", type="filepath")
gr.Button("Translate").click(
inference_image,
concurrency_limit=concurrency_limit,
inputs=image,
outputs=translated_text,
)
with gr.Row():
gr.Examples(
label="Choose an example", inputs=image, examples=examples_image
)
return tab
def create_env():
with gr.Blocks(theme=current_theme) as tab:
gr.Markdown(tech_env)
gr.Markdown(tech_libraries)
return tab
def create_authors():
with gr.Blocks(theme=current_theme) as tab:
gr.Markdown(authors_table)
return tab
def create_demo():
app_tab = create_app()
app_audio_tab = create_audio_app()
app_image_tab = create_image_app()
authors_tab = create_authors()
env_tab = create_env()
return gr.TabbedInterface(
[app_tab, app_audio_tab, app_image_tab, authors_tab, env_tab],
tab_names=[
"✍️ Text",
"🔊 Audio",
"👀 Image",
"👥 Authors",
"📦 Environment, Models, and Libraries",
],
)
if __name__ == "__main__":
demo = create_demo()
demo.queue()
demo.launch()
|