Spaces:
Running
on
Zero
Running
on
Zero
Delete search_history.py
Browse files- search_history.py +0 -154
search_history.py
DELETED
@@ -1,154 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import traceback
|
3 |
-
from typing import Optional, Dict, List
|
4 |
-
from history_manager import UserHistoryManager
|
5 |
-
|
6 |
-
class SearchHistoryComponent:
|
7 |
-
def __init__(self):
|
8 |
-
"""初始化搜索歷史組件"""
|
9 |
-
self.history_manager = UserHistoryManager()
|
10 |
-
|
11 |
-
def format_history_html(self, history_data: Optional[List[Dict]] = None) -> str:
|
12 |
-
"""將歷史記錄格式化為HTML"""
|
13 |
-
try:
|
14 |
-
if history_data is None:
|
15 |
-
history_data = self.history_manager.get_history()
|
16 |
-
|
17 |
-
if not history_data:
|
18 |
-
return """
|
19 |
-
<div style='text-align: center; padding: 20px; color: #666;'>
|
20 |
-
No search history yet. Try making some breed recommendations!
|
21 |
-
</div>
|
22 |
-
"""
|
23 |
-
|
24 |
-
html = "<div class='history-container'>"
|
25 |
-
|
26 |
-
for entry in reversed(history_data):
|
27 |
-
timestamp = entry.get('timestamp', 'Unknown time')
|
28 |
-
prefs = entry.get('preferences', {})
|
29 |
-
results = entry.get('results', [])
|
30 |
-
|
31 |
-
html += f"""
|
32 |
-
<div class="history-entry">
|
33 |
-
<div class="history-header">
|
34 |
-
<span class="timestamp">🕒 {timestamp}</span>
|
35 |
-
</div>
|
36 |
-
|
37 |
-
<div class="params-list">
|
38 |
-
<h4>Search Parameters:</h4>
|
39 |
-
<ul>
|
40 |
-
<li><span class="param-label">Living Space:</span> {prefs.get('living_space', 'N/A')}</li>
|
41 |
-
<li><span class="param-label">Exercise Time:</span> {prefs.get('exercise_time', 'N/A')} minutes</li>
|
42 |
-
<li><span class="param-label">Grooming:</span> {prefs.get('grooming_commitment', 'N/A')}</li>
|
43 |
-
<li><span class="param-label">Experience:</span> {prefs.get('experience_level', 'N/A')}</li>
|
44 |
-
<li><span class="param-label">Children at Home:</span> {"Yes" if prefs.get('has_children') else "No"}</li>
|
45 |
-
<li><span class="param-label">Noise Tolerance:</span> {prefs.get('noise_tolerance', 'N/A')}</li>
|
46 |
-
</ul>
|
47 |
-
</div>
|
48 |
-
|
49 |
-
<div class="results-list">
|
50 |
-
<h4>Top 5 Breed Matches:</h4>
|
51 |
-
<div class="breed-list">
|
52 |
-
"""
|
53 |
-
|
54 |
-
if results:
|
55 |
-
for i, result in enumerate(results[:5], 1):
|
56 |
-
breed_name = result.get('breed', 'Unknown breed').replace('_', ' ')
|
57 |
-
score = result.get('overall_score', result.get('final_score', 0))
|
58 |
-
html += f"""
|
59 |
-
<div class="breed-item">
|
60 |
-
<div class="breed-info">
|
61 |
-
<span class="breed-rank">#{i}</span>
|
62 |
-
<span class="breed-name">{breed_name}</span>
|
63 |
-
<span class="breed-score">{score*100:.1f}%</span>
|
64 |
-
</div>
|
65 |
-
</div>
|
66 |
-
"""
|
67 |
-
|
68 |
-
html += """
|
69 |
-
</div>
|
70 |
-
</div>
|
71 |
-
</div>
|
72 |
-
"""
|
73 |
-
|
74 |
-
html += "</div>"
|
75 |
-
return html
|
76 |
-
|
77 |
-
except Exception as e:
|
78 |
-
print(f"Error formatting history: {str(e)}")
|
79 |
-
print(traceback.format_exc())
|
80 |
-
return f"""
|
81 |
-
<div style='text-align: center; padding: 20px; color: #dc2626;'>
|
82 |
-
Error formatting history. Please try refreshing the page.
|
83 |
-
<br>Error details: {str(e)}
|
84 |
-
</div>
|
85 |
-
"""
|
86 |
-
|
87 |
-
def clear_history(self) -> str:
|
88 |
-
"""清除所有搜尋歷史"""
|
89 |
-
try:
|
90 |
-
success = self.history_manager.clear_all_history()
|
91 |
-
print(f"Clear history result: {success}")
|
92 |
-
return self.format_history_html()
|
93 |
-
except Exception as e:
|
94 |
-
print(f"Error in clear_history: {str(e)}")
|
95 |
-
print(traceback.format_exc())
|
96 |
-
return "Error clearing history"
|
97 |
-
|
98 |
-
def refresh_history(self) -> str:
|
99 |
-
"""刷新歷史記錄顯示"""
|
100 |
-
try:
|
101 |
-
return self.format_history_html()
|
102 |
-
except Exception as e:
|
103 |
-
print(f"Error in refresh_history: {str(e)}")
|
104 |
-
return "Error refreshing history"
|
105 |
-
|
106 |
-
def save_search(self, user_preferences: dict, results: list) -> bool:
|
107 |
-
"""保存搜索結果"""
|
108 |
-
return self.history_manager.save_history(user_preferences, results)
|
109 |
-
|
110 |
-
def create_history_tab() -> None:
|
111 |
-
"""創建歷史記錄標籤頁"""
|
112 |
-
history_component = SearchHistoryComponent()
|
113 |
-
|
114 |
-
with gr.TabItem("Recommendation Search History"):
|
115 |
-
gr.HTML("""
|
116 |
-
<div style='text-align: center; padding: 20px;'>
|
117 |
-
<h3 style='color: #2D3748; margin-bottom: 10px;'>Search History</h3>
|
118 |
-
<p style='color: #4A5568;'>View your previous breed recommendations and search preferences</p>
|
119 |
-
</div>
|
120 |
-
""")
|
121 |
-
|
122 |
-
with gr.Row():
|
123 |
-
with gr.Column(scale=4):
|
124 |
-
history_display = gr.HTML()
|
125 |
-
|
126 |
-
with gr.Row():
|
127 |
-
with gr.Column(scale=1):
|
128 |
-
clear_history_btn = gr.Button(
|
129 |
-
"🗑️ Clear History",
|
130 |
-
variant="secondary",
|
131 |
-
size="sm"
|
132 |
-
)
|
133 |
-
with gr.Column(scale=1):
|
134 |
-
refresh_btn = gr.Button(
|
135 |
-
"🔄 Refresh",
|
136 |
-
variant="secondary",
|
137 |
-
size="sm"
|
138 |
-
)
|
139 |
-
|
140 |
-
history_display.value = history_component.format_history_html()
|
141 |
-
|
142 |
-
clear_history_btn.click(
|
143 |
-
fn=history_component.clear_history,
|
144 |
-
outputs=[history_display],
|
145 |
-
api_name="clear_history"
|
146 |
-
)
|
147 |
-
|
148 |
-
refresh_btn.click(
|
149 |
-
fn=history_component.refresh_history,
|
150 |
-
outputs=[history_display],
|
151 |
-
api_name="refresh_history"
|
152 |
-
)
|
153 |
-
|
154 |
-
return history_component
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|