Text Classification
Transformers
Safetensors
PyTorch
qwen2
text-generation
causal-lm
qwen
decision-making
system-one
reasoning
jev
jevbench
joint-expected-value
classification
qlora
agent
decision-model
fast-inference
text-embeddings-inference
Instructions to use devankit7873/QJev-1.5B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use devankit7873/QJev-1.5B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="devankit7873/QJev-1.5B")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("devankit7873/QJev-1.5B") model = AutoModelForCausalLM.from_pretrained("devankit7873/QJev-1.5B", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload qjev.py with huggingface_hub
Browse files
qjev.py
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
QJev: High-Speed System 1 Decision Model Wrapper.
|
| 3 |
+
Supports Full Fused, 4-Bit NF4 Quantized, and Modular LoRA Adapter editions.
|
| 4 |
+
|
| 5 |
+
Usage:
|
| 6 |
+
from qjev import QJev
|
| 7 |
+
|
| 8 |
+
# Load any edition in 1 line:
|
| 9 |
+
model = QJev.from_pretrained("devankit7873/QJev-1.5B")
|
| 10 |
+
# or model = QJev.from_pretrained("devankit7873/QJev-1.5B-4bit")
|
| 11 |
+
# or model = QJev.from_pretrained("devankit7873/QJev-1.5B-LoRA")
|
| 12 |
+
|
| 13 |
+
result = model.decide(
|
| 14 |
+
context="Production database CPU spiked to 100% and connection pool is exhausted.",
|
| 15 |
+
choices=[
|
| 16 |
+
"Trigger immediate automated failover to standby replica",
|
| 17 |
+
"Restart primary database daemon",
|
| 18 |
+
"Double connection pool limits",
|
| 19 |
+
"Silence alerts and monitor"
|
| 20 |
+
]
|
| 21 |
+
)
|
| 22 |
+
print(result["best_choice"], result["confidence"])
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
import os
|
| 26 |
+
import argparse
|
| 27 |
+
import json
|
| 28 |
+
from typing import List, Dict, Any, Optional, Union
|
| 29 |
+
import torch
|
| 30 |
+
import torch.nn as nn
|
| 31 |
+
import torch.nn.functional as F
|
| 32 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 33 |
+
from safetensors.torch import load_file
|
| 34 |
+
from huggingface_hub import hf_hub_download
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class DecisionHead(nn.Module):
|
| 38 |
+
def __init__(self, hidden_size: int = 1536, intermediate_size: int = 512, max_choices: int = 10, dropout: float = 0.1):
|
| 39 |
+
super().__init__()
|
| 40 |
+
self.max_choices = max_choices
|
| 41 |
+
self.norm = nn.LayerNorm(hidden_size)
|
| 42 |
+
self.dense = nn.Linear(hidden_size, intermediate_size)
|
| 43 |
+
self.act = nn.GELU()
|
| 44 |
+
self.dropout = nn.Dropout(dropout)
|
| 45 |
+
self.out_proj = nn.Linear(intermediate_size, max_choices)
|
| 46 |
+
self.score_proj = nn.Linear(intermediate_size, 1)
|
| 47 |
+
|
| 48 |
+
def forward(self, x: torch.Tensor, num_choices: Optional[torch.Tensor] = None) -> torch.Tensor:
|
| 49 |
+
h = self.dropout(self.act(self.dense(self.norm(x))))
|
| 50 |
+
logits = self.out_proj(h)
|
| 51 |
+
if num_choices is not None:
|
| 52 |
+
batch_size = logits.size(0)
|
| 53 |
+
seq_range = torch.arange(self.max_choices, device=logits.device).unsqueeze(0).expand(batch_size, -1)
|
| 54 |
+
mask = seq_range >= num_choices.unsqueeze(1)
|
| 55 |
+
logits = logits.masked_fill(mask, -1e4)
|
| 56 |
+
return logits
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class QJev:
|
| 60 |
+
"""
|
| 61 |
+
Universal user-friendly client for QJev-1.5B decision models.
|
| 62 |
+
"""
|
| 63 |
+
def __init__(self, model_id_or_path: str = "devankit7873/QJev-1.5B", device: Optional[str] = None):
|
| 64 |
+
if device is None:
|
| 65 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 66 |
+
else:
|
| 67 |
+
self.device = torch.device(device)
|
| 68 |
+
|
| 69 |
+
is_cuda = self.device.type == "cuda"
|
| 70 |
+
dtype = torch.bfloat16 if is_cuda and torch.cuda.is_bf16_supported() else (torch.float16 if is_cuda else torch.float32)
|
| 71 |
+
|
| 72 |
+
print(f"Loading QJev from '{model_id_or_path}' on {self.device}...")
|
| 73 |
+
|
| 74 |
+
# Detect LoRA adapter repo vs Full/4-bit repo
|
| 75 |
+
is_lora = "lora" in model_id_or_path.lower() or os.path.exists(os.path.join(model_id_or_path, "adapter_config.json"))
|
| 76 |
+
|
| 77 |
+
if is_lora:
|
| 78 |
+
from peft import PeftModel
|
| 79 |
+
base_id = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 80 |
+
if os.path.exists("models/Qwen2.5-1.5B-Instruct"):
|
| 81 |
+
base_id = "models/Qwen2.5-1.5B-Instruct"
|
| 82 |
+
|
| 83 |
+
self.tokenizer = AutoTokenizer.from_pretrained(base_id, trust_remote_code=True)
|
| 84 |
+
base_model = AutoModelForCausalLM.from_pretrained(base_id, torch_dtype=dtype, device_map=self.device)
|
| 85 |
+
self.model = PeftModel.from_pretrained(base_model, model_id_or_path)
|
| 86 |
+
self.model.eval()
|
| 87 |
+
else:
|
| 88 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_id_or_path, trust_remote_code=True)
|
| 89 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 90 |
+
model_id_or_path,
|
| 91 |
+
torch_dtype=dtype if not "4bit" in model_id_or_path.lower() else None,
|
| 92 |
+
device_map=self.device
|
| 93 |
+
)
|
| 94 |
+
self.model.eval()
|
| 95 |
+
|
| 96 |
+
if self.tokenizer.pad_token is None:
|
| 97 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 98 |
+
|
| 99 |
+
# Load Decision Head
|
| 100 |
+
self.head = DecisionHead().to(device=self.device, dtype=dtype)
|
| 101 |
+
if os.path.isdir(model_id_or_path) and os.path.exists(os.path.join(model_id_or_path, "decision_head.safetensors")):
|
| 102 |
+
head_path = os.path.join(model_id_or_path, "decision_head.safetensors")
|
| 103 |
+
elif os.path.isdir(model_id_or_path) and os.path.exists(os.path.join(model_id_or_path, "decision_head.pt")):
|
| 104 |
+
head_path = os.path.join(model_id_or_path, "decision_head.pt")
|
| 105 |
+
else:
|
| 106 |
+
head_path = hf_hub_download(repo_id=model_id_or_path, filename="decision_head.safetensors")
|
| 107 |
+
|
| 108 |
+
if head_path.endswith(".safetensors"):
|
| 109 |
+
state = load_file(head_path)
|
| 110 |
+
else:
|
| 111 |
+
ckpt = torch.load(head_path, map_location="cpu")
|
| 112 |
+
state = ckpt.get("head_state_dict", ckpt)
|
| 113 |
+
|
| 114 |
+
self.head.load_state_dict(state)
|
| 115 |
+
self.head.eval()
|
| 116 |
+
print("Ready for real-time decisions (<35 ms)!")
|
| 117 |
+
|
| 118 |
+
@classmethod
|
| 119 |
+
def from_pretrained(cls, model_id_or_path: str = "devankit7873/QJev-1.5B", device: Optional[str] = None) -> "QJev":
|
| 120 |
+
return cls(model_id_or_path=model_id_or_path, device=device)
|
| 121 |
+
|
| 122 |
+
def _format_prompt(self, context: str, choices: List[str], question: Optional[str] = None) -> str:
|
| 123 |
+
choices_str = "\n".join([f"[{i+1}] {c}" for i, c in enumerate(choices)])
|
| 124 |
+
q_str = f"\n\nQuestion:\n{question.strip()}" if question else "\n\nQuestion:\nSelect the most appropriate option:"
|
| 125 |
+
return (
|
| 126 |
+
"<|im_start|>system\n"
|
| 127 |
+
"You are an expert decision judge. Evaluate the situation and determine the likelihood for each option.<|im_end|>\n"
|
| 128 |
+
f"<|im_start|>user\nContext:\n{context.strip()}{q_str}\n\nOptions:\n{choices_str}<|im_end|>\n"
|
| 129 |
+
"<|im_start|>assistant\nDecision:"
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
@torch.no_grad()
|
| 133 |
+
def decide(self, context: str, choices: List[str], question: Optional[str] = None) -> Dict[str, Any]:
|
| 134 |
+
"""
|
| 135 |
+
Evaluate context against up to 10 choices in a single forward pass.
|
| 136 |
+
Returns:
|
| 137 |
+
{
|
| 138 |
+
"best_choice": str,
|
| 139 |
+
"confidence": float (percentage, e.g. 86.72),
|
| 140 |
+
"probabilities": {choice: percentage, ...}
|
| 141 |
+
}
|
| 142 |
+
"""
|
| 143 |
+
if not choices:
|
| 144 |
+
raise ValueError("choices list cannot be empty")
|
| 145 |
+
if len(choices) > 10:
|
| 146 |
+
raise ValueError(f"QJev supports up to 10 choices, got {len(choices)}")
|
| 147 |
+
|
| 148 |
+
num_c = len(choices)
|
| 149 |
+
prompt = self._format_prompt(context, choices, question)
|
| 150 |
+
inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device)
|
| 151 |
+
|
| 152 |
+
# Access internal transformer
|
| 153 |
+
if hasattr(self.model, "peft_config"):
|
| 154 |
+
inner = getattr(self.model.base_model.model, "model", self.model.base_model.model)
|
| 155 |
+
else:
|
| 156 |
+
inner = getattr(self.model, "model", self.model)
|
| 157 |
+
|
| 158 |
+
out = inner(inputs["input_ids"], attention_mask=inputs["attention_mask"])
|
| 159 |
+
last_hidden = out.last_hidden_state[:, -1, :]
|
| 160 |
+
|
| 161 |
+
logits = self.head(last_hidden, num_choices=torch.tensor([num_c], device=self.device))
|
| 162 |
+
probs = F.softmax(logits[0, :num_c], dim=-1).cpu().tolist()
|
| 163 |
+
|
| 164 |
+
best_idx = int(torch.argmax(logits[0, :num_c]).item())
|
| 165 |
+
|
| 166 |
+
return {
|
| 167 |
+
"best_choice": choices[best_idx],
|
| 168 |
+
"confidence": round(probs[best_idx] * 100, 2),
|
| 169 |
+
"probabilities": {c: round(p * 100, 2) for c, p in zip(choices, probs)}
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
# Alias for flexibility
|
| 173 |
+
predict = decide
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def main():
|
| 177 |
+
parser = argparse.ArgumentParser(description="QJev: Instant Single-Pass Decision Model")
|
| 178 |
+
parser.add_argument("--model", type=str, default="devankit7873/QJev-1.5B", help="Model repo or local path")
|
| 179 |
+
parser.add_argument("--context", type=str, default=None, help="Decision situation/context")
|
| 180 |
+
parser.add_argument("--choices", nargs="+", default=None, help="List of candidate choices (up to 10)")
|
| 181 |
+
args = parser.parse_args()
|
| 182 |
+
|
| 183 |
+
# Prefer local export folder if available locally for speed
|
| 184 |
+
model_id = args.model
|
| 185 |
+
if model_id == "devankit7873/QJev-1.5B" and os.path.exists("export/Qwen2.5-1.5B-Decision-Head-Merged"):
|
| 186 |
+
model_id = "export/Qwen2.5-1.5B-Decision-Head-Merged"
|
| 187 |
+
|
| 188 |
+
client = QJev.from_pretrained(model_id)
|
| 189 |
+
|
| 190 |
+
if args.context and args.choices:
|
| 191 |
+
res = client.decide(args.context, args.choices)
|
| 192 |
+
else:
|
| 193 |
+
print("\n--- Example: Incident Triage Decision ---")
|
| 194 |
+
context = "Production database primary replica CPU spiked to 100% and connection pool is exhausted."
|
| 195 |
+
choices = [
|
| 196 |
+
"Trigger immediate automated failover to standby replica",
|
| 197 |
+
"Restart primary database daemon",
|
| 198 |
+
"Double connection pool limits without scaling compute",
|
| 199 |
+
"Silence alerts and monitor"
|
| 200 |
+
]
|
| 201 |
+
res = client.decide(context, choices)
|
| 202 |
+
|
| 203 |
+
print("\nDecision Result:")
|
| 204 |
+
print(f"👉 Recommended: {res['best_choice']} ({res['confidence']}%)")
|
| 205 |
+
print("\nProbabilities:")
|
| 206 |
+
for choice, prob in res["probabilities"].items():
|
| 207 |
+
print(f" [{prob:>5.2f}%] {choice}")
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
if __name__ == "__main__":
|
| 211 |
+
main()
|