Spaces:
Running
on
Zero
Running
on
Zero
Delete breed_recommendation.py
Browse files- breed_recommendation.py +0 -330
breed_recommendation.py
DELETED
@@ -1,330 +0,0 @@
|
|
1 |
-
import sqlite3
|
2 |
-
import gradio as gr
|
3 |
-
from typing import Generator
|
4 |
-
from dog_database import get_dog_description, dog_data
|
5 |
-
from breed_health_info import breed_health_info
|
6 |
-
from breed_noise_info import breed_noise_info
|
7 |
-
from scoring_calculation_system import UserPreferences, calculate_compatibility_score
|
8 |
-
from recommendation_html_format import format_recommendation_html, get_breed_recommendations
|
9 |
-
from search_history import create_history_tab, create_history_component
|
10 |
-
|
11 |
-
def create_custom_button_style():
|
12 |
-
return """
|
13 |
-
<style>
|
14 |
-
/* 確保有匹配到 */
|
15 |
-
button#find-match-btn {
|
16 |
-
background: linear-gradient(90deg, #ff5f6d 0%, #ffc371 100%) !important;
|
17 |
-
border: none !important;
|
18 |
-
border-radius: 30px !important;
|
19 |
-
padding: 12px 24px !important;
|
20 |
-
color: white !important;
|
21 |
-
font-weight: bold !important;
|
22 |
-
cursor: pointer !important;
|
23 |
-
transition: all 0.3s ease !important;
|
24 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1) !important;
|
25 |
-
width: 100% !important;
|
26 |
-
margin: 20px 0 !important;
|
27 |
-
font-size: 1.1em !important;
|
28 |
-
}
|
29 |
-
|
30 |
-
button#find-match-btn:hover {
|
31 |
-
background: linear-gradient(90deg, #ff4f5d 0%, #ffb361 100%) !important;
|
32 |
-
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2) !important;
|
33 |
-
transform: translateY(-2px) !important;
|
34 |
-
}
|
35 |
-
|
36 |
-
button#find-match-btn:active {
|
37 |
-
transform: translateY(1px) !important;
|
38 |
-
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2) !important;
|
39 |
-
}
|
40 |
-
|
41 |
-
#search-status {
|
42 |
-
text-align: center;
|
43 |
-
padding: 15px;
|
44 |
-
font-size: 1.1em;
|
45 |
-
color: #666;
|
46 |
-
margin: 10px 0;
|
47 |
-
border-radius: 8px;
|
48 |
-
background: rgba(200, 200, 200, 0.1); # 中性的背景色
|
49 |
-
transition: opacity 0.3s ease; # 平滑過渡效果
|
50 |
-
}
|
51 |
-
|
52 |
-
/* 強制覆蓋任何其他樣式 */
|
53 |
-
.gradio-button {
|
54 |
-
position: relative !important;
|
55 |
-
overflow: visible !important;
|
56 |
-
}
|
57 |
-
</style>
|
58 |
-
"""
|
59 |
-
|
60 |
-
def create_recommendation_tab(UserPreferences, get_breed_recommendations, format_recommendation_html, history_component):
|
61 |
-
|
62 |
-
with gr.TabItem("Breed Recommendation"):
|
63 |
-
with gr.Tabs():
|
64 |
-
with gr.Tab("Find by Criteria"):
|
65 |
-
gr.HTML("""
|
66 |
-
<div style='
|
67 |
-
text-align: center;
|
68 |
-
position: relative;
|
69 |
-
padding: 20px 0;
|
70 |
-
margin: 15px 0;
|
71 |
-
background: linear-gradient(to right, rgba(66, 153, 225, 0.1), rgba(72, 187, 120, 0.1));
|
72 |
-
border-radius: 10px;
|
73 |
-
'>
|
74 |
-
<!-- BETA 標籤 -->
|
75 |
-
<div style='
|
76 |
-
position: absolute;
|
77 |
-
top: 10px;
|
78 |
-
right: 20px;
|
79 |
-
background: linear-gradient(90deg, #4299e1, #48bb78);
|
80 |
-
color: white;
|
81 |
-
padding: 4px 12px;
|
82 |
-
border-radius: 15px;
|
83 |
-
font-size: 0.85em;
|
84 |
-
font-weight: 600;
|
85 |
-
letter-spacing: 1px;
|
86 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
87 |
-
'>BETA</div>
|
88 |
-
|
89 |
-
<!-- 主標題 -->
|
90 |
-
<p style='
|
91 |
-
font-size: 1.2em;
|
92 |
-
margin: 0;
|
93 |
-
padding: 0 20px;
|
94 |
-
line-height: 1.5;
|
95 |
-
background: linear-gradient(90deg, #4299e1, #48bb78);
|
96 |
-
-webkit-background-clip: text;
|
97 |
-
-webkit-text-fill-color: transparent;
|
98 |
-
font-weight: 600;
|
99 |
-
'>
|
100 |
-
Tell us about your lifestyle, and we'll recommend the perfect dog breeds for you!
|
101 |
-
</p>
|
102 |
-
|
103 |
-
<!-- 提示訊息 -->
|
104 |
-
<div style='
|
105 |
-
margin-top: 15px;
|
106 |
-
padding: 10px 20px;
|
107 |
-
background: linear-gradient(to right, rgba(66, 153, 225, 0.15), rgba(72, 187, 120, 0.15));
|
108 |
-
border-radius: 8px;
|
109 |
-
font-size: 0.9em;
|
110 |
-
color: #2D3748;
|
111 |
-
display: flex;
|
112 |
-
align-items: center;
|
113 |
-
justify-content: center;
|
114 |
-
gap: 8px;
|
115 |
-
'>
|
116 |
-
<span style="font-size: 1.2em;">🔬</span>
|
117 |
-
<span style="
|
118 |
-
letter-spacing: 0.3px;
|
119 |
-
line-height: 1.4;
|
120 |
-
"><strong>Beta Feature:</strong> Our matching algorithm is continuously improving. Results are for reference only.</span>
|
121 |
-
</div>
|
122 |
-
</div>
|
123 |
-
""")
|
124 |
-
|
125 |
-
with gr.Row():
|
126 |
-
with gr.Column():
|
127 |
-
living_space = gr.Radio(
|
128 |
-
choices=["apartment", "house_small", "house_large"],
|
129 |
-
label="What type of living space do you have?",
|
130 |
-
info="Choose your current living situation",
|
131 |
-
value="apartment"
|
132 |
-
)
|
133 |
-
|
134 |
-
yard_access = gr.Radio(
|
135 |
-
choices=["no_yard", "shared_yard", "private_yard"],
|
136 |
-
label="Yard Access Type",
|
137 |
-
info="Available outdoor space",
|
138 |
-
value="no_yard"
|
139 |
-
)
|
140 |
-
|
141 |
-
exercise_time = gr.Slider(
|
142 |
-
minimum=0,
|
143 |
-
maximum=180,
|
144 |
-
value=60,
|
145 |
-
label="Daily exercise time (minutes)",
|
146 |
-
info="Consider walks, play time, and training"
|
147 |
-
)
|
148 |
-
|
149 |
-
exercise_type = gr.Radio(
|
150 |
-
choices=["light_walks", "moderate_activity", "active_training"],
|
151 |
-
label="Exercise Style",
|
152 |
-
info="What kind of activities do you prefer?",
|
153 |
-
value="moderate_activity"
|
154 |
-
)
|
155 |
-
|
156 |
-
|
157 |
-
grooming_commitment = gr.Radio(
|
158 |
-
choices=["low", "medium", "high"],
|
159 |
-
label="Grooming commitment level",
|
160 |
-
info="Low: monthly, Medium: weekly, High: daily",
|
161 |
-
value="medium"
|
162 |
-
)
|
163 |
-
|
164 |
-
with gr.Column():
|
165 |
-
size_preference = gr.Radio(
|
166 |
-
choices=["no_preference", "small", "medium", "large", "giant"],
|
167 |
-
label="Preference Dog Size",
|
168 |
-
info="Select your preferred dog size - this will strongly filter the recommendations",
|
169 |
-
value = "no_preference"
|
170 |
-
)
|
171 |
-
experience_level = gr.Radio(
|
172 |
-
choices=["beginner", "intermediate", "advanced"],
|
173 |
-
label="Dog ownership experience",
|
174 |
-
info="Be honest - this helps find the right match",
|
175 |
-
value="beginner"
|
176 |
-
)
|
177 |
-
|
178 |
-
time_availability = gr.Radio(
|
179 |
-
choices=["limited", "moderate", "flexible"],
|
180 |
-
label="Time Availability",
|
181 |
-
info="Time available for dog care daily",
|
182 |
-
value="moderate"
|
183 |
-
)
|
184 |
-
|
185 |
-
has_children = gr.Checkbox(
|
186 |
-
label="Have children at home",
|
187 |
-
info="Helps recommend child-friendly breeds"
|
188 |
-
)
|
189 |
-
|
190 |
-
children_age = gr.Radio(
|
191 |
-
choices=["toddler", "school_age", "teenager"],
|
192 |
-
label="Children's Age Group",
|
193 |
-
info="Helps match with age-appropriate breeds",
|
194 |
-
visible=False # 默認隱藏,只在has_children=True時顯示
|
195 |
-
)
|
196 |
-
|
197 |
-
noise_tolerance = gr.Radio(
|
198 |
-
choices=["low", "medium", "high"],
|
199 |
-
label="Noise tolerance level",
|
200 |
-
info="Some breeds are more vocal than others",
|
201 |
-
value="medium"
|
202 |
-
)
|
203 |
-
|
204 |
-
def update_children_age_visibility(has_children):
|
205 |
-
return gr.update(visible=has_children)
|
206 |
-
|
207 |
-
has_children.change(
|
208 |
-
fn=update_children_age_visibility,
|
209 |
-
inputs=has_children,
|
210 |
-
outputs=children_age
|
211 |
-
)
|
212 |
-
gr.HTML(create_custom_button_style())
|
213 |
-
|
214 |
-
get_recommendations_btn = gr.Button(
|
215 |
-
"Find My Perfect Match! 🔍",
|
216 |
-
elem_id="find-match-btn"
|
217 |
-
)
|
218 |
-
|
219 |
-
search_status = gr.HTML(
|
220 |
-
'<div id="search-status">🔍 Sniffing out your perfect furry companion...</div>',
|
221 |
-
visible=False,
|
222 |
-
elem_id="search-status-container"
|
223 |
-
)
|
224 |
-
|
225 |
-
recommendation_output = gr.HTML(
|
226 |
-
label="Breed Recommendations",
|
227 |
-
visible=True, # 確保可��性
|
228 |
-
elem_id="recommendation-output"
|
229 |
-
)
|
230 |
-
|
231 |
-
def on_find_match_click(*args):
|
232 |
-
try:
|
233 |
-
print("Starting breed matching process...")
|
234 |
-
user_prefs = UserPreferences(
|
235 |
-
living_space=args[0],
|
236 |
-
yard_access=args[1],
|
237 |
-
exercise_time=args[2],
|
238 |
-
exercise_type=args[3],
|
239 |
-
grooming_commitment=args[4],
|
240 |
-
size_preference=args[5],
|
241 |
-
experience_level=args[6],
|
242 |
-
time_availability=args[7],
|
243 |
-
has_children=args[8],
|
244 |
-
children_age=args[9] if args[8] else None,
|
245 |
-
noise_tolerance=args[10],
|
246 |
-
space_for_play=True if args[0] != "apartment" else False,
|
247 |
-
other_pets=False,
|
248 |
-
climate="moderate",
|
249 |
-
health_sensitivity="medium",
|
250 |
-
barking_acceptance=args[10]
|
251 |
-
)
|
252 |
-
|
253 |
-
recommendations = get_breed_recommendations(user_prefs, top_n=15)
|
254 |
-
|
255 |
-
history_results = [{
|
256 |
-
'breed': rec['breed'],
|
257 |
-
'rank': rec['rank'],
|
258 |
-
'overall_score': rec['final_score'],
|
259 |
-
'base_score': rec['base_score'],
|
260 |
-
'bonus_score': rec['bonus_score'],
|
261 |
-
'scores': rec['scores']
|
262 |
-
} for rec in recommendations]
|
263 |
-
|
264 |
-
history_component.save_search(
|
265 |
-
user_preferences={
|
266 |
-
'living_space': args[0],
|
267 |
-
'yard_access': args[1],
|
268 |
-
'exercise_time': args[2],
|
269 |
-
'exercise_type': args[3],
|
270 |
-
'grooming_commitment': args[4],
|
271 |
-
'size_preference': args[5],
|
272 |
-
'experience_level': args[6],
|
273 |
-
'time_availability': args[7],
|
274 |
-
'has_children': args[8],
|
275 |
-
'children_age': args[9] if args[8] else None,
|
276 |
-
'noise_tolerance': args[10],
|
277 |
-
'search_type': 'Criteria'
|
278 |
-
},
|
279 |
-
results=history_results
|
280 |
-
)
|
281 |
-
|
282 |
-
return [
|
283 |
-
format_recommendation_html(recommendations, is_description_search=False),
|
284 |
-
gr.update(visible=False)
|
285 |
-
]
|
286 |
-
|
287 |
-
except Exception as e:
|
288 |
-
print(f"Error in find match: {str(e)}")
|
289 |
-
import traceback
|
290 |
-
print(traceback.format_exc())
|
291 |
-
return ["Error getting recommendations", gr.HTML.update(visible=False)]
|
292 |
-
|
293 |
-
def update_status_and_process(*args):
|
294 |
-
return [
|
295 |
-
gr.update(value=None, visible=True), # 更新可見性
|
296 |
-
gr.update(visible=True)
|
297 |
-
]
|
298 |
-
|
299 |
-
get_recommendations_btn.click(
|
300 |
-
fn=update_status_and_process, # 先執行狀態更新
|
301 |
-
outputs=[recommendation_output, search_status],
|
302 |
-
queue=False # 確保狀態更新立即執行
|
303 |
-
).then( # 然後執行主要處理邏輯
|
304 |
-
fn=on_find_match_click,
|
305 |
-
inputs=[
|
306 |
-
living_space,
|
307 |
-
yard_access,
|
308 |
-
exercise_time,
|
309 |
-
exercise_type,
|
310 |
-
grooming_commitment,
|
311 |
-
size_preference,
|
312 |
-
experience_level,
|
313 |
-
time_availability,
|
314 |
-
has_children,
|
315 |
-
children_age,
|
316 |
-
noise_tolerance
|
317 |
-
],
|
318 |
-
outputs=[recommendation_output, search_status]
|
319 |
-
)
|
320 |
-
|
321 |
-
return {
|
322 |
-
'living_space': living_space,
|
323 |
-
'exercise_time': exercise_time,
|
324 |
-
'grooming_commitment': grooming_commitment,
|
325 |
-
'experience_level': experience_level,
|
326 |
-
'has_children': has_children,
|
327 |
-
'noise_tolerance': noise_tolerance,
|
328 |
-
'get_recommendations_btn': get_recommendations_btn,
|
329 |
-
'recommendation_output': recommendation_output,
|
330 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|