Views
No views yet
pip install -U transformers, sau đó sao chép đoạn mã từ phần có liên quan đến usecase của bạn.torch.bfloat16 làm mặc định.1# pip install transformers torch accelerate
2from transformers import AutoTokenizer, AutoModelForCausalLM
3import torch
4
5# Khởi tạo tokenizer và model từ checkpoint đã lưu
6tokenizer = AutoTokenizer.from_pretrained("himmeow/vi-gemma-2b-RAG")
7model = AutoModelForCausalLM.from_pretrained(
8 "himmeow/vi-gemma-2b-RAG",
9 device_map="auto",
10 torch_dtype=torch.bfloat16
11)
12
13# Sử dụng GPU nếu có
14if torch.cuda.is_available():
15 model.to("cuda")
16
17# Định dạng prompt cho model
18prompt = """
19### Instruction and Input:
20Dựa vào ngữ cảnh/tài liệu sau:
21{}
22Hãy trả lời câu hỏi: {}
23
24### Response:
25{}
26"""
27
28# Chuẩn bị dữ liệu đầu vào
29input_data = """
30Short Tandem Repeats (STRs) là các trình tự DNA lặp lại ngắn (2- 6 nucleotides) xuất hiện phổ biến trong hệ gen của con người. Các trình tự này có tính đa hình rất cao trong tự nhiên, điều này khiến các STRs trở thành những markers di truyền rất quan trọng trong nghiên cứu bản đồ gen người và chuẩn đoán bệnh lý di truyền cũng như xác định danh tính trong lĩnh vực pháp y.
31Các STRs trở nên phổ biến tại các phòng xét nghiệm pháp y bởi vì việc nhân bản và phân tích STRs chỉ cần lượng DNA rất thấp ngay cả khi ở dạng bị phân hủy việc đinh danh vẫn có thể được thực hiện thành công. Hơn nữa việc phát hiện và đánh giá sự nhiễm DNA mẫu trong các mẫu vật có thể được giải quyết nhanh với kết quả phân tích STRs. Ở Hoa Kỳ hiện nay, từ bộ 13 markers nay đã tăng lên 20 markers chính đang được sử dụng để tạo ra một cơ sở dữ liệu DNA trên toàn đất nước được gọi là The FBI Combined DNA Index System (Expaned CODIS).
32CODIS và các cơ sử dữ liệu DNA tương tự đang được sử dụng thực sự thành công trong việc liên kết các hồ sơ DNA từ các tội phạm và các bằng chứng hiện trường vụ án. Kết quả định danh STRs cũng được sử dụng để hỗ trợ hàng trăm nghìn trường hợp xét nghiệm huyết thống cha con mỗi năm'
33"""
34query = "Hãy cho tôi biết một số tính chất của STRs được dùng để làm gì?"
35
36# Định dạng input text
37input_text = prompt.format(input_data, query," ")
38
39# Mã hóa input text thành input ids
40input_ids = tokenizer(input_text, return_tensors="pt")
41
42# Sử dụng GPU cho input ids nếu có
43if torch.cuda.is_available():
44 input_ids = input_ids.to("cuda")
45
46# Tạo văn bản bằng model
47outputs = model.generate(
48 **input_ids,
49 max_new_tokens=500,
50 no_repeat_ngram_size=5, # Ngăn chặn lặp lại các cụm từ 5 gram
51 # do_sample=True, # Kích hoạt chế độ tạo văn bản dựa trên lấy mẫu. Trong chế độ này, model sẽ chọn ngẫu nhiên token tiếp theo dựa trên xác suất được tính từ phân phối xác suất của các token.
52 # temperature=0.7, # Giảm temperature để kiểm soát tính ngẫu nhiên
53 # early_stopping=True, # Dừng tạo văn bản khi tìm thấy kết thúc phù hợp
54)
55# Giải mã và in kết quả
56print(tokenizer.decode(outputs[0]))
57
58'''
59<bos>
60### Instruction and Input:
61Dựa vào ngữ cảnh/tài liệu sau:
62
63Short Tandem Repeats (STRs) là các trình tự DNA lặp lại ngắn (2- 6 nucleotides) xuất hiện phổ biến trong hệ gen của con người. Các trình tự này có tính đa hình rất cao trong tự nhiên, điều này khiến các STRs trở thành những markers di truyền rất quan trọng trong nghiên cứu bản đồ gen người và chuẩn đoán bệnh lý di truyền cũng như xác định danh tính trong lĩnh vực pháp y.
64Các STRs trở nên phổ biến tại các phòng xét nghiệm pháp y bởi vì việc nhân bản và phân tích STRs chỉ cần lượng DNA rất thấp ngay cả khi ở dạng bị phân hủy việc đinh danh vẫn có thể được thực hiện thành công. Hơn nữa việc phát hiện và đánh giá sự nhiễm DNA mẫu trong các mẫu vật có thể được giải quyết nhanh với kết quả phân tích STRs. Ở Hoa Kỳ hiện nay, từ bộ 13 markers nay đã tăng lên 20 markers chính đang được sử dụng để tạo ra một cơ sở dữ liệu DNA trên toàn đất nước được gọi là The FBI Combined DNA Index System (Expaned CODIS).
65CODIS và các cơ sử dữ liệu DNA tương tự đang được sử dụng thực sự thành công trong việc liên kết các hồ sơ DNA từ các tội phạm và các bằng chứng hiện trường vụ án. Kết quả định danh STRs cũng được sử dụng để hỗ trợ hàng trăm nghìn trường hợp xét nghiệm huyết thống cha con mỗi năm'
66
67Hãy trả lời câu hỏi: Hãy cho tôi biết một số tính chất của STRs được dùng để làm gì?
68
69### Response:
70
71STRs được sử dụng để xác định danh tính, chuẩn đoán bệnh lý và xác định bệnh lý di truyền.
72<eos>
73'''pip install -U transformers, then copy the snippet from the section that is relevant for your usecase.torch.bfloat16 as the default dtype.1# pip install transformers torch accelerate
2
3from transformers import AutoTokenizer, AutoModelForCausalLM
4import torch
5
6# Initialize the tokenizer and model from the saved checkpoint
7tokenizer = AutoTokenizer.from_pretrained("himmeow/vi-gemma-2b-RAG")
8model = AutoModelForCausalLM.from_pretrained(
9 "himmeow/vi-gemma-2b-RAG",
10 device_map="auto",
11 torch_dtype=torch.bfloat16
12)
13
14# Use GPU if available
15if torch.cuda.is_available():
16 model.to("cuda")
17
18# Define the prompt format for the model
19prompt = """
20### Instruction and Input:
21Based on the following context/document:
22{}
23Please answer the question: {}
24
25### Response:
26{}
27"""
28
29# Prepare the input data
30input_data = """
31Short Tandem Repeats (STRs) are short (2-6 nucleotides) repeating DNA sequences that are widespread in the human genome. These sequences are highly polymorphic in nature, which makes STRs very important genetic markers in human gene mapping and diagnosis of hereditary diseases as well as identification in the field of forensics.
32STRs have become popular in forensic laboratories because the replication and analysis of STRs requires very small amounts of DNA, even in decomposed form, identification can still be performed successfully. Furthermore, the detection and assessment of sample DNA contamination in specimens can be quickly resolved with STR analysis results. In the United States today, the set of 13 markers has now been increased to 20 main markers being used to create a nationwide DNA database called The FBI Combined DNA Index System (Expaned CODIS).
33CODIS and similar DNA databases are being used very successfully in linking DNA records from criminals and crime scene evidence. STR identification results are also used to support hundreds of thousands of paternity test cases each year.'
34"""
35query = "Tell me what are some properties of STRs used for?"
36
37# Format the input text
38input_text = prompt.format(input_data, query," ")
39
40# Encode the input text into input ids
41input_ids = tokenizer(input_text, return_tensors="pt")
42
43# Use GPU for input ids if available
44if torch.cuda.is_available():
45 input_ids = input_ids.to("cuda")
46
47# Generate text using the model
48outputs = model.generate(
49 **input_ids,
50 max_new_tokens=500, # Limit the number of tokens generated
51 no_repeat_ngram_size=5, # Prevent repetition of 5-gram phrases
52 # do_sample=True,
53 # temperature=0.7, # Adjust the randomness of the generated text
54 # early_stopping=True, # Stop generating text when a suitable ending is found
55)
56# Decode and print the results
57print(tokenizer.decode(outputs[0]))
58
59
60