Views
No views yet
codellama/CodeLlama-13b-hf1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4# Model ve tokenizer yükleme
5model = AutoModelForCausalLM.from_pretrained(
6 "AlpYzc/code-llama-13b-turkish-custom",
7 torch_dtype=torch.float16,
8 device_map="auto"
9)
10tokenizer = AutoTokenizer.from_pretrained("AlpYzc/code-llama-13b-turkish-custom")
11
12# Kod üretme fonksiyonu
13def generate_code(prompt, max_length=200):
14 inputs = tokenizer(prompt, return_tensors="pt")
15
16 with torch.no_grad():
17 outputs = model.generate(
18 inputs.input_ids,
19 max_length=max_length,
20 temperature=0.7,
21 do_sample=True,
22 top_p=0.9,
23 pad_token_id=tokenizer.eos_token_id
24 )
25
26 return tokenizer.decode(outputs[0], skip_special_tokens=True)
27
28# Örnek kullanım
29prompt = "def factorial(n):"
30generated_code = generate_code(prompt)
31print(generated_code)1# Input: def factorial(n):
2# Output:
3def factorial(n):
4 if n == 0:
5 return 1
6 else:
7 return n * factorial(n - 1)1# Input: def binary_search(arr, target):
2# Output:
3def binary_search(arr, target):
4 left, right = 0, len(arr) - 1
5
6 while left <= right:
7 mid = (left + right) // 2
8 if arr[mid] == target:
9 return mid
10 elif arr[mid] < target:
11 left = mid + 1
12 else:
13 right = mid - 1
14
15 return -1