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