edvard-exe commited on
Commit
bf7d9e2
·
verified ·
1 Parent(s): 1cd11b5

Upload 9 files

Browse files
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Gradio
2
+ import gradio as gr
3
+
4
+ # ML
5
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
6
+ import torch
7
+ from typing import Tuple, Dict
8
+
9
+
10
+ model_path = "./"
11
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
12
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
13
+ model.eval()
14
+
15
+ reverse_label_mapping = {0: 'gpt-4.1-nano', 1: 'gpt-4.1', 2: 'o4-mini'}
16
+
17
+ def route_query(query: str) -> Tuple[str, float, Dict[str, str]]:
18
+ """
19
+ Route query endpoint
20
+ """
21
+
22
+ if not query.strip():
23
+ return "Please enter a query", 0.0, {}
24
+
25
+
26
+ inputs = tokenizer(
27
+ query,
28
+ padding='max_length',
29
+ truncation=True,
30
+ max_length=128,
31
+ return_tensors='pt'
32
+ )
33
+
34
+ with torch.no_grad():
35
+ outputs = model(**inputs)
36
+ predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
37
+ predicted_class = torch.argmax(predictions, dim=-1)
38
+ confidence = torch.max(predictions, dim=-1)[0]
39
+
40
+ probs = predictions.cpu().numpy()[0]
41
+ probabilities = {reverse_label_mapping[i]: f"{prob:.3f}" for i, prob in enumerate(probs)}
42
+ recommended_model = reverse_label_mapping[predicted_class.item()]
43
+
44
+ return recommended_model, f"{confidence.item():.3f}", probabilities
45
+
46
+ iface = gr.Interface(
47
+ fn=route_query,
48
+ inputs=gr.Textbox(lines=2, placeholder="Enter your query here..."),
49
+ outputs=[
50
+ gr.Textbox(label="Recommended Model"),
51
+ gr.Textbox(label="Confidence"),
52
+ gr.JSON(label="All Probabilities")
53
+ ],
54
+ title="GPT Router Model",
55
+ description="Enter a query to get routing recommendation to the appropriate GPT model"
56
+ )
57
+
58
+ if __name__ == "__main__":
59
+ iface.launch()
config.json ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "activation": "gelu",
3
+ "architectures": [
4
+ "DistilBertForSequenceClassification"
5
+ ],
6
+ "attention_dropout": 0.1,
7
+ "dim": 768,
8
+ "dropout": 0.3,
9
+ "hidden_dim": 3072,
10
+ "id2label": {
11
+ "0": "LABEL_0",
12
+ "1": "LABEL_1",
13
+ "2": "LABEL_2"
14
+ },
15
+ "initializer_range": 0.02,
16
+ "label2id": {
17
+ "LABEL_0": 0,
18
+ "LABEL_1": 1,
19
+ "LABEL_2": 2
20
+ },
21
+ "max_position_embeddings": 512,
22
+ "model_type": "distilbert",
23
+ "n_heads": 12,
24
+ "n_layers": 6,
25
+ "output_past": true,
26
+ "pad_token_id": 0,
27
+ "qa_dropout": 0.1,
28
+ "seq_classif_dropout": 0.2,
29
+ "sinusoidal_pos_embds": false,
30
+ "tie_weights_": true,
31
+ "torch_dtype": "float32",
32
+ "transformers_version": "4.52.2",
33
+ "vocab_size": 28996
34
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fc27a6a1ef282dfbbb5483e34b937c61b71adf685db6f70ab4f4cbe6c96663b0
3
+ size 263147764
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
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
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": false,
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
+ }
training_info.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "distilbert",
3
+ "task": "classification",
4
+ "num_labels": 3,
5
+ "training_steps": 1224,
6
+ "learning_rate": 2e-05,
7
+ "batch_size": 16,
8
+ "epochs": 3
9
+ }
vocab.txt ADDED
The diff for this file is too large to render. See raw diff