melikegks commited on
Commit
78101a7
·
verified ·
1 Parent(s): b401778

ZeroGPU tespitini SPACES_ZERO_GPU ortam değişkenine bağla

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -54,27 +54,31 @@ TALIMATLAR = {
54
  "Serbest (aşağıya yaz)": "",
55
  }
56
 
57
- # ZeroGPU donanımında `spaces` paketi vardır ve GPU yalnızca dekore edilmiş
58
- # fonksiyon çalışırken tahsis edilir. cpu-basic'te paket yoktur; aynı kod
59
- # dekoratörsüz ve CPU'da çalışsın diye ikisi de destekleniyor.
60
- try:
 
 
 
 
 
 
 
61
  import spaces
62
- ZEROGPU = True
63
- except ImportError:
64
  spaces = None
65
- ZEROGPU = False
66
-
67
- if not ZEROGPU:
68
  torch.set_num_threads(os.cpu_count() or 2)
69
 
 
 
70
  print("Model yükleniyor… ZeroGPU:", ZEROGPU, flush=True)
71
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ADI)
72
  model = AutoModelForCausalLM.from_pretrained(
73
  MODEL_ADI,
74
- dtype=torch.bfloat16 if (ZEROGPU or torch.cuda.is_available())
75
- else torch.float32,
76
  ).eval()
77
- if ZEROGPU or torch.cuda.is_available():
78
  model = model.to("cuda")
79
  IM_END_ID = tokenizer.convert_tokens_to_ids(IM_END)
80
  print("Model hazır. Cihaz:", next(model.parameters()).device, flush=True)
 
54
  "Serbest (aşağıya yaz)": "",
55
  }
56
 
57
+ # ZeroGPU tespiti ORTAM DEĞİŞKENİNDEN yapılmalı, paketin varlığından değil:
58
+ # `spaces` requirements.txt'te olduğu için cpu-basic'te de kuruluyor ve
59
+ # "import edilebiliyorsa GPU vardır" varsayımı yanlış sonuç veriyor.
60
+ # HF, ZeroGPU donanımında SPACES_ZERO_GPU=true tanımlar.
61
+ #
62
+ # Ayrıca ZeroGPU'da torch.cuda.is_available() açılışta False döner — GPU
63
+ # yalnızca @spaces.GPU ile dekore edilmiş fonksiyon çalışırken bağlanır.
64
+ # Bu yüzden cuda kontrolü tek başına da yeterli değil.
65
+ ZEROGPU = os.environ.get("SPACES_ZERO_GPU", "").lower() in ("1", "true")
66
+
67
+ if ZEROGPU:
68
  import spaces
69
+ else:
 
70
  spaces = None
 
 
 
71
  torch.set_num_threads(os.cpu_count() or 2)
72
 
73
+ GPU_VAR = ZEROGPU or torch.cuda.is_available()
74
+
75
  print("Model yükleniyor… ZeroGPU:", ZEROGPU, flush=True)
76
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ADI)
77
  model = AutoModelForCausalLM.from_pretrained(
78
  MODEL_ADI,
79
+ dtype=torch.bfloat16 if GPU_VAR else torch.float32,
 
80
  ).eval()
81
+ if GPU_VAR:
82
  model = model.to("cuda")
83
  IM_END_ID = tokenizer.convert_tokens_to_ids(IM_END)
84
  print("Model hazır. Cihaz:", next(model.parameters()).device, flush=True)