Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,506 +1,3 @@
|
|
1 |
-
# import os
|
2 |
-
# import numpy as np
|
3 |
-
# import torch
|
4 |
-
# import torch.nn as nn
|
5 |
-
# import gradio as gr
|
6 |
-
# import time
|
7 |
-
# from torchvision.models import efficientnet_v2_m, EfficientNet_V2_M_Weights
|
8 |
-
# from torchvision.ops import nms, box_iou
|
9 |
-
# import torch.nn.functional as F
|
10 |
-
# from torchvision import transforms
|
11 |
-
# from PIL import Image, ImageDraw, ImageFont, ImageFilter
|
12 |
-
# from breed_health_info import breed_health_info
|
13 |
-
# from breed_noise_info import breed_noise_info
|
14 |
-
# from dog_database import get_dog_description, dog_data
|
15 |
-
# from scoring_calculation_system import UserPreferences
|
16 |
-
# from recommendation_html_format import format_recommendation_html, get_breed_recommendations
|
17 |
-
# from history_manager import UserHistoryManager
|
18 |
-
# from search_history import create_history_tab, create_history_component
|
19 |
-
# from styles import get_css_styles
|
20 |
-
# from breed_detection import create_detection_tab
|
21 |
-
# from breed_comparison import create_comparison_tab
|
22 |
-
# from breed_recommendation import create_recommendation_tab
|
23 |
-
# from html_templates import (
|
24 |
-
# format_description_html,
|
25 |
-
# format_single_dog_result,
|
26 |
-
# format_multiple_breeds_result,
|
27 |
-
# format_error_message,
|
28 |
-
# format_warning_html,
|
29 |
-
# format_multi_dog_container,
|
30 |
-
# format_breed_details_html,
|
31 |
-
# get_color_scheme,
|
32 |
-
# get_akc_breeds_link
|
33 |
-
# )
|
34 |
-
# from urllib.parse import quote
|
35 |
-
# from ultralytics import YOLO
|
36 |
-
# import asyncio
|
37 |
-
# import traceback
|
38 |
-
|
39 |
-
|
40 |
-
# model_yolo = YOLO('yolov8l.pt')
|
41 |
-
|
42 |
-
# history_manager = UserHistoryManager()
|
43 |
-
|
44 |
-
# dog_breeds = ["Afghan_Hound", "African_Hunting_Dog", "Airedale", "American_Staffordshire_Terrier",
|
45 |
-
# "Appenzeller", "Australian_Terrier", "Bedlington_Terrier", "Bernese_Mountain_Dog", "Bichon_Frise",
|
46 |
-
# "Blenheim_Spaniel", "Border_Collie", "Border_Terrier", "Boston_Bull", "Bouvier_Des_Flandres",
|
47 |
-
# "Brabancon_Griffon", "Brittany_Spaniel", "Cardigan", "Chesapeake_Bay_Retriever",
|
48 |
-
# "Chihuahua", "Dachshund", "Dandie_Dinmont", "Doberman", "English_Foxhound", "English_Setter",
|
49 |
-
# "English_Springer", "EntleBucher", "Eskimo_Dog", "French_Bulldog", "German_Shepherd",
|
50 |
-
# "German_Short-Haired_Pointer", "Gordon_Setter", "Great_Dane", "Great_Pyrenees",
|
51 |
-
# "Greater_Swiss_Mountain_Dog","Havanese", "Ibizan_Hound", "Irish_Setter", "Irish_Terrier",
|
52 |
-
# "Irish_Water_Spaniel", "Irish_Wolfhound", "Italian_Greyhound", "Japanese_Spaniel",
|
53 |
-
# "Kerry_Blue_Terrier", "Labrador_Retriever", "Lakeland_Terrier", "Leonberg", "Lhasa",
|
54 |
-
# "Maltese_Dog", "Mexican_Hairless", "Newfoundland", "Norfolk_Terrier", "Norwegian_Elkhound",
|
55 |
-
# "Norwich_Terrier", "Old_English_Sheepdog", "Pekinese", "Pembroke", "Pomeranian",
|
56 |
-
# "Rhodesian_Ridgeback", "Rottweiler", "Saint_Bernard", "Saluki", "Samoyed",
|
57 |
-
# "Scotch_Terrier", "Scottish_Deerhound", "Sealyham_Terrier", "Shetland_Sheepdog", "Shiba_Inu",
|
58 |
-
# "Shih-Tzu", "Siberian_Husky", "Staffordshire_Bullterrier", "Sussex_Spaniel",
|
59 |
-
# "Tibetan_Mastiff", "Tibetan_Terrier", "Walker_Hound", "Weimaraner",
|
60 |
-
# "Welsh_Springer_Spaniel", "West_Highland_White_Terrier", "Yorkshire_Terrier",
|
61 |
-
# "Affenpinscher", "Basenji", "Basset", "Beagle", "Black-and-Tan_Coonhound", "Bloodhound",
|
62 |
-
# "Bluetick", "Borzoi", "Boxer", "Briard", "Bull_Mastiff", "Cairn", "Chow", "Clumber",
|
63 |
-
# "Cocker_Spaniel", "Collie", "Curly-Coated_Retriever", "Dhole", "Dingo",
|
64 |
-
# "Flat-Coated_Retriever", "Giant_Schnauzer", "Golden_Retriever", "Groenendael", "Keeshond",
|
65 |
-
# "Kelpie", "Komondor", "Kuvasz", "Malamute", "Malinois", "Miniature_Pinscher",
|
66 |
-
# "Miniature_Poodle", "Miniature_Schnauzer", "Otterhound", "Papillon", "Pug", "Redbone",
|
67 |
-
# "Schipperke", "Silky_Terrier", "Soft-Coated_Wheaten_Terrier", "Standard_Poodle",
|
68 |
-
# "Standard_Schnauzer", "Toy_Poodle", "Toy_Terrier", "Vizsla", "Whippet",
|
69 |
-
# "Wire-Haired_Fox_Terrier"]
|
70 |
-
|
71 |
-
|
72 |
-
# class MultiHeadAttention(nn.Module):
|
73 |
-
|
74 |
-
# def __init__(self, in_dim, num_heads=8):
|
75 |
-
# super().__init__()
|
76 |
-
# self.num_heads = num_heads
|
77 |
-
# self.head_dim = max(1, in_dim // num_heads)
|
78 |
-
# self.scaled_dim = self.head_dim * num_heads
|
79 |
-
# self.fc_in = nn.Linear(in_dim, self.scaled_dim)
|
80 |
-
# self.query = nn.Linear(self.scaled_dim, self.scaled_dim)
|
81 |
-
# self.key = nn.Linear(self.scaled_dim, self.scaled_dim)
|
82 |
-
# self.value = nn.Linear(self.scaled_dim, self.scaled_dim)
|
83 |
-
# self.fc_out = nn.Linear(self.scaled_dim, in_dim)
|
84 |
-
|
85 |
-
# def forward(self, x):
|
86 |
-
# N = x.shape[0]
|
87 |
-
# x = self.fc_in(x)
|
88 |
-
# q = self.query(x).view(N, self.num_heads, self.head_dim)
|
89 |
-
# k = self.key(x).view(N, self.num_heads, self.head_dim)
|
90 |
-
# v = self.value(x).view(N, self.num_heads, self.head_dim)
|
91 |
-
|
92 |
-
# energy = torch.einsum("nqd,nkd->nqk", [q, k])
|
93 |
-
# attention = F.softmax(energy / (self.head_dim ** 0.5), dim=2)
|
94 |
-
|
95 |
-
# out = torch.einsum("nqk,nvd->nqd", [attention, v])
|
96 |
-
# out = out.reshape(N, self.scaled_dim)
|
97 |
-
# out = self.fc_out(out)
|
98 |
-
# return out
|
99 |
-
|
100 |
-
# class BaseModel(nn.Module):
|
101 |
-
# def __init__(self, num_classes, device='cuda' if torch.cuda.is_available() else 'cpu'):
|
102 |
-
# super().__init__()
|
103 |
-
# self.device = device
|
104 |
-
# self.backbone = efficientnet_v2_m(weights=EfficientNet_V2_M_Weights.IMAGENET1K_V1)
|
105 |
-
# self.feature_dim = self.backbone.classifier[1].in_features
|
106 |
-
# self.backbone.classifier = nn.Identity()
|
107 |
-
|
108 |
-
# self.num_heads = max(1, min(8, self.feature_dim // 64))
|
109 |
-
# self.attention = MultiHeadAttention(self.feature_dim, num_heads=self.num_heads)
|
110 |
-
|
111 |
-
# self.classifier = nn.Sequential(
|
112 |
-
# nn.LayerNorm(self.feature_dim),
|
113 |
-
# nn.Dropout(0.3),
|
114 |
-
# nn.Linear(self.feature_dim, num_classes)
|
115 |
-
# )
|
116 |
-
|
117 |
-
# self.to(device)
|
118 |
-
|
119 |
-
# def forward(self, x):
|
120 |
-
# x = x.to(self.device)
|
121 |
-
# features = self.backbone(x)
|
122 |
-
# attended_features = self.attention(features)
|
123 |
-
# logits = self.classifier(attended_features)
|
124 |
-
# return logits, attended_features
|
125 |
-
|
126 |
-
# # Initialize model
|
127 |
-
# num_classes = len(dog_breeds)
|
128 |
-
# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
129 |
-
|
130 |
-
# # Initialize base model
|
131 |
-
# model = BaseModel(num_classes=num_classes, device=device).to(device)
|
132 |
-
|
133 |
-
# # Load model path
|
134 |
-
# model_path = "124_best_model_dog.pth"
|
135 |
-
# checkpoint = torch.load(model_path, map_location=device)
|
136 |
-
|
137 |
-
# # Load model state
|
138 |
-
# model.load_state_dict(checkpoint["base_model"], strict=False)
|
139 |
-
# model.eval()
|
140 |
-
|
141 |
-
# # Image preprocessing function
|
142 |
-
# def preprocess_image(image):
|
143 |
-
# # If the image is numpy.ndarray turn into PIL.Image
|
144 |
-
# if isinstance(image, np.ndarray):
|
145 |
-
# image = Image.fromarray(image)
|
146 |
-
|
147 |
-
# # Use torchvision.transforms to process images
|
148 |
-
# transform = transforms.Compose([
|
149 |
-
# transforms.Resize((224, 224)),
|
150 |
-
# transforms.ToTensor(),
|
151 |
-
# transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
152 |
-
# ])
|
153 |
-
|
154 |
-
# return transform(image).unsqueeze(0)
|
155 |
-
|
156 |
-
# async def predict_single_dog(image):
|
157 |
-
# """
|
158 |
-
# Predicts the dog breed using only the classifier.
|
159 |
-
# Args:
|
160 |
-
# image: PIL Image or numpy array
|
161 |
-
# Returns:
|
162 |
-
# tuple: (top1_prob, topk_breeds, relative_probs)
|
163 |
-
# """
|
164 |
-
# image_tensor = preprocess_image(image).to(device)
|
165 |
-
|
166 |
-
# with torch.no_grad():
|
167 |
-
# # Get model outputs (只使用logits,不需要features)
|
168 |
-
# logits = model(image_tensor)[0] # 如果model仍返回tuple,取第一個元素
|
169 |
-
# probs = F.softmax(logits, dim=1)
|
170 |
-
|
171 |
-
# # Classifier prediction
|
172 |
-
# top5_prob, top5_idx = torch.topk(probs, k=5)
|
173 |
-
# breeds = [dog_breeds[idx.item()] for idx in top5_idx[0]]
|
174 |
-
# probabilities = [prob.item() for prob in top5_prob[0]]
|
175 |
-
|
176 |
-
# # Calculate relative probabilities
|
177 |
-
# sum_probs = sum(probabilities[:3]) # 只取前三個來計算相對概率
|
178 |
-
# relative_probs = [f"{(prob/sum_probs * 100):.2f}%" for prob in probabilities[:3]]
|
179 |
-
|
180 |
-
# # Debug output
|
181 |
-
# print("\nClassifier Predictions:")
|
182 |
-
# for breed, prob in zip(breeds[:5], probabilities[:5]):
|
183 |
-
# print(f"{breed}: {prob:.4f}")
|
184 |
-
|
185 |
-
# return probabilities[0], breeds[:3], relative_probs
|
186 |
-
|
187 |
-
|
188 |
-
# async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
|
189 |
-
# results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
190 |
-
# dogs = []
|
191 |
-
# boxes = []
|
192 |
-
# for box in results.boxes:
|
193 |
-
# if box.cls == 16: # COCO dataset class for dog is 16
|
194 |
-
# xyxy = box.xyxy[0].tolist()
|
195 |
-
# confidence = box.conf.item()
|
196 |
-
# boxes.append((xyxy, confidence))
|
197 |
-
|
198 |
-
# if not boxes:
|
199 |
-
# dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
200 |
-
# else:
|
201 |
-
# nms_boxes = non_max_suppression(boxes, iou_threshold)
|
202 |
-
|
203 |
-
# for box, confidence in nms_boxes:
|
204 |
-
# x1, y1, x2, y2 = box
|
205 |
-
# w, h = x2 - x1, y2 - y1
|
206 |
-
# x1 = max(0, x1 - w * 0.05)
|
207 |
-
# y1 = max(0, y1 - h * 0.05)
|
208 |
-
# x2 = min(image.width, x2 + w * 0.05)
|
209 |
-
# y2 = min(image.height, y2 + h * 0.05)
|
210 |
-
# cropped_image = image.crop((x1, y1, x2, y2))
|
211 |
-
# dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
212 |
-
|
213 |
-
# return dogs
|
214 |
-
|
215 |
-
# def non_max_suppression(boxes, iou_threshold):
|
216 |
-
# keep = []
|
217 |
-
# boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
|
218 |
-
# while boxes:
|
219 |
-
# current = boxes.pop(0)
|
220 |
-
# keep.append(current)
|
221 |
-
# boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold]
|
222 |
-
# return keep
|
223 |
-
|
224 |
-
|
225 |
-
# def calculate_iou(box1, box2):
|
226 |
-
# x1 = max(box1[0], box2[0])
|
227 |
-
# y1 = max(box1[1], box2[1])
|
228 |
-
# x2 = min(box1[2], box2[2])
|
229 |
-
# y2 = min(box1[3], box2[3])
|
230 |
-
|
231 |
-
# intersection = max(0, x2 - x1) * max(0, y2 - y1)
|
232 |
-
# area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
|
233 |
-
# area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
|
234 |
-
|
235 |
-
# iou = intersection / float(area1 + area2 - intersection)
|
236 |
-
# return iou
|
237 |
-
|
238 |
-
|
239 |
-
# def create_breed_comparison(breed1: str, breed2: str) -> dict:
|
240 |
-
# breed1_info = get_dog_description(breed1)
|
241 |
-
# breed2_info = get_dog_description(breed2)
|
242 |
-
|
243 |
-
# # 標準化數值轉換
|
244 |
-
# value_mapping = {
|
245 |
-
# 'Size': {'Small': 1, 'Medium': 2, 'Large': 3, 'Giant': 4},
|
246 |
-
# 'Exercise_Needs': {'Low': 1, 'Moderate': 2, 'High': 3, 'Very High': 4},
|
247 |
-
# 'Care_Level': {'Low': 1, 'Moderate': 2, 'High': 3},
|
248 |
-
# 'Grooming_Needs': {'Low': 1, 'Moderate': 2, 'High': 3}
|
249 |
-
# }
|
250 |
-
|
251 |
-
# comparison_data = {
|
252 |
-
# breed1: {},
|
253 |
-
# breed2: {}
|
254 |
-
# }
|
255 |
-
|
256 |
-
# for breed, info in [(breed1, breed1_info), (breed2, breed2_info)]:
|
257 |
-
# comparison_data[breed] = {
|
258 |
-
# 'Size': value_mapping['Size'].get(info['Size'], 2), # 預設 Medium
|
259 |
-
# 'Exercise_Needs': value_mapping['Exercise_Needs'].get(info['Exercise Needs'], 2), # 預設 Moderate
|
260 |
-
# 'Care_Level': value_mapping['Care_Level'].get(info['Care Level'], 2),
|
261 |
-
# 'Grooming_Needs': value_mapping['Grooming_Needs'].get(info['Grooming Needs'], 2),
|
262 |
-
# 'Good_with_Children': info['Good with Children'] == 'Yes',
|
263 |
-
# 'Original_Data': info
|
264 |
-
# }
|
265 |
-
|
266 |
-
# return comparison_data
|
267 |
-
|
268 |
-
|
269 |
-
# async def predict(image):
|
270 |
-
# """
|
271 |
-
# Main prediction function that handles both single and multiple dog detection.
|
272 |
-
|
273 |
-
# Args:
|
274 |
-
# image: PIL Image or numpy array
|
275 |
-
|
276 |
-
# Returns:
|
277 |
-
# tuple: (html_output, annotated_image, initial_state)
|
278 |
-
# """
|
279 |
-
# if image is None:
|
280 |
-
# return format_warning_html("Please upload an image to start."), None, None
|
281 |
-
|
282 |
-
# try:
|
283 |
-
# if isinstance(image, np.ndarray):
|
284 |
-
# image = Image.fromarray(image)
|
285 |
-
|
286 |
-
# # Detect dogs in the image
|
287 |
-
# dogs = await detect_multiple_dogs(image)
|
288 |
-
# color_scheme = get_color_scheme(len(dogs) == 1)
|
289 |
-
|
290 |
-
# # Prepare for annotation
|
291 |
-
# annotated_image = image.copy()
|
292 |
-
# draw = ImageDraw.Draw(annotated_image)
|
293 |
-
|
294 |
-
# try:
|
295 |
-
# font = ImageFont.truetype("arial.ttf", 24)
|
296 |
-
# except:
|
297 |
-
# font = ImageFont.load_default()
|
298 |
-
|
299 |
-
# dogs_info = ""
|
300 |
-
|
301 |
-
# # Process each detected dog
|
302 |
-
# for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
303 |
-
# color = color_scheme if len(dogs) == 1 else color_scheme[i % len(color_scheme)]
|
304 |
-
|
305 |
-
# # Draw box and label on image
|
306 |
-
# draw.rectangle(box, outline=color, width=4)
|
307 |
-
# label = f"Dog {i+1}"
|
308 |
-
# label_bbox = draw.textbbox((0, 0), label, font=font)
|
309 |
-
# label_width = label_bbox[2] - label_bbox[0]
|
310 |
-
# label_height = label_bbox[3] - label_bbox[1]
|
311 |
-
|
312 |
-
# # Draw label background and text
|
313 |
-
# label_x = box[0] + 5
|
314 |
-
# label_y = box[1] + 5
|
315 |
-
# draw.rectangle(
|
316 |
-
# [label_x - 2, label_y - 2, label_x + label_width + 4, label_y + label_height + 4],
|
317 |
-
# fill='white',
|
318 |
-
# outline=color,
|
319 |
-
# width=2
|
320 |
-
# )
|
321 |
-
# draw.text((label_x, label_y), label, fill=color, font=font)
|
322 |
-
|
323 |
-
# # Predict breed
|
324 |
-
# top1_prob, topk_breeds, relative_probs = await predict_single_dog(cropped_image)
|
325 |
-
# combined_confidence = detection_confidence * top1_prob
|
326 |
-
|
327 |
-
# # Format results based on confidence with error handling
|
328 |
-
# try:
|
329 |
-
# if combined_confidence < 0.2:
|
330 |
-
# dogs_info += format_error_message(color, i+1)
|
331 |
-
# elif top1_prob >= 0.45:
|
332 |
-
# breed = topk_breeds[0]
|
333 |
-
# description = get_dog_description(breed)
|
334 |
-
# # Handle missing breed description
|
335 |
-
# if description is None:
|
336 |
-
# # 如果沒有描述,創建一個基本描述
|
337 |
-
# description = {
|
338 |
-
# "Name": breed,
|
339 |
-
# "Size": "Unknown",
|
340 |
-
# "Exercise Needs": "Unknown",
|
341 |
-
# "Grooming Needs": "Unknown",
|
342 |
-
# "Care Level": "Unknown",
|
343 |
-
# "Good with Children": "Unknown",
|
344 |
-
# "Description": f"Identified as {breed.replace('_', ' ')}"
|
345 |
-
# }
|
346 |
-
# dogs_info += format_single_dog_result(breed, description, color)
|
347 |
-
# else:
|
348 |
-
# # 修改format_multiple_breeds_result的調用,包含錯誤處理
|
349 |
-
# dogs_info += format_multiple_breeds_result(
|
350 |
-
# topk_breeds,
|
351 |
-
# relative_probs,
|
352 |
-
# color,
|
353 |
-
# i+1,
|
354 |
-
# lambda breed: get_dog_description(breed) or {
|
355 |
-
# "Name": breed,
|
356 |
-
# "Size": "Unknown",
|
357 |
-
# "Exercise Needs": "Unknown",
|
358 |
-
# "Grooming Needs": "Unknown",
|
359 |
-
# "Care Level": "Unknown",
|
360 |
-
# "Good with Children": "Unknown",
|
361 |
-
# "Description": f"Identified as {breed.replace('_', ' ')}"
|
362 |
-
# }
|
363 |
-
# )
|
364 |
-
# except Exception as e:
|
365 |
-
# print(f"Error formatting results for dog {i+1}: {str(e)}")
|
366 |
-
# dogs_info += format_error_message(color, i+1)
|
367 |
-
|
368 |
-
# # Wrap final HTML output
|
369 |
-
# html_output = format_multi_dog_container(dogs_info)
|
370 |
-
|
371 |
-
# # Prepare initial state
|
372 |
-
# initial_state = {
|
373 |
-
# "dogs_info": dogs_info,
|
374 |
-
# "image": annotated_image,
|
375 |
-
# "is_multi_dog": len(dogs) > 1,
|
376 |
-
# "html_output": html_output
|
377 |
-
# }
|
378 |
-
|
379 |
-
# return html_output, annotated_image, initial_state
|
380 |
-
|
381 |
-
# except Exception as e:
|
382 |
-
# error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
383 |
-
# print(error_msg)
|
384 |
-
# return format_warning_html(error_msg), None, None
|
385 |
-
|
386 |
-
|
387 |
-
# def show_details_html(choice, previous_output, initial_state):
|
388 |
-
# """
|
389 |
-
# Generate detailed HTML view for a selected breed.
|
390 |
-
|
391 |
-
# Args:
|
392 |
-
# choice: str, Selected breed option
|
393 |
-
# previous_output: str, Previous HTML output
|
394 |
-
# initial_state: dict, Current state information
|
395 |
-
|
396 |
-
# Returns:
|
397 |
-
# tuple: (html_output, gradio_update, updated_state)
|
398 |
-
# """
|
399 |
-
# if not choice:
|
400 |
-
# return previous_output, gr.update(visible=True), initial_state
|
401 |
-
|
402 |
-
# try:
|
403 |
-
# breed = choice.split("More about ")[-1]
|
404 |
-
# description = get_dog_description(breed)
|
405 |
-
# html_output = format_breed_details_html(description, breed)
|
406 |
-
|
407 |
-
# # Update state
|
408 |
-
# initial_state["current_description"] = html_output
|
409 |
-
# initial_state["original_buttons"] = initial_state.get("buttons", [])
|
410 |
-
|
411 |
-
# return html_output, gr.update(visible=True), initial_state
|
412 |
-
|
413 |
-
# except Exception as e:
|
414 |
-
# error_msg = f"An error occurred while showing details: {e}"
|
415 |
-
# print(error_msg)
|
416 |
-
# return format_warning_html(error_msg), gr.update(visible=True), initial_state
|
417 |
-
|
418 |
-
# def main():
|
419 |
-
# with gr.Blocks(css=get_css_styles()) as iface:
|
420 |
-
# # Header HTML
|
421 |
-
|
422 |
-
# gr.HTML("""
|
423 |
-
# <header style='text-align: center; padding: 20px; margin-bottom: 20px;'>
|
424 |
-
# <h1 style='font-size: 2.5em; margin-bottom: 10px; color: #2D3748;'>
|
425 |
-
# 🐾 PawMatch AI
|
426 |
-
# </h1>
|
427 |
-
# <h2 style='font-size: 1.2em; font-weight: normal; color: #4A5568; margin-top: 5px;'>
|
428 |
-
# Your Smart Dog Breed Guide
|
429 |
-
# </h2>
|
430 |
-
# <div style='width: 50px; height: 3px; background: linear-gradient(90deg, #4299e1, #48bb78); margin: 15px auto;'></div>
|
431 |
-
# <p style='color: #718096; font-size: 0.9em;'>
|
432 |
-
# Powered by AI • Breed Recognition • Smart Matching • Companion Guide
|
433 |
-
# </p>
|
434 |
-
# </header>
|
435 |
-
# """)
|
436 |
-
|
437 |
-
# # 先創建歷史組件實例(但不創建標籤頁)
|
438 |
-
# history_component = create_history_component()
|
439 |
-
|
440 |
-
# with gr.Tabs():
|
441 |
-
# # 1. 品種檢測標籤頁
|
442 |
-
# example_images = [
|
443 |
-
# 'Border_Collie.jpg',
|
444 |
-
# 'Golden_Retriever.jpeg',
|
445 |
-
# 'Saint_Bernard.jpeg',
|
446 |
-
# 'Samoyed.jpg',
|
447 |
-
# 'French_Bulldog.jpeg'
|
448 |
-
# ]
|
449 |
-
# detection_components = create_detection_tab(predict, example_images)
|
450 |
-
|
451 |
-
# # 2. 品種比較標籤頁
|
452 |
-
# comparison_components = create_comparison_tab(
|
453 |
-
# dog_breeds=dog_breeds,
|
454 |
-
# get_dog_description=get_dog_description,
|
455 |
-
# breed_health_info=breed_health_info,
|
456 |
-
# breed_noise_info=breed_noise_info
|
457 |
-
# )
|
458 |
-
|
459 |
-
# # 3. 品種推薦標籤頁
|
460 |
-
# recommendation_components = create_recommendation_tab(
|
461 |
-
# UserPreferences=UserPreferences,
|
462 |
-
# get_breed_recommendations=get_breed_recommendations,
|
463 |
-
# format_recommendation_html=format_recommendation_html,
|
464 |
-
# history_component=history_component
|
465 |
-
# )
|
466 |
-
|
467 |
-
|
468 |
-
# # 4. 最後創建歷史記錄標籤頁
|
469 |
-
# create_history_tab(history_component)
|
470 |
-
|
471 |
-
# # Footer
|
472 |
-
# gr.HTML('''
|
473 |
-
# <div style="
|
474 |
-
# display: flex;
|
475 |
-
# align-items: center;
|
476 |
-
# justify-content: center;
|
477 |
-
# gap: 20px;
|
478 |
-
# padding: 20px 0;
|
479 |
-
# ">
|
480 |
-
# <p style="
|
481 |
-
# font-family: 'Arial', sans-serif;
|
482 |
-
# font-size: 14px;
|
483 |
-
# font-weight: 500;
|
484 |
-
# letter-spacing: 2px;
|
485 |
-
# background: linear-gradient(90deg, #555, #007ACC);
|
486 |
-
# -webkit-background-clip: text;
|
487 |
-
# -webkit-text-fill-color: transparent;
|
488 |
-
# margin: 0;
|
489 |
-
# text-transform: uppercase;
|
490 |
-
# display: inline-block;
|
491 |
-
# ">EXPLORE THE CODE →</p>
|
492 |
-
# <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/PawMatchAI" style="text-decoration: none;">
|
493 |
-
# <img src="https://img.shields.io/badge/GitHub-PawMatch_AI-007ACC?logo=github&style=for-the-badge">
|
494 |
-
# </a>
|
495 |
-
# </div>
|
496 |
-
# ''')
|
497 |
-
|
498 |
-
# return iface
|
499 |
-
|
500 |
-
# if __name__ == "__main__":
|
501 |
-
# iface = main()
|
502 |
-
# iface.launch()
|
503 |
-
|
504 |
import os
|
505 |
import numpy as np
|
506 |
import torch
|
@@ -541,23 +38,18 @@ import traceback
|
|
541 |
import spaces
|
542 |
import torch.cuda.amp
|
543 |
|
544 |
-
# os.environ['CUDA_VISIBLE_DEVICES'] = '0'
|
545 |
-
# os.environ['HF_ZERO_GPU'] = '1' # 明確告訴系統我們要使用 ZeroGPU
|
546 |
-
# os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:512'
|
547 |
|
548 |
@spaces.GPU
|
549 |
def get_device():
|
550 |
print("Initializing device configuration...")
|
551 |
|
552 |
try:
|
553 |
-
# 強制進行 CUDA 初始化
|
554 |
torch.cuda.init()
|
555 |
# 使用 mixed precision
|
556 |
torch.set_float32_matmul_precision('medium')
|
557 |
|
558 |
if torch.cuda.is_available():
|
559 |
device = torch.device('cuda')
|
560 |
-
# 設置默認的 CUDA 設備
|
561 |
torch.cuda.set_device(device)
|
562 |
print(f"Successfully initialized CUDA device")
|
563 |
return device
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import numpy as np
|
3 |
import torch
|
|
|
38 |
import spaces
|
39 |
import torch.cuda.amp
|
40 |
|
|
|
|
|
|
|
41 |
|
42 |
@spaces.GPU
|
43 |
def get_device():
|
44 |
print("Initializing device configuration...")
|
45 |
|
46 |
try:
|
|
|
47 |
torch.cuda.init()
|
48 |
# 使用 mixed precision
|
49 |
torch.set_float32_matmul_precision('medium')
|
50 |
|
51 |
if torch.cuda.is_available():
|
52 |
device = torch.device('cuda')
|
|
|
53 |
torch.cuda.set_device(device)
|
54 |
print(f"Successfully initialized CUDA device")
|
55 |
return device
|