Views
No views yet
unsloth/llama-3-8b-bnb-4bitunsloth/llama-3-8b-bnb-4bit) and then apply the LoRA adapters from this repository.1import torch
2from peft import PeftModel
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5# Specify the base model and the LoRA adapter path
6base_model_name = "unsloth/llama-3-8b-bnb-4bit"
7adapter_path = "tahamajs/Llama-3-8B-bitcoin-predictor"
8
9# Load the base model in 4-bit
10model = AutoModelForCausalLM.from_pretrained(
11 base_model_name,
12 load_in_4bit=True,
13 torch_dtype=torch.bfloat16,
14 device_map="auto",
15)
16
17# Load the tokenizer
18tokenizer = AutoTokenizer.from_pretrained(base_model_name)
19
20# Load the LoRA adapter
21model = PeftModel.from_pretrained(model, adapter_path)
22
23# --- Prepare your prompt ---
24# The prompt must follow the same structure as the training data.
25instruction = "3838.00, 3933.23, 3925.02, 3964.91, 4022.25"
26input_text = (
27 "Based on the historical data and technical analysis, predict the next day's Bitcoin closing price. "
28 "The prediction date is 2019-02-25 (Weekday). "
29 "Technical Analysis for 2019-02-24: BTC Volume was 10795439487. The 14-day RSI is 72.58. "
30 "The price is above the 50-day EMA (3705.93) and below the 200-day EMA (5021.57). "
31 "Macro-Economic Context: S&P 500 closed at 2792.67; Gold at 1329.50; Oil at 57.26; US Dollar Index at 96.41. "
32 "Social Media Sentiment: On 2019-02-24, there were 4150 tweets. Sample: 'RT @APompliano: The gap between the legacy financial system and the digital world is growing daily...'"
33)
34
35# Format the prompt using the Llama 3 chat template
36prompt = (
37 f"<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n"
38 f"Instruction: {instruction}\n\nInput: {input_text}<|eot_id|>"
39 f"<|start_header_id|>assistant<|end_header_id|>\n\n"
40)
41
42# Tokenize the input
43inputs = tokenizer(prompt.format(instruction=instruction, input_text=input_text), return_tensors="pt", truncation=True).to("cuda")
44
45# Generate the prediction
46with torch.no_grad():
47 outputs = model.generate(
48 input_ids=inputs["input_ids"],
49 max_new_tokens=50,
50 eos_token_id=tokenizer.eos_token_id,
51 do_sample=True,
52 temperature=0.6,
53 top_p=0.9,
54 )
55
56response = tokenizer.decode(outputs[0], skip_special_tokens=True)
57# Extract the assistant's response
58prediction = response.split("<|end_header_id|>\n\n")[-1]
59print(f"Predicted Price: {prediction}")