Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -141,6 +141,9 @@ def fetch_current_prices(item_ids: List[str], cities: List[str], qualities: str
|
|
| 141 |
if not all_data:
|
| 142 |
return pd.DataFrame()
|
| 143 |
df = pd.DataFrame(all_data)
|
|
|
|
|
|
|
|
|
|
| 144 |
for col in ["sell_price_min_date", "sell_price_max_date", "buy_price_min_date", "buy_price_max_date"]:
|
| 145 |
if col in df.columns:
|
| 146 |
df[col] = pd.to_datetime(df[col], errors="coerce")
|
|
@@ -176,14 +179,18 @@ def fetch_history(item_ids: List[str], cities: List[str], time_scale: int = 24,
|
|
| 176 |
return pd.DataFrame()
|
| 177 |
df = pd.DataFrame(all_records)
|
| 178 |
df["timestamp"] = pd.to_datetime(df["timestamp"], errors="coerce")
|
| 179 |
-
df["avg_price"] = pd.to_numeric(df["avg_price"], errors="coerce")
|
| 180 |
-
df["item_count"] = pd.to_numeric(df["item_count"], errors="coerce").fillna(0)
|
| 181 |
return df
|
| 182 |
|
| 183 |
|
| 184 |
def clean_market_data(df: pd.DataFrame) -> pd.DataFrame:
|
| 185 |
if df.empty:
|
| 186 |
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
df = df[df["avg_price"] > 0].copy()
|
| 188 |
if "item_id" in df.columns:
|
| 189 |
for item in df["item_id"].unique():
|
|
@@ -192,7 +199,8 @@ def clean_market_data(df: pd.DataFrame) -> pd.DataFrame:
|
|
| 192 |
continue
|
| 193 |
lower = df.loc[mask, "avg_price"].quantile(0.005)
|
| 194 |
upper = df.loc[mask, "avg_price"].quantile(0.995)
|
| 195 |
-
|
|
|
|
| 196 |
df = df.sort_values(["item_id", "city", "timestamp"]).reset_index(drop=True)
|
| 197 |
return df
|
| 198 |
|
|
@@ -224,6 +232,8 @@ def build_features(df: pd.DataFrame) -> pd.DataFrame:
|
|
| 224 |
return df
|
| 225 |
|
| 226 |
df = df.sort_values(["item_id", "city", "timestamp"]).copy()
|
|
|
|
|
|
|
| 227 |
|
| 228 |
df["hour"] = df["timestamp"].dt.hour
|
| 229 |
df["dayofweek"] = df["timestamp"].dt.dayofweek
|
|
@@ -293,8 +303,8 @@ def prepare_model_data(featured_df: pd.DataFrame) -> Tuple[pd.DataFrame, pd.Seri
|
|
| 293 |
upper = model_df["target_return_24h"].quantile(0.999)
|
| 294 |
model_df["target_return_24h"] = model_df["target_return_24h"].clip(lower, upper)
|
| 295 |
|
| 296 |
-
X = model_df[FEATURE_COLS]
|
| 297 |
-
y = model_df["target_return_24h"]
|
| 298 |
meta = model_df[["item_id", "city", "timestamp", "avg_price", "target_return_24h"]].copy()
|
| 299 |
|
| 300 |
return X, y, meta
|
|
@@ -348,7 +358,7 @@ def predict_24h_returns(model: lgb.LGBMRegressor, latest_features: pd.DataFrame)
|
|
| 348 |
print(f"Eksik özellikler: {missing}")
|
| 349 |
return pd.DataFrame()
|
| 350 |
|
| 351 |
-
X_pred = latest_features[FEATURE_COLS].dropna()
|
| 352 |
if X_pred.empty:
|
| 353 |
return pd.DataFrame()
|
| 354 |
|
|
@@ -512,8 +522,11 @@ class DataPipeline:
|
|
| 512 |
self.prices_df = prices
|
| 513 |
self.status_message = f"{len(self.history_df)} satır tarihsel veri, {len(self.prices_df)} anlık fiyat alındı."
|
| 514 |
|
| 515 |
-
|
| 516 |
-
|
|
|
|
|
|
|
|
|
|
| 517 |
|
| 518 |
self.featured_df = build_features(self.history_df)
|
| 519 |
self._backtest_cache = None
|
|
@@ -549,6 +562,8 @@ class DataPipeline:
|
|
| 549 |
|
| 550 |
if not preds.empty and not self.prices_df.empty:
|
| 551 |
price_info = self.prices_df[["item_id", "city", "sell_price_min", "buy_price_max"]].copy()
|
|
|
|
|
|
|
| 552 |
preds = preds.merge(price_info, on=["item_id", "city"], how="left")
|
| 553 |
preds["spread"] = preds["sell_price_min"] - preds["buy_price_max"]
|
| 554 |
else:
|
|
@@ -600,10 +615,10 @@ def get_analysis_table():
|
|
| 600 |
latest = pipeline.featured_df.sort_values("timestamp").groupby(["item_id", "city"]).tail(1).copy()
|
| 601 |
|
| 602 |
if not pipeline.prices_df.empty:
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
)
|
| 607 |
else:
|
| 608 |
latest["sell_price_min"] = np.nan
|
| 609 |
latest["buy_price_max"] = np.nan
|
|
@@ -658,6 +673,8 @@ def get_arbitrage_table():
|
|
| 658 |
return pd.DataFrame({"Bilgi": ["Anlık fiyat verisi yok."]})
|
| 659 |
|
| 660 |
prices = pipeline.prices_df.copy()
|
|
|
|
|
|
|
| 661 |
prices = prices[prices["sell_price_min"] > 0]
|
| 662 |
|
| 663 |
if prices.empty:
|
|
@@ -765,37 +782,89 @@ def list_csv_files() -> pd.DataFrame:
|
|
| 765 |
|
| 766 |
|
| 767 |
def refresh_data_ui():
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
pipeline.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 771 |
|
| 772 |
-
|
|
|
|
|
|
|
|
|
|
| 773 |
|
| 774 |
return (
|
| 775 |
pipeline.status_message,
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
bt_table,
|
| 782 |
bt_fig,
|
| 783 |
sim_table,
|
| 784 |
-
|
| 785 |
)
|
| 786 |
|
| 787 |
|
| 788 |
def initial_load():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 789 |
return (
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
get_arbitrage_table(),
|
| 794 |
-
get_model_performance_plot(),
|
| 795 |
pd.DataFrame(),
|
| 796 |
go.Figure().update_layout(title="Verileri yenilemek için butona tıklayın", template="plotly_dark"),
|
| 797 |
pd.DataFrame(),
|
| 798 |
-
|
|
|
|
|
|
|
| 799 |
)
|
| 800 |
|
| 801 |
|
|
|
|
| 141 |
if not all_data:
|
| 142 |
return pd.DataFrame()
|
| 143 |
df = pd.DataFrame(all_data)
|
| 144 |
+
for col in ["sell_price_min", "sell_price_max", "buy_price_min", "buy_price_max"]:
|
| 145 |
+
if col in df.columns:
|
| 146 |
+
df[col] = pd.to_numeric(df[col], errors="coerce").astype("float64")
|
| 147 |
for col in ["sell_price_min_date", "sell_price_max_date", "buy_price_min_date", "buy_price_max_date"]:
|
| 148 |
if col in df.columns:
|
| 149 |
df[col] = pd.to_datetime(df[col], errors="coerce")
|
|
|
|
| 179 |
return pd.DataFrame()
|
| 180 |
df = pd.DataFrame(all_records)
|
| 181 |
df["timestamp"] = pd.to_datetime(df["timestamp"], errors="coerce")
|
| 182 |
+
df["avg_price"] = pd.to_numeric(df["avg_price"], errors="coerce").astype("float64")
|
| 183 |
+
df["item_count"] = pd.to_numeric(df["item_count"], errors="coerce").fillna(0).astype("float64")
|
| 184 |
return df
|
| 185 |
|
| 186 |
|
| 187 |
def clean_market_data(df: pd.DataFrame) -> pd.DataFrame:
|
| 188 |
if df.empty:
|
| 189 |
return df
|
| 190 |
+
df = df.copy()
|
| 191 |
+
df["avg_price"] = pd.to_numeric(df["avg_price"], errors="coerce").astype("float64")
|
| 192 |
+
if "item_count" in df.columns:
|
| 193 |
+
df["item_count"] = pd.to_numeric(df["item_count"], errors="coerce").fillna(0).astype("float64")
|
| 194 |
df = df[df["avg_price"] > 0].copy()
|
| 195 |
if "item_id" in df.columns:
|
| 196 |
for item in df["item_id"].unique():
|
|
|
|
| 199 |
continue
|
| 200 |
lower = df.loc[mask, "avg_price"].quantile(0.005)
|
| 201 |
upper = df.loc[mask, "avg_price"].quantile(0.995)
|
| 202 |
+
if pd.notna(lower) and pd.notna(upper) and upper > lower:
|
| 203 |
+
df.loc[mask, "avg_price"] = df.loc[mask, "avg_price"].clip(lower, upper)
|
| 204 |
df = df.sort_values(["item_id", "city", "timestamp"]).reset_index(drop=True)
|
| 205 |
return df
|
| 206 |
|
|
|
|
| 232 |
return df
|
| 233 |
|
| 234 |
df = df.sort_values(["item_id", "city", "timestamp"]).copy()
|
| 235 |
+
df["avg_price"] = pd.to_numeric(df["avg_price"], errors="coerce").astype("float64")
|
| 236 |
+
df["item_count"] = pd.to_numeric(df["item_count"], errors="coerce").fillna(0).astype("float64")
|
| 237 |
|
| 238 |
df["hour"] = df["timestamp"].dt.hour
|
| 239 |
df["dayofweek"] = df["timestamp"].dt.dayofweek
|
|
|
|
| 303 |
upper = model_df["target_return_24h"].quantile(0.999)
|
| 304 |
model_df["target_return_24h"] = model_df["target_return_24h"].clip(lower, upper)
|
| 305 |
|
| 306 |
+
X = model_df[FEATURE_COLS].astype("float64")
|
| 307 |
+
y = model_df["target_return_24h"].astype("float64")
|
| 308 |
meta = model_df[["item_id", "city", "timestamp", "avg_price", "target_return_24h"]].copy()
|
| 309 |
|
| 310 |
return X, y, meta
|
|
|
|
| 358 |
print(f"Eksik özellikler: {missing}")
|
| 359 |
return pd.DataFrame()
|
| 360 |
|
| 361 |
+
X_pred = latest_features[FEATURE_COLS].astype("float64").dropna()
|
| 362 |
if X_pred.empty:
|
| 363 |
return pd.DataFrame()
|
| 364 |
|
|
|
|
| 522 |
self.prices_df = prices
|
| 523 |
self.status_message = f"{len(self.history_df)} satır tarihsel veri, {len(self.prices_df)} anlık fiyat alındı."
|
| 524 |
|
| 525 |
+
try:
|
| 526 |
+
save_to_csv(self.history_df, "history")
|
| 527 |
+
save_to_csv(self.prices_df, "prices")
|
| 528 |
+
except Exception as e:
|
| 529 |
+
print(f"CSV kaydetme hatası: {e}")
|
| 530 |
|
| 531 |
self.featured_df = build_features(self.history_df)
|
| 532 |
self._backtest_cache = None
|
|
|
|
| 562 |
|
| 563 |
if not preds.empty and not self.prices_df.empty:
|
| 564 |
price_info = self.prices_df[["item_id", "city", "sell_price_min", "buy_price_max"]].copy()
|
| 565 |
+
price_info["sell_price_min"] = pd.to_numeric(price_info["sell_price_min"], errors="coerce").astype("float64")
|
| 566 |
+
price_info["buy_price_max"] = pd.to_numeric(price_info["buy_price_max"], errors="coerce").astype("float64")
|
| 567 |
preds = preds.merge(price_info, on=["item_id", "city"], how="left")
|
| 568 |
preds["spread"] = preds["sell_price_min"] - preds["buy_price_max"]
|
| 569 |
else:
|
|
|
|
| 615 |
latest = pipeline.featured_df.sort_values("timestamp").groupby(["item_id", "city"]).tail(1).copy()
|
| 616 |
|
| 617 |
if not pipeline.prices_df.empty:
|
| 618 |
+
price_info = pipeline.prices_df[["item_id", "city", "sell_price_min", "buy_price_max"]].copy()
|
| 619 |
+
price_info["sell_price_min"] = pd.to_numeric(price_info["sell_price_min"], errors="coerce").astype("float64")
|
| 620 |
+
price_info["buy_price_max"] = pd.to_numeric(price_info["buy_price_max"], errors="coerce").astype("float64")
|
| 621 |
+
latest = latest.merge(price_info, on=["item_id", "city"], how="left")
|
| 622 |
else:
|
| 623 |
latest["sell_price_min"] = np.nan
|
| 624 |
latest["buy_price_max"] = np.nan
|
|
|
|
| 673 |
return pd.DataFrame({"Bilgi": ["Anlık fiyat verisi yok."]})
|
| 674 |
|
| 675 |
prices = pipeline.prices_df.copy()
|
| 676 |
+
prices["sell_price_min"] = pd.to_numeric(prices["sell_price_min"], errors="coerce").astype("float64")
|
| 677 |
+
prices["buy_price_max"] = pd.to_numeric(prices["buy_price_max"], errors="coerce").astype("float64")
|
| 678 |
prices = prices[prices["sell_price_min"] > 0]
|
| 679 |
|
| 680 |
if prices.empty:
|
|
|
|
| 782 |
|
| 783 |
|
| 784 |
def refresh_data_ui():
|
| 785 |
+
try:
|
| 786 |
+
pipeline.fetch_data()
|
| 787 |
+
if not pipeline.featured_df.empty:
|
| 788 |
+
pipeline.train_model()
|
| 789 |
+
except Exception as e:
|
| 790 |
+
pipeline.status_message = f"Beklenmeyen hata: {e}"
|
| 791 |
+
print(f"Refresh hatası: {e}")
|
| 792 |
+
import traceback
|
| 793 |
+
traceback.print_exc()
|
| 794 |
+
|
| 795 |
+
try:
|
| 796 |
+
bt_table, bt_fig, sim_table = get_backtest_outputs()
|
| 797 |
+
except Exception as e:
|
| 798 |
+
print(f"Backtest hatası: {e}")
|
| 799 |
+
bt_table = pd.DataFrame({"Bilgi": [f"Backtest hatası: {e}"]})
|
| 800 |
+
bt_fig = go.Figure().update_layout(title="Backtest hatası", template="plotly_dark")
|
| 801 |
+
sim_table = pd.DataFrame()
|
| 802 |
+
|
| 803 |
+
try:
|
| 804 |
+
dash = get_dashboard_metrics()
|
| 805 |
+
except Exception as e:
|
| 806 |
+
dash = pd.DataFrame({"Bilgi": [f"Hata: {e}"]})
|
| 807 |
+
|
| 808 |
+
try:
|
| 809 |
+
ana = get_analysis_table()
|
| 810 |
+
except Exception as e:
|
| 811 |
+
ana = pd.DataFrame({"Bilgi": [f"Hata: {e}"]})
|
| 812 |
+
|
| 813 |
+
try:
|
| 814 |
+
pred = get_prediction_table()
|
| 815 |
+
except Exception as e:
|
| 816 |
+
pred = pd.DataFrame({"Bilgi": [f"Hata: {e}"]})
|
| 817 |
+
|
| 818 |
+
try:
|
| 819 |
+
arb = get_arbitrage_table()
|
| 820 |
+
except Exception as e:
|
| 821 |
+
arb = pd.DataFrame({"Bilgi": [f"Hata: {e}"]})
|
| 822 |
+
|
| 823 |
+
try:
|
| 824 |
+
perf = get_model_performance_plot()
|
| 825 |
+
except Exception as e:
|
| 826 |
+
perf = go.Figure().update_layout(title=f"Hata: {e}", template="plotly_dark")
|
| 827 |
|
| 828 |
+
try:
|
| 829 |
+
csv_df = list_csv_files()
|
| 830 |
+
except Exception:
|
| 831 |
+
csv_df = pd.DataFrame(columns=["Dosya", "Boyut (KB)", "Son Değişiklik"])
|
| 832 |
|
| 833 |
return (
|
| 834 |
pipeline.status_message,
|
| 835 |
+
dash,
|
| 836 |
+
ana,
|
| 837 |
+
pred,
|
| 838 |
+
arb,
|
| 839 |
+
perf,
|
| 840 |
bt_table,
|
| 841 |
bt_fig,
|
| 842 |
sim_table,
|
| 843 |
+
csv_df,
|
| 844 |
)
|
| 845 |
|
| 846 |
|
| 847 |
def initial_load():
|
| 848 |
+
try:
|
| 849 |
+
dash = get_dashboard_metrics()
|
| 850 |
+
except Exception:
|
| 851 |
+
dash = pd.DataFrame({"Metrik": ["Durum"], "Değer": ["Hazır"]})
|
| 852 |
+
|
| 853 |
+
try:
|
| 854 |
+
csv_df = list_csv_files()
|
| 855 |
+
except Exception:
|
| 856 |
+
csv_df = pd.DataFrame(columns=["Dosya", "Boyut (KB)", "Son Değişiklik"])
|
| 857 |
+
|
| 858 |
return (
|
| 859 |
+
dash,
|
| 860 |
+
pd.DataFrame(),
|
| 861 |
+
pd.DataFrame(),
|
|
|
|
|
|
|
| 862 |
pd.DataFrame(),
|
| 863 |
go.Figure().update_layout(title="Verileri yenilemek için butona tıklayın", template="plotly_dark"),
|
| 864 |
pd.DataFrame(),
|
| 865 |
+
go.Figure().update_layout(title="Backtest için veri gerekli", template="plotly_dark"),
|
| 866 |
+
pd.DataFrame(),
|
| 867 |
+
csv_df,
|
| 868 |
)
|
| 869 |
|
| 870 |
|