Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
# --- MODELİ YÜKLE (AMAZON CHRONOS-2) ---
|
| 7 |
-
# Free Tier (CPU) için optimize ayarlar
|
| 8 |
-
print("🚀 Chronos-2 Modeli Yükleniyor...")
|
| 9 |
|
|
|
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def predict(context_str, prediction_length):
|
| 23 |
"""
|
| 24 |
-
|
| 25 |
-
Çıktı: Gelecek fiyat tahmini (String)
|
| 26 |
"""
|
|
|
|
| 27 |
if pipeline is None:
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
try:
|
| 31 |
-
#
|
| 32 |
series_strings = context_str.split('|')
|
| 33 |
-
|
| 34 |
tensor_list = []
|
|
|
|
| 35 |
for s in series_strings:
|
| 36 |
clean_s = s.strip()
|
| 37 |
if not clean_s: continue
|
|
@@ -41,15 +61,14 @@ def predict(context_str, prediction_length):
|
|
| 41 |
if not tensor_list:
|
| 42 |
return "Error: Veri boş."
|
| 43 |
|
| 44 |
-
#
|
| 45 |
context_tensor = torch.tensor(tensor_list).unsqueeze(0)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
forecast = pipeline.predict(context_tensor, int(prediction_length))
|
| 49 |
|
| 50 |
-
#
|
| 51 |
future_price = forecast[0].quantile(0.5).item()
|
| 52 |
-
|
| 53 |
return str(future_price)
|
| 54 |
|
| 55 |
except Exception as e:
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Kütüphane kontrolü ve yükleme
|
| 7 |
try:
|
| 8 |
+
from chronos import ChronosPipeline
|
| 9 |
+
LIB_AVAILABLE = True
|
| 10 |
+
except ImportError as e:
|
| 11 |
+
LIB_ERROR = str(e)
|
| 12 |
+
LIB_AVAILABLE = False
|
| 13 |
+
|
| 14 |
+
# Global Değişkenler
|
| 15 |
+
pipeline = None
|
| 16 |
+
MODEL_LOAD_ERROR = None
|
| 17 |
+
|
| 18 |
+
def load_model():
|
| 19 |
+
global pipeline, MODEL_LOAD_ERROR
|
| 20 |
+
|
| 21 |
+
if not LIB_AVAILABLE:
|
| 22 |
+
MODEL_LOAD_ERROR = f"Kütüphane Hatası: {LIB_ERROR}"
|
| 23 |
+
return
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
print("🚀 Chronos-2 Modeli Yükleniyor...")
|
| 27 |
+
# Free Tier (CPU) için optimize ayarlar
|
| 28 |
+
pipeline = ChronosPipeline.from_pretrained(
|
| 29 |
+
"amazon/chronos-2",
|
| 30 |
+
device_map="cpu",
|
| 31 |
+
dtype=torch.float32,
|
| 32 |
+
)
|
| 33 |
+
print("✅ Model Başarıyla Yüklendi!")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"❌ Model Yükleme Hatası: {e}")
|
| 36 |
+
MODEL_LOAD_ERROR = str(e)
|
| 37 |
+
|
| 38 |
+
# Başlangıçta yüklemeyi dene
|
| 39 |
+
load_model()
|
| 40 |
|
| 41 |
def predict(context_str, prediction_length):
|
| 42 |
"""
|
| 43 |
+
Tahmin Fonksiyonu
|
|
|
|
| 44 |
"""
|
| 45 |
+
# 1. Model Yüklü mü Kontrol Et
|
| 46 |
if pipeline is None:
|
| 47 |
+
# Hatanın DETAYINI döndür ki görelim
|
| 48 |
+
return f"Error: {MODEL_LOAD_ERROR}"
|
| 49 |
|
| 50 |
try:
|
| 51 |
+
# 2. Veriyi Hazırla
|
| 52 |
series_strings = context_str.split('|')
|
|
|
|
| 53 |
tensor_list = []
|
| 54 |
+
|
| 55 |
for s in series_strings:
|
| 56 |
clean_s = s.strip()
|
| 57 |
if not clean_s: continue
|
|
|
|
| 61 |
if not tensor_list:
|
| 62 |
return "Error: Veri boş."
|
| 63 |
|
| 64 |
+
# 3. Tensor Oluştur
|
| 65 |
context_tensor = torch.tensor(tensor_list).unsqueeze(0)
|
| 66 |
|
| 67 |
+
# 4. Tahmin Yap
|
| 68 |
forecast = pipeline.predict(context_tensor, int(prediction_length))
|
| 69 |
|
| 70 |
+
# 5. Sonuç
|
| 71 |
future_price = forecast[0].quantile(0.5).item()
|
|
|
|
| 72 |
return str(future_price)
|
| 73 |
|
| 74 |
except Exception as e:
|