0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

2) Deploy a Pet-ID Gradio App to Hugging Face Spaces with Custom Domains

Posted at

Summary: Tạo ứng dụng Gradio phân loại giống (hoặc nhận dạng cá thể) và triển khai lên Hugging Face Spaces. Tối ưu tốc độ, bật queue, cấu hình thẻ SEO cơ bản và custom domain.

Mục lục

Kiến trúc app

Cấu trúc dự án

Gradio Blocks + hàng đợi

Dùng ONNX Runtime trên CPU

SEO metadata cơ bản cho Spaces

Custom domain (khái niệm)

Đo lường với UTM & Analytics

Further reading

  1. Kiến trúc app

Tệp app.py chạy Gradio UI

Model ONNX chạy bằng onnxruntime CPU

requirements.txt khai báo phụ thuộc

Thêm vài ảnh mẫu để dùng thử

  1. Cấu trúc dự án
    /pet-app/
    app.py
    petbreed.onnx
    classes.txt
    requirements.txt
    sample/
    corgi.jpg
    poodle.jpg

requirements.txt:

gradio==4.44.0
onnxruntime==1.18.0
opencv-python-headless==4.10.0
numpy

  1. Gradio Blocks + queue
    import gradio as gr
    import onnxruntime as ort
    import numpy as np, cv2

sess = ort.InferenceSession("petbreed.onnx", providers=["CPUExecutionProvider"])
CLASSES = open("classes.txt").read().splitlines()

def preprocess(img):
x = cv2.resize(img, (224,224))[:,:,::-1].astype(np.float32)/255.0
x = np.transpose(x, (2,0,1))[None]
return x

def predict(img):
x = preprocess(img)
logits = sess.run(None, {"input": x})[0][0]
logits -= logits.max()
p = np.exp(logits); p /= p.sum()
topk = p.argsort()[-3:][::-1]
return {CLASSES[i]: float(p[i]) for i in topk}

with gr.Blocks(title="Pet Breed Classifier") as demo:
gr.Markdown("# Pet Breed Classifier\nUpload an image to get top-3 predictions.")
with gr.Row():
inp = gr.Image(type="numpy", label="Upload")
out = gr.Label(num_top_classes=3, label="Prediction")
btn = gr.Button("Predict")
btn.click(fn=predict, inputs=inp, outputs=out)
gr.Examples(
examples=["sample/corgi.jpg", "sample/poodle.jpg"],
inputs=inp
)

demo.queue(concurrency_count=2, max_size=16).launch()

  1. Dùng ONNX Runtime trên CPU

Với model nhẹ, CPU đủ nhanh.

Nếu cần GPU, chọn Space type GPU và cài onnxruntime-gpu.

  1. SEO metadata cơ bản cho Spaces

Trong Blocks(title=...) đã có title. Có thể thêm mô tả ngắn bằng Markdown đầu trang: 1–2 câu nêu tác dụng app + từ khóa tự nhiên (pet breed classifier, dog/cat, demo). Tránh nhồi từ khóa.

  1. Custom domain (khái niệm)

Spaces hỗ trợ gắn domain tuỳ chọn (tùy điều kiện hiện hành).

Lợi ích: thương hiệu nhất quán, dễ chia sẻ.

Thao tác: cấu hình DNS → xác thực → trỏ tới Space (xem doc hiện thời của Hugging Face).

  1. Đo lường với UTM & Analytics

Khi chia sẻ link trên X/Reddit/Pinterest, thêm UTM (?utm_source=qiita&utm_medium=post).

Có thể gắn Google Analytics theo hướng dẫn của Spaces (nếu hỗ trợ ở thời điểm bạn triển khai).

  1. Further reading

Gradio Docs

Hugging Face Spaces Docs

ONNX Runtime Docs

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?