Update app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1,64 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
#
|
|
|
|
| 7 |
try:
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# Global Model Değişkeni
|
| 19 |
-
pipeline = None
|
| 20 |
-
MODEL_LOAD_ERROR = None
|
| 21 |
-
|
| 22 |
-
# Modeli Yükleme Fonksiyonu
|
| 23 |
-
def load_model():
|
| 24 |
-
global pipeline, MODEL_LOAD_ERROR
|
| 25 |
-
|
| 26 |
-
if not LIB_AVAILABLE:
|
| 27 |
-
MODEL_LOAD_ERROR = f"chronos-forecasting kütüphanesi yüklü değil: {LIB_ERROR}"
|
| 28 |
-
return
|
| 29 |
-
|
| 30 |
-
try:
|
| 31 |
-
print("🚀 Chronos-2 Modeli Yükleniyor...")
|
| 32 |
-
# Free Tier (CPU) için optimize ayarlar
|
| 33 |
-
pipeline = Chronos2Pipeline.from_pretrained(
|
| 34 |
-
"amazon/chronos-2",
|
| 35 |
-
device_map="cpu",
|
| 36 |
-
dtype=torch.float32,
|
| 37 |
-
)
|
| 38 |
-
print("✅ Model Başarıyla Yüklendi!")
|
| 39 |
-
except Exception as e:
|
| 40 |
-
print(f"❌ Model Yükleme Hatası: {e}")
|
| 41 |
-
MODEL_LOAD_ERROR = str(e)
|
| 42 |
-
|
| 43 |
-
# Başlangıçta yüklemeyi dene
|
| 44 |
-
load_model()
|
| 45 |
|
| 46 |
def predict(context_str, prediction_length):
|
| 47 |
-
"""
|
| 48 |
-
Tahmin Fonksiyonu
|
| 49 |
-
"""
|
| 50 |
-
# 1. Model Yüklü mü Kontrol Et
|
| 51 |
if pipeline is None:
|
| 52 |
-
return
|
| 53 |
|
| 54 |
try:
|
| 55 |
-
#
|
| 56 |
series_strings = context_str.split('|')
|
| 57 |
tensor_list = []
|
| 58 |
|
| 59 |
for s in series_strings:
|
| 60 |
clean_s = s.strip()
|
| 61 |
if not clean_s: continue
|
|
|
|
| 62 |
data_list = [float(x) for x in clean_s.split(',')]
|
| 63 |
tensor_list.append(data_list)
|
| 64 |
|
| 65 |
-
if not tensor_list:
|
| 66 |
-
return "Error: Veri boş."
|
| 67 |
|
| 68 |
-
#
|
|
|
|
| 69 |
context_tensor = torch.tensor(tensor_list).unsqueeze(0)
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
# 5. Sonucu Al (Medyan)
|
| 79 |
-
# Chronos-2 forecast nesnesi döner, quantile metodunu kullanırız.
|
| 80 |
-
# İlk serinin (Fiyat) sonucunu alıyoruz.
|
| 81 |
-
if hasattr(forecast[0], 'quantile'):
|
| 82 |
-
future_price = forecast[0].quantile(0.5).item()
|
| 83 |
-
else:
|
| 84 |
-
# Eğer tensör dönerse (Eski sürüm veya farklı yapı)
|
| 85 |
-
# Bu durumda numpy/torch medyanı alırız ama Chronos2Pipeline genelde nesne döner.
|
| 86 |
-
future_price = 0.0 # Fallback
|
| 87 |
-
|
| 88 |
return str(future_price)
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
return f"Error: {str(e)}"
|
| 92 |
|
| 93 |
-
# Arayüzü Başlat
|
| 94 |
iface = gr.Interface(fn=predict, inputs=["text", "number"], outputs="text")
|
| 95 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
+
from chronos import Chronos2Pipeline
|
| 5 |
|
| 6 |
+
# --- MODELİ YÜKLE ---
|
| 7 |
+
print("🚀 Chronos-2 Modeli Yükleniyor...")
|
| 8 |
try:
|
| 9 |
+
pipeline = Chronos2Pipeline.from_pretrained(
|
| 10 |
+
"amazon/chronos-2",
|
| 11 |
+
device_map="cpu",
|
| 12 |
+
dtype=torch.float32,
|
| 13 |
+
)
|
| 14 |
+
print("✅ Model Başarıyla Yüklendi!")
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"❌ Model Yükleme Hatası: {e}")
|
| 17 |
+
pipeline = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def predict(context_str, prediction_length):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
if pipeline is None:
|
| 21 |
+
return "Error: Model yüklenemedi."
|
| 22 |
|
| 23 |
try:
|
| 24 |
+
# 1. Veriyi Parçala
|
| 25 |
series_strings = context_str.split('|')
|
| 26 |
tensor_list = []
|
| 27 |
|
| 28 |
for s in series_strings:
|
| 29 |
clean_s = s.strip()
|
| 30 |
if not clean_s: continue
|
| 31 |
+
# Veriyi listeye çevir
|
| 32 |
data_list = [float(x) for x in clean_s.split(',')]
|
| 33 |
tensor_list.append(data_list)
|
| 34 |
|
| 35 |
+
if not tensor_list: return "Error: Veri boş."
|
|
|
|
| 36 |
|
| 37 |
+
# 2. Tensor Oluştur (Batch, Variates, Time)
|
| 38 |
+
# Örn: (1, 5, 200)
|
| 39 |
context_tensor = torch.tensor(tensor_list).unsqueeze(0)
|
| 40 |
|
| 41 |
+
# --- KRİTİK KONTROL ---
|
| 42 |
+
# Eğer veri çok büyük veya çok küçükse (Outlier), model sapıtabilir.
|
| 43 |
+
# Fiyat verisi (ilk seri) 0 olmamalı.
|
| 44 |
+
if context_tensor[0, 0, -1] == 0:
|
| 45 |
+
return "Error: Son fiyat 0 olamaz."
|
| 46 |
+
|
| 47 |
+
# 3. Tahmin Yap
|
| 48 |
+
forecast = pipeline.predict(context_tensor, int(prediction_length))
|
| 49 |
+
|
| 50 |
+
# 4. Sonucu Al
|
| 51 |
+
# Chronos-2 çok değişkenli tahminde, her değişken için tahmin üretebilir.
|
| 52 |
+
# Bizim hedefimiz FİYAT (0. indeks)
|
| 53 |
+
price_forecast = forecast[0] # İlk seri (Fiyat)
|
| 54 |
+
|
| 55 |
+
# Medyanı al
|
| 56 |
+
future_price = price_forecast.quantile(0.5).item()
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
return str(future_price)
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
return f"Error: {str(e)}"
|
| 62 |
|
|
|
|
| 63 |
iface = gr.Interface(fn=predict, inputs=["text", "number"], outputs="text")
|
| 64 |
iface.launch()
|