cuziycloud commited on
Commit
81d7e8f
·
verified ·
1 Parent(s): 7c29e01

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoImageProcessor, SiglipForImageClassification
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # 1. Tải mô hình AI và bộ xử lý ảnh từ Hugging Face
7
+ model_name = "prithivMLmods/Dog-Breed-120"
8
+ processor = AutoImageProcessor.from_pretrained(model_name)
9
+ model = SiglipForImageClassification.from_pretrained(model_name)
10
+
11
+ # 2. Định nghĩa hàm dự đoán
12
+ def predict(image):
13
+ # Xử lý ảnh đầu vào để mô hình có thể hiểu được
14
+ inputs = processor(images=image, return_tensors="pt")
15
+
16
+ # Đưa ảnh đã xử lý vào mô hình để nhận kết quả
17
+ with torch.no_grad():
18
+ outputs = model(**inputs)
19
+
20
+ logits = outputs.logits
21
+
22
+ # Tìm ra giống chó có xác suất cao nhất
23
+ predicted_class_idx = logits.argmax(-1).item()
24
+
25
+ # Lấy tên của giống chó và độ tin cậy
26
+ breed_name = model.config.id2label[predicted_class_idx]
27
+ confidence_score = torch.nn.functional.softmax(logits, dim=1)[0][predicted_class_idx].item()
28
+
29
+ # Định dạng kết quả trả về
30
+ return f"{breed_name} ({confidence_score:.1%})"
31
+
32
+ # 3. Tạo giao diện người dùng
33
+ iface = gr.Interface(
34
+ fn=predict,
35
+ inputs=gr.Image(type="pil", label="Tải ảnh chú chó của bạn lên đây"),
36
+ outputs=gr.Textbox(label="Kết quả dự đoán"),
37
+ title="AI Nhận Diện Giống Chó",
38
+ description="Ứng dụng này sử dụng mô hình AI để nhận diện 120 giống chó khác nhau. Hãy thử tải một bức ảnh lên!",
39
+ examples=[
40
+ ["https://huggingface.co/spaces/prithivMLmods/Dog-Breed-120/resolve/main/samoyed.jpg"],
41
+ ["https://huggingface.co/spaces/prithivMLmods/Dog-Breed-120/resolve/main/beagle.jpg"]
42
+ ]
43
+ )
44
+
45
+ # 4
46
+ iface.launch()