cutechicken commited on
Commit
44a7786
โ€ข
1 Parent(s): e5e0be0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -7,21 +7,31 @@ import json
7
  from datetime import datetime
8
  import torch
9
  from transformers import AutoModelForCausalLM, AutoTokenizer
10
- import tempfile
11
 
12
  # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •
13
  HF_TOKEN = os.getenv("HF_TOKEN")
14
  MODEL_ID = "CohereForAI/c4ai-command-r-plus-08-2024"
15
 
16
- # ์ž„์‹œ ๋””๋ ‰ํ† ๋ฆฌ ์„ค์ •
17
- TEMP_DIR = tempfile.mkdtemp()
18
- os.environ["TRANSFORMERS_CACHE"] = os.path.join(TEMP_DIR, "transformers_cache")
19
- os.environ["TORCH_HOME"] = os.path.join(TEMP_DIR, "torch_cache")
20
- os.environ["HF_HOME"] = os.path.join(TEMP_DIR, "huggingface")
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  class ModelManager:
23
  def __init__(self):
24
- self.cache_dir = os.path.join(TEMP_DIR, "model_cache")
25
  os.makedirs(self.cache_dir, exist_ok=True)
26
  self.model = None
27
  self.tokenizer = None
@@ -29,6 +39,7 @@ class ModelManager:
29
 
30
  def setup_model(self):
31
  try:
 
32
  print("ํ† ํฌ๋‚˜์ด์ € ๋กœ๋”ฉ ์‹œ์ž‘...")
33
  self.tokenizer = AutoTokenizer.from_pretrained(
34
  MODEL_ID,
@@ -51,18 +62,7 @@ class ModelManager:
51
  print("๋ชจ๋ธ ๋กœ๋”ฉ ์™„๋ฃŒ")
52
  except Exception as e:
53
  print(f"๋ชจ๋ธ ๋กœ๋”ฉ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}")
54
- try:
55
- print("๊ธฐ๋ณธ ์„ค์ •์œผ๋กœ ๋‹ค์‹œ ์‹œ๋„...")
56
- self.model = AutoModelForCausalLM.from_pretrained(
57
- MODEL_ID,
58
- token=HF_TOKEN,
59
- torch_dtype=torch.float16,
60
- trust_remote_code=True,
61
- cache_dir=self.cache_dir
62
- )
63
- print("๊ธฐ๋ณธ ์„ค์ •์œผ๋กœ ๋ชจ๋ธ ๋กœ๋”ฉ ์™„๋ฃŒ")
64
- except Exception as e2:
65
- raise Exception(f"๋ชจ๋ธ ๋กœ๋”ฉ ์™„์ „ํžˆ ์‹คํŒจ: {e2}")
66
 
67
  class ChatHistory:
68
  def __init__(self):
 
7
  from datetime import datetime
8
  import torch
9
  from transformers import AutoModelForCausalLM, AutoTokenizer
 
10
 
11
  # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •
12
  HF_TOKEN = os.getenv("HF_TOKEN")
13
  MODEL_ID = "CohereForAI/c4ai-command-r-plus-08-2024"
14
 
15
+ # ์˜๊ตฌ ์Šคํ† ๋ฆฌ์ง€ ๊ฒฝ๋กœ ์„ค์ •
16
+ PERSISTENT_DIR = "/persistent/model_cache"
17
+ os.makedirs(PERSISTENT_DIR, exist_ok=True)
18
+
19
+ # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •
20
+ os.environ["TRANSFORMERS_CACHE"] = os.path.join(PERSISTENT_DIR, "transformers_cache")
21
+ os.environ["TORCH_HOME"] = os.path.join(PERSISTENT_DIR, "torch_cache")
22
+ os.environ["HF_HOME"] = os.path.join(PERSISTENT_DIR, "huggingface")
23
+
24
+ # ๊ฐ ์บ์‹œ ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ
25
+ for cache_dir in [
26
+ os.environ["TRANSFORMERS_CACHE"],
27
+ os.environ["TORCH_HOME"],
28
+ os.environ["HF_HOME"]
29
+ ]:
30
+ os.makedirs(cache_dir, exist_ok=True)
31
 
32
  class ModelManager:
33
  def __init__(self):
34
+ self.cache_dir = os.path.join(PERSISTENT_DIR, "model_files")
35
  os.makedirs(self.cache_dir, exist_ok=True)
36
  self.model = None
37
  self.tokenizer = None
 
39
 
40
  def setup_model(self):
41
  try:
42
+ print(f"์บ์‹œ ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ: {self.cache_dir}")
43
  print("ํ† ํฌ๋‚˜์ด์ € ๋กœ๋”ฉ ์‹œ์ž‘...")
44
  self.tokenizer = AutoTokenizer.from_pretrained(
45
  MODEL_ID,
 
62
  print("๋ชจ๋ธ ๋กœ๋”ฉ ์™„๋ฃŒ")
63
  except Exception as e:
64
  print(f"๋ชจ๋ธ ๋กœ๋”ฉ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}")
65
+ raise Exception(f"๋ชจ๋ธ ๋กœ๋”ฉ ์‹คํŒจ: {e}")
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  class ChatHistory:
68
  def __init__(self):