ChandruK commited on
Commit
84df194
·
verified ·
1 Parent(s): af8f15a

updated readme: corrected the model usage code

Browse files
Files changed (1) hide show
  1. README.md +3 -20
README.md CHANGED
@@ -43,25 +43,15 @@ import torch
43
  import torch.nn.functional as F
44
  from transformers import AutoModelForSequenceClassification, AutoTokenizer
45
 
46
- # Load ModernBERT NLI model and tokenizer
47
- MODEL_ID = "dleemiller/ModernCE-base-nli"
48
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
49
 
50
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
51
  model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID).to(device)
52
  model.eval()
53
 
54
- # Dynamically resolve Entailment label index from config
55
- label_mapping = model.config.id2label
56
- ENTAILMENT_IDX = None
57
- for idx, label_name in label_mapping.items():
58
- if label_name.lower() in ["entailment", "entail"]:
59
- ENTAILMENT_IDX = idx
60
- break
61
- if ENTAILMENT_IDX is None:
62
- ENTAILMENT_IDX = 1
63
-
64
- # 1. Define Candidate Labels and Hypothesis Template
65
  CANDIDATE_LABELS = [
66
  "a Word document creation request",
67
  "an Excel spreadsheet creation request",
@@ -72,14 +62,11 @@ CANDIDATE_LABELS = [
72
  ]
73
  HYPOTHESIS_TEMPLATE = "This user prompt is {}."
74
 
75
- # 2. Input Prompt
76
  prompt = "Generate a formal project proposal in Word format."
77
 
78
- # 3. Construct Premise-Hypothesis Pairs
79
  hypotheses = [HYPOTHESIS_TEMPLATE.format(label) for label in CANDIDATE_LABELS]
80
  premises = [prompt] * len(CANDIDATE_LABELS)
81
 
82
- # 4. Tokenize Pairs
83
  inputs = tokenizer(
84
  text=premises,
85
  text_pair=hypotheses,
@@ -89,20 +76,17 @@ inputs = tokenizer(
89
  return_tensors="pt",
90
  ).to(device)
91
 
92
- # 5. Model Inference
93
  with torch.no_grad():
94
  outputs = model(**inputs)
95
  logits = outputs.logits
96
  entailment_logits = logits[:, ENTAILMENT_IDX] if logits.shape[-1] >= 3 else logits[:, 1]
97
 
98
- # Softmax probability normalization with temperature scaling
99
  probabilities = F.softmax(entailment_logits / 0.5, dim=-1)
100
  top_prob, top_idx = torch.max(probabilities, dim=-1)
101
 
102
  predicted_label = CANDIDATE_LABELS[top_idx.item()]
103
  confidence = top_prob.item() * 100
104
 
105
- # Build dictionary containing key details and all category probabilities
106
  label_scores = {
107
  label: round(prob.item() * 100, 2)
108
  for label, prob in zip(CANDIDATE_LABELS, probabilities)
@@ -115,6 +99,5 @@ output_json_data = {
115
  "all_scores": label_scores,
116
  }
117
 
118
- # Output as formatted JSON string
119
  json_output = json.dumps(output_json_data, indent=2)
120
  print(json_output)
 
43
  import torch.nn.functional as F
44
  from transformers import AutoModelForSequenceClassification, AutoTokenizer
45
 
46
+ MODEL_ID = "ChandruK/MarkOne"
 
47
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
48
 
49
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
50
  model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID).to(device)
51
  model.eval()
52
 
53
+ ENTAILMENT_IDX = 1
54
+
 
 
 
 
 
 
 
 
 
55
  CANDIDATE_LABELS = [
56
  "a Word document creation request",
57
  "an Excel spreadsheet creation request",
 
62
  ]
63
  HYPOTHESIS_TEMPLATE = "This user prompt is {}."
64
 
 
65
  prompt = "Generate a formal project proposal in Word format."
66
 
 
67
  hypotheses = [HYPOTHESIS_TEMPLATE.format(label) for label in CANDIDATE_LABELS]
68
  premises = [prompt] * len(CANDIDATE_LABELS)
69
 
 
70
  inputs = tokenizer(
71
  text=premises,
72
  text_pair=hypotheses,
 
76
  return_tensors="pt",
77
  ).to(device)
78
 
 
79
  with torch.no_grad():
80
  outputs = model(**inputs)
81
  logits = outputs.logits
82
  entailment_logits = logits[:, ENTAILMENT_IDX] if logits.shape[-1] >= 3 else logits[:, 1]
83
 
 
84
  probabilities = F.softmax(entailment_logits / 0.5, dim=-1)
85
  top_prob, top_idx = torch.max(probabilities, dim=-1)
86
 
87
  predicted_label = CANDIDATE_LABELS[top_idx.item()]
88
  confidence = top_prob.item() * 100
89
 
 
90
  label_scores = {
91
  label: round(prob.item() * 100, 2)
92
  for label, prob in zip(CANDIDATE_LABELS, probabilities)
 
99
  "all_scores": label_scores,
100
  }
101
 
 
102
  json_output = json.dumps(output_json_data, indent=2)
103
  print(json_output)