Spaces:
Sleeping
Sleeping
EcoFriendlyWoodVerneer commited on
Commit ·
e7d4e19
1
Parent(s): 16f422b
Base
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
+
|
| 6 |
+
# ----------------------------
|
| 7 |
+
# Intent mapping (inlined)
|
| 8 |
+
# ----------------------------
|
| 9 |
+
ID_TO_INTENT = {
|
| 10 |
+
0: "price_check",
|
| 11 |
+
1: "product_information",
|
| 12 |
+
2: "product_search",
|
| 13 |
+
3: "promo_discount",
|
| 14 |
+
4: "return_refund",
|
| 15 |
+
5: "stock_check",
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
INTENT_TO_ID = {intent: idx for idx, intent in ID_TO_INTENT.items()}
|
| 19 |
+
|
| 20 |
+
def get_intent_from_id(label_id: int) -> str:
|
| 21 |
+
return ID_TO_INTENT.get(label_id, f"unknown_intent_{label_id}")
|
| 22 |
+
|
| 23 |
+
# ----------------------------
|
| 24 |
+
# Model load
|
| 25 |
+
# ----------------------------
|
| 26 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 27 |
+
MODEL_DIR = os.path.join(BASE_DIR, "models", "intent_bert_model") # adjust if your folder name differs
|
| 28 |
+
|
| 29 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 30 |
+
|
| 31 |
+
tok = AutoTokenizer.from_pretrained(MODEL_DIR)
|
| 32 |
+
mdl = AutoModelForSequenceClassification.from_pretrained(MODEL_DIR).to(device)
|
| 33 |
+
mdl.eval()
|
| 34 |
+
|
| 35 |
+
# ----------------------------
|
| 36 |
+
# API function
|
| 37 |
+
# ----------------------------
|
| 38 |
+
def intent_only(message: str):
|
| 39 |
+
message = (message or "").strip()
|
| 40 |
+
if not message:
|
| 41 |
+
return {"intent": None, "confidence": 0.0}
|
| 42 |
+
|
| 43 |
+
inputs = tok(message, return_tensors="pt", truncation=True, max_length=256).to(device)
|
| 44 |
+
|
| 45 |
+
with torch.no_grad():
|
| 46 |
+
logits = mdl(**inputs).logits[0]
|
| 47 |
+
probs = torch.softmax(logits, dim=-1)
|
| 48 |
+
|
| 49 |
+
label_id = int(torch.argmax(probs).item())
|
| 50 |
+
confidence = float(torch.max(probs).item())
|
| 51 |
+
|
| 52 |
+
return {
|
| 53 |
+
"intent": get_intent_from_id(label_id),
|
| 54 |
+
"confidence": confidence,
|
| 55 |
+
"label_id": label_id, # remove later if you want
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
# ----------------------------
|
| 59 |
+
# Gradio app (minimal UI, API-first)
|
| 60 |
+
# ----------------------------
|
| 61 |
+
demo = gr.Interface(
|
| 62 |
+
fn=intent_only,
|
| 63 |
+
inputs=gr.Textbox(label="message"),
|
| 64 |
+
outputs=gr.JSON(label="intent"),
|
| 65 |
+
title="Pure Intent Classifier (No GenAI)",
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
demo.api_name = "/intent"
|
| 69 |
+
demo.launch()
|
models/intent_bert_model/config.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"BertForSequenceClassification"
|
| 4 |
+
],
|
| 5 |
+
"attention_probs_dropout_prob": 0.1,
|
| 6 |
+
"classifier_dropout": null,
|
| 7 |
+
"dtype": "float32",
|
| 8 |
+
"gradient_checkpointing": false,
|
| 9 |
+
"hidden_act": "gelu",
|
| 10 |
+
"hidden_dropout_prob": 0.1,
|
| 11 |
+
"hidden_size": 768,
|
| 12 |
+
"id2label": {
|
| 13 |
+
"0": "LABEL_0",
|
| 14 |
+
"1": "LABEL_1",
|
| 15 |
+
"2": "LABEL_2",
|
| 16 |
+
"3": "LABEL_3",
|
| 17 |
+
"4": "LABEL_4",
|
| 18 |
+
"5": "LABEL_5"
|
| 19 |
+
},
|
| 20 |
+
"initializer_range": 0.02,
|
| 21 |
+
"intermediate_size": 3072,
|
| 22 |
+
"label2id": {
|
| 23 |
+
"LABEL_0": 0,
|
| 24 |
+
"LABEL_1": 1,
|
| 25 |
+
"LABEL_2": 2,
|
| 26 |
+
"LABEL_3": 3,
|
| 27 |
+
"LABEL_4": 4,
|
| 28 |
+
"LABEL_5": 5
|
| 29 |
+
},
|
| 30 |
+
"layer_norm_eps": 1e-12,
|
| 31 |
+
"max_position_embeddings": 512,
|
| 32 |
+
"model_type": "bert",
|
| 33 |
+
"num_attention_heads": 12,
|
| 34 |
+
"num_hidden_layers": 12,
|
| 35 |
+
"pad_token_id": 0,
|
| 36 |
+
"position_embedding_type": "absolute",
|
| 37 |
+
"problem_type": "single_label_classification",
|
| 38 |
+
"transformers_version": "4.57.6",
|
| 39 |
+
"type_vocab_size": 2,
|
| 40 |
+
"use_cache": true,
|
| 41 |
+
"vocab_size": 30522
|
| 42 |
+
}
|
models/intent_bert_model/model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:74f4cb1c8682cb84b405be4cd461ea889c88c2e1796a7e129030f2d4eab14b16
|
| 3 |
+
size 437970952
|
models/intent_bert_model/special_tokens_map.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": "[CLS]",
|
| 3 |
+
"mask_token": "[MASK]",
|
| 4 |
+
"pad_token": "[PAD]",
|
| 5 |
+
"sep_token": "[SEP]",
|
| 6 |
+
"unk_token": "[UNK]"
|
| 7 |
+
}
|
models/intent_bert_model/tokenizer_config.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "[PAD]",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"100": {
|
| 12 |
+
"content": "[UNK]",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"101": {
|
| 20 |
+
"content": "[CLS]",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"102": {
|
| 28 |
+
"content": "[SEP]",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"103": {
|
| 36 |
+
"content": "[MASK]",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"clean_up_tokenization_spaces": true,
|
| 45 |
+
"cls_token": "[CLS]",
|
| 46 |
+
"do_basic_tokenize": true,
|
| 47 |
+
"do_lower_case": true,
|
| 48 |
+
"extra_special_tokens": {},
|
| 49 |
+
"mask_token": "[MASK]",
|
| 50 |
+
"model_max_length": 512,
|
| 51 |
+
"never_split": null,
|
| 52 |
+
"pad_token": "[PAD]",
|
| 53 |
+
"sep_token": "[SEP]",
|
| 54 |
+
"strip_accents": null,
|
| 55 |
+
"tokenize_chinese_chars": true,
|
| 56 |
+
"tokenizer_class": "BertTokenizer",
|
| 57 |
+
"unk_token": "[UNK]"
|
| 58 |
+
}
|
models/intent_bert_model/vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|