Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,28 +37,40 @@ def save_data(data):
|
|
| 37 |
except Exception as e:
|
| 38 |
print(f"Save Error: {e}")
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
if not prof_list:
|
| 43 |
return pd.DataFrame(columns=["狀態", "姓名", "大學", "系所", "標籤"])
|
| 44 |
|
|
|
|
|
|
|
| 45 |
data = []
|
| 46 |
-
for p in
|
|
|
|
|
|
|
| 47 |
status_map = {'match': '✅', 'mismatch': '❌', 'pending': '❓'}
|
| 48 |
-
status_icon = status_map.get(
|
| 49 |
-
has_detail = "📄" if
|
| 50 |
|
| 51 |
-
tags = ", ".join(
|
| 52 |
|
| 53 |
data.append([
|
| 54 |
f"{status_icon} {has_detail}",
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
tags
|
| 59 |
])
|
| 60 |
return pd.DataFrame(data, columns=["狀態", "姓名", "大學", "系所", "標籤"])
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# --- Event Handlers ---
|
| 63 |
|
| 64 |
def search_professors(query, current_saved):
|
|
@@ -66,63 +78,48 @@ def search_professors(query, current_saved):
|
|
| 66 |
|
| 67 |
try:
|
| 68 |
results = gemini_service.search_professors(query)
|
| 69 |
-
|
| 70 |
-
# 但為了顯示,我們需要跟 saved 做比對 (merge logic)
|
| 71 |
-
return format_df(results), results, gr.update(visible=True) # Show Load More
|
| 72 |
except Exception as e:
|
| 73 |
raise gr.Error(f"搜尋失敗: {e}")
|
| 74 |
|
| 75 |
-
def load_more(query, current_search_results):
|
| 76 |
if not query: return gr.update(), current_search_results
|
| 77 |
|
| 78 |
current_names = [p['name'] for p in current_search_results]
|
| 79 |
try:
|
| 80 |
new_results = gemini_service.search_professors(query, exclude_names=current_names)
|
| 81 |
|
| 82 |
-
# 去重
|
| 83 |
existing_keys = set(get_key(p) for p in current_search_results)
|
| 84 |
for p in new_results:
|
| 85 |
if get_key(p) not in existing_keys:
|
| 86 |
current_search_results.append(p)
|
| 87 |
|
| 88 |
-
return format_df(current_search_results), current_search_results
|
| 89 |
except Exception as e:
|
| 90 |
raise gr.Error(f"載入失敗: {e}")
|
| 91 |
|
| 92 |
-
def select_professor_from_df(evt: gr.SelectData, search_results, saved_data):
|
| 93 |
-
|
| 94 |
index = evt.index[0]
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
# 我們需要知道目前顯示的是哪一份資料。
|
| 101 |
-
# 簡單做法:搜尋後,search_results 會更新。
|
| 102 |
-
# 我們先假設使用者是在點擊 search_results (如果沒搜尋,就顯示 saved)
|
| 103 |
-
|
| 104 |
-
target_list = search_results if search_results else saved_data
|
| 105 |
-
if index >= len(target_list): return gr.update(), gr.update(), gr.update(), None, None
|
| 106 |
|
| 107 |
prof = target_list[index]
|
| 108 |
|
| 109 |
-
# 優先找已存檔的詳細資料
|
| 110 |
key = get_key(prof)
|
| 111 |
saved_prof = next((p for p in saved_data if get_key(p) == key), None)
|
| 112 |
current_prof = saved_prof if saved_prof else prof
|
| 113 |
|
| 114 |
-
# 準備回傳
|
| 115 |
details_md = ""
|
| 116 |
-
chat_history = []
|
| 117 |
|
| 118 |
-
# 檢查是否有 Details
|
| 119 |
if current_prof.get('details') and len(current_prof.get('details')) > 10:
|
| 120 |
details_md = current_prof['details']
|
| 121 |
if not saved_prof:
|
| 122 |
saved_data.insert(0, current_prof)
|
| 123 |
save_data(saved_data)
|
| 124 |
else:
|
| 125 |
-
# 需要 Call API
|
| 126 |
gr.Info(f"正在調查 {current_prof['name']}...")
|
| 127 |
try:
|
| 128 |
res = gemini_service.get_professor_details(current_prof)
|
|
@@ -130,7 +127,6 @@ def select_professor_from_df(evt: gr.SelectData, search_results, saved_data):
|
|
| 130 |
current_prof['sources'] = res['sources']
|
| 131 |
details_md = res['text']
|
| 132 |
|
| 133 |
-
# 更新存檔
|
| 134 |
if saved_prof:
|
| 135 |
saved_prof.update(current_prof)
|
| 136 |
else:
|
|
@@ -139,48 +135,102 @@ def select_professor_from_df(evt: gr.SelectData, search_results, saved_data):
|
|
| 139 |
except Exception as e:
|
| 140 |
raise gr.Error(f"調查失敗: {e}")
|
| 141 |
|
| 142 |
-
# 格式化 Sources
|
| 143 |
if current_prof.get('sources'):
|
| 144 |
details_md += "\n\n### 📚 參考來源\n"
|
| 145 |
for s in current_prof['sources']:
|
| 146 |
details_md += f"- [{s['title']}]({s['uri']})\n"
|
| 147 |
|
| 148 |
return (
|
| 149 |
-
gr.update(visible=True),
|
| 150 |
-
details_md,
|
| 151 |
-
[],
|
| 152 |
-
current_prof,
|
| 153 |
-
saved_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
)
|
| 155 |
|
| 156 |
def chat_response(history, message, selected_prof):
|
| 157 |
if not selected_prof: return history, ""
|
| 158 |
-
|
| 159 |
context = selected_prof.get('details', '')
|
| 160 |
if not context: return history, ""
|
| 161 |
|
| 162 |
-
# 轉換 history 給 service 用
|
| 163 |
-
# Gradio history is [[user, bot], [user, bot]]
|
| 164 |
service_history = []
|
| 165 |
for h in history:
|
| 166 |
service_history.append({"role": "user", "content": h[0]})
|
| 167 |
-
if h[1]:
|
| 168 |
-
service_history.append({"role": "model", "content": h[1]})
|
| 169 |
|
| 170 |
try:
|
| 171 |
reply = gemini_service.chat_with_ai(service_history, message, context)
|
| 172 |
history.append((message, reply))
|
| 173 |
except Exception as e:
|
| 174 |
history.append((message, f"Error: {e}"))
|
| 175 |
-
|
| 176 |
return history, ""
|
| 177 |
|
| 178 |
-
def update_status(status, selected_prof, saved_data):
|
| 179 |
if not selected_prof: return gr.update(), saved_data
|
| 180 |
|
| 181 |
selected_prof['status'] = status if selected_prof.get('status') != status else None
|
| 182 |
|
| 183 |
-
# Update in saved list
|
| 184 |
key = get_key(selected_prof)
|
| 185 |
for i, p in enumerate(saved_data):
|
| 186 |
if get_key(p) == key:
|
|
@@ -188,39 +238,44 @@ def update_status(status, selected_prof, saved_data):
|
|
| 188 |
break
|
| 189 |
save_data(saved_data)
|
| 190 |
|
| 191 |
-
|
|
|
|
| 192 |
|
| 193 |
-
def remove_prof(selected_prof, saved_data):
|
| 194 |
if not selected_prof: return gr.update(), gr.update(value=None), saved_data, gr.update(visible=False)
|
| 195 |
|
| 196 |
key = get_key(selected_prof)
|
| 197 |
new_saved = [p for p in saved_data if get_key(p) != key]
|
| 198 |
save_data(new_saved)
|
| 199 |
|
|
|
|
|
|
|
| 200 |
return (
|
| 201 |
gr.Info("已移除"),
|
| 202 |
-
format_df(new_saved),
|
| 203 |
new_saved,
|
| 204 |
-
gr.update(visible=False)
|
| 205 |
)
|
| 206 |
|
| 207 |
def toggle_view(mode, search_res, saved_data):
|
| 208 |
if mode == "搜尋結果":
|
| 209 |
-
return format_df(search_res), gr.update(visible=True)
|
| 210 |
else:
|
| 211 |
-
return format_df(saved_data), gr.update(visible=False)
|
| 212 |
|
| 213 |
# --- UI Layout ---
|
| 214 |
|
| 215 |
-
|
|
|
|
| 216 |
|
| 217 |
# State
|
| 218 |
saved_state = gr.State(load_data())
|
| 219 |
search_res_state = gr.State([])
|
| 220 |
selected_prof_state = gr.State(None)
|
| 221 |
|
|
|
|
| 222 |
gr.Markdown("# Prof.404 🎓 開箱教授去哪兒? (Gradio Edition)")
|
| 223 |
-
|
| 224 |
with gr.Row():
|
| 225 |
search_input = gr.Textbox(label="搜尋研究領域", placeholder="例如: LLM, 量子計算...", scale=4)
|
| 226 |
search_btn = gr.Button("🔍 搜尋", variant="primary", scale=1)
|
|
@@ -243,12 +298,28 @@ with gr.Blocks(title="開箱教授去哪兒?", theme=gr.themes.Soft()) as demo
|
|
| 243 |
with gr.Column(scale=2, visible=False) as details_col:
|
| 244 |
detail_md = gr.Markdown("詳細資料...")
|
| 245 |
|
|
|
|
| 246 |
with gr.Row():
|
| 247 |
btn_match = gr.Button("✅ 符合")
|
| 248 |
btn_mismatch = gr.Button("❌ 不符")
|
| 249 |
btn_pending = gr.Button("❓ 待觀察")
|
| 250 |
btn_remove = gr.Button("🗑️ 移除", variant="stop")
|
| 251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
gr.Markdown("### 💬 AI 助手")
|
| 253 |
chatbot = gr.Chatbot(height=300)
|
| 254 |
msg = gr.Textbox(label="提問")
|
|
@@ -256,66 +327,58 @@ with gr.Blocks(title="開箱教授去哪兒?", theme=gr.themes.Soft()) as demo
|
|
| 256 |
|
| 257 |
# --- Wiring ---
|
| 258 |
|
| 259 |
-
# Search
|
| 260 |
search_btn.click(
|
| 261 |
search_professors,
|
| 262 |
inputs=[search_input, saved_state],
|
| 263 |
outputs=[prof_df, search_res_state, load_more_btn]
|
| 264 |
)
|
| 265 |
|
| 266 |
-
# Load More
|
| 267 |
load_more_btn.click(
|
| 268 |
load_more,
|
| 269 |
-
inputs=[search_input, search_res_state],
|
| 270 |
outputs=[prof_df, search_res_state]
|
| 271 |
)
|
| 272 |
|
| 273 |
-
# View Toggle
|
| 274 |
view_radio.change(
|
| 275 |
toggle_view,
|
| 276 |
inputs=[view_radio, search_res_state, saved_state],
|
| 277 |
outputs=[prof_df, load_more_btn]
|
| 278 |
)
|
| 279 |
|
| 280 |
-
# Select Row
|
| 281 |
prof_df.select(
|
| 282 |
select_professor_from_df,
|
| 283 |
-
inputs=[search_res_state, saved_state],
|
| 284 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
| 285 |
)
|
| 286 |
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
chat_response,
|
| 295 |
-
inputs=[chatbot, msg, selected_prof_state],
|
| 296 |
-
outputs=[chatbot, msg]
|
| 297 |
)
|
| 298 |
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
|
|
|
| 303 |
|
| 304 |
for btn, status in [(btn_match, 'match'), (btn_mismatch, 'mismatch'), (btn_pending, 'pending')]:
|
| 305 |
btn.click(
|
| 306 |
update_status,
|
| 307 |
-
inputs=[gr.State(status), selected_prof_state, saved_state],
|
| 308 |
-
outputs=[
|
| 309 |
-
).then(
|
| 310 |
-
refresh_current_view,
|
| 311 |
-
inputs=[view_radio, search_res_state, saved_state],
|
| 312 |
-
outputs=[prof_df]
|
| 313 |
)
|
| 314 |
|
| 315 |
-
# Remove
|
| 316 |
btn_remove.click(
|
| 317 |
remove_prof,
|
| 318 |
-
inputs=[selected_prof_state, saved_state],
|
| 319 |
outputs=[gr.State(None), prof_df, saved_state, details_col]
|
| 320 |
)
|
| 321 |
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
print(f"Save Error: {e}")
|
| 39 |
|
| 40 |
+
def format_df(source_list, saved_list):
|
| 41 |
+
if not source_list:
|
|
|
|
| 42 |
return pd.DataFrame(columns=["狀態", "姓名", "大學", "系所", "標籤"])
|
| 43 |
|
| 44 |
+
saved_map = {get_key(p): p for p in saved_list}
|
| 45 |
+
|
| 46 |
data = []
|
| 47 |
+
for p in source_list:
|
| 48 |
+
display_p = saved_map.get(get_key(p), p)
|
| 49 |
+
|
| 50 |
status_map = {'match': '✅', 'mismatch': '❌', 'pending': '❓'}
|
| 51 |
+
status_icon = status_map.get(display_p.get('status'), '')
|
| 52 |
+
has_detail = "📄" if display_p.get('details') else ""
|
| 53 |
|
| 54 |
+
tags = ", ".join(display_p.get('tags', []))
|
| 55 |
|
| 56 |
data.append([
|
| 57 |
f"{status_icon} {has_detail}",
|
| 58 |
+
display_p['name'],
|
| 59 |
+
display_p['university'],
|
| 60 |
+
display_p['department'],
|
| 61 |
tags
|
| 62 |
])
|
| 63 |
return pd.DataFrame(data, columns=["狀態", "姓名", "大學", "系所", "標籤"])
|
| 64 |
|
| 65 |
+
def get_tags_text(prof):
|
| 66 |
+
if not prof or not prof.get('tags'):
|
| 67 |
+
return "目前標籤: (無)"
|
| 68 |
+
return "🏷️ " + ", ".join([f"`{t}`" for t in prof['tags']])
|
| 69 |
+
|
| 70 |
+
def get_tags_choices(prof):
|
| 71 |
+
if not prof: return []
|
| 72 |
+
return prof.get('tags', [])
|
| 73 |
+
|
| 74 |
# --- Event Handlers ---
|
| 75 |
|
| 76 |
def search_professors(query, current_saved):
|
|
|
|
| 78 |
|
| 79 |
try:
|
| 80 |
results = gemini_service.search_professors(query)
|
| 81 |
+
return format_df(results, current_saved), results, gr.update(visible=True)
|
|
|
|
|
|
|
| 82 |
except Exception as e:
|
| 83 |
raise gr.Error(f"搜尋失敗: {e}")
|
| 84 |
|
| 85 |
+
def load_more(query, current_search_results, current_saved):
|
| 86 |
if not query: return gr.update(), current_search_results
|
| 87 |
|
| 88 |
current_names = [p['name'] for p in current_search_results]
|
| 89 |
try:
|
| 90 |
new_results = gemini_service.search_professors(query, exclude_names=current_names)
|
| 91 |
|
|
|
|
| 92 |
existing_keys = set(get_key(p) for p in current_search_results)
|
| 93 |
for p in new_results:
|
| 94 |
if get_key(p) not in existing_keys:
|
| 95 |
current_search_results.append(p)
|
| 96 |
|
| 97 |
+
return format_df(current_search_results, current_saved), current_search_results
|
| 98 |
except Exception as e:
|
| 99 |
raise gr.Error(f"載入失敗: {e}")
|
| 100 |
|
| 101 |
+
def select_professor_from_df(evt: gr.SelectData, search_results, saved_data, view_mode):
|
| 102 |
+
if not evt: return [gr.update()] * 8
|
| 103 |
index = evt.index[0]
|
| 104 |
|
| 105 |
+
target_list = saved_data if view_mode == "追蹤清單" else search_results
|
| 106 |
+
if not target_list or index >= len(target_list):
|
| 107 |
+
return gr.update(), gr.update(), gr.update(), None, None, gr.update(), gr.update(), gr.update()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
prof = target_list[index]
|
| 110 |
|
|
|
|
| 111 |
key = get_key(prof)
|
| 112 |
saved_prof = next((p for p in saved_data if get_key(p) == key), None)
|
| 113 |
current_prof = saved_prof if saved_prof else prof
|
| 114 |
|
|
|
|
| 115 |
details_md = ""
|
|
|
|
| 116 |
|
|
|
|
| 117 |
if current_prof.get('details') and len(current_prof.get('details')) > 10:
|
| 118 |
details_md = current_prof['details']
|
| 119 |
if not saved_prof:
|
| 120 |
saved_data.insert(0, current_prof)
|
| 121 |
save_data(saved_data)
|
| 122 |
else:
|
|
|
|
| 123 |
gr.Info(f"正在調查 {current_prof['name']}...")
|
| 124 |
try:
|
| 125 |
res = gemini_service.get_professor_details(current_prof)
|
|
|
|
| 127 |
current_prof['sources'] = res['sources']
|
| 128 |
details_md = res['text']
|
| 129 |
|
|
|
|
| 130 |
if saved_prof:
|
| 131 |
saved_prof.update(current_prof)
|
| 132 |
else:
|
|
|
|
| 135 |
except Exception as e:
|
| 136 |
raise gr.Error(f"調查失敗: {e}")
|
| 137 |
|
|
|
|
| 138 |
if current_prof.get('sources'):
|
| 139 |
details_md += "\n\n### 📚 參考來源\n"
|
| 140 |
for s in current_prof['sources']:
|
| 141 |
details_md += f"- [{s['title']}]({s['uri']})\n"
|
| 142 |
|
| 143 |
return (
|
| 144 |
+
gr.update(visible=True),
|
| 145 |
+
details_md,
|
| 146 |
+
[],
|
| 147 |
+
current_prof,
|
| 148 |
+
saved_data,
|
| 149 |
+
get_tags_text(current_prof),
|
| 150 |
+
gr.update(choices=get_tags_choices(current_prof), value=None),
|
| 151 |
+
gr.update(visible=True)
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
def add_tag(new_tag, selected_prof, saved_data, view_mode, search_results):
|
| 155 |
+
if not selected_prof or not new_tag:
|
| 156 |
+
return gr.update(), gr.update(), gr.update(), saved_data, gr.update()
|
| 157 |
+
|
| 158 |
+
if 'tags' not in selected_prof: selected_prof['tags'] = []
|
| 159 |
+
|
| 160 |
+
if new_tag not in selected_prof['tags']:
|
| 161 |
+
selected_prof['tags'].append(new_tag)
|
| 162 |
+
|
| 163 |
+
key = get_key(selected_prof)
|
| 164 |
+
found = False
|
| 165 |
+
for i, p in enumerate(saved_data):
|
| 166 |
+
if get_key(p) == key:
|
| 167 |
+
saved_data[i] = selected_prof
|
| 168 |
+
found = True
|
| 169 |
+
break
|
| 170 |
+
if not found:
|
| 171 |
+
saved_data.insert(0, selected_prof)
|
| 172 |
+
|
| 173 |
+
save_data(saved_data)
|
| 174 |
+
gr.Info(f"已新增標籤: {new_tag}")
|
| 175 |
+
|
| 176 |
+
target_list = saved_data if view_mode == "追蹤清單" else search_results
|
| 177 |
+
new_df = format_df(target_list, saved_data)
|
| 178 |
+
|
| 179 |
+
return (
|
| 180 |
+
gr.update(value=""),
|
| 181 |
+
get_tags_text(selected_prof),
|
| 182 |
+
gr.update(choices=selected_prof['tags']),
|
| 183 |
+
saved_data,
|
| 184 |
+
new_df
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
def remove_tag(tag_to_remove, selected_prof, saved_data, view_mode, search_results):
|
| 188 |
+
if not selected_prof or not tag_to_remove:
|
| 189 |
+
return gr.update(), gr.update(), saved_data, gr.update()
|
| 190 |
+
|
| 191 |
+
if 'tags' in selected_prof and tag_to_remove in selected_prof['tags']:
|
| 192 |
+
selected_prof['tags'].remove(tag_to_remove)
|
| 193 |
+
|
| 194 |
+
key = get_key(selected_prof)
|
| 195 |
+
for i, p in enumerate(saved_data):
|
| 196 |
+
if get_key(p) == key:
|
| 197 |
+
saved_data[i] = selected_prof
|
| 198 |
+
break
|
| 199 |
+
save_data(saved_data)
|
| 200 |
+
gr.Info(f"已移除標籤: {tag_to_remove}")
|
| 201 |
+
|
| 202 |
+
target_list = saved_data if view_mode == "追蹤清單" else search_results
|
| 203 |
+
new_df = format_df(target_list, saved_data)
|
| 204 |
+
|
| 205 |
+
return (
|
| 206 |
+
get_tags_text(selected_prof),
|
| 207 |
+
gr.update(choices=selected_prof['tags'], value=None),
|
| 208 |
+
saved_data,
|
| 209 |
+
new_df
|
| 210 |
)
|
| 211 |
|
| 212 |
def chat_response(history, message, selected_prof):
|
| 213 |
if not selected_prof: return history, ""
|
|
|
|
| 214 |
context = selected_prof.get('details', '')
|
| 215 |
if not context: return history, ""
|
| 216 |
|
|
|
|
|
|
|
| 217 |
service_history = []
|
| 218 |
for h in history:
|
| 219 |
service_history.append({"role": "user", "content": h[0]})
|
| 220 |
+
if h[1]: service_history.append({"role": "model", "content": h[1]})
|
|
|
|
| 221 |
|
| 222 |
try:
|
| 223 |
reply = gemini_service.chat_with_ai(service_history, message, context)
|
| 224 |
history.append((message, reply))
|
| 225 |
except Exception as e:
|
| 226 |
history.append((message, f"Error: {e}"))
|
|
|
|
| 227 |
return history, ""
|
| 228 |
|
| 229 |
+
def update_status(status, selected_prof, saved_data, view_mode, search_results):
|
| 230 |
if not selected_prof: return gr.update(), saved_data
|
| 231 |
|
| 232 |
selected_prof['status'] = status if selected_prof.get('status') != status else None
|
| 233 |
|
|
|
|
| 234 |
key = get_key(selected_prof)
|
| 235 |
for i, p in enumerate(saved_data):
|
| 236 |
if get_key(p) == key:
|
|
|
|
| 238 |
break
|
| 239 |
save_data(saved_data)
|
| 240 |
|
| 241 |
+
target_list = saved_data if view_mode == "追蹤清單" else search_results
|
| 242 |
+
return format_df(target_list, saved_data), saved_data
|
| 243 |
|
| 244 |
+
def remove_prof(selected_prof, saved_data, view_mode, search_results):
|
| 245 |
if not selected_prof: return gr.update(), gr.update(value=None), saved_data, gr.update(visible=False)
|
| 246 |
|
| 247 |
key = get_key(selected_prof)
|
| 248 |
new_saved = [p for p in saved_data if get_key(p) != key]
|
| 249 |
save_data(new_saved)
|
| 250 |
|
| 251 |
+
target_list = new_saved if view_mode == "追蹤清單" else search_results
|
| 252 |
+
|
| 253 |
return (
|
| 254 |
gr.Info("已移除"),
|
| 255 |
+
format_df(target_list, new_saved),
|
| 256 |
new_saved,
|
| 257 |
+
gr.update(visible=False)
|
| 258 |
)
|
| 259 |
|
| 260 |
def toggle_view(mode, search_res, saved_data):
|
| 261 |
if mode == "搜尋結果":
|
| 262 |
+
return format_df(search_res, saved_data), gr.update(visible=True)
|
| 263 |
else:
|
| 264 |
+
return format_df(saved_data, saved_data), gr.update(visible=False)
|
| 265 |
|
| 266 |
# --- UI Layout ---
|
| 267 |
|
| 268 |
+
# 🌟 這裡修正了標題,加入 Prof.404
|
| 269 |
+
with gr.Blocks(title="Prof.404 開箱教授去哪兒?", theme=gr.themes.Soft()) as demo:
|
| 270 |
|
| 271 |
# State
|
| 272 |
saved_state = gr.State(load_data())
|
| 273 |
search_res_state = gr.State([])
|
| 274 |
selected_prof_state = gr.State(None)
|
| 275 |
|
| 276 |
+
# 🌟 頁面大標題也一併修正
|
| 277 |
gr.Markdown("# Prof.404 🎓 開箱教授去哪兒? (Gradio Edition)")
|
| 278 |
+
|
| 279 |
with gr.Row():
|
| 280 |
search_input = gr.Textbox(label="搜尋研究領域", placeholder="例如: LLM, 量子計算...", scale=4)
|
| 281 |
search_btn = gr.Button("🔍 搜尋", variant="primary", scale=1)
|
|
|
|
| 298 |
with gr.Column(scale=2, visible=False) as details_col:
|
| 299 |
detail_md = gr.Markdown("詳細資料...")
|
| 300 |
|
| 301 |
+
# Status Buttons
|
| 302 |
with gr.Row():
|
| 303 |
btn_match = gr.Button("✅ 符合")
|
| 304 |
btn_mismatch = gr.Button("❌ 不符")
|
| 305 |
btn_pending = gr.Button("❓ 待觀察")
|
| 306 |
btn_remove = gr.Button("🗑️ 移除", variant="stop")
|
| 307 |
|
| 308 |
+
gr.Markdown("---")
|
| 309 |
+
|
| 310 |
+
# Tags Management
|
| 311 |
+
with gr.Column(visible=False) as tags_row:
|
| 312 |
+
tags_display = gr.Markdown("目前標籤: (無)")
|
| 313 |
+
with gr.Row():
|
| 314 |
+
tag_input = gr.Textbox(label="新增標籤", placeholder="輸入後按新增...", scale=3)
|
| 315 |
+
tag_add_btn = gr.Button("➕ 新增", scale=1)
|
| 316 |
+
|
| 317 |
+
with gr.Accordion("刪除標籤", open=False):
|
| 318 |
+
with gr.Row():
|
| 319 |
+
tag_dropdown = gr.Dropdown(label="選擇標籤", choices=[], scale=3)
|
| 320 |
+
tag_del_btn = gr.Button("🗑️ 刪除", scale=1, variant="secondary")
|
| 321 |
+
|
| 322 |
+
gr.Markdown("---")
|
| 323 |
gr.Markdown("### 💬 AI 助手")
|
| 324 |
chatbot = gr.Chatbot(height=300)
|
| 325 |
msg = gr.Textbox(label="提問")
|
|
|
|
| 327 |
|
| 328 |
# --- Wiring ---
|
| 329 |
|
|
|
|
| 330 |
search_btn.click(
|
| 331 |
search_professors,
|
| 332 |
inputs=[search_input, saved_state],
|
| 333 |
outputs=[prof_df, search_res_state, load_more_btn]
|
| 334 |
)
|
| 335 |
|
|
|
|
| 336 |
load_more_btn.click(
|
| 337 |
load_more,
|
| 338 |
+
inputs=[search_input, search_res_state, saved_state],
|
| 339 |
outputs=[prof_df, search_res_state]
|
| 340 |
)
|
| 341 |
|
|
|
|
| 342 |
view_radio.change(
|
| 343 |
toggle_view,
|
| 344 |
inputs=[view_radio, search_res_state, saved_state],
|
| 345 |
outputs=[prof_df, load_more_btn]
|
| 346 |
)
|
| 347 |
|
|
|
|
| 348 |
prof_df.select(
|
| 349 |
select_professor_from_df,
|
| 350 |
+
inputs=[search_res_state, saved_state, view_radio],
|
| 351 |
+
outputs=[
|
| 352 |
+
details_col, detail_md, chatbot, selected_prof_state, saved_state,
|
| 353 |
+
tags_display, tag_dropdown, tags_row
|
| 354 |
+
]
|
| 355 |
)
|
| 356 |
|
| 357 |
+
send_btn.click(chat_response, inputs=[chatbot, msg, selected_prof_state], outputs=[chatbot, msg])
|
| 358 |
+
msg.submit(chat_response, inputs=[chatbot, msg, selected_prof_state], outputs=[chatbot, msg])
|
| 359 |
+
|
| 360 |
+
tag_add_btn.click(
|
| 361 |
+
add_tag,
|
| 362 |
+
inputs=[tag_input, selected_prof_state, saved_state, view_radio, search_res_state],
|
| 363 |
+
outputs=[tag_input, tags_display, tag_dropdown, saved_state, prof_df]
|
|
|
|
|
|
|
|
|
|
| 364 |
)
|
| 365 |
|
| 366 |
+
tag_del_btn.click(
|
| 367 |
+
remove_tag,
|
| 368 |
+
inputs=[tag_dropdown, selected_prof_state, saved_state, view_radio, search_res_state],
|
| 369 |
+
outputs=[tags_display, tag_dropdown, saved_state, prof_df]
|
| 370 |
+
)
|
| 371 |
|
| 372 |
for btn, status in [(btn_match, 'match'), (btn_mismatch, 'mismatch'), (btn_pending, 'pending')]:
|
| 373 |
btn.click(
|
| 374 |
update_status,
|
| 375 |
+
inputs=[gr.State(status), selected_prof_state, saved_state, view_radio, search_res_state],
|
| 376 |
+
outputs=[prof_df, saved_state]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
)
|
| 378 |
|
|
|
|
| 379 |
btn_remove.click(
|
| 380 |
remove_prof,
|
| 381 |
+
inputs=[selected_prof_state, saved_state, view_radio, search_res_state],
|
| 382 |
outputs=[gr.State(None), prof_df, saved_state, details_col]
|
| 383 |
)
|
| 384 |
|