Create inference.py
Browse files- inference.py +46 -0
inference.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# inference.py
|
| 2 |
+
# ==============================
|
| 3 |
+
# Single inference entry point
|
| 4 |
+
# Handles Base / Core / Skill transparently
|
| 5 |
+
# ==============================
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
from model_loader import load_model
|
| 9 |
+
from router import route_skill
|
| 10 |
+
import config
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def generate_response(prompt: str):
|
| 14 |
+
"""
|
| 15 |
+
Main inference function.
|
| 16 |
+
Decides skill softly, then generates response.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
# Decide skill (or None)
|
| 20 |
+
skill = route_skill(prompt)
|
| 21 |
+
|
| 22 |
+
# Load model (Base + optional Core + optional Skill)
|
| 23 |
+
model, tokenizer = load_model(skill)
|
| 24 |
+
|
| 25 |
+
# Prepare input
|
| 26 |
+
inputs = tokenizer(
|
| 27 |
+
prompt,
|
| 28 |
+
return_tensors="pt"
|
| 29 |
+
).to(model.device)
|
| 30 |
+
|
| 31 |
+
# Generate
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
output = model.generate(
|
| 34 |
+
**inputs,
|
| 35 |
+
max_new_tokens=config.MAX_NEW_TOKENS,
|
| 36 |
+
temperature=config.TEMPERATURE,
|
| 37 |
+
top_p=config.TOP_P,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Decode
|
| 41 |
+
response = tokenizer.decode(
|
| 42 |
+
output[0][inputs["input_ids"].shape[-1]:],
|
| 43 |
+
skip_special_tokens=True
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
return response.strip()
|