Spaces:
Running
Running
Update breed_recommendation.py
Browse files- breed_recommendation.py +73 -15
breed_recommendation.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
import sqlite3
|
2 |
import gradio as gr
|
|
|
|
|
3 |
from dog_database import get_dog_description, dog_data
|
4 |
from breed_health_info import breed_health_info
|
5 |
from breed_noise_info import breed_noise_info
|
@@ -528,33 +530,89 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
528 |
elem_id="recommendation-output"
|
529 |
)
|
530 |
|
531 |
-
def on_find_match_click(*args):
|
|
|
|
|
|
|
|
|
532 |
try:
|
533 |
-
#
|
534 |
-
|
535 |
<div style="
|
536 |
text-align: center;
|
537 |
padding: 20px;
|
538 |
background: linear-gradient(to right, rgba(66, 153, 225, 0.1), rgba(72, 187, 120, 0.1));
|
539 |
border-radius: 10px;
|
540 |
margin: 20px 0;
|
|
|
541 |
">
|
542 |
<div style="font-size: 40px; margin-bottom: 10px;">🐕</div>
|
543 |
-
<
|
544 |
color: #2D3748;
|
545 |
font-size: 1.2em;
|
546 |
-
margin:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
547 |
">
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
554 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
555 |
""")
|
556 |
-
|
557 |
-
#
|
|
|
|
|
|
|
558 |
user_prefs = UserPreferences(
|
559 |
living_space=args[0],
|
560 |
yard_access=args[1],
|
@@ -604,13 +662,13 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
604 |
)
|
605 |
|
606 |
# 返回最終結果
|
607 |
-
|
608 |
|
609 |
except Exception as e:
|
610 |
print(f"Error in find match: {str(e)}")
|
611 |
import traceback
|
612 |
print(traceback.format_exc())
|
613 |
-
|
614 |
<div style="
|
615 |
text-align: center;
|
616 |
padding: 20px;
|
|
|
1 |
import sqlite3
|
2 |
import gradio as gr
|
3 |
+
import asyncio
|
4 |
+
from typing import Generator
|
5 |
from dog_database import get_dog_description, dog_data
|
6 |
from breed_health_info import breed_health_info
|
7 |
from breed_noise_info import breed_noise_info
|
|
|
530 |
elem_id="recommendation-output"
|
531 |
)
|
532 |
|
533 |
+
def on_find_match_click(*args) -> Generator:
|
534 |
+
"""
|
535 |
+
處理推薦按鈕點擊事件的函數
|
536 |
+
使用生成器來處理中間狀態和最終結果
|
537 |
+
"""
|
538 |
try:
|
539 |
+
# 首先返回 loading 狀態
|
540 |
+
yield gr.HTML("""
|
541 |
<div style="
|
542 |
text-align: center;
|
543 |
padding: 20px;
|
544 |
background: linear-gradient(to right, rgba(66, 153, 225, 0.1), rgba(72, 187, 120, 0.1));
|
545 |
border-radius: 10px;
|
546 |
margin: 20px 0;
|
547 |
+
animation: fadeIn 0.5s ease-in-out;
|
548 |
">
|
549 |
<div style="font-size: 40px; margin-bottom: 10px;">🐕</div>
|
550 |
+
<div style="
|
551 |
color: #2D3748;
|
552 |
font-size: 1.2em;
|
553 |
+
margin-bottom: 15px;
|
554 |
+
font-weight: 500;
|
555 |
+
">Finding your perfect match...</div>
|
556 |
+
|
557 |
+
<!-- 進度指示動畫 -->
|
558 |
+
<div style="
|
559 |
+
display: flex;
|
560 |
+
justify-content: center;
|
561 |
+
gap: 8px;
|
562 |
+
margin: 15px 0;
|
563 |
">
|
564 |
+
<div class="paw-print" style="
|
565 |
+
width: 12px;
|
566 |
+
height: 12px;
|
567 |
+
background: #4299e1;
|
568 |
+
border-radius: 50%;
|
569 |
+
animation: bounce 0.6s infinite ease-in-out;
|
570 |
+
animation-delay: 0s;
|
571 |
+
"></div>
|
572 |
+
<div class="paw-print" style="
|
573 |
+
width: 12px;
|
574 |
+
height: 12px;
|
575 |
+
background: #4299e1;
|
576 |
+
border-radius: 50%;
|
577 |
+
animation: bounce 0.6s infinite ease-in-out;
|
578 |
+
animation-delay: 0.2s;
|
579 |
+
"></div>
|
580 |
+
<div class="paw-print" style="
|
581 |
+
width: 12px;
|
582 |
+
height: 12px;
|
583 |
+
background: #4299e1;
|
584 |
+
border-radius: 50%;
|
585 |
+
animation: bounce 0.6s infinite ease-in-out;
|
586 |
+
animation-delay: 0.4s;
|
587 |
+
"></div>
|
588 |
+
</div>
|
589 |
+
|
590 |
+
<!-- 提示訊息 -->
|
591 |
+
<div style="
|
592 |
+
color: #4A5568;
|
593 |
+
font-size: 0.9em;
|
594 |
+
font-style: italic;
|
595 |
+
margin-top: 10px;
|
596 |
+
">Analyzing your preferences and our database of good boys and girls...</div>
|
597 |
</div>
|
598 |
+
|
599 |
+
<style>
|
600 |
+
@keyframes bounce {
|
601 |
+
0%, 100% { transform: translateY(0); }
|
602 |
+
50% { transform: translateY(-10px); }
|
603 |
+
}
|
604 |
+
|
605 |
+
@keyframes fadeIn {
|
606 |
+
from { opacity: 0; }
|
607 |
+
to { opacity: 1; }
|
608 |
+
}
|
609 |
+
</style>
|
610 |
""")
|
611 |
+
|
612 |
+
# 添加短暫延遲使 loading 動畫可見
|
613 |
+
yield asyncio.sleep(1)
|
614 |
+
|
615 |
+
# 創建用戶偏好對象並獲取推薦
|
616 |
user_prefs = UserPreferences(
|
617 |
living_space=args[0],
|
618 |
yard_access=args[1],
|
|
|
662 |
)
|
663 |
|
664 |
# 返回最終結果
|
665 |
+
yield format_recommendation_html(recommendations, is_description_search=False)
|
666 |
|
667 |
except Exception as e:
|
668 |
print(f"Error in find match: {str(e)}")
|
669 |
import traceback
|
670 |
print(traceback.format_exc())
|
671 |
+
yield gr.HTML("""
|
672 |
<div style="
|
673 |
text-align: center;
|
674 |
padding: 20px;
|