Update app.py
Browse files
app.py
CHANGED
|
@@ -247,17 +247,14 @@ class Translators:
|
|
| 247 |
model = AutoModelForCausalLM.from_pretrained(
|
| 248 |
self.model_name,
|
| 249 |
dtype=torch.bfloat16,
|
| 250 |
-
device_map="auto"
|
| 251 |
-
max_memory={0: "16GB"},
|
| 252 |
-
)
|
| 253 |
tokenizer = AutoTokenizer.from_pretrained(self.model_name)
|
| 254 |
context = {"tone": "Formal and precise"}
|
| 255 |
system = f"Translate the user's text to {self.tl}. Provide the final translation in a formal tone immediately immediately without any other text."
|
| 256 |
messages = [
|
| 257 |
{"role": "system", "content": system},
|
| 258 |
{"role": "user", "content": self.input_text},
|
| 259 |
-
]
|
| 260 |
-
|
| 261 |
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 262 |
print(prompt)
|
| 263 |
inputs = tokenizer(prompt, return_tensors="pt").to(self.device)
|
|
@@ -266,8 +263,7 @@ class Translators:
|
|
| 266 |
outputs = model.generate(
|
| 267 |
**inputs,
|
| 268 |
max_new_tokens=self.max_new_tokens,
|
| 269 |
-
)
|
| 270 |
-
|
| 271 |
generated_tokens = outputs[0][input_length:]
|
| 272 |
translation = tokenizer.decode(generated_tokens, skip_special_tokens=True)
|
| 273 |
return translation
|
|
|
|
| 247 |
model = AutoModelForCausalLM.from_pretrained(
|
| 248 |
self.model_name,
|
| 249 |
dtype=torch.bfloat16,
|
| 250 |
+
device_map="auto").to(self.device)
|
|
|
|
|
|
|
| 251 |
tokenizer = AutoTokenizer.from_pretrained(self.model_name)
|
| 252 |
context = {"tone": "Formal and precise"}
|
| 253 |
system = f"Translate the user's text to {self.tl}. Provide the final translation in a formal tone immediately immediately without any other text."
|
| 254 |
messages = [
|
| 255 |
{"role": "system", "content": system},
|
| 256 |
{"role": "user", "content": self.input_text},
|
| 257 |
+
]
|
|
|
|
| 258 |
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 259 |
print(prompt)
|
| 260 |
inputs = tokenizer(prompt, return_tensors="pt").to(self.device)
|
|
|
|
| 263 |
outputs = model.generate(
|
| 264 |
**inputs,
|
| 265 |
max_new_tokens=self.max_new_tokens,
|
| 266 |
+
)
|
|
|
|
| 267 |
generated_tokens = outputs[0][input_length:]
|
| 268 |
translation = tokenizer.decode(generated_tokens, skip_special_tokens=True)
|
| 269 |
return translation
|