DeepLearning101 commited on
Commit
a571fbf
·
verified ·
1 Parent(s): 5b3bc74

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +353 -390
app.py CHANGED
@@ -8,7 +8,8 @@ from huggingface_hub import HfApi, hf_hub_download
8
 
9
  # Load Env
10
  load_dotenv()
11
- SAVE_FILE = os.getenv("SAVE_FILE_NAME", "saved_companies.json")
 
12
  HF_TOKEN = os.getenv("HF_TOKEN")
13
  DATASET_REPO_ID = os.getenv("DATASET_REPO_ID")
14
 
@@ -19,438 +20,400 @@ except Exception as e:
19
  print(f"Service Error: {e}")
20
  gemini_service = None
21
 
22
- # --- Helper Functions ---
23
 
24
- def get_key(c):
25
- return f"{c['name']}"
26
-
27
- def load_data():
28
  data = []
29
- # 1. 嘗試從雲端下載
30
  if HF_TOKEN and DATASET_REPO_ID:
31
  try:
32
- print(f"正在同步雲端資料: {DATASET_REPO_ID}...")
33
- hf_hub_download(
34
- repo_id=DATASET_REPO_ID,
35
- filename=SAVE_FILE,
36
- repo_type="dataset",
37
- token=HF_TOKEN,
38
- local_dir="." # 覆蓋本地檔案
39
- )
40
- print("雲端同步完成。")
41
- except Exception as e:
42
- print(f"雲端同步略過 (初次啟動或無權限): {e}")
43
-
44
- # 2. 讀取檔案
45
- if os.path.exists(SAVE_FILE):
46
  try:
47
- with open(SAVE_FILE, 'r', encoding='utf-8') as f:
48
- data = json.load(f)
49
- except:
50
- data = []
51
  return data
52
 
53
- def save_data(data):
54
- # 1. 存本地
55
  try:
56
- with open(SAVE_FILE, 'w', encoding='utf-8') as f:
57
- json.dump(data, f, ensure_ascii=False, indent=2)
58
- except Exception as e:
59
- print(f"Save Error: {e}")
60
- return
61
-
62
- # 2. 上傳雲端
63
  if HF_TOKEN and DATASET_REPO_ID:
64
  try:
65
  api = HfApi(token=HF_TOKEN)
66
- api.upload_file(
67
- path_or_fileobj=SAVE_FILE,
68
- path_in_repo=SAVE_FILE,
69
- repo_id=DATASET_REPO_ID,
70
- repo_type="dataset",
71
- commit_message="Sync company data"
72
- )
73
- except Exception as e:
74
- print(f"Upload Error: {e}")
75
-
76
- def format_df(source_list, saved_list):
77
- if not source_list:
78
- return pd.DataFrame(columns=["狀態", "公司名稱", "產業類別", "標籤"])
79
-
80
- if saved_list is None:
81
- saved_list = []
82
-
83
- saved_map = {get_key(c): c for c in saved_list}
84
-
85
- data = []
86
- for c in source_list:
87
- display_c = saved_map.get(get_key(c), c)
88
-
89
- status_map = {'good': '✅ 優質', 'risk': '⚠️ 風險', 'pending': '❓ 未定'}
90
- status_icon = status_map.get(display_c.get('status'), '')
91
- has_detail = "📄" if display_c.get('details') else ""
92
-
93
- tags = ", ".join(display_c.get('tags', []))
94
-
95
- data.append([
96
- f"{status_icon} {has_detail}",
97
- display_c['name'],
98
- display_c.get('industry', '未知'),
99
- tags
100
- ])
101
- return pd.DataFrame(data, columns=["狀態", "公司名稱", "產業類別", "標籤"])
102
 
103
- def get_tags_text(comp):
104
- if not comp or not comp.get('tags'):
105
- return "目前標籤: (無)"
106
- return "🏷️ " + ", ".join([f"`{t}`" for t in comp['tags']])
107
 
108
- def get_tags_choices(comp):
109
- if not comp: return []
110
- return comp.get('tags', [])
111
 
112
- # --- Event Handlers ---
113
 
114
- def search_companies(query, current_saved):
 
 
 
 
 
 
 
 
 
 
 
 
115
  if not query: return gr.update(), current_saved, gr.update()
116
-
117
  try:
118
- results = gemini_service.search_companies(query)
119
- return format_df(results, current_saved), results, gr.update(visible=True)
120
- except Exception as e:
121
- raise gr.Error(f"搜尋失敗: {e}")
122
 
123
- def load_more(query, current_results, current_saved):
124
- if not query: return gr.update(), current_results
125
-
126
- current_names = [c['name'] for c in current_results]
127
  try:
128
- new_results = gemini_service.search_companies(query, exclude_names=current_names)
129
-
130
- existing_keys = set(get_key(c) for c in current_results)
131
- for c in new_results:
132
- if get_key(c) not in existing_keys:
133
- current_results.append(c)
134
-
135
- return format_df(current_results, current_saved), current_results
136
- except Exception as e:
137
- raise gr.Error(f"載入失敗: {e}")
138
-
139
- def select_company(evt: gr.SelectData, search_results, saved_data, view_mode):
140
- if not evt: return [gr.update()] * 8
141
- index = evt.index[0]
142
-
143
- target_list = saved_data if view_mode == "追蹤清單" else search_results
144
- if not target_list or index >= len(target_list):
145
- return gr.update(), gr.update(), gr.update(), None, None, gr.update(), gr.update(), gr.update()
146
-
147
- comp = target_list[index]
148
-
149
- key = get_key(comp)
150
- saved_comp = next((c for c in saved_data if get_key(c) == key), None)
151
- current_comp = saved_comp if saved_comp else comp
152
-
153
- details_md = ""
154
-
155
- # Check Cache
156
- if current_comp.get('details') and len(current_comp.get('details')) > 10:
157
- details_md = current_comp['details']
158
- if not saved_comp:
159
- saved_data.insert(0, current_comp)
160
- save_data(saved_data)
161
  else:
162
- # Call API
163
- gr.Info(f"正在調查 {current_comp['name']} (查詢統編、PTT評價)...")
164
  try:
165
- res = gemini_service.get_company_details(current_comp)
166
- current_comp['details'] = res['text']
167
- current_comp['sources'] = res['sources']
168
- details_md = res['text']
169
-
170
- if saved_comp:
171
- saved_comp.update(current_comp)
172
- else:
173
- saved_data.insert(0, current_comp)
174
- save_data(saved_data)
175
- except Exception as e:
176
- raise gr.Error(f"調查失敗: {e}")
177
-
178
- if current_comp.get('sources'):
179
- details_md += "\n\n### 📚 資料來源\n"
180
- for s in current_comp['sources']:
181
- details_md += f"- [{s['title']}]({s['uri']})\n"
182
-
183
- return (
184
- gr.update(visible=True),
185
- details_md,
186
- [],
187
- current_comp,
188
- saved_data,
189
- get_tags_text(current_comp),
190
- gr.update(choices=get_tags_choices(current_comp), value=None),
191
- gr.update(visible=True)
192
- )
193
-
194
- def add_tag(new_tag, selected_comp, saved_data, view_mode, search_results):
195
- if not selected_comp or not new_tag:
196
- return gr.update(), gr.update(), gr.update(), saved_data, gr.update()
197
-
198
- if 'tags' not in selected_comp: selected_comp['tags'] = []
199
-
200
- if new_tag not in selected_comp['tags']:
201
- selected_comp['tags'].append(new_tag)
202
-
203
- key = get_key(selected_comp)
204
  found = False
205
- for i, c in enumerate(saved_data):
206
- if get_key(c) == key:
207
- saved_data[i] = selected_comp
208
- found = True
209
- break
210
- if not found:
211
- saved_data.insert(0, selected_comp)
212
-
213
- save_data(saved_data)
214
- gr.Info(f"已新增標籤: {new_tag}")
215
-
216
- target_list = saved_data if view_mode == "追蹤清單" else search_results
217
- new_df = format_df(target_list, saved_data)
218
-
219
- return (
220
- gr.update(value=""),
221
- get_tags_text(selected_comp),
222
- gr.update(choices=selected_comp['tags']),
223
- saved_data,
224
- new_df
225
- )
226
-
227
- def remove_tag(tag_to_remove, selected_comp, saved_data, view_mode, search_results):
228
- if not selected_comp or not tag_to_remove:
229
- return gr.update(), gr.update(), saved_data, gr.update()
230
-
231
- if 'tags' in selected_comp and tag_to_remove in selected_comp['tags']:
232
- selected_comp['tags'].remove(tag_to_remove)
233
-
234
- key = get_key(selected_comp)
235
- for i, c in enumerate(saved_data):
236
- if get_key(c) == key:
237
- saved_data[i] = selected_comp
238
- break
239
- save_data(saved_data)
240
- gr.Info(f"已移除標籤: {tag_to_remove}")
241
-
242
- target_list = saved_data if view_mode == "追蹤清單" else search_results
243
- new_df = format_df(target_list, saved_data)
244
-
245
- return (
246
- get_tags_text(selected_comp),
247
- gr.update(choices=selected_comp['tags'], value=None),
248
- saved_data,
249
- new_df
250
- )
251
-
252
- def chat_response(history, message, selected_comp):
253
- if not selected_comp: return history, ""
254
- context = selected_comp.get('details', '')
255
- if not context: return history, ""
256
-
257
- service_history = []
258
- for h in history:
259
- service_history.append({"role": "user", "content": h[0]})
260
- if h[1]: service_history.append({"role": "model", "content": h[1]})
261
-
262
  try:
263
- reply = gemini_service.chat_with_ai(service_history, message, context)
264
- history.append((message, reply))
265
- except Exception as e:
266
- history.append((message, f"Error: {e}"))
267
- return history, ""
268
-
269
- def update_status(status, selected_comp, saved_data, view_mode, search_results):
270
- if not selected_comp: return gr.update(), saved_data
271
-
272
- selected_comp['status'] = status if selected_comp.get('status') != status else None
273
-
274
- key = get_key(selected_comp)
275
- for i, c in enumerate(saved_data):
276
- if get_key(c) == key:
277
- saved_data[i] = selected_comp
278
- break
279
- save_data(saved_data)
280
-
281
- target_list = saved_data if view_mode == "追蹤清單" else search_results
282
- return format_df(target_list, saved_data), saved_data
283
 
284
- def remove_comp(selected_comp, saved_data, view_mode, search_results):
285
- if not selected_comp: return gr.update(), gr.update(value=None), saved_data, gr.update(visible=False)
286
-
287
- key = get_key(selected_comp)
288
- new_saved = [c for c in saved_data if get_key(c) != key]
289
- save_data(new_saved)
290
-
291
- target_list = new_saved if view_mode == "追蹤清單" else search_results
292
-
293
- return (
294
- gr.Info("已移除"),
295
- format_df(target_list, new_saved),
296
- new_saved,
297
- gr.update(visible=False)
298
- )
299
-
300
- def toggle_view(mode, search_res, saved_data):
301
- if mode == "搜尋結果":
302
- return format_df(search_res, saved_data), gr.update(visible=True)
 
 
 
 
 
 
303
  else:
304
- return format_df(saved_data, saved_data), gr.update(visible=False)
305
-
306
- def init_on_load():
307
- data = load_data()
308
- return data, format_df(data, data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
309
 
310
  # --- UI Layout ---
311
 
312
- with gr.Blocks(title="Com.404 公司去那兒?", theme=gr.themes.Soft()) as demo:
313
 
314
- saved_state = gr.State([])
315
- search_res_state = gr.State([])
316
- selected_comp_state = gr.State(None)
317
-
318
  gr.Markdown("""
319
  <div align="center">
320
 
321
- # 🏢 Com.404 - 公司去那兒?
322
 
323
- [![Hugging Face Spaces](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue)](https://huggingface.co/spaces/DeepLearning101/Com.404)
324
  [![GitHub](https://img.shields.io/badge/GitHub-Repo-black)](https://github.com/Deep-Learning-101/prof-404)
325
  [![Powered by](https://img.shields.io/badge/Powered%20by-Gemini%202.0%20Flash-4285F4?logo=google)](https://deepmind.google/technologies/gemini/)
326
 
327
- **產業導航、公司徵信、AI 諮詢,拒絕當求職與合作的無頭蒼蠅 🪰**
328
- <span style="font-size: 0.9em; color: gray;">(支援雲端同步!Space 重啟資料不遺失 🔄 | API KEY RPD,建議自行 Fork)</span>
329
-
330
- 👉 歡迎 Star [GitHub](https://github.com/Deep-Learning-101/prof-404) ⭐ 覺得不錯 👈
331
- <h3>🧠 補腦專區:<a href="https://deep-learning-101.github.io/" target="_blank">Deep Learning 101</a></h3>
332
-
333
- | 🔥 技術傳送門 (Tech Stack) | 📚 必讀心法 (Must Read) |
334
- | :--- | :--- |
335
- | 🤖 [**大語言模型 (LLM)**](https://deep-learning-101.github.io/Large-Language-Model) | 🏹 [**策略篇:企業入門策略**](https://deep-learning-101.github.io/Blog/AIBeginner) |
336
- | 📝 [**自然語言處理 (NLP)**](https://deep-learning-101.github.io/Natural-Language-Processing) | 📊 [**評測篇:臺灣 LLM 分析**](https://deep-learning-101.github.io/Blog/TW-LLM-Benchmark) |
337
- | 👁️ [**電腦視覺 (CV)**](https://deep-learning-101.github.io//Computer-Vision) | 🛠️ [**實戰篇:打造高精準 RAG**](https://deep-learning-101.github.io/RAG) |
338
- | 🎤 [**語音處理 (Speech)**](https://deep-learning-101.github.io/Speech-Processing) | 🕳️ [**避坑篇:AI Agent 開發陷阱**](https://deep-learning-101.github.io/agent) |
339
  </div>
340
  """)
341
-
342
- with gr.Row():
343
- search_input = gr.Textbox(label="探索領域或公司", placeholder="輸入產業領域 (如: 量子計算)、技術關鍵字或公司名稱...", scale=4)
344
- search_btn = gr.Button("🔍 搜尋", variant="primary", scale=1)
345
-
346
- with gr.Row():
347
- view_radio = gr.Radio(["搜尋結果", "追蹤清單"], label="顯示模式", value="追蹤清單")
348
-
349
- with gr.Row():
350
- # Left: List
351
- with gr.Column(scale=1):
352
- comp_df = gr.Dataframe(
353
- headers=["狀態", "公司名稱", "產業類別", "標籤"],
354
- datatype=["str", "str", "str", "str"],
355
- interactive=False,
356
- label="公司列表 (點擊查看詳情)"
357
- )
358
- load_more_btn = gr.Button("載入更多", visible=False)
359
 
360
- # Right: Details
361
- with gr.Column(scale=2, visible=False) as details_col:
362
- detail_md = gr.Markdown("詳細資料...")
 
 
 
 
363
 
364
- # Chat Section
365
- with gr.Column(elem_classes="chat-section"):
366
- gr.Markdown("### 🤖 商業顧問 (已閱讀下方報告)")
367
- # 這裡 Chatbot 使用預設設定 (相容 Tuple 格式)
368
- chatbot = gr.Chatbot(height=250)
369
- with gr.Row():
370
- msg = gr.Textbox(label="提問", placeholder="例如:這間公司適合新鮮人嗎?有勞資糾紛嗎?", scale=4)
371
- send_btn = gr.Button("送出", scale=1)
372
-
373
- gr.Markdown("---")
374
-
375
- # Tags & Status
376
- with gr.Column(visible=False) as tags_row:
377
- tags_display = gr.Markdown("目前標籤: (無)")
378
- with gr.Row():
379
- tag_input = gr.Textbox(label="新增標籤", placeholder="例如: 薪水高, 加班多...", scale=3)
380
- tag_add_btn = gr.Button("➕ 新增", scale=1)
381
- with gr.Accordion("刪除標籤", open=False):
 
 
 
 
 
 
 
 
 
 
 
382
  with gr.Row():
383
- tag_dropdown = gr.Dropdown(label="選擇標籤", choices=[], scale=3)
384
- tag_del_btn = gr.Button("🗑️ 刪除", scale=1, variant="secondary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385
 
386
  with gr.Row():
387
- btn_good = gr.Button(" 優質")
388
- btn_risk = gr.Button("⚠️ 風險")
389
- btn_pending = gr.Button("❓ 未定")
390
- btn_remove = gr.Button("🗑️ 移除", variant="stop")
391
-
392
- # --- Wiring ---
393
-
394
- demo.load(init_on_load, inputs=None, outputs=[saved_state, comp_df])
395
-
396
- # 🌟 這裡修正了:search_companies
397
- search_btn.click(
398
- search_companies,
399
- inputs=[search_input, saved_state],
400
- outputs=[comp_df, search_res_state, load_more_btn]
401
- ).then(
402
- lambda: gr.update(value="搜尋結果"), outputs=[view_radio]
403
- )
404
-
405
- load_more_btn.click(
406
- load_more,
407
- inputs=[search_input, search_res_state, saved_state],
408
- outputs=[comp_df, search_res_state]
409
- )
410
-
411
- view_radio.change(
412
- toggle_view,
413
- inputs=[view_radio, search_res_state, saved_state],
414
- outputs=[comp_df, load_more_btn]
415
- )
416
-
417
- # 🌟 這裡修正了:select_company
418
- comp_df.select(
419
- select_company,
420
- inputs=[search_res_state, saved_state, view_radio],
421
- outputs=[
422
- details_col, detail_md, chatbot, selected_comp_state, saved_state,
423
- tags_display, tag_dropdown, tags_row
424
- ]
425
- )
426
-
427
- send_btn.click(chat_response, inputs=[chatbot, msg, selected_comp_state], outputs=[chatbot, msg])
428
- msg.submit(chat_response, inputs=[chatbot, msg, selected_comp_state], outputs=[chatbot, msg])
429
-
430
- tag_add_btn.click(
431
- add_tag,
432
- inputs=[tag_input, selected_comp_state, saved_state, view_radio, search_res_state],
433
- outputs=[tag_input, tags_display, tag_dropdown, saved_state, comp_df]
434
- )
435
- tag_del_btn.click(
436
- remove_tag,
437
- inputs=[tag_dropdown, selected_comp_state, saved_state, view_radio, search_res_state],
438
- outputs=[tags_display, tag_dropdown, saved_state, comp_df]
439
- )
440
-
441
- for btn, status in [(btn_good, 'good'), (btn_risk, 'risk'), (btn_pending, 'pending')]:
442
- btn.click(
443
- update_status,
444
- inputs=[gr.State(status), selected_comp_state, saved_state, view_radio, search_res_state],
445
- outputs=[comp_df, saved_state]
446
- )
447
-
448
- # 🌟 這裡修正了:remove_comp
449
- btn_remove.click(
450
- remove_comp,
451
- inputs=[selected_comp_state, saved_state, view_radio, search_res_state],
452
- outputs=[gr.State(None), comp_df, saved_state, details_col]
453
- )
454
 
455
  if __name__ == "__main__":
456
  demo.launch()
 
8
 
9
  # Load Env
10
  load_dotenv()
11
+ PROF_SAVE_FILE = "saved_professors.json"
12
+ COMP_SAVE_FILE = "saved_companies.json"
13
  HF_TOKEN = os.getenv("HF_TOKEN")
14
  DATASET_REPO_ID = os.getenv("DATASET_REPO_ID")
15
 
 
20
  print(f"Service Error: {e}")
21
  gemini_service = None
22
 
23
+ # --- Shared Helper Functions ---
24
 
25
+ def load_data(filename):
 
 
 
26
  data = []
 
27
  if HF_TOKEN and DATASET_REPO_ID:
28
  try:
29
+ hf_hub_download(repo_id=DATASET_REPO_ID, filename=filename, repo_type="dataset", token=HF_TOKEN, local_dir=".")
30
+ except: pass
31
+ if os.path.exists(filename):
 
 
 
 
 
 
 
 
 
 
 
32
  try:
33
+ with open(filename, 'r', encoding='utf-8') as f: data = json.load(f)
34
+ except: data = []
 
 
35
  return data
36
 
37
+ def save_data(data, filename):
 
38
  try:
39
+ with open(filename, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=2)
40
+ except: return
 
 
 
 
 
41
  if HF_TOKEN and DATASET_REPO_ID:
42
  try:
43
  api = HfApi(token=HF_TOKEN)
44
+ api.upload_file(path_or_fileobj=filename, path_in_repo=filename, repo_id=DATASET_REPO_ID, repo_type="dataset", commit_message=f"Sync {filename}")
45
+ except: pass
46
+
47
+ def get_tags_text(item):
48
+ if not item or not item.get('tags'): return "目前標籤: (無)"
49
+ return "🏷️ " + ", ".join([f"`{t}`" for t in item['tags']])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
+ def get_tags_choices(item):
52
+ return item.get('tags', []) if item else []
 
 
53
 
54
+ # --- 🎓 Professor Logic ---
 
 
55
 
56
+ def prof_get_key(p): return f"{p['name']}-{p['university']}"
57
 
58
+ def prof_format_df(source_list, saved_list):
59
+ if not source_list: return pd.DataFrame(columns=["狀態", "姓名", "大學", "系所", "標籤"])
60
+ if saved_list is None: saved_list = []
61
+ saved_map = {prof_get_key(p): p for p in saved_list}
62
+ data = []
63
+ for p in source_list:
64
+ dp = saved_map.get(prof_get_key(p), p)
65
+ icon = {'match':'✅','mismatch':'❌','pending':'❓'}.get(dp.get('status'), '')
66
+ detail = "📄" if dp.get('details') else ""
67
+ data.append([f"{icon} {detail}", dp['name'], dp['university'], dp['department'], ", ".join(dp.get('tags', []))])
68
+ return pd.DataFrame(data, columns=["狀態", "姓名", "大學", "系所", "標籤"])
69
+
70
+ def prof_search(query, current_saved):
71
  if not query: return gr.update(), current_saved, gr.update()
 
72
  try:
73
+ res = gemini_service.search_professors(query)
74
+ return prof_format_df(res, current_saved), res, gr.update(visible=True)
75
+ except Exception as e: raise gr.Error(f"搜尋失敗: {e}")
 
76
 
77
+ def prof_load_more(query, cur_res, cur_saved):
78
+ if not query: return gr.update(), cur_res
 
 
79
  try:
80
+ new_res = gemini_service.search_professors(query, exclude_names=[p['name'] for p in cur_res])
81
+ exist_keys = set(prof_get_key(p) for p in cur_res)
82
+ for p in new_res:
83
+ if prof_get_key(p) not in exist_keys: cur_res.append(p)
84
+ return prof_format_df(cur_res, cur_saved), cur_res
85
+ except Exception as e: raise gr.Error(f"載入失敗: {e}")
86
+
87
+ def prof_select(evt: gr.SelectData, search_res, saved_data, view_mode):
88
+ if not evt: return [gr.update()]*8
89
+ idx = evt.index[0]
90
+ target = saved_data if view_mode == "追蹤清單" else search_res
91
+ if not target or idx >= len(target): return [gr.update()]*8
92
+
93
+ p = target[idx]
94
+ key = prof_get_key(p)
95
+ saved_p = next((x for x in saved_data if prof_get_key(x) == key), None)
96
+ curr = saved_p if saved_p else p
97
+
98
+ md = ""
99
+ if curr.get('details') and len(curr.get('details')) > 10:
100
+ md = curr['details']
101
+ if not saved_p: saved_data.insert(0, curr); save_data(saved_data, PROF_SAVE_FILE)
 
 
 
 
 
 
 
 
 
 
 
102
  else:
103
+ gr.Info(f"正在調查 {curr['name']}...")
 
104
  try:
105
+ res = gemini_service.get_professor_details(curr)
106
+ curr['details'] = res['text']; curr['sources'] = res['sources']
107
+ md = res['text']
108
+ if saved_p: saved_p.update(curr)
109
+ else: saved_data.insert(0, curr)
110
+ save_data(saved_data, PROF_SAVE_FILE)
111
+ except Exception as e: raise gr.Error(f"調查失敗: {e}")
112
+
113
+ if curr.get('sources'): md += "\n\n### 📚 參考來源\n" + "\n".join([f"- [{s['title']}]({s['uri']})" for s in curr['sources']])
114
+ return gr.update(visible=True), md, [], curr, saved_data, get_tags_text(curr), gr.update(choices=get_tags_choices(curr), value=None), gr.update(visible=True)
115
+
116
+ def prof_chat(hist, msg, curr):
117
+ if not curr: return hist, ""
118
+ try:
119
+ reply = gemini_service.chat_with_ai(
120
+ [{"role":"user","content":h[0]} for h in hist if h[0]] + ([{"role":"model","content":h[1]} for h in hist if h[1]]),
121
+ msg, curr.get('details', ''), "你是學術顧問,請根據這份教授資料回答"
122
+ )
123
+ hist.append((msg, reply))
124
+ except Exception as e: hist.append((msg, f"Error: {e}"))
125
+ return hist, ""
126
+
127
+ def prof_add_tag(tag, curr, saved, mode, res):
128
+ if not curr or not tag: return gr.update(), gr.update(), gr.update(), saved, gr.update()
129
+ if 'tags' not in curr: curr['tags'] = []
130
+ if tag not in curr['tags']:
131
+ curr['tags'].append(tag)
132
+ key = prof_get_key(curr)
 
 
 
 
 
 
 
 
 
 
 
133
  found = False
134
+ for i, p in enumerate(saved):
135
+ if prof_get_key(p) == key: saved[i] = curr; found=True; break
136
+ if not found: saved.insert(0, curr)
137
+ save_data(saved, PROF_SAVE_FILE)
138
+ return gr.update(value=""), get_tags_text(curr), gr.update(choices=curr['tags']), saved, prof_format_df(saved if mode=="追蹤清單" else res, saved)
139
+
140
+ def prof_remove_tag(tag, curr, saved, mode, res):
141
+ if not curr or not tag: return gr.update(), gr.update(), saved, gr.update()
142
+ if 'tags' in curr and tag in curr['tags']:
143
+ curr['tags'].remove(tag)
144
+ key = prof_get_key(curr)
145
+ for i, p in enumerate(saved):
146
+ if prof_get_key(p) == key: saved[i] = curr; break
147
+ save_data(saved, PROF_SAVE_FILE)
148
+ return get_tags_text(curr), gr.update(choices=curr['tags'], value=None), saved, prof_format_df(saved if mode=="追蹤清單" else res, saved)
149
+
150
+ def prof_update_status(stat, curr, saved, mode, res):
151
+ if not curr: return gr.update(), saved
152
+ curr['status'] = stat if curr.get('status') != stat else None
153
+ key = prof_get_key(curr)
154
+ for i, p in enumerate(saved):
155
+ if prof_get_key(p) == key: saved[i] = curr; break
156
+ save_data(saved, PROF_SAVE_FILE)
157
+ return prof_format_df(saved if mode=="追蹤清單" else res, saved), saved
158
+
159
+ def prof_remove(curr, saved, mode, res):
160
+ if not curr: return gr.update(), gr.update(value=None), saved, gr.update(visible=False)
161
+ key = prof_get_key(curr)
162
+ new_saved = [p for p in saved if prof_get_key(p) != key]
163
+ save_data(new_saved, PROF_SAVE_FILE)
164
+ return gr.Info("已移除"), prof_format_df(new_saved if mode=="追蹤清單" else res, new_saved), new_saved, gr.update(visible=False)
165
+
166
+ def prof_toggle(mode, res, saved):
167
+ return prof_format_df(res if mode=="搜尋結果" else saved, saved), gr.update(visible=mode=="搜尋結果")
168
+
169
+ # --- 🏢 Company Logic ---
170
+
171
+ def comp_get_key(c): return f"{c['name']}"
172
+
173
+ def comp_format_df(source_list, saved_list):
174
+ if not source_list: return pd.DataFrame(columns=["狀態", "公司名稱", "產業類別", "標籤"])
175
+ if saved_list is None: saved_list = []
176
+ saved_map = {comp_get_key(c): c for c in saved_list}
177
+ data = []
178
+ for c in source_list:
179
+ dc = saved_map.get(comp_get_key(c), c)
180
+ icon = {'good':'✅','risk':'⚠️','pending':'❓'}.get(dc.get('status'), '')
181
+ detail = "📄" if dc.get('details') else ""
182
+ data.append([f"{icon} {detail}", dc['name'], dc.get('industry','未知'), ", ".join(dc.get('tags', []))])
183
+ return pd.DataFrame(data, columns=["狀態", "公司名稱", "產業類別", "標籤"])
184
+
185
+ def comp_search(query, current_saved):
186
+ if not query: return gr.update(), current_saved, gr.update()
 
 
 
 
187
  try:
188
+ res = gemini_service.search_companies(query)
189
+ return comp_format_df(res, current_saved), res, gr.update(visible=True)
190
+ except Exception as e: raise gr.Error(f"搜尋失敗: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
 
192
+ def comp_load_more(query, cur_res, cur_saved):
193
+ if not query: return gr.update(), cur_res
194
+ try:
195
+ new_res = gemini_service.search_companies(query, exclude_names=[c['name'] for c in cur_res])
196
+ exist_keys = set(comp_get_key(c) for c in cur_res)
197
+ for c in new_res:
198
+ if comp_get_key(c) not in exist_keys: cur_res.append(c)
199
+ return comp_format_df(cur_res, cur_saved), cur_res
200
+ except Exception as e: raise gr.Error(f"載入失敗: {e}")
201
+
202
+ def comp_select(evt: gr.SelectData, search_res, saved_data, view_mode):
203
+ if not evt: return [gr.update()]*8
204
+ idx = evt.index[0]
205
+ target = saved_data if view_mode == "追蹤清單" else search_res
206
+ if not target or idx >= len(target): return [gr.update()]*8
207
+
208
+ c = target[idx]
209
+ key = comp_get_key(c)
210
+ saved_c = next((x for x in saved_data if comp_get_key(x) == key), None)
211
+ curr = saved_c if saved_c else c
212
+
213
+ md = ""
214
+ if curr.get('details') and len(curr.get('details')) > 10:
215
+ md = curr['details']
216
+ if not saved_c: saved_data.insert(0, curr); save_data(saved_data, COMP_SAVE_FILE)
217
  else:
218
+ gr.Info(f"正在調查 {curr['name']}...")
219
+ try:
220
+ res = gemini_service.get_company_details(curr)
221
+ curr['details'] = res['text']; curr['sources'] = res['sources']
222
+ md = res['text']
223
+ if saved_c: saved_c.update(curr)
224
+ else: saved_data.insert(0, curr)
225
+ save_data(saved_data, COMP_SAVE_FILE)
226
+ except Exception as e: raise gr.Error(f"調查失敗: {e}")
227
+
228
+ if curr.get('sources'): md += "\n\n### 📚 資料來源\n" + "\n".join([f"- [{s['title']}]({s['uri']})" for s in curr['sources']])
229
+ return gr.update(visible=True), md, [], curr, saved_data, get_tags_text(curr), gr.update(choices=get_tags_choices(curr), value=None), gr.update(visible=True)
230
+
231
+ def comp_chat(hist, msg, curr):
232
+ if not curr: return hist, ""
233
+ try:
234
+ reply = gemini_service.chat_with_ai(
235
+ [{"role":"user","content":h[0]} for h in hist if h[0]] + ([{"role":"model","content":h[1]} for h in hist if h[1]]),
236
+ msg, curr.get('details', ''), "你是商業顧問,請根據這份公司調查報告回答"
237
+ )
238
+ hist.append((msg, reply))
239
+ except Exception as e: hist.append((msg, f"Error: {e}"))
240
+ return hist, ""
241
+
242
+ def comp_add_tag(tag, curr, saved, mode, res):
243
+ if not curr or not tag: return gr.update(), gr.update(), gr.update(), saved, gr.update()
244
+ if 'tags' not in curr: curr['tags'] = []
245
+ if tag not in curr['tags']:
246
+ curr['tags'].append(tag)
247
+ key = comp_get_key(curr)
248
+ found = False
249
+ for i, c in enumerate(saved):
250
+ if comp_get_key(c) == key: saved[i] = curr; found=True; break
251
+ if not found: saved.insert(0, curr)
252
+ save_data(saved, COMP_SAVE_FILE)
253
+ return gr.update(value=""), get_tags_text(curr), gr.update(choices=curr['tags']), saved, comp_format_df(saved if mode=="追蹤清單" else res, saved)
254
+
255
+ def comp_remove_tag(tag, curr, saved, mode, res):
256
+ if not curr or not tag: return gr.update(), gr.update(), saved, gr.update()
257
+ if 'tags' in curr and tag in curr['tags']:
258
+ curr['tags'].remove(tag)
259
+ key = comp_get_key(curr)
260
+ for i, c in enumerate(saved):
261
+ if comp_get_key(c) == key: saved[i] = curr; break
262
+ save_data(saved, COMP_SAVE_FILE)
263
+ return get_tags_text(curr), gr.update(choices=curr['tags'], value=None), saved, comp_format_df(saved if mode=="追蹤清單" else res, saved)
264
+
265
+ def comp_update_status(stat, curr, saved, mode, res):
266
+ if not curr: return gr.update(), saved
267
+ curr['status'] = stat if curr.get('status') != stat else None
268
+ key = comp_get_key(curr)
269
+ for i, c in enumerate(saved):
270
+ if comp_get_key(c) == key: saved[i] = curr; break
271
+ save_data(saved, COMP_SAVE_FILE)
272
+ return comp_format_df(saved if mode=="追蹤清單" else res, saved), saved
273
+
274
+ def comp_remove(curr, saved, mode, res):
275
+ if not curr: return gr.update(), gr.update(value=None), saved, gr.update(visible=False)
276
+ key = comp_get_key(curr)
277
+ new_saved = [c for c in saved if comp_get_key(c) != key]
278
+ save_data(new_saved, COMP_SAVE_FILE)
279
+ return gr.Info("已移除"), comp_format_df(new_saved if mode=="追蹤清單" else res, new_saved), new_saved, gr.update(visible=False)
280
+
281
+ def comp_toggle(mode, res, saved):
282
+ return comp_format_df(res if mode=="搜尋結果" else saved, saved), gr.update(visible=mode=="搜尋結果")
283
+
284
+ # --- Initialize ---
285
+ def prof_init(): d = load_data(PROF_SAVE_FILE); return d, prof_format_df(d, d)
286
+ def comp_init(): d = load_data(COMP_SAVE_FILE); return d, comp_format_df(d, d)
287
 
288
  # --- UI Layout ---
289
 
290
+ with gr.Blocks(title="404 產學導航系統", theme=gr.themes.Soft()) as demo:
291
 
 
 
 
 
292
  gr.Markdown("""
293
  <div align="center">
294
 
295
+ # 🚀 404 產學導航 (Prof.404 + Com.404)
296
 
297
+ [![Hugging Face](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue)](https://huggingface.co/spaces/DeepLearning101/Prof.404)
298
  [![GitHub](https://img.shields.io/badge/GitHub-Repo-black)](https://github.com/Deep-Learning-101/prof-404)
299
  [![Powered by](https://img.shields.io/badge/Powered%20by-Gemini%202.0%20Flash-4285F4?logo=google)](https://deepmind.google/technologies/gemini/)
300
 
301
+ **產學雙棲、研究導航、商業徵信,你的全方位 AI 顧問**
302
+ <span style="font-size: 0.8em; color: gray;">(支援雲端同步 | 教授與公司資料分開儲存)</span>
 
 
 
 
 
 
 
 
 
 
303
  </div>
304
  """)
305
+
306
+ with gr.Tabs():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
307
 
308
+ # ==========================
309
+ # Tab 1: 🎓 教授去哪兒?
310
+ # ==========================
311
+ with gr.Tab("🎓 找教授 (Prof.404)"):
312
+ prof_saved = gr.State([])
313
+ prof_res = gr.State([])
314
+ prof_sel = gr.State(None)
315
 
316
+ with gr.Row():
317
+ p_in = gr.Textbox(label="搜尋教授", placeholder="輸入研究領域 (如: LLM)...", scale=4)
318
+ p_btn = gr.Button("🔍 搜尋", variant="primary", scale=1)
319
+
320
+ p_view = gr.Radio(["搜尋結果", "追蹤清單"], label="顯示模式", value="追蹤清單")
321
+
322
+ with gr.Row():
323
+ with gr.Column(scale=1):
324
+ p_df = gr.Dataframe(headers=["狀態","姓名","大學","系所","標籤"], datatype=["str","str","str","str","str"], interactive=False)
325
+ p_load = gr.Button("載入更多", visible=False)
326
+
327
+ with gr.Column(scale=2, visible=False) as p_col:
328
+ p_md = gr.Markdown("...")
329
+ with gr.Column():
330
+ gr.Markdown("### 🤖 學術顧問")
331
+ p_chat = gr.Chatbot(height=250)
332
+ with gr.Row():
333
+ p_msg = gr.Textbox(label="提問", scale=4)
334
+ p_send = gr.Button("送出", scale=1)
335
+ gr.Markdown("---")
336
+ with gr.Column(visible=False) as p_tag_row:
337
+ p_tag_disp = gr.Markdown("標籤: (無)")
338
+ with gr.Row():
339
+ p_tag_in = gr.Textbox(label="新增標籤", scale=3)
340
+ p_tag_add = gr.Button("➕", scale=1)
341
+ with gr.Accordion("刪除標籤", open=False):
342
+ with gr.Row():
343
+ p_tag_drop = gr.Dropdown(label="選擇標籤", choices=[], scale=3)
344
+ p_tag_del = gr.Button("🗑️", scale=1, variant="secondary")
345
  with gr.Row():
346
+ p_good = gr.Button("✅ 符合")
347
+ p_bad = gr.Button(" 不符")
348
+ p_pend = gr.Button("❓ 待觀察")
349
+ p_rem = gr.Button("🗑️ 移除", variant="stop")
350
+
351
+ # Wiring Prof
352
+ demo.load(prof_init, None, [prof_saved, p_df])
353
+ p_btn.click(prof_search, [p_in, prof_saved], [p_df, prof_res, p_load]).then(lambda: gr.update(value="搜尋結果"), outputs=[p_view])
354
+ p_load.click(prof_load_more, [p_in, prof_res, prof_saved], [p_df, prof_res])
355
+ p_view.change(prof_toggle, [p_view, prof_res, prof_saved], [p_df, p_load])
356
+ p_df.select(prof_select, [prof_res, prof_saved, p_view], [p_col, p_md, p_chat, prof_sel, prof_saved, p_tag_disp, p_tag_drop, p_tag_row])
357
+ p_send.click(prof_chat, [p_chat, p_msg, prof_sel], [p_chat, p_msg]); p_msg.submit(prof_chat, [p_chat, p_msg, prof_sel], [p_chat, p_msg])
358
+ p_tag_add.click(prof_add_tag, [p_tag_in, prof_sel, prof_saved, p_view, prof_res], [p_tag_in, p_tag_disp, p_tag_drop, prof_saved, p_df])
359
+ p_tag_del.click(prof_remove_tag, [p_tag_drop, prof_sel, prof_saved, p_view, prof_res], [p_tag_disp, p_tag_drop, prof_saved, p_df])
360
+ for btn, s in [(p_good,'match'),(p_bad,'mismatch'),(p_pend,'pending')]: btn.click(prof_update_status, [gr.State(s), prof_sel, prof_saved, p_view, prof_res], [p_df, prof_saved])
361
+ p_rem.click(prof_remove, [prof_sel, prof_saved, p_view, prof_res], [gr.State(None), p_df, prof_saved, p_col])
362
+
363
+ # ==========================
364
+ # Tab 2: 🏢 公司去那兒?
365
+ # ==========================
366
+ with gr.Tab("🏢 找公司 (Com.404)"):
367
+ comp_saved = gr.State([])
368
+ comp_res = gr.State([])
369
+ comp_sel = gr.State(None)
370
 
371
  with gr.Row():
372
+ c_in = gr.Textbox(label="搜尋公司/領域", placeholder="輸入產業 (如: 量子計算) 或公司名稱...", scale=4)
373
+ c_btn = gr.Button("🔍 搜尋", variant="primary", scale=1)
374
+
375
+ c_view = gr.Radio(["搜尋結果", "追蹤清單"], label="顯示模式", value="追蹤清單")
376
+
377
+ with gr.Row():
378
+ with gr.Column(scale=1):
379
+ c_df = gr.Dataframe(headers=["狀態","公司名稱","產業類別","標籤"], datatype=["str","str","str","str"], interactive=False)
380
+ c_load = gr.Button("載入更多", visible=False)
381
+
382
+ with gr.Column(scale=2, visible=False) as c_col:
383
+ c_md = gr.Markdown("...")
384
+ with gr.Column():
385
+ gr.Markdown("### 🤖 商業顧問")
386
+ c_chat = gr.Chatbot(height=250)
387
+ with gr.Row():
388
+ c_msg = gr.Textbox(label="提問", scale=4)
389
+ c_send = gr.Button("送出", scale=1)
390
+ gr.Markdown("---")
391
+ with gr.Column(visible=False) as c_tag_row:
392
+ c_tag_disp = gr.Markdown("標籤: (無)")
393
+ with gr.Row():
394
+ c_tag_in = gr.Textbox(label="新增標籤", scale=3)
395
+ c_tag_add = gr.Button("➕", scale=1)
396
+ with gr.Accordion("刪除標籤", open=False):
397
+ with gr.Row():
398
+ c_tag_drop = gr.Dropdown(label="選擇標籤", choices=[], scale=3)
399
+ c_tag_del = gr.Button("🗑️", scale=1, variant="secondary")
400
+ with gr.Row():
401
+ c_good = gr.Button("✅ 優質")
402
+ c_risk = gr.Button("⚠️ 風險")
403
+ c_pend = gr.Button("❓ 未定")
404
+ c_rem = gr.Button("🗑️ 移除", variant="stop")
405
+
406
+ # Wiring Comp
407
+ demo.load(comp_init, None, [comp_saved, c_df])
408
+ c_btn.click(comp_search, [c_in, comp_saved], [c_df, comp_res, c_load]).then(lambda: gr.update(value="搜尋結果"), outputs=[c_view])
409
+ c_load.click(comp_load_more, [c_in, comp_res, comp_saved], [c_df, comp_res])
410
+ c_view.change(comp_toggle, [c_view, comp_res, comp_saved], [c_df, c_load])
411
+ c_df.select(comp_select, [comp_res, comp_saved, c_view], [c_col, c_md, c_chat, comp_sel, comp_saved, c_tag_disp, c_tag_drop, c_tag_row])
412
+ c_send.click(comp_chat, [c_chat, c_msg, comp_sel], [c_chat, c_msg]); c_msg.submit(comp_chat, [c_chat, c_msg, comp_sel], [c_chat, c_msg])
413
+ c_tag_add.click(comp_add_tag, [c_tag_in, comp_sel, comp_saved, c_view, comp_res], [c_tag_in, c_tag_disp, c_tag_drop, comp_saved, c_df])
414
+ c_tag_del.click(comp_remove_tag, [c_tag_drop, comp_sel, comp_saved, c_view, comp_res], [c_tag_disp, c_tag_drop, comp_saved, c_df])
415
+ for btn, s in [(c_good,'good'),(c_risk,'risk'),(c_pend,'pending')]: btn.click(comp_update_status, [gr.State(s), comp_sel, comp_saved, c_view, comp_res], [c_df, comp_saved])
416
+ c_rem.click(comp_remove, [comp_sel, comp_saved, c_view, comp_res], [gr.State(None), c_df, comp_saved, c_col])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417
 
418
  if __name__ == "__main__":
419
  demo.launch()