raxhel commited on
Commit
ac3ca2a
·
verified ·
1 Parent(s): 3872cb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -23
app.py CHANGED
@@ -296,36 +296,28 @@ async def analyze_with_som(embedding, predicted_family, filename):
296
  try:
297
  print("\n🗺️ Performing SOM analysis (New Logic)...")
298
 
299
- # 1. 先去抓資料
300
- hf_embeddings = await get_embeddings_from_hf_dataset(filename)
301
-
302
- embedding_to_use = None
303
- data_source = ""
304
-
305
- if hf_embeddings and len(hf_embeddings) > 0:
306
- # 2. 如果找到了 -> 計算平均 embedding
307
- print(f"✅ Found {len(hf_embeddings)} embeddings from HF dataset. Calculating average.")
308
- embedding_to_use = np.mean(hf_embeddings, axis=0, dtype=np.float32)
309
- data_source = "hf_dataset_avg"
310
  else:
311
- # 3. Fallback: 使用模型即時產生的 embedding
312
- print(f"⚠️ No HF data found, falling back to new embedding.")
313
- if isinstance(embedding, list):
314
- embedding_to_use = np.array(embedding, dtype=np.float32)
315
- else:
316
- embedding_to_use = embedding
317
- data_source = "realtime_model" # (之前這裡是 background_data,現在更正)
318
-
319
  # 4. 根據選定的 embedding 決定座標
320
  winner_row, winner_col = som_analyzer.find_winner_neuron(embedding_to_use)
321
- print(f" 📍 Winner neuron: ({winner_row}, {winner_col}) (Source: {data_source})")
 
 
 
 
322
 
323
  # 5. 分析座標周圍 (使用已抓取的 hf_embeddings 或 fallback)
324
  if hf_embeddings and len(hf_embeddings) > 0:
325
- # (使用原有的鄰域分析邏輯)
326
  features_prob = analyze_som_region_probability(
327
  som_analyzer, winner_row, winner_col, hf_embeddings, radius=2
328
  )
 
329
  else:
330
  # (使用原有的 fallback 邏輯)
331
  print(f"⚠️ No HF data for prob, using background data")
@@ -333,8 +325,7 @@ async def analyze_with_som(embedding, predicted_family, filename):
333
  winner_row, winner_col, radius=1
334
  )
335
  features_prob = {k: float(v) for k, v in features_dict.items()}
336
- # 確保 data_source 反映這一點
337
- data_source = "background_data_prob" if data_source == "realtime_model" else data_source
338
 
339
  # 6. 組合最終結果 (此部分不變)
340
  features_binary = {
@@ -357,6 +348,12 @@ async def analyze_with_som(embedding, predicted_family, filename):
357
  import traceback
358
  traceback.print_exc()
359
  return None
 
 
 
 
 
 
360
 
361
 
362
  @app.post("/predict")
 
296
  try:
297
  print("\n🗺️ Performing SOM analysis (New Logic)...")
298
 
299
+ # 1. 決定用於 "定位" 的 Embedding
300
+ # (我們假設傳入的 'embedding' 已經是最好的了,例如高注意力的那一個)
301
+ if isinstance(embedding, list):
302
+ embedding_to_use = np.array(embedding, dtype=np.float32)
 
 
 
 
 
 
 
303
  else:
304
+ embedding_to_use = embedding
305
+
 
 
 
 
 
 
306
  # 4. 根據選定的 embedding 決定座標
307
  winner_row, winner_col = som_analyzer.find_winner_neuron(embedding_to_use)
308
+ print(f" 📍 Winner neuron: ({winner_row}, {winner_col}) (Source: high-attention segment)")
309
+
310
+ # 1. (原步驟1) 現在去抓取 "所有" 相關的 embeddings,用於 "鄰域分析"
311
+ hf_embeddings = await get_embeddings_from_hf_dataset(filename)
312
+ data_source = "high-attention (pos) + " # 標記數據源
313
 
314
  # 5. 分析座標周圍 (使用已抓取的 hf_embeddings 或 fallback)
315
  if hf_embeddings and len(hf_embeddings) > 0:
316
+ print(f" Analysing region using {len(hf_embeddings)} HF embeddings...")
317
  features_prob = analyze_som_region_probability(
318
  som_analyzer, winner_row, winner_col, hf_embeddings, radius=2
319
  )
320
+ data_source += "hf_dataset (prob)"
321
  else:
322
  # (使用原有的 fallback 邏輯)
323
  print(f"⚠️ No HF data for prob, using background data")
 
325
  winner_row, winner_col, radius=1
326
  )
327
  features_prob = {k: float(v) for k, v in features_dict.items()}
328
+ data_source += "background_data (prob)"
 
329
 
330
  # 6. 組合最終結果 (此部分不變)
331
  features_binary = {
 
348
  import traceback
349
  traceback.print_exc()
350
  return None
351
+
352
+ except Exception as e:
353
+ print(f"❌ SOM analysis error: {e}")
354
+ import traceback
355
+ traceback.print_exc()
356
+ return None
357
 
358
 
359
  @app.post("/predict")