Spaces:
Running
Running
add gguf
Browse files- model_gguf.py +41 -0
- requirements.txt +2 -0
model_gguf.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import snapshot_download
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
model_id = "tencent/HY-MT1.5-1.8B-GGUF"
|
| 6 |
+
gguf_file = "HY-MT1.5-1.8B-Q8_0.gguf"
|
| 7 |
+
local_dir = "./models"
|
| 8 |
+
model_path = snapshot_download(model_id, local_dir=local_dir)
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, gguf_file=gguf_file)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(model_path, gguf_file=gguf_file)
|
| 11 |
+
|
| 12 |
+
def run(
|
| 13 |
+
text: str = "It’s on the house.",
|
| 14 |
+
target_language: str = "Portuguese",
|
| 15 |
+
):
|
| 16 |
+
messages = [
|
| 17 |
+
{
|
| 18 |
+
"role": "user",
|
| 19 |
+
"content": f"Translate the following segment into {target_language}, without additional explanation.\n\n{text}"
|
| 20 |
+
},
|
| 21 |
+
]
|
| 22 |
+
tokenized_chat = tokenizer.apply_chat_template(
|
| 23 |
+
messages,
|
| 24 |
+
tokenize=True,
|
| 25 |
+
add_generation_prompt=False,
|
| 26 |
+
return_tensors="pt"
|
| 27 |
+
)
|
| 28 |
+
input_ids = tokenized_chat.to(model.device)
|
| 29 |
+
input_length = input_ids.shape[1]
|
| 30 |
+
|
| 31 |
+
outputs = model.generate(input_ids, max_new_tokens=2048)
|
| 32 |
+
# 2. Fatiamos o tensor: pegamos do [input_length:] até o fim
|
| 33 |
+
# Isso isola apenas os tokens novos gerados
|
| 34 |
+
generated_tokens = outputs[0][input_length:]
|
| 35 |
+
|
| 36 |
+
output_text = tokenizer.decode(generated_tokens, skip_special_tokens=True)
|
| 37 |
+
return output_text
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
translated_text = run("Now let's make my mum's favourite. So three mars bars into the pan. Then we add the tuna and just stir for a bit, just let the chocolate and fish infuse. A sprinkle of olive oil and some tomato ketchup. Now smell that. Oh boy this is going to be incredible.")
|
| 41 |
+
print(translated_text)
|
requirements.txt
CHANGED
|
@@ -2,3 +2,5 @@ gradio
|
|
| 2 |
huggingface_hub
|
| 3 |
transformers
|
| 4 |
torch
|
|
|
|
|
|
|
|
|
| 2 |
huggingface_hub
|
| 3 |
transformers
|
| 4 |
torch
|
| 5 |
+
gguf>=0.10.0
|
| 6 |
+
accelerate
|