Views
No views yet
1# Install llama-cpp-python
2# pip install llama-cpp-python
3
4from llama_cpp import Llama
5
6# Load the model
7model_path = "arif-butt/tinyllama-peft-gguf" # or local path to .gguf file
8llm = Llama(
9 model_path=model_path,
10 n_ctx=2048, # Context length
11 n_threads=4, # Number of CPU threads
12 n_gpu_layers=0, # Set >0 for GPU offloading
13 verbose=False,
14)
15
16# Simple prompt
17prompt = "Q: Name all the courses Arif butt teach?\nA:"
18
19# Generate response
20output = llm(
21 prompt,
22 max_tokens=100, # Maximum tokens to generate
23 temperature=0.2, # Lower = more deterministic
24 top_p=0.95, # Nucleus sampling
25 repeat_penalty=1.1, # Penalize repetition
26 stop=["Q:", "\nQ:"], # Stop sequences
27)
28
29print(output["choices"][0]["text"])
30
31Option 2: Using Transformers with llama-cpp
32from transformers import AutoTokenizer
33from llama_cpp import Llama
34
35# Load tokenizer
36tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
37
38# Load GGUF model
39model_path = "arif-butt/tinyllama-peft-gguf"
40llm = Llama(model_path=model_path, n_ctx=2048)
41
42def generate_response(prompt, max_tokens=100, temperature=0.7):
43 """Generate response using the GGUF model"""
44 output = llm(
45 prompt,
46 max_tokens=max_tokens,
47 temperature=temperature,
48 stop=["Q:", "\nQ:", "User:", "\nUser:"],
49 )
50 return output["choices"][0]["text"]
51
52# Test
53prompt = "Q: What is machine learning?\nA:"
54response = generate_response(prompt)
55print(f"Response: {response}")
56
57Option 3: Using llama.cpp CLI
58# Download the model
59wget https://huggingface.co/arif-butt/tinyllama-peft-gguf/resolve/main/tinyllama-peft-q4_k_m.gguf
60
61# Run inference
62./main -m tinyllama-peft-q4_k_m.gguf \
63 -p "Q: Name all the courses Arif butt teach?\nA:" \
64 -n 100 \
65 -t 4 \
66 --temp 0.2 \
67 --top_p 0.95