File size: 11,736 Bytes
af58cc7
 
 
7653a75
 
 
 
 
af58cc7
90e8b09
af58cc7
90e8b09
22ea572
 
 
 
 
af58cc7
0636764
 
 
 
 
 
af58cc7
f55d282
 
 
481a579
 
 
 
7653a75
 
18fee24
7653a75
 
 
481a579
7653a75
 
481a579
7653a75
 
 
 
 
 
af58cc7
0636764
af58cc7
48973da
af58cc7
 
55e8ee9
af58cc7
e0101eb
7653a75
55e8ee9
af58cc7
 
 
 
90e8b09
 
0636764
 
 
90e8b09
 
 
f55d282
 
 
 
90e8b09
 
55e8ee9
 
 
 
 
 
af58cc7
90e8b09
 
 
 
 
 
 
 
 
9b91c8a
f55d282
 
 
 
 
 
 
90e8b09
55e8ee9
af58cc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57d9237
af58cc7
 
 
 
 
 
 
b5863bd
 
 
 
f55d282
 
af58cc7
 
 
 
 
0636764
 
 
af58cc7
 
2dc4e00
22ea572
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f9ec7e
90e8b09
af58cc7
 
 
7bdd58e
af58cc7
 
 
0636764
af58cc7
2e99c9d
 
 
 
 
af58cc7
0636764
 
 
af58cc7
22ea572
af58cc7
 
6ba5eff
af58cc7
 
 
6ba5eff
af58cc7
 
 
 
 
 
22ea572
af58cc7
 
90e8b09
 
 
 
 
 
 
 
 
 
 
 
 
 
0636764
 
 
90e8b09
 
0636764
90e8b09
 
 
 
 
 
 
0636764
 
 
90e8b09
 
 
 
 
 
0636764
 
 
90e8b09
 
0636764
90e8b09
 
 
0636764
90e8b09
 
 
 
 
 
 
0636764
 
 
90e8b09
22ea572
90e8b09
 
 
 
 
 
 
 
 
 
 
 
22ea572
f55d282
 
 
9b91c8a
f55d282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22ea572
f55d282
 
 
 
 
 
 
 
 
 
 
 
22ea572
90e8b09
af58cc7
2dc4e00
 
 
 
48973da
2dc4e00
af58cc7
2dc4e00
 
 
0636764
22ea572
2dc4e00
 
0636764
2dc4e00
0636764
2dc4e00
90e8b09
2dc4e00
 
 
 
0636764
2dc4e00
90e8b09
 
 
 
0636764
90e8b09
48973da
90e8b09
 
0636764
22ea572
90e8b09
 
0636764
90e8b09
0636764
90e8b09
 
 
 
 
 
 
 
f55d282
 
 
 
 
 
 
9b91c8a
48973da
903aa61
 
 
22ea572
f55d282
 
 
 
 
 
9b91c8a
f55d282
 
 
 
 
 
 
9b91c8a
f55d282
af58cc7
2dc4e00
6ba5eff
90e8b09
2dc4e00
48973da
2dc4e00
 
af58cc7
2dc4e00
 
0636764
2dc4e00
48973da
2dc4e00
af58cc7
2dc4e00
af58cc7
 
2dc4e00
 
90e8b09
9b91c8a
2dc4e00
 
 
 
9b91c8a
2dc4e00
2270071
90e8b09
f55d282
2dc4e00
 
 
 
af58cc7
0636764
af58cc7
2dc4e00
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
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 transformers import (
    AutoModelForCausalLM,
    AutoTokenizer,
    AutoProcessor,
    MoonshineForConditionalGeneration,
)

from doctr.io import DocumentFile
from doctr.models import ocr_predictor

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-en-uk"
concurrency_limit = 5
current_theme = Base()

# Load the model
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    device_map=device,
    torch_dtype=torch_dtype,
    trust_remote_code=True,
)
model.eval()
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Load ASR
audio_processor = AutoProcessor.from_pretrained("UsefulSensors/moonshine-base")
audio_model = MoonshineForConditionalGeneration.from_pretrained(
    "UsefulSensors/moonshine-base", attn_implementation="sdpa"
)
audio_model.to(device)
audio_model.to(torch_dtype)

# Load OCR
ocr_model = ocr_predictor(pretrained=True)
ocr_model.to(device)

# Examples
examples_text = [
    "WP: F-16s are unlikely to make a significant difference on the battlefield",
    "Missile and 7 of 8 Shaheeds shot down over Ukraine",
    "Olympic Games 2024. Schedule of competitions for Ukrainian athletes on 28 July",
    "Harris' campaign raised more than $200 million in less than a week",
    "Over the week, the NBU sold almost $800 million on the interbank market",
    "Paris 2024. Day 2: Text broadcast",
]
examples_audio = [
    "example_1.wav",
    "example_2.wav",
    "example_3.wav",
    "example_4.wav",
    "example_5.wav",
    "example_6.wav",
    "example_7.wav",
]
examples_image = [
    "example_1.jpg",
    "example_2.jpg",
    "example_3.jpg",
    "example_4.jpg",
    "example_5.jpg",
    "example_6.jpg",
]

title = "EN-UK 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 English to Ukrainian using [kulyk-en-uk](https://huggingface.co/Yehor/kulyk-en-uk) model. Also, check [UK-EN Translator](https://huggingface.co/spaces/Yehor/uk-en-translator) out.
""".strip()


tech_env = f"""
#### Environment

- Python: {sys.version}

#### Models

- [kulyk-en-uk](https://huggingface.co/Yehor/kulyk-en-uk)
- [moonshine-base](https://huggingface.co/UsefulSensors/moonshine-base)
- [doctr](https://github.com/mindee/doctr)
""".strip()

tech_libraries = f"""
#### Libraries

- torch: {version("torch")}
- gradio: {version("gradio")}
- transformers: {version("transformers")}
""".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)

        translated_text = translated_text.strip()
        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...")

    if isinstance(audio, str):
        audio_array, sr = torchaudio.load(audio)
        audio_array = audio_array.squeeze()
    else:
        audio_array, sr = audio

    r_sr = audio_processor.feature_extractor.sampling_rate

    print("Audio processor SR:", r_sr)
    print("Audio file SR:", sr)

    if r_sr != sr:
        print("Resampling...")
        resampler = T.Resample(orig_freq=sr, new_freq=r_sr)
        audio_array = resampler(audio_array)

    inputs = audio_processor(audio_array, return_tensors="pt", sampling_rate=r_sr)
    inputs = inputs.to(device, dtype=torch_dtype)

    # to avoid hallucination loops, we limit the maximum length of the generated text based expected number of tokens per second
    token_limit_factor = (
        6.5 / audio_processor.feature_extractor.sampling_rate
    )  # Maximum of 6.5 tokens per second
    seq_lens = inputs.attention_mask.sum(dim=-1)
    max_length = int((seq_lens * token_limit_factor).max().item())

    generated_ids = audio_model.generate(**inputs, max_length=max_length)

    predictions = audio_processor.batch_decode(generated_ids, skip_special_tokens=True)

    print("Predictions:", predictions)

    text = predictions[0]

    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 isinstance(image, str):
        doc = DocumentFile.from_images(image)
    else:
        raise gr.Error("Please paste your image file.")

    result = ocr_model(doc)

    text = result.render()

    print("Text:", text)

    results = []

    sentences = [text.replace("\n", " ")]

    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
            )

    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()