ahuggingface01 commited on
Commit
4f75e01
·
verified ·
1 Parent(s): 6752e0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -1,13 +1,20 @@
 
1
  import torch
2
  import gradio as gr
3
  from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
4
 
 
 
 
5
  MODEL_ID = "deepseek-ai/DeepSeek-R1"
6
 
7
- device = "cuda" if torch.cuda.is_available() else "cpu"
8
- print("Using device:", device)
 
 
 
9
 
10
- # 4-bit config (24GB VRAM optimized)
11
  bnb_config = BitsAndBytesConfig(
12
  load_in_4bit=True,
13
  bnb_4bit_compute_dtype=torch.float16,
@@ -15,22 +22,25 @@ bnb_config = BitsAndBytesConfig(
15
  bnb_4bit_quant_type="nf4",
16
  )
17
 
18
- # Load tokenizer
19
  tokenizer = AutoTokenizer.from_pretrained(
20
  MODEL_ID,
21
  trust_remote_code=True
22
  )
23
 
24
- # Load model
25
  model = AutoModelForCausalLM.from_pretrained(
26
  MODEL_ID,
27
  quantization_config=bnb_config,
28
  device_map="auto",
29
- trust_remote_code=True
 
30
  )
31
 
32
  model.eval()
33
 
 
 
34
  def chat_fn(message, history):
35
  messages = []
36
 
@@ -65,9 +75,10 @@ def chat_fn(message, history):
65
  return response
66
 
67
 
 
68
  demo = gr.ChatInterface(
69
  fn=chat_fn,
70
- title="DeepSeek-R1 32B (4bit) - 24GB VRAM",
71
  chatbot=gr.Chatbot(height=500),
72
  )
73
 
 
1
+ import os
2
  import torch
3
  import gradio as gr
4
  from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
5
 
6
+ # ---- Safety: prevent VRAM fragmentation ----
7
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
8
+
9
  MODEL_ID = "deepseek-ai/DeepSeek-R1"
10
 
11
+ # ---- HARD GPU CHECK ----
12
+ if not torch.cuda.is_available():
13
+ raise RuntimeError("❌ GPU not detected. Please enable GPU hardware in HF Space settings.")
14
+
15
+ print("✅ Using GPU:", torch.cuda.get_device_name(0))
16
 
17
+ # ---- 4bit quant config (24GB optimized) ----
18
  bnb_config = BitsAndBytesConfig(
19
  load_in_4bit=True,
20
  bnb_4bit_compute_dtype=torch.float16,
 
22
  bnb_4bit_quant_type="nf4",
23
  )
24
 
25
+ # ---- Load tokenizer ----
26
  tokenizer = AutoTokenizer.from_pretrained(
27
  MODEL_ID,
28
  trust_remote_code=True
29
  )
30
 
31
+ # ---- Load model ----
32
  model = AutoModelForCausalLM.from_pretrained(
33
  MODEL_ID,
34
  quantization_config=bnb_config,
35
  device_map="auto",
36
+ trust_remote_code=True,
37
+ attn_implementation="flash_attention_2"
38
  )
39
 
40
  model.eval()
41
 
42
+
43
+ # ---- Chat Function ----
44
  def chat_fn(message, history):
45
  messages = []
46
 
 
75
  return response
76
 
77
 
78
+ # ---- Gradio UI ----
79
  demo = gr.ChatInterface(
80
  fn=chat_fn,
81
+ title="DeepSeek-R1 32B (4bit) - 24GB GPU",
82
  chatbot=gr.Chatbot(height=500),
83
  )
84