Create app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,135 @@
|
|
| 1 |
-
gradio
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import onnxruntime as ort
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
from transformers import AutoTokenizer
|
| 7 |
+
|
| 8 |
+
# ---------------------------------------------------------
|
| 9 |
+
# Download ONNX files directly from Hugging Face model hubs
|
| 10 |
+
# ---------------------------------------------------------
|
| 11 |
+
|
| 12 |
+
# 1) Multilingual sentiment (DistilBERT)
|
| 13 |
+
multilingual_onnx = hf_hub_download(
|
| 14 |
+
repo_id="lxyuan/distilbert-base-multilingual-cased-sentiments-student",
|
| 15 |
+
filename="onnx/model.onnx"
|
| 16 |
+
)
|
| 17 |
+
tokenizer_multilingual = AutoTokenizer.from_pretrained(
|
| 18 |
+
"lxyuan/distilbert-base-multilingual-cased-sentiments-student"
|
| 19 |
+
)
|
| 20 |
+
session_multilingual = ort.InferenceSession(multilingual_onnx, providers=["CPUExecutionProvider"])
|
| 21 |
+
|
| 22 |
+
# 2) SDG BERT
|
| 23 |
+
sdgbert_onnx = hf_hub_download(
|
| 24 |
+
repo_id="sadickam/sdgBERT",
|
| 25 |
+
filename="onnx/model.onnx"
|
| 26 |
+
)
|
| 27 |
+
tokenizer_sdg = AutoTokenizer.from_pretrained("sadickam/sdgBERT")
|
| 28 |
+
session_sdg = ort.InferenceSession(sdgbert_onnx, providers=["CPUExecutionProvider"])
|
| 29 |
+
|
| 30 |
+
# 3) German sentiment BERT
|
| 31 |
+
german_onnx = hf_hub_download(
|
| 32 |
+
repo_id="oliverguhr/german-sentiment-bert",
|
| 33 |
+
filename="onnx/model.onnx"
|
| 34 |
+
)
|
| 35 |
+
tokenizer_german = AutoTokenizer.from_pretrained("oliverguhr/german-sentiment-bert")
|
| 36 |
+
session_german = ort.InferenceSession(german_onnx, providers=["CPUExecutionProvider"])
|
| 37 |
+
|
| 38 |
+
# 4) ViT-small image classifier
|
| 39 |
+
vit_onnx = hf_hub_download(
|
| 40 |
+
repo_id="WinKawaks/vit-small-patch16-224",
|
| 41 |
+
filename="onnx/model.onnx"
|
| 42 |
+
)
|
| 43 |
+
session_vit = ort.InferenceSession(vit_onnx, providers=["CPUExecutionProvider"])
|
| 44 |
+
|
| 45 |
+
# Basic preprocessing params (ImageNet)
|
| 46 |
+
IMAGE_SIZE = 224
|
| 47 |
+
MEAN = [0.485, 0.456, 0.406]
|
| 48 |
+
STD = [0.229, 0.224, 0.225]
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# ---------------------------------------------------------
|
| 52 |
+
# Inference functions
|
| 53 |
+
# ---------------------------------------------------------
|
| 54 |
+
def softmax(x):
|
| 55 |
+
e = np.exp(x - np.max(x))
|
| 56 |
+
return e / e.sum(axis=-1, keepdims=True)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def run_multilingual(text):
|
| 60 |
+
inputs = tokenizer_multilingual(text, return_tensors="np", truncation=True, padding=True)
|
| 61 |
+
inputs = {k: v.astype(np.int64) for k, v in inputs.items()}
|
| 62 |
+
logits = session_multilingual.run(None, inputs)[0][0]
|
| 63 |
+
probs = softmax(logits)
|
| 64 |
+
labels = tokenizer_multilingual.model.config.id2label
|
| 65 |
+
return {labels[i]: float(probs[i]) for i in range(len(probs))}
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def run_sdg(text):
|
| 69 |
+
inputs = tokenizer_sdg(text, return_tensors="np", truncation=True, padding=True)
|
| 70 |
+
inputs = {k: v.astype(np.int64) for k, v in inputs.items()}
|
| 71 |
+
logits = session_sdg.run(None, inputs)[0][0]
|
| 72 |
+
probs = softmax(logits)
|
| 73 |
+
labels = tokenizer_sdg.model.config.id2label
|
| 74 |
+
return {labels[i]: float(probs[i]) for i in range(len(probs))}
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def run_german(text):
|
| 78 |
+
inputs = tokenizer_german(text, return_tensors="np", truncation=True, padding=True)
|
| 79 |
+
inputs = {k: v.astype(np.int64) for k, v in inputs.items()}
|
| 80 |
+
logits = session_german.run(None, inputs)[0][0]
|
| 81 |
+
probs = softmax(logits)
|
| 82 |
+
labels = tokenizer_german.model.config.id2label
|
| 83 |
+
return {labels[i]: float(probs[i]) for i in range(len(probs))}
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def preprocess_vit(image):
|
| 87 |
+
image = image.convert("RGB").resize((IMAGE_SIZE, IMAGE_SIZE))
|
| 88 |
+
arr = np.array(image).astype(np.float32) / 255.0
|
| 89 |
+
arr = (arr - MEAN) / STD
|
| 90 |
+
arr = arr.transpose(2, 0, 1)
|
| 91 |
+
return arr[np.newaxis, :]
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def run_vit(image):
|
| 95 |
+
arr = preprocess_vit(image)
|
| 96 |
+
input_name = session_vit.get_inputs()[0].name
|
| 97 |
+
logits = session_vit.run(None, {input_name: arr})[0][0]
|
| 98 |
+
probs = softmax(logits)
|
| 99 |
+
top5 = probs.argsort()[::-1][:5]
|
| 100 |
+
return {f"class_{i}": float(probs[i]) for i in top5}
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# ---------------------------------------------------------
|
| 104 |
+
# Gradio UI
|
| 105 |
+
# ---------------------------------------------------------
|
| 106 |
+
def inference(model, text, image):
|
| 107 |
+
if model == "Multilingual Sentiment":
|
| 108 |
+
return run_multilingual(text)
|
| 109 |
+
if model == "SDG Classification":
|
| 110 |
+
return run_sdg(text)
|
| 111 |
+
if model == "German Sentiment":
|
| 112 |
+
return run_german(text)
|
| 113 |
+
if model == "ViT Image Classification":
|
| 114 |
+
if image is None:
|
| 115 |
+
return {"error": "Upload an image"}
|
| 116 |
+
return run_vit(image)
|
| 117 |
+
return {"error": "Invalid selection"}
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
with gr.Blocks() as demo:
|
| 121 |
+
gr.Markdown("# π Multi-Model ONNX Inference (HF-loaded)")
|
| 122 |
+
|
| 123 |
+
model_choice = gr.Dropdown(
|
| 124 |
+
["Multilingual Sentiment", "SDG Classification", "German Sentiment", "ViT Image Classification"],
|
| 125 |
+
label="Choose a model"
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
text_in = gr.Textbox(label="Text Input", lines=3)
|
| 129 |
+
img_in = gr.Image(label="Image Input", type="pil")
|
| 130 |
+
output = gr.JSON(label="Output")
|
| 131 |
+
|
| 132 |
+
run_btn = gr.Button("Run Inference")
|
| 133 |
+
run_btn.click(fn=inference, inputs=[model_choice, text_in, img_in], outputs=[output])
|
| 134 |
+
|
| 135 |
+
demo.launch()
|