File size: 2,014 Bytes
85b9553
 
1eab707
0644eb8
 
0fb075f
 
85b9553
0fb075f
85b9553
0fb075f
 
 
85b9553
 
 
 
 
 
1eab707
85b9553
 
1eab707
0fb075f
85b9553
 
 
 
 
 
 
 
0fb075f
 
85b9553
 
 
 
 
 
 
 
5e4ae11
 
85b9553
 
 
4e63f26
ef7bd75
4e63f26
 
85b9553
 
 
4e63f26
85b9553
 
 
 
 
 
4e63f26
85b9553
0fb075f
 
85b9553
 
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
import time

from transformers import T5ForConditionalGeneration, T5Tokenizer, GenerationConfig
import gradio as gr

MODEL_NAME = "jbochi/madlad400-3b-mt"

print(f"Loading {MODEL_NAME} tokenizer...")
tokenizer = T5Tokenizer.from_pretrained(MODEL_NAME)
print(f"Loading {MODEL_NAME} model...")
model = T5ForConditionalGeneration.from_pretrained(MODEL_NAME, device_map="auto")


def inference(input_text, target_language, max_length):
    global model, tokenizer
    start_time = time.time()
    input_ids = tokenizer(
        f"<2{target_language}> {input_text}", return_tensors="pt"
    ).input_ids
    outputs = model.generate(
        input_ids=input_ids.to(model.device),
        generation_config=GenerationConfig(max_length=max_length),
    )
    result = tokenizer.decode(outputs[0], skip_special_tokens=True)
    end_time = time.time()
    result = {
        'result': result,
        'inference_time': end_time - start_time,
        'input_token_ids': input_ids[0].tolist(),
        'output_token_ids': outputs[0].tolist(),
    }
    return result


def run():
    tokens = [tokenizer.decode(i) for i in range(500)]
    lang_codes = [token[2:-1] for token in tokens if token.startswith("<2")]
    inputs = [
        gr.components.Textbox(lines=5, label="Input text"),
        gr.components.Dropdown(lang_codes, value="en", label="Target Language"),
        gr.components.Slider(
            minimum=5,
            maximum=128,
            value=50,
            label="Max length",
        ),
    ]
    examples = [
        ["I'm a mad lad!", "es", 50],
        ["千里之行,始於足下", "en", 50],
    ]
    outputs = gr.components.JSON()
    title = f"{MODEL_NAME} demo"
    demo_status = "Demo is running on CPU"
    description = f"Details: https://huggingface.co/{MODEL_NAME}. {demo_status}"
    gr.Interface(
        inference,
        inputs,
        outputs,
        title=title,
        description=description,
        examples=examples,
    ).launch()


if __name__ == "__main__":
    run()