Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,27 +4,31 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
| 4 |
from peft import PeftModel, PeftConfig
|
| 5 |
import spaces
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Load model and tokenizer
|
| 8 |
MODEL_PATH = "sagar007/phi2_finetune"
|
| 9 |
-
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)
|
| 11 |
tokenizer.pad_token = tokenizer.eos_token
|
| 12 |
|
| 13 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
"microsoft/phi-2",
|
| 15 |
-
torch_dtype=torch.
|
| 16 |
device_map="auto",
|
| 17 |
trust_remote_code=True
|
| 18 |
)
|
| 19 |
|
| 20 |
peft_config = PeftConfig.from_pretrained(MODEL_PATH)
|
| 21 |
model = PeftModel.from_pretrained(base_model, MODEL_PATH)
|
|
|
|
| 22 |
model.eval()
|
| 23 |
|
| 24 |
@spaces.GPU(duration=60)
|
| 25 |
def generate_response(instruction, max_length=512):
|
| 26 |
prompt = f"Instruction: {instruction}\nResponse:"
|
| 27 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
| 28 |
|
| 29 |
with torch.no_grad():
|
| 30 |
outputs = model.generate(
|
|
|
|
| 4 |
from peft import PeftModel, PeftConfig
|
| 5 |
import spaces
|
| 6 |
|
| 7 |
+
# Check if CUDA is available and set the device
|
| 8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
+
print(f"Using device: {device}")
|
| 10 |
+
|
| 11 |
# Load model and tokenizer
|
| 12 |
MODEL_PATH = "sagar007/phi2_finetune"
|
|
|
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)
|
| 14 |
tokenizer.pad_token = tokenizer.eos_token
|
| 15 |
|
| 16 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
"microsoft/phi-2",
|
| 18 |
+
torch_dtype=torch.float16 if device.type == "cuda" else torch.float32,
|
| 19 |
device_map="auto",
|
| 20 |
trust_remote_code=True
|
| 21 |
)
|
| 22 |
|
| 23 |
peft_config = PeftConfig.from_pretrained(MODEL_PATH)
|
| 24 |
model = PeftModel.from_pretrained(base_model, MODEL_PATH)
|
| 25 |
+
model.to(device)
|
| 26 |
model.eval()
|
| 27 |
|
| 28 |
@spaces.GPU(duration=60)
|
| 29 |
def generate_response(instruction, max_length=512):
|
| 30 |
prompt = f"Instruction: {instruction}\nResponse:"
|
| 31 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 32 |
|
| 33 |
with torch.no_grad():
|
| 34 |
outputs = model.generate(
|