try
Browse files
app.py
CHANGED
|
@@ -1,23 +1,34 @@
|
|
|
|
|
| 1 |
from unsloth import FastLanguageModel
|
| 2 |
-
from peft import PeftModel
|
| 3 |
-
from transformers import AutoTokenizer
|
| 4 |
-
import gradio as gr
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
base_model_name = "unsloth/Llama-3.2-3B-Instruct"
|
| 7 |
base_model, tokenizer = FastLanguageModel.from_pretrained(
|
| 8 |
model_name=base_model_name,
|
| 9 |
max_seq_length=2048,
|
| 10 |
-
dtype=None,
|
| 11 |
-
load_in_4bit=
|
| 12 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
lora_model_name = "oskaralf/lora_model" # Hugging Face repository for LoRA adapters
|
| 16 |
model = PeftModel.from_pretrained(base_model, lora_model_name)
|
|
|
|
|
|
|
|
|
|
| 17 |
FastLanguageModel.for_inference(model)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
def chatbot(input_text):
|
| 20 |
-
inputs = tokenizer(input_text, return_tensors="pt").to(
|
| 21 |
outputs = model.generate(input_ids=inputs["input_ids"], max_new_tokens=64)
|
| 22 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
return response
|
|
|
|
| 1 |
+
import torch
|
| 2 |
from unsloth import FastLanguageModel
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Check if CUDA is available
|
| 5 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 6 |
+
|
| 7 |
+
# Load the base model
|
| 8 |
base_model_name = "unsloth/Llama-3.2-3B-Instruct"
|
| 9 |
base_model, tokenizer = FastLanguageModel.from_pretrained(
|
| 10 |
model_name=base_model_name,
|
| 11 |
max_seq_length=2048,
|
| 12 |
+
dtype=None, # Auto-detect data type
|
| 13 |
+
load_in_4bit=False, # Disable 4-bit quantization for CPU
|
| 14 |
)
|
| 15 |
+
base_model.to(device)
|
| 16 |
+
|
| 17 |
+
# Apply LoRA adapters
|
| 18 |
+
from peft import PeftModel
|
| 19 |
|
| 20 |
+
lora_model_name = "oskaralf/lora_model" # Replace with your LoRA model path
|
|
|
|
| 21 |
model = PeftModel.from_pretrained(base_model, lora_model_name)
|
| 22 |
+
model.to(device)
|
| 23 |
+
|
| 24 |
+
# Prepare for inference
|
| 25 |
FastLanguageModel.for_inference(model)
|
| 26 |
|
| 27 |
+
# Gradio interface
|
| 28 |
+
import gradio as gr
|
| 29 |
+
|
| 30 |
def chatbot(input_text):
|
| 31 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(device)
|
| 32 |
outputs = model.generate(input_ids=inputs["input_ids"], max_new_tokens=64)
|
| 33 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 34 |
return response
|