raxhel commited on
Commit
adf277b
·
verified ·
1 Parent(s): 80a8480

update som

Browse files
Files changed (1) hide show
  1. app.py +42 -16
app.py CHANGED
@@ -269,36 +269,60 @@ def analyze_som_region_probability(
269
 
270
 
271
  async def analyze_with_som(embedding, predicted_family, filename):
272
- """ (from app.py) """
 
 
 
 
 
 
273
  if not SOM_AVAILABLE:
274
  print("⚠️ SOM analysis disabled")
275
  return None
276
  try:
277
- print("\n🗺️ Performing SOM analysis...")
278
- if isinstance(embedding, list):
279
- embedding_array = np.array(embedding, dtype=np.float32)
280
- else:
281
- embedding_array = embedding
282
-
283
- winner_row, winner_col = som_analyzer.find_winner_neuron(embedding_array)
284
- print(f" 📍 Winner neuron: ({winner_row}, {winner_col})")
285
 
 
286
  hf_embeddings = await get_embeddings_from_hf_dataset(filename)
287
 
 
 
 
288
  if hf_embeddings and len(hf_embeddings) > 0:
289
- print(f"✅ Using {len(hf_embeddings)} embeddings from HF dataset")
290
- features_prob = analyze_som_region_probability( #
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
291
  som_analyzer, winner_row, winner_col, hf_embeddings, radius=2
292
  )
293
- data_source = "hf_dataset"
294
  else:
295
- print(f"⚠️ No HF data found, using background data")
 
296
  features_dict = som_analyzer.predict_features_from_position(
297
  winner_row, winner_col, radius=1
298
  )
299
  features_prob = {k: float(v) for k, v in features_dict.items()}
300
- data_source = "background_data"
301
-
 
 
302
  features_binary = {
303
  feature: 1 if prob > 0.2 else 0
304
  for feature, prob in features_prob.items()
@@ -306,7 +330,6 @@ async def analyze_with_som(embedding, predicted_family, filename):
306
  print(f" ✅ Features detected: {features_binary}")
307
 
308
  result = {
309
- # 🐛 (from app.py) Bug Fix: col_winner -> winner_col
310
  "winner_position": {"row": int(winner_row), "col": int(winner_col)},
311
  "features": features_binary,
312
  "features_probability": features_prob,
@@ -314,8 +337,11 @@ async def analyze_with_som(embedding, predicted_family, filename):
314
  }
315
  print(f"✅ SOM analysis complete!")
316
  return result
 
317
  except Exception as e:
318
  print(f"❌ SOM analysis error: {e}")
 
 
319
  return None
320
 
321
 
 
269
 
270
 
271
  async def analyze_with_som(embedding, predicted_family, filename):
272
+ """
273
+ (from app.py)
274
+ ✨ 修改後的 SOM 分析邏輯:
275
+ 1. 優先從 HF Dataset 抓取 embeddings。
276
+ 2. 如果找到,用它們的「平均值」來決定座標。
277
+ 3. 如果沒找到,才用模型即時產生的 embedding。
278
+ """
279
  if not SOM_AVAILABLE:
280
  print("⚠️ SOM analysis disabled")
281
  return None
282
  try:
283
+ print("\n🗺️ Performing SOM analysis (New Logic)...")
 
 
 
 
 
 
 
284
 
285
+ # 1. 先去抓資料
286
  hf_embeddings = await get_embeddings_from_hf_dataset(filename)
287
 
288
+ embedding_to_use = None
289
+ data_source = ""
290
+
291
  if hf_embeddings and len(hf_embeddings) > 0:
292
+ # 2. 如果找到了 -> 計算平均 embedding
293
+ print(f"✅ Found {len(hf_embeddings)} embeddings from HF dataset. Calculating average.")
294
+ embedding_to_use = np.mean(hf_embeddings, axis=0, dtype=np.float32)
295
+ data_source = "hf_dataset_avg"
296
+ else:
297
+ # 3. Fallback: 使用模型即時產生的 embedding
298
+ print(f"⚠️ No HF data found, falling back to new embedding.")
299
+ if isinstance(embedding, list):
300
+ embedding_to_use = np.array(embedding, dtype=np.float32)
301
+ else:
302
+ embedding_to_use = embedding
303
+ data_source = "realtime_model" # (之前這裡是 background_data,現在更正)
304
+
305
+ # 4. 根據選定的 embedding 決定座標
306
+ winner_row, winner_col = som_analyzer.find_winner_neuron(embedding_to_use)
307
+ print(f" 📍 Winner neuron: ({winner_row}, {winner_col}) (Source: {data_source})")
308
+
309
+ # 5. 分析座標周圍 (使用已抓取的 hf_embeddings 或 fallback)
310
+ if hf_embeddings and len(hf_embeddings) > 0:
311
+ # (使用原有的鄰域分析邏輯)
312
+ features_prob = analyze_som_region_probability(
313
  som_analyzer, winner_row, winner_col, hf_embeddings, radius=2
314
  )
 
315
  else:
316
+ # (使用原有的 fallback 邏輯)
317
+ print(f"⚠️ No HF data for prob, using background data")
318
  features_dict = som_analyzer.predict_features_from_position(
319
  winner_row, winner_col, radius=1
320
  )
321
  features_prob = {k: float(v) for k, v in features_dict.items()}
322
+ # 確保 data_source 反映這一點
323
+ data_source = "background_data_prob" if data_source == "realtime_model" else data_source
324
+
325
+ # 6. 組合最終結果 (此部分不變)
326
  features_binary = {
327
  feature: 1 if prob > 0.2 else 0
328
  for feature, prob in features_prob.items()
 
330
  print(f" ✅ Features detected: {features_binary}")
331
 
332
  result = {
 
333
  "winner_position": {"row": int(winner_row), "col": int(winner_col)},
334
  "features": features_binary,
335
  "features_probability": features_prob,
 
337
  }
338
  print(f"✅ SOM analysis complete!")
339
  return result
340
+
341
  except Exception as e:
342
  print(f"❌ SOM analysis error: {e}")
343
+ import traceback
344
+ traceback.print_exc()
345
  return None
346
 
347