Locutusque commited on
Commit
a68cd58
·
verified ·
1 Parent(s): 5fef8e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -9,11 +9,9 @@ import os
9
 
10
  model_choices = ["Locutusque/Esmeralda-31B"]
11
 
12
- # Holds the single currently-loaded model
13
  CURRENT = {"name": None, "pipe": None}
14
 
15
  def predownload_all_models():
16
- """Download model weights to the local HF cache without loading them onto any device."""
17
  for model_name in model_choices:
18
  print(f"Downloading {model_name}...")
19
  snapshot_download(repo_id=model_name, token=os.environ["token"])
@@ -26,24 +24,27 @@ def _unload_current():
26
  CURRENT["pipe"] = None
27
  CURRENT["name"] = None
28
  gc.collect()
29
- if torch.cuda.is_available():
30
- torch.cuda.empty_cache()
31
 
32
  def get_model(model_name):
33
- # If a different model is loaded, unload it first
34
  if CURRENT["name"] != model_name:
35
  _unload_current()
36
  task = "image-text-to-text" if model_name == "Locutusque/Esmeralda-31B" else "text-generation"
37
- print(f"Loading {model_name} onto GPU...")
 
 
 
 
38
  pipe = pipeline(
39
  task,
40
  model=model_name,
41
- device_map="cuda",
42
  torch_dtype=torch.bfloat16,
43
  trust_remote_code=True,
44
  token=os.environ["token"],
45
  use_fast=True,
46
  )
 
 
47
  tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, token=os.environ["token"])
48
  tokenizer.eos_token = "<|im_end|>"
49
  pipe.tokenizer = tokenizer
@@ -51,7 +52,7 @@ def get_model(model_name):
51
  CURRENT["pipe"] = pipe
52
  return CURRENT["pipe"]
53
 
54
- @spaces.GPU(duration=90)
55
  def generate(
56
  message,
57
  history,
 
9
 
10
  model_choices = ["Locutusque/Esmeralda-31B"]
11
 
 
12
  CURRENT = {"name": None, "pipe": None}
13
 
14
  def predownload_all_models():
 
15
  for model_name in model_choices:
16
  print(f"Downloading {model_name}...")
17
  snapshot_download(repo_id=model_name, token=os.environ["token"])
 
24
  CURRENT["pipe"] = None
25
  CURRENT["name"] = None
26
  gc.collect()
27
+ torch.cuda.empty_cache()
 
28
 
29
  def get_model(model_name):
 
30
  if CURRENT["name"] != model_name:
31
  _unload_current()
32
  task = "image-text-to-text" if model_name == "Locutusque/Esmeralda-31B" else "text-generation"
33
+ from transformers import BitsAndBytesConfig
34
+ model_kwargs["quantization_config"] = BitsAndBytesConfig(
35
+ load_in_8bit=True,
36
+ )
37
+ model_kwargs["device_map"] = "cuda"
38
  pipe = pipeline(
39
  task,
40
  model=model_name,
 
41
  torch_dtype=torch.bfloat16,
42
  trust_remote_code=True,
43
  token=os.environ["token"],
44
  use_fast=True,
45
  )
46
+ pipe.model.to("cuda")
47
+ pipe.device = torch.device("cuda")
48
  tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, token=os.environ["token"])
49
  tokenizer.eos_token = "<|im_end|>"
50
  pipe.tokenizer = tokenizer
 
52
  CURRENT["pipe"] = pipe
53
  return CURRENT["pipe"]
54
 
55
+ @spaces.GPU(duration=45)
56
  def generate(
57
  message,
58
  history,