Spaces:
Runtime error
Runtime error
Commit
β’
1c51c8b
1
Parent(s):
9b659cc
initial commit
Browse files- app.py +67 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
|
3 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer, GenerationConfig
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
MODEL_NAME = "jbochi/madlad400-10b-mt"
|
7 |
+
|
8 |
+
print(f"Loading {MODEL_NAME} tokenizer...")
|
9 |
+
tokenizer = T5Tokenizer.from_pretrained(MODEL_NAME)
|
10 |
+
print(f"Loading {MODEL_NAME} model...")
|
11 |
+
model = T5ForConditionalGeneration.from_pretrained(MODEL_NAME, device_map="auto")
|
12 |
+
|
13 |
+
|
14 |
+
def inference(input_text, target_language, max_length):
|
15 |
+
global model, tokenizer
|
16 |
+
start_time = time.time()
|
17 |
+
input_ids = tokenizer(
|
18 |
+
f"<2{target_language}> {input_text}", return_tensors="pt"
|
19 |
+
).input_ids
|
20 |
+
outputs = model.generate(
|
21 |
+
input_ids=input_ids.to(model.device),
|
22 |
+
generation_config=GenerationConfig(max_length=max_length),
|
23 |
+
)
|
24 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
25 |
+
end_time = time.time()
|
26 |
+
result = {
|
27 |
+
'result': result,
|
28 |
+
'inference_time': end_time - start_time,
|
29 |
+
'input_token_ids': input_ids[0].tolist(),
|
30 |
+
'output_token_ids': outputs[0].tolist(),
|
31 |
+
}
|
32 |
+
return result
|
33 |
+
|
34 |
+
|
35 |
+
def run():
|
36 |
+
tokens = [tokenizer.decode(i) for i in range(500)]
|
37 |
+
lang_codes = [token[2:-1] for token in tokens if token.startswith("<2")]
|
38 |
+
inputs = [
|
39 |
+
gr.components.Textbox(lines=5, label="Input text"),
|
40 |
+
gr.components.Dropdown(lang_codes, value="shn", label="Target Language"),
|
41 |
+
gr.components.Slider(
|
42 |
+
minimum=5,
|
43 |
+
maximum=128,
|
44 |
+
value=50,
|
45 |
+
label="Max length",
|
46 |
+
),
|
47 |
+
]
|
48 |
+
examples = [
|
49 |
+
["I'm walking on the beach", "shn", 50],
|
50 |
+
["αααΊααα―ααΊαΆαα", "en", 50],
|
51 |
+
]
|
52 |
+
outputs = gr.components.JSON()
|
53 |
+
title = f"{MODEL_NAME} demo"
|
54 |
+
demo_status = "Demo is running on CPU"
|
55 |
+
description = f"Details: https://huggingface.co/{MODEL_NAME}. {demo_status}"
|
56 |
+
gr.Interface(
|
57 |
+
inference,
|
58 |
+
inputs,
|
59 |
+
outputs,
|
60 |
+
title=title,
|
61 |
+
description=description,
|
62 |
+
examples=examples,
|
63 |
+
).launch()
|
64 |
+
|
65 |
+
|
66 |
+
if __name__ == "__main__":
|
67 |
+
run()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
sentencepiece
|
4 |
+
safetensors
|
5 |
+
accelerate
|