Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM,
|
| 4 |
from threading import Thread
|
| 5 |
import sys
|
| 6 |
|
|
@@ -16,35 +16,44 @@ You are not a tool. You are SAMAI — think, then speak."""
|
|
| 16 |
print("Loading model...")
|
| 17 |
|
| 18 |
# ===== Check for CUDA =====
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
print(" Please select a GPU hardware in your Hugging Face Space settings")
|
| 22 |
-
print(" (e.g., T4 small) or run locally with a CUDA-compatible GPU.")
|
| 23 |
-
sys.exit(1) # stop here – no point continuing
|
| 24 |
-
|
| 25 |
-
# ===== GPU path – use 4-bit with optimisations =====
|
| 26 |
-
bnb_config = BitsAndBytesConfig(
|
| 27 |
-
load_in_4bit=True,
|
| 28 |
-
bnb_4bit_quant_type="nf4",
|
| 29 |
-
bnb_4bit_compute_dtype=torch.float16, # T4 native
|
| 30 |
-
bnb_4bit_use_double_quant=True,
|
| 31 |
-
)
|
| 32 |
|
| 33 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
model.eval()
|
| 45 |
print("✅ Ready!")
|
| 46 |
|
| 47 |
-
# ===== Generation function (tuple history
|
| 48 |
def respond(message, history, max_tokens, temperature):
|
| 49 |
messages = [{"role": "system", "content": SAMAI_SYSTEM}]
|
| 50 |
for user_msg, bot_msg in history:
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
| 4 |
from threading import Thread
|
| 5 |
import sys
|
| 6 |
|
|
|
|
| 16 |
print("Loading model...")
|
| 17 |
|
| 18 |
# ===== Check for CUDA =====
|
| 19 |
+
has_cuda = torch.cuda.is_available()
|
| 20 |
+
print(f"CUDA available: {has_cuda}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 23 |
|
| 24 |
+
if has_cuda:
|
| 25 |
+
# ----- GPU path: use 4-bit quantization -----
|
| 26 |
+
from transformers import BitsAndBytesConfig
|
| 27 |
+
bnb_config = BitsAndBytesConfig(
|
| 28 |
+
load_in_4bit=True,
|
| 29 |
+
bnb_4bit_quant_type="nf4",
|
| 30 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 31 |
+
bnb_4bit_use_double_quant=True,
|
| 32 |
+
)
|
| 33 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 34 |
+
MODEL_ID,
|
| 35 |
+
quantization_config=bnb_config,
|
| 36 |
+
device_map="cuda",
|
| 37 |
+
trust_remote_code=True,
|
| 38 |
+
dtype=torch.float16,
|
| 39 |
+
attn_implementation="sdpa",
|
| 40 |
+
)
|
| 41 |
+
else:
|
| 42 |
+
# ----- CPU path: load full FP16 model (requires ~28 GB RAM) -----
|
| 43 |
+
print("⚠️ No GPU detected. Loading the model in FP16 on CPU (memory intensive).")
|
| 44 |
+
print(" If you run out of memory, consider using a smaller model or a GPU.")
|
| 45 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 46 |
+
MODEL_ID,
|
| 47 |
+
device_map="cpu",
|
| 48 |
+
trust_remote_code=True,
|
| 49 |
+
torch_dtype=torch.float16, # half precision to save memory
|
| 50 |
+
low_cpu_mem_usage=True, # memory efficient loading
|
| 51 |
+
)
|
| 52 |
|
| 53 |
model.eval()
|
| 54 |
print("✅ Ready!")
|
| 55 |
|
| 56 |
+
# ===== Generation function (tuple history) =====
|
| 57 |
def respond(message, history, max_tokens, temperature):
|
| 58 |
messages = [{"role": "system", "content": SAMAI_SYSTEM}]
|
| 59 |
for user_msg, bot_msg in history:
|