Views
No views yet
1
2!pip install git+https://github.com/huggingface/transformers.git -U
3!pip install accelerate peft -U
4
5import torch
6from transformers import AutoModelForCausalLM, AutoTokenizer
7
8# --- YAPILANDIRMA ---
9# Yeni oluşturduğun modelin Hugging Face ID'si
10model_id = "sedatyilmazer/kanunlar"
11
12def run_kanunlar_inference():
13 print(f"🚀 Blackwell GPU üzerinde '{model_id}' yükleniyor...")
14
15 try:
16 # 1. Tokenizer ve Model Yükleme
17 # Blackwell mimarisi için torch_dtype=torch.bfloat16 en verimli tercihtir.
18 tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
19
20 model = AutoModelForCausalLM.from_pretrained(
21 model_id,
22 torch_dtype=torch.bfloat16,
23 device_map="auto",
24 trust_remote_code=True
25 )
26
27 print("✅ Model başarıyla yüklendi! Sorgu işleniyor...")
28
29 # 2. Test Sorusu (Prompt)
30 # Qwen modelleri genellikle ChatML formatını (<|im_start|>) sever.
31 user_query = "KVKK nedir?"
32
33 prompt = f"<|im_start|>user\n{user_query}<|im_end|>\n<|im_start|>assistant\n"
34 inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
35
36 # 3. Yanıt Üretme (Generation)
37 with torch.no_grad():
38 output_ids = model.generate(
39 **inputs,
40 max_new_tokens=32000, # Blackwell'de bellek sorunu olmadığı için yüksek tutabilirsin
41 temperature=0.3, # Hukuk metinlerinde tutarlılık için düşük sıcaklık
42 top_p=0.9,
43 repetition_penalty=1.1,
44 do_sample=True,
45 pad_token_id=tokenizer.eos_token_id
46 )
47
48 # 4. Çıktıyı Decode Etme
49 response = tokenizer.decode(output_ids[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
50
51 print("\n" + "="*60)
52 print(f"SORU: {user_query}")
53 print("-" * 60)
54 print(f"KANUNLAR YANITI:\n\n{response}")
55 print("="*60)
56
57 except Exception as e:
58 print(f"❌ HATA: {str(e)}")
59
60# Testi Başlat
61run_kanunlar_inference()