Chạy mô hình Hugging Face

Làm việc với Hugging Face

Jacob H. Marquez

Lead Data Engineer

Suy luận với Hugging Face

suy_luan.jpg

Làm việc với Hugging Face

Suy luận cục bộ

suy_luan_cuc_bo.png

Làm việc với Hugging Face

Nhà cung cấp suy luận

nha_cung_cap_suy_luan.png

1 https://huggingface.co/docs/inference-providers/en/index
Làm việc với Hugging Face

Suy luận với Hugging Face

 

Suy luận cục bộ

 

  • 🆓 Miễn phí
  • 😃 Tiện lợi
  • 😴 Chậm, tốn tài nguyên

 

Nhà cung cấp suy luận

 

  • 💨 Nhanh
  • 🆓 Miễn phí khởi đầu
1 https://huggingface.co/docs/inference-providers/en/index
Làm việc với Hugging Face

Giới thiệu thư viện Transformers

 

  • Đơn giản hóa việc dùng mô hình đã huấn luyện sẵn

 

1 https://github.com/huggingface/transformers
Làm việc với Hugging Face

Pipeline

from transformers import pipeline


gpt2_pipeline = pipeline(task="text-generation", model="openai-community/gpt2")
print(gpt2_pipeline("What if AI"))
[{'generated_text': 'What if AI wouldn\'t be used?"\n\nI had to agree with your
  theory. If a machine\'s learning algorithm is a perfect match for all of a
  human\'s needs, then you may not have a problem with it. My problem was whether'}]
1 Model Card: https://huggingface.co/openai-community/gpt2
Làm việc với Hugging Face

Điều chỉnh tham số Pipeline

from transformers import pipeline

gpt2_pipeline = pipeline(task="text-generation", model="openai-community/gpt2")

results = gpt2_pipeline("What if AI", max_new_tokens=10, num_return_sequences=2)
for result in results: print(result['generated_text'])
What if AI had never existed?  
What if AI could be really smarter than us?
Làm việc với Hugging Face

Dùng nhà cung cấp suy luận

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="together",
    api_key=os.environ["HF_TOKEN"],
)
1 https://huggingface.co/docs/inference-providers/en/index
Làm việc với Hugging Face
completion = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V3",
    messages=[
        {
            "role": "user",
            "content": "What is the capital of France?"
        }
    ],
)
1 https://huggingface.co/docs/inference-providers/en/index
Làm việc với Hugging Face
print(completion.choices[0].message)
The capital of France is **Paris**. It is known for its iconic landmarks such as
the Eiffel Tower, the Louvre Museum, and Notre-Dame Cathedral.  

Would you like any additional information about Paris or France?
Làm việc với Hugging Face

Ayo berlatih!

Làm việc với Hugging Face

Preparing Video For Download...