Spaces:
Sleeping
Sleeping
File size: 4,780 Bytes
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 |
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 test if a given model repository can be used with SQL Server ONNX.
In some tests, discovered that is not any ONNX model that works with sql server CREATE EXTERNAL MODEL.
For works, the input parameters of neural network must contains specific names, and output also.
I dont know if this behavior will be ketp in future verisons of SQL 2025...
But, while we dont have official doc about this, this repo can help discovery if a given model will work with sql server if you plan download to use with ONNX.
Just input the model name bellow in format user/model-name (check examples).
Look at JSON output. If "supported" is True, then you can use with SQL...
Soon bring a default tested list!
**IMPORTANT**: To check, this space will attempt donwload the model onnx file. If is big 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'
) |