samithcs commited on
Commit
e3629ce
·
verified ·
1 Parent(s): 5393fcc

Update src/components/model_nlp_intent.py

Browse files
Files changed (1) hide show
  1. src/components/model_nlp_intent.py +18 -6
src/components/model_nlp_intent.py CHANGED
@@ -5,21 +5,33 @@ from io import BytesIO
5
  import joblib
6
 
7
  # Load once at startup
8
- MODEL_PATH = "samithcs/nlp_intent_model/nlp_intent/intent_model"
9
- TOKENIZER_PATH = "samithcs/nlp_intent_model/nlp_intent/intent_tokenizer"
 
 
10
  LABEL_URL = "https://huggingface.co/samithcs/nlp_intent_model/resolve/main/nlp_intent/label_encoder.joblib"
11
 
12
- model = TFDistilBertForSequenceClassification.from_pretrained(MODEL_PATH, from_tf=True)
13
- tokenizer = DistilBertTokenizer.from_pretrained(TOKENIZER_PATH)
 
 
 
 
 
 
 
 
 
 
14
  response = requests.get(LABEL_URL)
15
  label_encoder = joblib.load(BytesIO(response.content))
16
 
17
 
18
-
19
  def predict_intent(text: str) -> dict:
20
  inputs = tokenizer(text, return_tensors="tf", truncation=True, padding=True, max_length=128)
21
  outputs = model(inputs)
22
  predicted_class = tf.argmax(outputs.logits, axis=1).numpy()[0]
23
  intent = label_encoder.inverse_transform([predicted_class])[0]
24
  confidence = float(tf.nn.softmax(outputs.logits)[0][predicted_class].numpy())
25
- return {"intent": intent, "confidence": confidence}
 
 
5
  import joblib
6
 
7
  # Load once at startup
8
+
9
+ MODEL_URL = "samithcs/nlp_intent_model"
10
+ MODEL_SUBFOLDER = "nlp_intent/intent_model"
11
+ TOKENIZER_SUBFOLDER = "nlp_intent/intent_tokenizer"
12
  LABEL_URL = "https://huggingface.co/samithcs/nlp_intent_model/resolve/main/nlp_intent/label_encoder.joblib"
13
 
14
+ # Load model and tokenizer with subfolder parameter
15
+ model = TFDistilBertForSequenceClassification.from_pretrained(
16
+ MODEL_URL,
17
+ subfolder=MODEL_SUBFOLDER,
18
+ from_tf=True
19
+ )
20
+ tokenizer = DistilBertTokenizer.from_pretrained(
21
+ MODEL_URL,
22
+ subfolder=TOKENIZER_SUBFOLDER
23
+ )
24
+
25
+ # Load label encoder
26
  response = requests.get(LABEL_URL)
27
  label_encoder = joblib.load(BytesIO(response.content))
28
 
29
 
 
30
  def predict_intent(text: str) -> dict:
31
  inputs = tokenizer(text, return_tensors="tf", truncation=True, padding=True, max_length=128)
32
  outputs = model(inputs)
33
  predicted_class = tf.argmax(outputs.logits, axis=1).numpy()[0]
34
  intent = label_encoder.inverse_transform([predicted_class])[0]
35
  confidence = float(tf.nn.softmax(outputs.logits)[0][predicted_class].numpy())
36
+
37
+ return {"intent": intent, "confidence": confidence}