Spaces:
Sleeping
Sleeping
reviewd text
Browse files
app.py
CHANGED
|
@@ -1,162 +1,166 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import onnx
|
| 3 |
-
from huggingface_hub import HfApi
|
| 4 |
-
import json
|
| 5 |
-
import sys
|
| 6 |
-
import os
|
| 7 |
-
import io
|
| 8 |
-
import requests
|
| 9 |
-
from urllib.parse import urlparse
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
HfClient = HfApi();
|
| 13 |
-
|
| 14 |
-
ONNX_PREFERED = [
|
| 15 |
-
"model.onnx",
|
| 16 |
-
"onnx/model.onnx"
|
| 17 |
-
]
|
| 18 |
-
|
| 19 |
-
ONNX_CACHE = {
|
| 20 |
-
'models': {
|
| 21 |
-
|
| 22 |
-
}
|
| 23 |
-
}
|
| 24 |
-
|
| 25 |
-
def is_url(path):
|
| 26 |
-
try:
|
| 27 |
-
result = urlparse(path)
|
| 28 |
-
return all([result.scheme, result.netloc])
|
| 29 |
-
except ValueError:
|
| 30 |
-
return False
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def load_model(path):
|
| 34 |
-
if is_url(path):
|
| 35 |
-
print(f"Downloading model from: {path}...", file=sys.stderr)
|
| 36 |
-
try:
|
| 37 |
-
response = requests.get(path)
|
| 38 |
-
response.raise_for_status() # Check for HTTP errors
|
| 39 |
-
# Load from binary stream
|
| 40 |
-
return onnx.load(io.BytesIO(response.content))
|
| 41 |
-
except requests.exceptions.RequestException as e:
|
| 42 |
-
print(f"Error downloading model: {e}", file=sys.stderr)
|
| 43 |
-
sys.exit(1)
|
| 44 |
-
else:
|
| 45 |
-
# Check if local file exists before loading
|
| 46 |
-
if not os.path.exists(path):
|
| 47 |
-
print(f"Error: File not found at {path}", file=sys.stderr)
|
| 48 |
-
sys.exit(1)
|
| 49 |
-
|
| 50 |
-
return onnx.load(path)
|
| 51 |
-
|
| 52 |
-
def CheckSqlOnnx(path):
|
| 53 |
-
|
| 54 |
-
OnnxModel = load_model(path);
|
| 55 |
-
|
| 56 |
-
initializer_names = {init.name for init in OnnxModel.graph.initializer}
|
| 57 |
-
inputs = [inp.name for inp in OnnxModel.graph.input if inp.name not in initializer_names]
|
| 58 |
-
outputs = [out.name for out in OnnxModel.graph.output]
|
| 59 |
-
|
| 60 |
-
required_inputs = {"input_ids", "attention_mask"}
|
| 61 |
-
required_outputs = {"token_embeddings", "sentence_embedding"}
|
| 62 |
-
|
| 63 |
-
is_supported = (
|
| 64 |
-
required_inputs.issubset(inputs) and
|
| 65 |
-
required_outputs.issubset(outputs)
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
OnnxInouts = {
|
| 69 |
-
"supported": is_supported,
|
| 70 |
-
"inputs": inputs,
|
| 71 |
-
"outputs": outputs
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
return OnnxInouts;
|
| 75 |
-
|
| 76 |
-
def CheckModel(repo_id: str, path: str | None = None):
|
| 77 |
-
|
| 78 |
-
MODELS_CACHE = ONNX_CACHE['models'];
|
| 79 |
-
|
| 80 |
-
CacheSlot = MODELS_CACHE.get(repo_id);
|
| 81 |
-
|
| 82 |
-
if CacheSlot:
|
| 83 |
-
return json.dumps(CacheSlot, indent = 2);
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
model_info = HfClient.model_info(repo_id=repo_id)
|
| 88 |
-
|
| 89 |
-
# Extract filenames from RepoSibling objects
|
| 90 |
-
sibling_files = [s.rfilename for s in model_info.siblings]
|
| 91 |
-
|
| 92 |
-
onnx_path = None
|
| 93 |
-
|
| 94 |
-
if path:
|
| 95 |
-
if path in sibling_files:
|
| 96 |
-
onnx_path = path
|
| 97 |
-
else:
|
| 98 |
-
return f"Error: ONNX file not found: {path}"
|
| 99 |
-
else:
|
| 100 |
-
for p in ONNX_PREFERED:
|
| 101 |
-
if p in sibling_files:
|
| 102 |
-
onnx_path = p
|
| 103 |
-
break
|
| 104 |
-
|
| 105 |
-
if not onnx_path:
|
| 106 |
-
onnx_path = next(
|
| 107 |
-
(f for f in sibling_files if f.lower().endswith(".onnx")),
|
| 108 |
-
None
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
if not onnx_path:
|
| 112 |
-
raise "Error: No ONNX model found in repository";
|
| 113 |
-
|
| 114 |
-
# Build Hugging Face raw file URL
|
| 115 |
-
file_url = f"https://huggingface.co/{repo_id}/resolve/main/{onnx_path}"
|
| 116 |
-
|
| 117 |
-
# Check SQL ONNX compatibility
|
| 118 |
-
OnnxInfo = CheckSqlOnnx(file_url)
|
| 119 |
-
|
| 120 |
-
CacheSlot = {
|
| 121 |
-
'url': file_url
|
| 122 |
-
,'onnx': OnnxInfo
|
| 123 |
-
}
|
| 124 |
-
|
| 125 |
-
MODELS_CACHE[repo_id] = CacheSlot
|
| 126 |
-
|
| 127 |
-
return json.dumps({**CacheSlot, 'cached': False}, indent = 2);
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
with gr.Blocks() as demo:
|
| 131 |
-
|
| 132 |
-
gr.Markdown("""
|
| 133 |
-
This sample app
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
gr.
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import onnx
|
| 3 |
+
from huggingface_hub import HfApi
|
| 4 |
+
import json
|
| 5 |
+
import sys
|
| 6 |
+
import os
|
| 7 |
+
import io
|
| 8 |
+
import requests
|
| 9 |
+
from urllib.parse import urlparse
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
HfClient = HfApi();
|
| 13 |
+
|
| 14 |
+
ONNX_PREFERED = [
|
| 15 |
+
"model.onnx",
|
| 16 |
+
"onnx/model.onnx"
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
ONNX_CACHE = {
|
| 20 |
+
'models': {
|
| 21 |
+
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
def is_url(path):
|
| 26 |
+
try:
|
| 27 |
+
result = urlparse(path)
|
| 28 |
+
return all([result.scheme, result.netloc])
|
| 29 |
+
except ValueError:
|
| 30 |
+
return False
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def load_model(path):
|
| 34 |
+
if is_url(path):
|
| 35 |
+
print(f"Downloading model from: {path}...", file=sys.stderr)
|
| 36 |
+
try:
|
| 37 |
+
response = requests.get(path)
|
| 38 |
+
response.raise_for_status() # Check for HTTP errors
|
| 39 |
+
# Load from binary stream
|
| 40 |
+
return onnx.load(io.BytesIO(response.content))
|
| 41 |
+
except requests.exceptions.RequestException as e:
|
| 42 |
+
print(f"Error downloading model: {e}", file=sys.stderr)
|
| 43 |
+
sys.exit(1)
|
| 44 |
+
else:
|
| 45 |
+
# Check if local file exists before loading
|
| 46 |
+
if not os.path.exists(path):
|
| 47 |
+
print(f"Error: File not found at {path}", file=sys.stderr)
|
| 48 |
+
sys.exit(1)
|
| 49 |
+
|
| 50 |
+
return onnx.load(path)
|
| 51 |
+
|
| 52 |
+
def CheckSqlOnnx(path):
|
| 53 |
+
|
| 54 |
+
OnnxModel = load_model(path);
|
| 55 |
+
|
| 56 |
+
initializer_names = {init.name for init in OnnxModel.graph.initializer}
|
| 57 |
+
inputs = [inp.name for inp in OnnxModel.graph.input if inp.name not in initializer_names]
|
| 58 |
+
outputs = [out.name for out in OnnxModel.graph.output]
|
| 59 |
+
|
| 60 |
+
required_inputs = {"input_ids", "attention_mask"}
|
| 61 |
+
required_outputs = {"token_embeddings", "sentence_embedding"}
|
| 62 |
+
|
| 63 |
+
is_supported = (
|
| 64 |
+
required_inputs.issubset(inputs) and
|
| 65 |
+
required_outputs.issubset(outputs)
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
OnnxInouts = {
|
| 69 |
+
"supported": is_supported,
|
| 70 |
+
"inputs": inputs,
|
| 71 |
+
"outputs": outputs
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
return OnnxInouts;
|
| 75 |
+
|
| 76 |
+
def CheckModel(repo_id: str, path: str | None = None):
|
| 77 |
+
|
| 78 |
+
MODELS_CACHE = ONNX_CACHE['models'];
|
| 79 |
+
|
| 80 |
+
CacheSlot = MODELS_CACHE.get(repo_id);
|
| 81 |
+
|
| 82 |
+
if CacheSlot:
|
| 83 |
+
return json.dumps(CacheSlot, indent = 2);
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
model_info = HfClient.model_info(repo_id=repo_id)
|
| 88 |
+
|
| 89 |
+
# Extract filenames from RepoSibling objects
|
| 90 |
+
sibling_files = [s.rfilename for s in model_info.siblings]
|
| 91 |
+
|
| 92 |
+
onnx_path = None
|
| 93 |
+
|
| 94 |
+
if path:
|
| 95 |
+
if path in sibling_files:
|
| 96 |
+
onnx_path = path
|
| 97 |
+
else:
|
| 98 |
+
return f"Error: ONNX file not found: {path}"
|
| 99 |
+
else:
|
| 100 |
+
for p in ONNX_PREFERED:
|
| 101 |
+
if p in sibling_files:
|
| 102 |
+
onnx_path = p
|
| 103 |
+
break
|
| 104 |
+
|
| 105 |
+
if not onnx_path:
|
| 106 |
+
onnx_path = next(
|
| 107 |
+
(f for f in sibling_files if f.lower().endswith(".onnx")),
|
| 108 |
+
None
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
if not onnx_path:
|
| 112 |
+
raise "Error: No ONNX model found in repository";
|
| 113 |
+
|
| 114 |
+
# Build Hugging Face raw file URL
|
| 115 |
+
file_url = f"https://huggingface.co/{repo_id}/resolve/main/{onnx_path}"
|
| 116 |
+
|
| 117 |
+
# Check SQL ONNX compatibility
|
| 118 |
+
OnnxInfo = CheckSqlOnnx(file_url)
|
| 119 |
+
|
| 120 |
+
CacheSlot = {
|
| 121 |
+
'url': file_url
|
| 122 |
+
,'onnx': OnnxInfo
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
MODELS_CACHE[repo_id] = CacheSlot
|
| 126 |
+
|
| 127 |
+
return json.dumps({**CacheSlot, 'cached': False}, indent = 2);
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
with gr.Blocks() as demo:
|
| 131 |
+
|
| 132 |
+
gr.Markdown("""
|
| 133 |
+
This sample app tests whether a given model repository can be used with SQL Server ONNX.
|
| 134 |
+
|
| 135 |
+
In some tests, it was discovered that not every ONNX model works with SQL Server `CREATE EXTERNAL MODEL`.
|
| 136 |
+
For it to work, the input parameters of the neural network must contain specific names, and the output as well.
|
| 137 |
+
|
| 138 |
+
I don’t know if this behavior will be kept in future versions of SQL Server 2025.
|
| 139 |
+
|
| 140 |
+
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.
|
| 141 |
+
|
| 142 |
+
Just input the model name below in the format `user/model-name` (check the examples).
|
| 143 |
+
|
| 144 |
+
Look at the JSON output. If `"supported"` is `true`, then you can use it with SQL Server.
|
| 145 |
+
|
| 146 |
+
Soon, I will bring a default tested list!
|
| 147 |
+
|
| 148 |
+
**IMPORTANT**: To check this, the space will attempt to download the model ONNX file. If it is large, it can take several minutes.
|
| 149 |
+
|
| 150 |
+
""")
|
| 151 |
+
|
| 152 |
+
ModelPath = gr.Textbox(label="Model Repository", submit_btn = True);
|
| 153 |
+
ModelInfoOut = gr.Textbox(label="Model Info", lines = 10)
|
| 154 |
+
|
| 155 |
+
ModelPath.submit(fn=CheckModel, inputs=ModelPath, outputs=ModelInfoOut)
|
| 156 |
+
|
| 157 |
+
gr.Examples([
|
| 158 |
+
["intfloat/multilingual-e5-large"]
|
| 159 |
+
,["mixedbread-ai/mxbai-embed-xsmall-v1"]
|
| 160 |
+
,["nsense/all-MiniLM-L6-v2-onnx"]
|
| 161 |
+
|
| 162 |
+
], ModelPath)
|
| 163 |
+
|
| 164 |
+
demo.launch(
|
| 165 |
+
server_name = '0.0.0.0'
|
| 166 |
)
|