Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,8 @@ from huggingface_hub import HfApi, hf_hub_download
|
|
| 8 |
|
| 9 |
# Load Env
|
| 10 |
load_dotenv()
|
| 11 |
-
|
|
|
|
| 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
|
| 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 |
-
|
| 33 |
-
|
| 34 |
-
|
| 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(
|
| 48 |
-
|
| 49 |
-
except:
|
| 50 |
-
data = []
|
| 51 |
return data
|
| 52 |
|
| 53 |
-
def save_data(data):
|
| 54 |
-
# 1. 存本地
|
| 55 |
try:
|
| 56 |
-
with open(
|
| 57 |
-
|
| 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 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 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
|
| 104 |
-
|
| 105 |
-
return "目前標籤: (無)"
|
| 106 |
-
return "🏷️ " + ", ".join([f"`{t}`" for t in comp['tags']])
|
| 107 |
|
| 108 |
-
|
| 109 |
-
if not comp: return []
|
| 110 |
-
return comp.get('tags', [])
|
| 111 |
|
| 112 |
-
|
| 113 |
|
| 114 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
if not query: return gr.update(), current_saved, gr.update()
|
| 116 |
-
|
| 117 |
try:
|
| 118 |
-
|
| 119 |
-
return
|
| 120 |
-
except Exception as e:
|
| 121 |
-
raise gr.Error(f"搜尋失敗: {e}")
|
| 122 |
|
| 123 |
-
def
|
| 124 |
-
if not query: return gr.update(),
|
| 125 |
-
|
| 126 |
-
current_names = [c['name'] for c in current_results]
|
| 127 |
try:
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 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 |
-
|
| 163 |
-
gr.Info(f"正在調查 {current_comp['name']} (查詢統編、PTT評價)...")
|
| 164 |
try:
|
| 165 |
-
res = gemini_service.
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 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,
|
| 206 |
-
if
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
return (
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
)
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
if
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
if
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
return (
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 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 |
-
|
| 264 |
-
|
| 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
|
| 285 |
-
if not
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
)
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
else:
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
# --- UI Layout ---
|
| 311 |
|
| 312 |
-
with gr.Blocks(title="
|
| 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 |
-
#
|
| 322 |
|
| 323 |
-
[](https://github.com/Deep-Learning-101/prof-404)
|
| 325 |
[](https://deepmind.google/technologies/gemini/)
|
| 326 |
|
| 327 |
-
|
| 328 |
-
<span style="font-size: 0.
|
| 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.
|
| 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 |
-
#
|
| 361 |
-
|
| 362 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
gr.
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
with gr.Row():
|
| 383 |
-
|
| 384 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
|
| 386 |
with gr.Row():
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 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 |
+
[](https://huggingface.co/spaces/DeepLearning101/Prof.404)
|
| 298 |
[](https://github.com/Deep-Learning-101/prof-404)
|
| 299 |
[](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()
|