Spaces:
Running
on
Zero
Running
on
Zero
added gpu support
Browse files
app.py
CHANGED
@@ -1,6 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Define model paths
|
6 |
MODEL_PATHS = {
|
@@ -14,7 +19,8 @@ MODEL_PATHS = {
|
|
14 |
TOKEN = os.environ['TOKEN']
|
15 |
|
16 |
# Translation function for Nano and Large models
|
17 |
-
|
|
|
18 |
translator = pipeline("translation", model=model_path, token=TOKEN)
|
19 |
translated = translator(
|
20 |
text,
|
@@ -25,21 +31,24 @@ def translate_nano_large(text, model_path):
|
|
25 |
do_sample=False,
|
26 |
pad_token_id=translator.tokenizer.pad_token_id,
|
27 |
bos_token_id=translator.tokenizer.bos_token_id,
|
28 |
-
eos_token_id=translator.tokenizer.eos_token_id
|
|
|
29 |
)
|
30 |
return translated[0]["translation_text"]
|
31 |
|
32 |
# Translation function for Ultra and Supreme models
|
33 |
-
|
|
|
34 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_path, token=TOKEN)
|
35 |
-
tokenizer = AutoTokenizer.from_pretrained(model_path, token=TOKEN)
|
36 |
translator = pipeline(
|
37 |
"translation",
|
38 |
model=model,
|
39 |
tokenizer=tokenizer,
|
40 |
max_length=512,
|
41 |
src_lang="eng_Latn", # Keep src_lang and tgt_lang in the pipeline
|
42 |
-
tgt_lang="ary_Arab"
|
|
|
43 |
)
|
44 |
translation = translator(text)[0]['translation_text']
|
45 |
return translation
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
|
3 |
import os
|
4 |
+
import torch
|
5 |
+
import spaces
|
6 |
+
|
7 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
8 |
+
print(f'[INFO] Using device: {device}')
|
9 |
|
10 |
# Define model paths
|
11 |
MODEL_PATHS = {
|
|
|
19 |
TOKEN = os.environ['TOKEN']
|
20 |
|
21 |
# Translation function for Nano and Large models
|
22 |
+
@spaces.GPU
|
23 |
+
def translate_nano_large(text, model_path, device='cuda:0'):
|
24 |
translator = pipeline("translation", model=model_path, token=TOKEN)
|
25 |
translated = translator(
|
26 |
text,
|
|
|
31 |
do_sample=False,
|
32 |
pad_token_id=translator.tokenizer.pad_token_id,
|
33 |
bos_token_id=translator.tokenizer.bos_token_id,
|
34 |
+
eos_token_id=translator.tokenizer.eos_token_id,
|
35 |
+
device=device,
|
36 |
)
|
37 |
return translated[0]["translation_text"]
|
38 |
|
39 |
# Translation function for Ultra and Supreme models
|
40 |
+
@spaces.GPU
|
41 |
+
def translate_ultra_supreme(text, model_path, device='cuda:0'):
|
42 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_path, token=TOKEN)
|
43 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, src_lang="eng_Latn", tgt_lang="ary_Arab", token=TOKEN)
|
44 |
translator = pipeline(
|
45 |
"translation",
|
46 |
model=model,
|
47 |
tokenizer=tokenizer,
|
48 |
max_length=512,
|
49 |
src_lang="eng_Latn", # Keep src_lang and tgt_lang in the pipeline
|
50 |
+
tgt_lang="ary_Arab",
|
51 |
+
device=device,
|
52 |
)
|
53 |
translation = translator(text)[0]['translation_text']
|
54 |
return translation
|