Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,8 @@ import torch.nn.functional as F
|
|
| 8 |
from torchvision import transforms
|
| 9 |
from PIL import Image
|
| 10 |
from data_manager import get_dog_description
|
|
|
|
|
|
|
| 11 |
|
| 12 |
dog_breeds = ["Afghan_Hound(阿富汗獵犬)", "African_Hunting_Dog(非洲野犬)", "Airedale(艾爾谷犬)",
|
| 13 |
"American_Staffordshire_Terrier(美國斯塔福郡梗)", "Appenzeller(亞賓澤爾犬)",
|
|
@@ -139,18 +141,53 @@ def preprocess_image(image):
|
|
| 139 |
|
| 140 |
return transform(image).unsqueeze(0)
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
def predict(image):
|
| 143 |
try:
|
| 144 |
image_tensor = preprocess_image(image)
|
| 145 |
with torch.no_grad():
|
| 146 |
-
logits,
|
| 147 |
_, predicted = torch.max(logits, 1)
|
| 148 |
-
|
| 149 |
breed = dog_breeds[predicted.item()] # Map label to breed name
|
| 150 |
-
|
| 151 |
# Retrieve breed description
|
| 152 |
description = get_dog_description(breed)
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
# Formatting the description for better display
|
| 155 |
if isinstance(description, dict):
|
| 156 |
description_str = f"**Breed**: {description['Breed']}\n\n"
|
|
@@ -164,89 +201,70 @@ def predict(image):
|
|
| 164 |
description_str += f"**Description**: {description['Description']}\n\n"
|
| 165 |
else:
|
| 166 |
description_str = description
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
| 168 |
return description_str
|
| 169 |
except Exception as e:
|
| 170 |
return f"An error occurred: {e}"
|
| 171 |
|
|
|
|
| 172 |
iface = gr.Interface(
|
| 173 |
fn=predict,
|
| 174 |
-
inputs=gr.Image(label="Upload
|
| 175 |
-
outputs="
|
| 176 |
-
title="<
|
| 177 |
-
description="<
|
| 178 |
examples=['Border_Collie.jpg',
|
| 179 |
'Golden_Retriever.jpeg',
|
| 180 |
'Saint_Bernard.jpeg',
|
| 181 |
'French_Bulldog.jpeg',
|
| 182 |
'Samoyed.jpg'],
|
| 183 |
-
|
| 184 |
-
/*
|
| 185 |
-
.
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
.gr-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
}
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
.output-markdown {
|
| 228 |
-
font-family: 'Noto Sans', sans-serif;
|
| 229 |
-
line-height: 1.75;
|
| 230 |
-
color: #34495E;
|
| 231 |
-
overflow: hidden;
|
| 232 |
-
max-height: none;
|
| 233 |
-
}
|
| 234 |
-
|
| 235 |
-
/* 調整範例圖片區塊樣式 */
|
| 236 |
-
.examples img {
|
| 237 |
-
border-radius: 10px;
|
| 238 |
-
transition: transform 0.3s ease, box-shadow 0.3s ease;
|
| 239 |
-
}
|
| 240 |
-
.examples img:hover {
|
| 241 |
-
transform: scale(1.05);
|
| 242 |
-
box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.15);
|
| 243 |
-
}
|
| 244 |
-
|
| 245 |
-
/* 去掉範例圖片滾動條 */
|
| 246 |
-
.examples {
|
| 247 |
-
overflow: hidden;
|
| 248 |
-
}
|
| 249 |
-
""", theme='default')
|
| 250 |
|
| 251 |
|
| 252 |
|
|
|
|
| 8 |
from torchvision import transforms
|
| 9 |
from PIL import Image
|
| 10 |
from data_manager import get_dog_description
|
| 11 |
+
import wikipedia
|
| 12 |
+
from urllib.parse import quote
|
| 13 |
|
| 14 |
dog_breeds = ["Afghan_Hound(阿富汗獵犬)", "African_Hunting_Dog(非洲野犬)", "Airedale(艾爾谷犬)",
|
| 15 |
"American_Staffordshire_Terrier(美國斯塔福郡梗)", "Appenzeller(亞賓澤爾犬)",
|
|
|
|
| 141 |
|
| 142 |
return transform(image).unsqueeze(0)
|
| 143 |
|
| 144 |
+
# def predict(image):
|
| 145 |
+
# try:
|
| 146 |
+
# image_tensor = preprocess_image(image)
|
| 147 |
+
# with torch.no_grad():
|
| 148 |
+
# logits, _ = model(image_tensor)
|
| 149 |
+
# _, predicted = torch.max(logits, 1)
|
| 150 |
+
|
| 151 |
+
# breed = dog_breeds[predicted.item()] # Map label to breed name
|
| 152 |
+
|
| 153 |
+
# # Retrieve breed description
|
| 154 |
+
# description = get_dog_description(breed)
|
| 155 |
+
|
| 156 |
+
# # Formatting the description for better display
|
| 157 |
+
# if isinstance(description, dict):
|
| 158 |
+
# description_str = f"**Breed**: {description['Breed']}\n\n"
|
| 159 |
+
# description_str += f"**Size**: {description['Size']}\n\n"
|
| 160 |
+
# description_str += f"**Lifespan**: {description['Lifespan']}\n\n"
|
| 161 |
+
# description_str += f"**Temperament**: {description['Temperament']}\n\n"
|
| 162 |
+
# description_str += f"**Care Level**: {description['Care Level']}\n\n"
|
| 163 |
+
# description_str += f"**Good with Children**: {description['Good with Children']}\n\n"
|
| 164 |
+
# description_str += f"**Exercise Needs**: {description['Exercise Needs']}\n\n"
|
| 165 |
+
# description_str += f"**Grooming Needs**: {description['Grooming Needs']}\n\n"
|
| 166 |
+
# description_str += f"**Description**: {description['Description']}\n\n"
|
| 167 |
+
# else:
|
| 168 |
+
# description_str = description
|
| 169 |
+
|
| 170 |
+
# return description_str
|
| 171 |
+
# except Exception as e:
|
| 172 |
+
# return f"An error occurred: {e}"
|
| 173 |
+
|
| 174 |
def predict(image):
|
| 175 |
try:
|
| 176 |
image_tensor = preprocess_image(image)
|
| 177 |
with torch.no_grad():
|
| 178 |
+
logits, * = model(image_tensor)
|
| 179 |
_, predicted = torch.max(logits, 1)
|
|
|
|
| 180 |
breed = dog_breeds[predicted.item()] # Map label to breed name
|
| 181 |
+
|
| 182 |
# Retrieve breed description
|
| 183 |
description = get_dog_description(breed)
|
| 184 |
+
|
| 185 |
+
# Generate Wikipedia link
|
| 186 |
+
try:
|
| 187 |
+
wiki_link = wikipedia.page(f"{breed} dog").url
|
| 188 |
+
except:
|
| 189 |
+
wiki_link = f"https://en.wikipedia.org/wiki/Special:Search?search={quote(breed)}+dog"
|
| 190 |
+
|
| 191 |
# Formatting the description for better display
|
| 192 |
if isinstance(description, dict):
|
| 193 |
description_str = f"**Breed**: {description['Breed']}\n\n"
|
|
|
|
| 201 |
description_str += f"**Description**: {description['Description']}\n\n"
|
| 202 |
else:
|
| 203 |
description_str = description
|
| 204 |
+
|
| 205 |
+
# Add Wikipedia link
|
| 206 |
+
description_str += f"\n\n[Click here to view the Wikipedia page for {breed}]({wiki_link})"
|
| 207 |
+
|
| 208 |
return description_str
|
| 209 |
except Exception as e:
|
| 210 |
return f"An error occurred: {e}"
|
| 211 |
|
| 212 |
+
|
| 213 |
iface = gr.Interface(
|
| 214 |
fn=predict,
|
| 215 |
+
inputs=gr.Image(label="Upload a dog image", type="numpy"),
|
| 216 |
+
outputs=gr.Markdown(label="Prediction Results"),
|
| 217 |
+
title="<h1 style='font-family:Roboto; font-weight:bold; color:#2C3E50; text-align:center;'>🐶 Dog Breed Classifier 🔍</h1>",
|
| 218 |
+
description="<p style='font-family:Open Sans; color:#34495E; text-align:center;'>Upload a picture of a dog, and AI will predict its breed, provide detailed information, and include a Wikipedia link!</p>",
|
| 219 |
examples=['Border_Collie.jpg',
|
| 220 |
'Golden_Retriever.jpeg',
|
| 221 |
'Saint_Bernard.jpeg',
|
| 222 |
'French_Bulldog.jpeg',
|
| 223 |
'Samoyed.jpg'],
|
| 224 |
+
css = """
|
| 225 |
+
/* 新增樣式 */
|
| 226 |
+
.container {
|
| 227 |
+
max-width: 900px;
|
| 228 |
+
margin: 0 auto;
|
| 229 |
+
padding: 20px;
|
| 230 |
+
background-color: rgba(255, 255, 255, 0.9);
|
| 231 |
+
border-radius: 15px;
|
| 232 |
+
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
.gr-form {
|
| 236 |
+
display: flex;
|
| 237 |
+
flex-direction: column;
|
| 238 |
+
align-items: center;
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
.gr-box {
|
| 242 |
+
width: 100%;
|
| 243 |
+
max-width: 500px;
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
.output-markdown, .output-image {
|
| 247 |
+
margin-top: 20px;
|
| 248 |
+
padding: 15px;
|
| 249 |
+
background-color: #f5f5f5;
|
| 250 |
+
border-radius: 10px;
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
.examples {
|
| 254 |
+
display: flex;
|
| 255 |
+
justify-content: center;
|
| 256 |
+
flex-wrap: wrap;
|
| 257 |
+
gap: 10px;
|
| 258 |
+
margin-top: 20px;
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
.examples img {
|
| 262 |
+
width: 100px;
|
| 263 |
+
height: 100px;
|
| 264 |
+
object-fit: cover;
|
| 265 |
+
}
|
| 266 |
+
""",
|
| 267 |
+
theme='default')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
|
| 270 |
|