Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ import torch.nn.functional as F
|
|
9 |
from torchvision import transforms
|
10 |
from PIL import Image, ImageDraw, ImageFont, ImageFilter
|
11 |
from data_manager import get_dog_description, UserPreferences, get_breed_recommendations, format_recommendation_html
|
|
|
12 |
from urllib.parse import quote
|
13 |
from ultralytics import YOLO
|
14 |
import asyncio
|
@@ -274,7 +275,7 @@ def create_breed_comparison(breed1: str, breed2: str) -> dict:
|
|
274 |
"""比較兩個狗品種的特性"""
|
275 |
breed1_info = get_dog_description(breed1)
|
276 |
breed2_info = get_dog_description(breed2)
|
277 |
-
|
278 |
# 標準化數值轉換
|
279 |
value_mapping = {
|
280 |
'Size': {'Small': 1, 'Medium': 2, 'Large': 3, 'Giant': 4},
|
@@ -282,12 +283,12 @@ def create_breed_comparison(breed1: str, breed2: str) -> dict:
|
|
282 |
'Care_Level': {'Low': 1, 'Moderate': 2, 'High': 3},
|
283 |
'Grooming_Needs': {'Low': 1, 'Moderate': 2, 'High': 3}
|
284 |
}
|
285 |
-
|
286 |
comparison_data = {
|
287 |
breed1: {},
|
288 |
breed2: {}
|
289 |
}
|
290 |
-
|
291 |
for breed, info in [(breed1, breed1_info), (breed2, breed2_info)]:
|
292 |
comparison_data[breed] = {
|
293 |
'Size': value_mapping['Size'].get(info['Size'], 2), # 預設 Medium
|
@@ -297,7 +298,7 @@ def create_breed_comparison(breed1: str, breed2: str) -> dict:
|
|
297 |
'Good_with_Children': info['Good with Children'] == 'Yes',
|
298 |
'Original_Data': info
|
299 |
}
|
300 |
-
|
301 |
return comparison_data
|
302 |
|
303 |
|
@@ -1260,6 +1261,90 @@ with gr.Blocks(css="""
|
|
1260 |
.bonus-score .progress {
|
1261 |
background: linear-gradient(90deg, #48bb78, #68d391);
|
1262 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1263 |
""") as iface:
|
1264 |
|
1265 |
gr.HTML("""
|
@@ -1276,40 +1361,36 @@ with gr.Blocks(css="""
|
|
1276 |
</p>
|
1277 |
</header>
|
1278 |
""")
|
1279 |
-
|
1280 |
# 使用 Tabs 來分隔兩個功能
|
1281 |
with gr.Tabs():
|
1282 |
# 第一個 Tab:原有的辨識功能
|
1283 |
with gr.TabItem("Breed Detection"):
|
1284 |
-
gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, the model will predict its breed and provide detailed information!</p>")
|
1285 |
gr.HTML("<p style='text-align: center; color: #666; font-size: 0.9em;'>Note: The model's predictions may not always be 100% accurate, and it is recommended to use the results as a reference.</p>")
|
1286 |
-
|
1287 |
with gr.Row():
|
1288 |
input_image = gr.Image(label="Upload a dog image", type="pil")
|
1289 |
output_image = gr.Image(label="Annotated Image")
|
1290 |
-
|
1291 |
output = gr.HTML(label="Prediction Results")
|
1292 |
initial_state = gr.State()
|
1293 |
-
|
1294 |
input_image.change(
|
1295 |
predict,
|
1296 |
inputs=input_image,
|
1297 |
outputs=[output, output_image, initial_state]
|
1298 |
)
|
1299 |
-
|
1300 |
gr.Examples(
|
1301 |
-
examples=['Border_Collie.jpg',
|
1302 |
-
'Golden_Retriever.jpeg',
|
1303 |
-
'Saint_Bernard.jpeg',
|
1304 |
-
'Samoyed.jpg',
|
1305 |
-
'French_Bulldog.jpeg'],
|
1306 |
inputs=input_image
|
1307 |
)
|
1308 |
-
|
1309 |
# 第二個 Tab:品種比較功能
|
1310 |
with gr.TabItem("Breed Comparison"):
|
1311 |
gr.HTML("<p style='text-align: center;'>Select two dog breeds to compare their characteristics and care requirements.</p>")
|
1312 |
-
|
1313 |
with gr.Row():
|
1314 |
breed1_dropdown = gr.Dropdown(
|
1315 |
choices=dog_breeds,
|
@@ -1321,17 +1402,17 @@ with gr.Blocks(css="""
|
|
1321 |
label="Select Second Breed",
|
1322 |
value="Border_Collie"
|
1323 |
)
|
1324 |
-
|
1325 |
compare_btn = gr.Button("Compare Breeds")
|
1326 |
comparison_output = gr.HTML(label="Comparison Results")
|
1327 |
-
|
1328 |
def show_comparison(breed1, breed2):
|
1329 |
if not breed1 or not breed2:
|
1330 |
return "Please select two breeds to compare"
|
1331 |
-
|
1332 |
breed1_info = get_dog_description(breed1)
|
1333 |
breed2_info = get_dog_description(breed2)
|
1334 |
-
|
1335 |
html_output = f"""
|
1336 |
<div class="dog-info-card">
|
1337 |
<div class="comparison-grid" style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
|
@@ -1370,7 +1451,7 @@ with gr.Blocks(css="""
|
|
1370 |
</div>
|
1371 |
</div>
|
1372 |
</div>
|
1373 |
-
|
1374 |
<div class="breed-info">
|
1375 |
<h2 class="section-title">
|
1376 |
<span class="icon">🐕</span> {breed2.replace('_', ' ')}
|
@@ -1410,17 +1491,17 @@ with gr.Blocks(css="""
|
|
1410 |
</div>
|
1411 |
"""
|
1412 |
return html_output
|
1413 |
-
|
1414 |
compare_btn.click(
|
1415 |
show_comparison,
|
1416 |
inputs=[breed1_dropdown, breed2_dropdown],
|
1417 |
outputs=comparison_output
|
1418 |
)
|
1419 |
-
|
1420 |
# 第三個 Tab:品種推薦功能
|
1421 |
with gr.TabItem("Breed Recommendation"):
|
1422 |
gr.HTML("<p style='text-align: center;'>Tell us about your lifestyle, and we'll recommend the perfect dog breeds for you!</p>")
|
1423 |
-
|
1424 |
with gr.Row():
|
1425 |
with gr.Column():
|
1426 |
living_space = gr.Radio(
|
@@ -1429,7 +1510,7 @@ with gr.Blocks(css="""
|
|
1429 |
info="Choose your current living situation",
|
1430 |
value="apartment"
|
1431 |
)
|
1432 |
-
|
1433 |
exercise_time = gr.Slider(
|
1434 |
minimum=0,
|
1435 |
maximum=180,
|
@@ -1437,14 +1518,14 @@ with gr.Blocks(css="""
|
|
1437 |
label="Daily exercise time (minutes)",
|
1438 |
info="Consider walks, play time, and training"
|
1439 |
)
|
1440 |
-
|
1441 |
grooming_commitment = gr.Radio(
|
1442 |
choices=["low", "medium", "high"],
|
1443 |
label="Grooming commitment level",
|
1444 |
info="Low: monthly, Medium: weekly, High: daily",
|
1445 |
value="medium"
|
1446 |
)
|
1447 |
-
|
1448 |
with gr.Column():
|
1449 |
experience_level = gr.Radio(
|
1450 |
choices=["beginner", "intermediate", "advanced"],
|
@@ -1452,20 +1533,20 @@ with gr.Blocks(css="""
|
|
1452 |
info="Be honest - this helps find the right match",
|
1453 |
value="beginner"
|
1454 |
)
|
1455 |
-
|
1456 |
has_children = gr.Checkbox(
|
1457 |
label="Have children at home",
|
1458 |
info="Helps recommend child-friendly breeds"
|
1459 |
)
|
1460 |
-
|
1461 |
noise_tolerance = gr.Radio(
|
1462 |
choices=["low", "medium", "high"],
|
1463 |
label="Noise tolerance level",
|
1464 |
info="Some breeds are more vocal than others",
|
1465 |
value="medium"
|
1466 |
)
|
1467 |
-
|
1468 |
-
|
1469 |
# 設置按鈕的點擊事件
|
1470 |
get_recommendations_btn = gr.Button("Find My Perfect Match! 🔍", variant="primary")
|
1471 |
recommendation_output = gr.HTML(label="Breed Recommendations")
|
@@ -1484,27 +1565,27 @@ with gr.Blocks(css="""
|
|
1484 |
other_pets=False,
|
1485 |
climate="moderate"
|
1486 |
)
|
1487 |
-
|
1488 |
recommendations = get_breed_recommendations(user_prefs)
|
1489 |
return format_recommendation_html(recommendations)
|
1490 |
except Exception as e:
|
1491 |
print(f"Error in process_recommendations: {str(e)}")
|
1492 |
return f"An error occurred: {str(e)}"
|
1493 |
-
|
1494 |
# 這行是關鍵 - 確保按鈕點擊事件有正確連接到處理函數
|
1495 |
get_recommendations_btn.click(
|
1496 |
fn=process_recommendations, # 處理函數
|
1497 |
inputs=[
|
1498 |
living_space,
|
1499 |
-
exercise_time,
|
1500 |
grooming_commitment,
|
1501 |
-
experience_level,
|
1502 |
-
has_children,
|
1503 |
noise_tolerance
|
1504 |
],
|
1505 |
outputs=recommendation_output # 輸出結果的位置
|
1506 |
)
|
1507 |
-
|
1508 |
gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
|
1509 |
|
1510 |
if __name__ == "__main__":
|
|
|
9 |
from torchvision import transforms
|
10 |
from PIL import Image, ImageDraw, ImageFont, ImageFilter
|
11 |
from data_manager import get_dog_description, UserPreferences, get_breed_recommendations, format_recommendation_html
|
12 |
+
from breed_health_info import breed_health_info, default_health_note
|
13 |
from urllib.parse import quote
|
14 |
from ultralytics import YOLO
|
15 |
import asyncio
|
|
|
275 |
"""比較兩個狗品種的特性"""
|
276 |
breed1_info = get_dog_description(breed1)
|
277 |
breed2_info = get_dog_description(breed2)
|
278 |
+
|
279 |
# 標準化數值轉換
|
280 |
value_mapping = {
|
281 |
'Size': {'Small': 1, 'Medium': 2, 'Large': 3, 'Giant': 4},
|
|
|
283 |
'Care_Level': {'Low': 1, 'Moderate': 2, 'High': 3},
|
284 |
'Grooming_Needs': {'Low': 1, 'Moderate': 2, 'High': 3}
|
285 |
}
|
286 |
+
|
287 |
comparison_data = {
|
288 |
breed1: {},
|
289 |
breed2: {}
|
290 |
}
|
291 |
+
|
292 |
for breed, info in [(breed1, breed1_info), (breed2, breed2_info)]:
|
293 |
comparison_data[breed] = {
|
294 |
'Size': value_mapping['Size'].get(info['Size'], 2), # 預設 Medium
|
|
|
298 |
'Good_with_Children': info['Good with Children'] == 'Yes',
|
299 |
'Original_Data': info
|
300 |
}
|
301 |
+
|
302 |
return comparison_data
|
303 |
|
304 |
|
|
|
1261 |
.bonus-score .progress {
|
1262 |
background: linear-gradient(90deg, #48bb78, #68d391);
|
1263 |
}
|
1264 |
+
|
1265 |
+
.health-section {
|
1266 |
+
margin: 25px 0;
|
1267 |
+
padding: 24px;
|
1268 |
+
background-color: #f8f9fb;
|
1269 |
+
border-radius: 12px;
|
1270 |
+
border: 1px solid #e1e4e8;
|
1271 |
+
}
|
1272 |
+
|
1273 |
+
.health-section .subsection-title {
|
1274 |
+
font-size: 1.3em;
|
1275 |
+
font-weight: 600;
|
1276 |
+
margin-bottom: 20px;
|
1277 |
+
display: flex;
|
1278 |
+
align-items: center;
|
1279 |
+
gap: 8px;
|
1280 |
+
color: #2c3e50;
|
1281 |
+
}
|
1282 |
+
|
1283 |
+
.health-info {
|
1284 |
+
background-color: white;
|
1285 |
+
padding: 24px;
|
1286 |
+
border-radius: 8px;
|
1287 |
+
margin: 15px 0;
|
1288 |
+
border: 1px solid #e1e4e8;
|
1289 |
+
}
|
1290 |
+
|
1291 |
+
.health-details {
|
1292 |
+
font-size: 1.1rem;
|
1293 |
+
line-height: 1.6;
|
1294 |
+
}
|
1295 |
+
|
1296 |
+
.health-details h4 {
|
1297 |
+
color: #2c3e50;
|
1298 |
+
font-size: 1.15rem;
|
1299 |
+
font-weight: 600;
|
1300 |
+
margin: 20px 0 15px 0;
|
1301 |
+
}
|
1302 |
+
|
1303 |
+
.health-details h4:first-child {
|
1304 |
+
margin-top: 0;
|
1305 |
+
}
|
1306 |
+
|
1307 |
+
.health-details ul {
|
1308 |
+
list-style-type: none;
|
1309 |
+
padding-left: 0;
|
1310 |
+
margin: 0 0 25px 0;
|
1311 |
+
}
|
1312 |
+
|
1313 |
+
.health-details ul li {
|
1314 |
+
margin-bottom: 12px;
|
1315 |
+
padding-left: 20px;
|
1316 |
+
position: relative;
|
1317 |
+
}
|
1318 |
+
|
1319 |
+
.health-details ul li:before {
|
1320 |
+
content: "•";
|
1321 |
+
position: absolute;
|
1322 |
+
left: 0;
|
1323 |
+
color: #2c3e50;
|
1324 |
+
}
|
1325 |
+
|
1326 |
+
.health-disclaimer {
|
1327 |
+
margin-top: 20px;
|
1328 |
+
color: #666;
|
1329 |
+
font-size: 1.05rem;
|
1330 |
+
line-height: 1.6;
|
1331 |
+
padding-top: 20px;
|
1332 |
+
border-top: 1px solid #e1e4e8;
|
1333 |
+
}
|
1334 |
+
|
1335 |
+
.health-disclaimer p {
|
1336 |
+
margin: 8px 0;
|
1337 |
+
padding-left: 15px;
|
1338 |
+
position: relative;
|
1339 |
+
}
|
1340 |
+
|
1341 |
+
.health-disclaimer p:before {
|
1342 |
+
content: "»";
|
1343 |
+
position: absolute;
|
1344 |
+
left: 0;
|
1345 |
+
color: #666;
|
1346 |
+
}
|
1347 |
+
|
1348 |
""") as iface:
|
1349 |
|
1350 |
gr.HTML("""
|
|
|
1361 |
</p>
|
1362 |
</header>
|
1363 |
""")
|
1364 |
+
|
1365 |
# 使用 Tabs 來分隔兩個功能
|
1366 |
with gr.Tabs():
|
1367 |
# 第一個 Tab:原有的辨識功能
|
1368 |
with gr.TabItem("Breed Detection"):
|
1369 |
+
gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed and provide detailed information!</p>")
|
1370 |
gr.HTML("<p style='text-align: center; color: #666; font-size: 0.9em;'>Note: The model's predictions may not always be 100% accurate, and it is recommended to use the results as a reference.</p>")
|
1371 |
+
|
1372 |
with gr.Row():
|
1373 |
input_image = gr.Image(label="Upload a dog image", type="pil")
|
1374 |
output_image = gr.Image(label="Annotated Image")
|
1375 |
+
|
1376 |
output = gr.HTML(label="Prediction Results")
|
1377 |
initial_state = gr.State()
|
1378 |
+
|
1379 |
input_image.change(
|
1380 |
predict,
|
1381 |
inputs=input_image,
|
1382 |
outputs=[output, output_image, initial_state]
|
1383 |
)
|
1384 |
+
|
1385 |
gr.Examples(
|
1386 |
+
examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
|
|
|
|
|
|
|
|
|
1387 |
inputs=input_image
|
1388 |
)
|
1389 |
+
|
1390 |
# 第二個 Tab:品種比較功能
|
1391 |
with gr.TabItem("Breed Comparison"):
|
1392 |
gr.HTML("<p style='text-align: center;'>Select two dog breeds to compare their characteristics and care requirements.</p>")
|
1393 |
+
|
1394 |
with gr.Row():
|
1395 |
breed1_dropdown = gr.Dropdown(
|
1396 |
choices=dog_breeds,
|
|
|
1402 |
label="Select Second Breed",
|
1403 |
value="Border_Collie"
|
1404 |
)
|
1405 |
+
|
1406 |
compare_btn = gr.Button("Compare Breeds")
|
1407 |
comparison_output = gr.HTML(label="Comparison Results")
|
1408 |
+
|
1409 |
def show_comparison(breed1, breed2):
|
1410 |
if not breed1 or not breed2:
|
1411 |
return "Please select two breeds to compare"
|
1412 |
+
|
1413 |
breed1_info = get_dog_description(breed1)
|
1414 |
breed2_info = get_dog_description(breed2)
|
1415 |
+
|
1416 |
html_output = f"""
|
1417 |
<div class="dog-info-card">
|
1418 |
<div class="comparison-grid" style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
|
|
|
1451 |
</div>
|
1452 |
</div>
|
1453 |
</div>
|
1454 |
+
|
1455 |
<div class="breed-info">
|
1456 |
<h2 class="section-title">
|
1457 |
<span class="icon">🐕</span> {breed2.replace('_', ' ')}
|
|
|
1491 |
</div>
|
1492 |
"""
|
1493 |
return html_output
|
1494 |
+
|
1495 |
compare_btn.click(
|
1496 |
show_comparison,
|
1497 |
inputs=[breed1_dropdown, breed2_dropdown],
|
1498 |
outputs=comparison_output
|
1499 |
)
|
1500 |
+
|
1501 |
# 第三個 Tab:品種推薦功能
|
1502 |
with gr.TabItem("Breed Recommendation"):
|
1503 |
gr.HTML("<p style='text-align: center;'>Tell us about your lifestyle, and we'll recommend the perfect dog breeds for you!</p>")
|
1504 |
+
|
1505 |
with gr.Row():
|
1506 |
with gr.Column():
|
1507 |
living_space = gr.Radio(
|
|
|
1510 |
info="Choose your current living situation",
|
1511 |
value="apartment"
|
1512 |
)
|
1513 |
+
|
1514 |
exercise_time = gr.Slider(
|
1515 |
minimum=0,
|
1516 |
maximum=180,
|
|
|
1518 |
label="Daily exercise time (minutes)",
|
1519 |
info="Consider walks, play time, and training"
|
1520 |
)
|
1521 |
+
|
1522 |
grooming_commitment = gr.Radio(
|
1523 |
choices=["low", "medium", "high"],
|
1524 |
label="Grooming commitment level",
|
1525 |
info="Low: monthly, Medium: weekly, High: daily",
|
1526 |
value="medium"
|
1527 |
)
|
1528 |
+
|
1529 |
with gr.Column():
|
1530 |
experience_level = gr.Radio(
|
1531 |
choices=["beginner", "intermediate", "advanced"],
|
|
|
1533 |
info="Be honest - this helps find the right match",
|
1534 |
value="beginner"
|
1535 |
)
|
1536 |
+
|
1537 |
has_children = gr.Checkbox(
|
1538 |
label="Have children at home",
|
1539 |
info="Helps recommend child-friendly breeds"
|
1540 |
)
|
1541 |
+
|
1542 |
noise_tolerance = gr.Radio(
|
1543 |
choices=["low", "medium", "high"],
|
1544 |
label="Noise tolerance level",
|
1545 |
info="Some breeds are more vocal than others",
|
1546 |
value="medium"
|
1547 |
)
|
1548 |
+
|
1549 |
+
|
1550 |
# 設置按鈕的點擊事件
|
1551 |
get_recommendations_btn = gr.Button("Find My Perfect Match! 🔍", variant="primary")
|
1552 |
recommendation_output = gr.HTML(label="Breed Recommendations")
|
|
|
1565 |
other_pets=False,
|
1566 |
climate="moderate"
|
1567 |
)
|
1568 |
+
|
1569 |
recommendations = get_breed_recommendations(user_prefs)
|
1570 |
return format_recommendation_html(recommendations)
|
1571 |
except Exception as e:
|
1572 |
print(f"Error in process_recommendations: {str(e)}")
|
1573 |
return f"An error occurred: {str(e)}"
|
1574 |
+
|
1575 |
# 這行是關鍵 - 確保按鈕點擊事件有正確連接到處理函數
|
1576 |
get_recommendations_btn.click(
|
1577 |
fn=process_recommendations, # 處理函數
|
1578 |
inputs=[
|
1579 |
living_space,
|
1580 |
+
exercise_time,
|
1581 |
grooming_commitment,
|
1582 |
+
experience_level,
|
1583 |
+
has_children,
|
1584 |
noise_tolerance
|
1585 |
],
|
1586 |
outputs=recommendation_output # 輸出結果的位置
|
1587 |
)
|
1588 |
+
|
1589 |
gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
|
1590 |
|
1591 |
if __name__ == "__main__":
|