Paul commited on
Commit
0b7fa6f
·
1 Parent(s): ade9c14

update 2 endpoints

Browse files
.DS_Store ADDED
Binary file (8.2 kB). View file
 
__pycache__/ml_service.cpython-313.pyc CHANGED
Binary files a/__pycache__/ml_service.cpython-313.pyc and b/__pycache__/ml_service.cpython-313.pyc differ
 
__pycache__/schemas.cpython-313.pyc CHANGED
Binary files a/__pycache__/schemas.cpython-313.pyc and b/__pycache__/schemas.cpython-313.pyc differ
 
app.py CHANGED
@@ -1,30 +1,85 @@
 
 
1
  import gradio as gr
2
  from typing import List, Dict, Any
3
 
4
- from ml_service import get_ml_service
5
 
6
 
7
- def predict_labels(text: str) -> List[Dict[str, Any]]:
8
- service = get_ml_service()
9
- return service.predict(text)
 
 
 
10
 
 
 
 
 
 
 
 
 
11
 
12
- title = "ML Text Classification (DistilBERT)"
13
- description = (
14
- "Enter text to get multi-label predictions. Uses the model in `model/`."
15
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  with gr.Blocks(title=title) as demo:
18
  gr.Markdown(f"# {title}\n{description}")
19
- with gr.Row():
20
- inp = gr.Textbox(lines=6, label="Input Text", placeholder="Type text here…")
21
- with gr.Row():
22
- out = gr.JSON(label="Predictions")
23
- with gr.Row():
24
- btn = gr.Button("Predict")
25
-
26
- # Expose predict API at /predict
27
- btn.click(predict_labels, inputs=inp, outputs=out, api_name="predict")
 
 
 
28
 
29
 
30
  if __name__ == "__main__":
 
1
+ import json
2
+ import os
3
  import gradio as gr
4
  from typing import List, Dict, Any
5
 
6
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
7
 
8
 
9
+ class DualModelService:
10
+ def __init__(self, intent_dir: str = "./intent_model", tone_dir: str = "./tone_model"):
11
+ self.intent_dir = intent_dir
12
+ self.tone_dir = tone_dir
13
+ self._clf_cache: Dict[str, Any] = {}
14
+ self._labels_cache: Dict[str, List[str]] = {}
15
 
16
+ def _load_labels(self, model_dir: str) -> List[str]:
17
+ if model_dir in self._labels_cache:
18
+ return self._labels_cache[model_dir]
19
+ label_path = os.path.join(model_dir, "label_cols.json")
20
+ with open(label_path, "r", encoding="utf-8") as f:
21
+ labels = json.load(f)
22
+ self._labels_cache[model_dir] = labels
23
+ return labels
24
 
25
+ def _load_pipeline(self, model_dir: str):
26
+ if model_dir in self._clf_cache:
27
+ return self._clf_cache[model_dir]
28
+ model = AutoModelForSequenceClassification.from_pretrained(model_dir)
29
+ tokenizer = AutoTokenizer.from_pretrained(model_dir)
30
+ clf = pipeline(
31
+ "text-classification",
32
+ model=model,
33
+ tokenizer=tokenizer,
34
+ return_all_scores=True,
35
+ )
36
+ self._clf_cache[model_dir] = clf
37
+ return clf
38
+
39
+ def _predict_sorted(self, text: str, model_dir: str) -> List[Dict[str, float]]:
40
+ clf = self._load_pipeline(model_dir)
41
+ labels = self._load_labels(model_dir)
42
+ processed = text.replace("|||", "[SEP]")
43
+ scores = clf(processed)[0]
44
+ pairs = [(labels[idx], scores[idx]["score"]) for idx in range(len(labels))]
45
+ pairs_sorted = sorted(pairs, key=lambda x: x[1], reverse=True)
46
+ return [{"label": name, "score": float(score)} for name, score in pairs_sorted]
47
+
48
+ def predict_intent(self, text: str) -> List[Dict[str, float]]:
49
+ return self._predict_sorted(text, self.intent_dir)
50
+
51
+ def predict_tone(self, text: str) -> List[Dict[str, float]]:
52
+ return self._predict_sorted(text, self.tone_dir)
53
+
54
+
55
+ _dual_service = DualModelService()
56
+
57
+
58
+ def predict_intent(text: str) -> List[Dict[str, float]]:
59
+ return _dual_service.predict_intent(text)
60
+
61
+
62
+ def predict_tone(text: str) -> List[Dict[str, float]]:
63
+ return _dual_service.predict_tone(text)
64
+
65
+
66
+ title = "Text Classification: Intent & Tone"
67
+ description = "Enter text; get sorted labels with scores for Intent and Tone."
68
 
69
  with gr.Blocks(title=title) as demo:
70
  gr.Markdown(f"# {title}\n{description}")
71
+ with gr.Tabs():
72
+ with gr.TabItem("Intent"):
73
+ intent_in = gr.Textbox(lines=6, label="Input Text", placeholder="Type text here…")
74
+ intent_out = gr.JSON(label="Intent Predictions (sorted)")
75
+ intent_btn = gr.Button("Predict Intent")
76
+ intent_btn.click(predict_intent, inputs=intent_in, outputs=intent_out, api_name="/intent")
77
+
78
+ with gr.TabItem("Tone"):
79
+ tone_in = gr.Textbox(lines=6, label="Input Text", placeholder="Type text here…")
80
+ tone_out = gr.JSON(label="Tone Predictions (sorted)")
81
+ tone_btn = gr.Button("Predict Tone")
82
+ tone_btn.click(predict_tone, inputs=tone_in, outputs=tone_out, api_name="/tone")
83
 
84
 
85
  if __name__ == "__main__":
intent_model/.DS_Store ADDED
Binary file (6.15 kB). View file
 
intent_model/config.json ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "activation": "gelu",
3
+ "architectures": [
4
+ "DistilBertForSequenceClassification"
5
+ ],
6
+ "attention_dropout": 0.1,
7
+ "dim": 768,
8
+ "dropout": 0.1,
9
+ "dtype": "float32",
10
+ "hidden_dim": 3072,
11
+ "id2label": {
12
+ "0": "LABEL_0",
13
+ "1": "LABEL_1",
14
+ "2": "LABEL_2",
15
+ "3": "LABEL_3",
16
+ "4": "LABEL_4",
17
+ "5": "LABEL_5",
18
+ "6": "LABEL_6"
19
+ },
20
+ "initializer_range": 0.02,
21
+ "label2id": {
22
+ "LABEL_0": 0,
23
+ "LABEL_1": 1,
24
+ "LABEL_2": 2,
25
+ "LABEL_3": 3,
26
+ "LABEL_4": 4,
27
+ "LABEL_5": 5,
28
+ "LABEL_6": 6
29
+ },
30
+ "max_position_embeddings": 512,
31
+ "model_type": "distilbert",
32
+ "n_heads": 12,
33
+ "n_layers": 6,
34
+ "pad_token_id": 0,
35
+ "problem_type": "multi_label_classification",
36
+ "qa_dropout": 0.1,
37
+ "seq_classif_dropout": 0.2,
38
+ "sinusoidal_pos_embds": false,
39
+ "tie_weights_": true,
40
+ "transformers_version": "4.57.1",
41
+ "vocab_size": 30522
42
+ }
intent_model/label_cols.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ "partner_intent_distance",
3
+ "partner_intent_flirt",
4
+ "partner_intent_invite",
5
+ "partner_intent_rapport",
6
+ "partner_intent_reframe",
7
+ "partner_intent_smalltalk",
8
+ "partner_intent_test"
9
+ ]
intent_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
+ }
intent_model/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
intent_model/tokenizer_config.json ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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": false,
45
+ "cls_token": "[CLS]",
46
+ "do_lower_case": true,
47
+ "extra_special_tokens": {},
48
+ "mask_token": "[MASK]",
49
+ "model_max_length": 512,
50
+ "pad_token": "[PAD]",
51
+ "sep_token": "[SEP]",
52
+ "strip_accents": null,
53
+ "tokenize_chinese_chars": true,
54
+ "tokenizer_class": "DistilBertTokenizer",
55
+ "unk_token": "[UNK]"
56
+ }
intent_model/vocab.txt ADDED
The diff for this file is too large to render. See raw diff
 
tone_model/config.json ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "activation": "gelu",
3
+ "architectures": [
4
+ "DistilBertForSequenceClassification"
5
+ ],
6
+ "attention_dropout": 0.1,
7
+ "dim": 768,
8
+ "dropout": 0.1,
9
+ "dtype": "float32",
10
+ "hidden_dim": 3072,
11
+ "id2label": {
12
+ "0": "LABEL_0",
13
+ "1": "LABEL_1",
14
+ "2": "LABEL_2",
15
+ "3": "LABEL_3",
16
+ "4": "LABEL_4",
17
+ "5": "LABEL_5",
18
+ "6": "LABEL_6",
19
+ "7": "LABEL_7",
20
+ "8": "LABEL_8"
21
+ },
22
+ "initializer_range": 0.02,
23
+ "label2id": {
24
+ "LABEL_0": 0,
25
+ "LABEL_1": 1,
26
+ "LABEL_2": 2,
27
+ "LABEL_3": 3,
28
+ "LABEL_4": 4,
29
+ "LABEL_5": 5,
30
+ "LABEL_6": 6,
31
+ "LABEL_7": 7,
32
+ "LABEL_8": 8
33
+ },
34
+ "max_position_embeddings": 512,
35
+ "model_type": "distilbert",
36
+ "n_heads": 12,
37
+ "n_layers": 6,
38
+ "pad_token_id": 0,
39
+ "problem_type": "multi_label_classification",
40
+ "qa_dropout": 0.1,
41
+ "seq_classif_dropout": 0.2,
42
+ "sinusoidal_pos_embds": false,
43
+ "tie_weights_": true,
44
+ "transformers_version": "4.57.1",
45
+ "vocab_size": 30522
46
+ }
tone_model/label_cols.json ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ "partner_tone_assertive",
3
+ "partner_tone_curious",
4
+ "partner_tone_plain",
5
+ "partner_tone_playful",
6
+ "partner_tone_provocative",
7
+ "partner_tone_suggestive",
8
+ "partner_tone_uncertain",
9
+ "partner_tone_vulnerable",
10
+ "partner_tone_warm"
11
+ ]
tone_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
+ }
tone_model/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tone_model/tokenizer_config.json ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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": false,
45
+ "cls_token": "[CLS]",
46
+ "do_lower_case": true,
47
+ "extra_special_tokens": {},
48
+ "mask_token": "[MASK]",
49
+ "model_max_length": 512,
50
+ "pad_token": "[PAD]",
51
+ "sep_token": "[SEP]",
52
+ "strip_accents": null,
53
+ "tokenize_chinese_chars": true,
54
+ "tokenizer_class": "DistilBertTokenizer",
55
+ "unk_token": "[UNK]"
56
+ }
tone_model/vocab.txt ADDED
The diff for this file is too large to render. See raw diff