File size: 4,768 Bytes
945a9df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e151ad4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import gradio as gr
import onnx
from huggingface_hub import HfApi
import json
import sys
import os
import io
import requests
from urllib.parse import urlparse


HfClient = HfApi();

ONNX_PREFERED = [
    "model.onnx",
    "onnx/model.onnx"    
]

ONNX_CACHE = {
    'models': {
        
    }
}

def is_url(path):
    try:
        result = urlparse(path)
        return all([result.scheme, result.netloc])
    except ValueError:
        return False


def load_model(path):
    if is_url(path):
        print(f"Downloading model from: {path}...", file=sys.stderr)
        try:
            response = requests.get(path)
            response.raise_for_status() # Check for HTTP errors
            # Load from binary stream
            return onnx.load(io.BytesIO(response.content))
        except requests.exceptions.RequestException as e:
            print(f"Error downloading model: {e}", file=sys.stderr)
            sys.exit(1)
    else:
        # Check if local file exists before loading
        if not os.path.exists(path):
            print(f"Error: File not found at {path}", file=sys.stderr)
            sys.exit(1)
        
        return onnx.load(path)

def CheckSqlOnnx(path):
    
    OnnxModel = load_model(path);
    
    initializer_names = {init.name for init in OnnxModel.graph.initializer}
    inputs = [inp.name for inp in OnnxModel.graph.input if inp.name not in initializer_names]
    outputs = [out.name for out in OnnxModel.graph.output]

    required_inputs = {"input_ids", "attention_mask"}
    required_outputs = {"token_embeddings", "sentence_embedding"}

    is_supported = (
        required_inputs.issubset(inputs) and
        required_outputs.issubset(outputs)
    )

    OnnxInouts = {
        "supported": is_supported,
        "inputs": inputs,
        "outputs": outputs
    }
    
    return OnnxInouts;

def CheckModel(repo_id: str, path: str | None = None):
    
    MODELS_CACHE = ONNX_CACHE['models'];
    
    CacheSlot = MODELS_CACHE.get(repo_id);
    
    if CacheSlot:
        return json.dumps(CacheSlot, indent = 2);
    
    
    
    model_info = HfClient.model_info(repo_id=repo_id)

    # Extract filenames from RepoSibling objects
    sibling_files = [s.rfilename for s in model_info.siblings]

    onnx_path = None

    if path:
        if path in sibling_files:
            onnx_path = path
        else:
            return f"Error: ONNX file not found: {path}"
    else:
        for p in ONNX_PREFERED:
            if p in sibling_files:
                onnx_path = p
                break

        if not onnx_path:
            onnx_path = next(
                (f for f in sibling_files if f.lower().endswith(".onnx")),
                None
            )

    if not onnx_path:
        raise "Error: No ONNX model found in repository";

    # Build Hugging Face raw file URL
    file_url = f"https://huggingface.co/{repo_id}/resolve/main/{onnx_path}"

    # Check SQL ONNX compatibility
    OnnxInfo = CheckSqlOnnx(file_url)
    
    CacheSlot = {
         'url': file_url
        ,'onnx': OnnxInfo
    }
    
    MODELS_CACHE[repo_id] = CacheSlot

    return json.dumps({**CacheSlot, 'cached': False}, indent = 2);


with gr.Blocks() as demo:
    
    gr.Markdown("""
        This sample app tests whether a given model repository can be used with SQL Server ONNX.  
        
        In some tests, it was discovered that not every ONNX model works with SQL Server `CREATE EXTERNAL MODEL`.  
        For it to work, the input parameters of the neural network must contain specific names, and the output as well.  
        
        I don’t know if this behavior will be kept in future versions of SQL Server 2025.  
        
        However, while we don’t have official documentation about this, this repository can help discover whether a given model will work with SQL Server if you plan to download and use it with ONNX.  
        
        Just input the model name below in the format `user/model-name` (check the examples).  
        
        Look at the JSON output. If `"supported"` is `true`, then you can use it with SQL Server.  
        
        Soon, I will bring a default tested list!  
        
        **IMPORTANT**: To check this, the space will attempt to download the model ONNX file. If it is large, it can take several minutes.  

    """)
    
    ModelPath = gr.Textbox(label="Model Repository", submit_btn = True);
    ModelInfoOut = gr.Textbox(label="Model Info", lines = 10)
    
    ModelPath.submit(fn=CheckModel, inputs=ModelPath, outputs=ModelInfoOut)
    
    gr.Examples([
         ["intfloat/multilingual-e5-large"]
        ,["mixedbread-ai/mxbai-embed-xsmall-v1"]
        ,["nsense/all-MiniLM-L6-v2-onnx"]
    
    ], ModelPath)

demo.launch(
    server_name = '0.0.0.0'
)