Spaces:
Sleeping
Sleeping
fix normalizing windows
Browse files249 unmodified lines25025125225325425523 unmodified lines27928028128228328428528628728828936 unmodified lines326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360249 unmodified linesexisting = load_dataset(hf_repo_id, token=hf_token)all_splits = list(existing.keys())print(f"๐ ๊ธฐ์กด splits: {all_splits}")except Exception:existing = DatasetDict()print("๐ ๊ธฐ์กด repo ์์ โ ์๋ก ์์ฑ")23 unmodified linesif user_id in existing:# ๊ธฐ์กด ๋ฐ์ดํฐ ์ ๊ทํ ๋ฐ ๋ณํฉold_df = existing[user_id].to_pandas()old_df["windows"] = old_df["windows"].apply(lambda w: [str(v) for v in w] if isinstance(w, list) else [])merged_df = pd.concat([old_df, new_df], ignore_index=True)existing[user_id] = Dataset.from_pandas(merged_df)print(f"๐ {user_id}: ๊ธฐ์กด ๋ฐ์ดํฐ์ ๋ณํฉ ({len(old_df)} + {len(new_df)} = {len(merged_df)}๊ฐ ๋ ์ฝ๋)")else:existing[user_id] = new_dataset36 unmodified linesraise HTTPException(status_code=500, detail=f"๋ฐฐ์น ํธ์ ์คํจ: {str(e)}")def normalize_windows(record):w = record.get("windows")result = []if isinstance(w, list):for item in w:if isinstance(item, dict):result.extend([str(v) for v in item.values() if v is not None])elif item is not None:result.append(str(item))elif isinstance(w, dict):result = [str(v) for v in w.values() if v is not None]record["windows"] = [x for x in result if x not in ("", "None")]return recorddef df_to_dataset(df):import pyarrow as paschema = pa.schema([("session_id", pa.string()),("measure_date", pa.string()),("rms", pa.float64()),("freq", pa.float64()),("fatigue", pa.float64()),("mode", pa.string()),("window_count", pa.int64()),("windows", pa.list_(pa.string())),("measurement_count", pa.int64()),("batch_date", pa.string()),("batch_size", pa.int64()),("timestamp", pa.string())])return Dataset.from_pandas(df, schema=schema)249 unmodified lines25025125225325425525625725825926026126226326426526626726826923 unmodified lines29329429529629729829930030130230330436 unmodified lines341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376249 unmodified linesexisting = load_dataset(hf_repo_id, token=hf_token)all_splits = list(existing.keys())print(f"๐ ๊ธฐ์กด splits: {all_splits}")# ๊ธฐ์กด ๋ฐ์ดํฐ๋ฅผ ์์ ํ ์๋ก ์์ฑ (์คํค๋ง ํต์ผ)new_existing = DatasetDict()for user_id in existing.keys():df = existing[user_id].to_pandas()# windows ํ๋๋ฅผ ๋ฌธ์์ด ๋ฆฌ์คํธ๋ก ๊ฐ์ ๋ณํdf["windows"] = df["windows"].apply(lambda w: [str(v) for v in w] if isinstance(w, list) and len(w) > 0 else [])# ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์๋ก ์์ฑํ์ฌ ์คํค๋ง ํต์ผnew_existing[user_id] = df_to_dataset(df)print(f"๐ง {user_id}: ๊ธฐ์กด ๋ฐ์ดํฐ ์ฌ์์ฑ ์๋ฃ")existing = new_existingexcept Exception:existing = DatasetDict()print("๐ ๊ธฐ์กด repo ์์ โ ์๋ก ์์ฑ")23 unmodified linesif user_id in existing:# ๊ธฐ์กด ๋ฐ์ดํฐ ์ ๊ทํ ๋ฐ ๋ณํฉold_df = existing[user_id].to_pandas()# ๊ธฐ์กด windows ๋ฐ์ดํฐ๋ฅผ ๋ฌธ์์ด ๋ฆฌ์คํธ๋ก ์ ๊ทํold_df["windows"] = old_df["windows"].apply(lambda w: [str(v) for v in w] if isinstance(w, list) and len(w) > 0 else [])merged_df = pd.concat([old_df, new_df], ignore_index=True)existing[user_id] = df_to_dataset(merged_df)print(f"๐ {user_id}: ๊ธฐ์กด ๋ฐ์ดํฐ์ ๋ณํฉ ({len(old_df)} + {len(new_df)} = {len(merged_df)}๊ฐ ๋ ์ฝ๋)")else:existing[user_id] = new_dataset36 unmodified linesraise HTTPException(status_code=500, detail=f"๋ฐฐ์น ํธ์ ์คํจ: {str(e)}")def normalize_windows(record):w = record.get("windows", [])result = []if isinstance(w, list):for item in w:if isinstance(item, dict):# ๋์ ๋๋ฆฌ์ ๋ชจ๋ ๊ฐ๋ค์ ๋ฌธ์์ด๋ก ๋ณํfor v in item.values():if v is not None and str(v).strip():result.append(str(v))elif item is not None and str(item).strip():result.append(str(item))elif isinstance(w, dict):# ๋์ ๋๋ฆฌ์ ๋ชจ๋ ๊ฐ๋ค์ ๋ฌธ์์ด๋ก ๋ณํfor v in w.values():if v is not None and str(v).strip():result.append(str(v))else:# windows๊ฐ ์๊ฑฐ๋ ๋ค๋ฅธ ํ์ ์ธ ๊ฒฝ์ฐ ๋น ๋ฆฌ์คํธresult = []record["windows"] = resultprint(f"๐ Windows ์ ๊ทํ: {w} โ {result}")return recorddef df_to_dataset(df):"""DataFrame์ Dataset์ผ๋ก ๋ณํ (windows ํ๋ ์ ๊ทํ)"""# windows ํ๋๊ฐ ๋ฆฌ์คํธ์ธ์ง ํ์ธํ๊ณ ์ ๊ทํif 'windows' in df.columns:df['windows'] = df['windows'].apply(lambda x: x if isinstance(x, list) else [])return Dataset.from_pandas(df)
app.py+40-24