Spaces:
Build error
Build error
Update app.py
Browse files123456789101112131415161718193 unmodified lines23242526272829303132333435363738394041import torchfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom peft import PeftModelimport gradio as gr# Use GPU if availabledevice = "cuda" if torch.cuda.is_available() else "cpu"# Base model and adapter pathsbase_model_name = "microsoft/phi-2" # Pull from HF Hub directlyimport torchfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom peft import PeftModelimport gradio as grimport os# Use GPU if availabledevice = "cuda" if torch.cuda.is_available() else "cpu"3 unmodified linesadapter_path = "Shriti09/Microsoft-Phi-QLora" # Update with your Hugging Face repo path# Create an offload directory to store the model partsoffload_dir = "./offload" # Replace with your desired path# Ensure the offload directory existsos.makedirs(offload_dir, exist_ok=True)print("🔧 Loading base model...")base_model = AutoModelForCausalLM.from_pretrained(base_model_name,device_map="auto",torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,offload_dir=offload_dir # Set offload directory here)print("🔧 Loading LoRA adapter...")adapter_model = PeftModel.from_pretrained(base_model, adapter_path)1234567891011123 unmodified lines16171819202122232425262728293031323334353637import torchfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom peft import PeftModelimport gradio as grimport osfrom accelerate import Accelerator# Initialize the Accelerator to manage device placement and offloadingaccelerator = Accelerator()# Use GPU if availabledevice = "cuda" if torch.cuda.is_available() else "cpu"3 unmodified linesadapter_path = "Shriti09/Microsoft-Phi-QLora" # Update with your Hugging Face repo path# Create an offload directory to store the model partsoffload_dir = "./offload_dir" # Set offload directory here# Ensure the offload directory existsos.makedirs(offload_dir, exist_ok=True)print("🔧 Loading base model...")# Using the Accelerator to load the model and dispatch to the correct devicesbase_model = AutoModelForCausalLM.from_pretrained(base_model_name,device_map="auto", # Automatically map the model to available devicestorch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32)# Ensure the model is offloaded when necessarybase_model = accelerator.prepare(base_model)print("🔧 Loading LoRA adapter...")adapter_model = PeftModel.from_pretrained(base_model, adapter_path)
app.py+11-15