Spaces:
Configuration error
Configuration error
Upload model_utils.py
Browse files- model/model_utils.py +17 -0
model/model_utils.py
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
def load_model():
|
| 5 |
+
model_name = "bigcode/starcoder"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
|
| 8 |
+
model.eval()
|
| 9 |
+
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
model.to(device)
|
| 12 |
+
return tokenizer, model, device
|
| 13 |
+
|
| 14 |
+
def generate_explanation(prompt, tokenizer, model, device):
|
| 15 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 16 |
+
output = model.generate(**inputs, max_new_tokens=512, temperature=0.7)
|
| 17 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|