Gabriel382 commited on
Commit
b1ed57d
Β·
1 Parent(s): 3a8d286

setting model manually

Browse files
Files changed (2) hide show
  1. app.py +10 -8
  2. requirements.txt +1 -1
app.py CHANGED
@@ -3,19 +3,21 @@ from fastapi import FastAPI
3
  from pydantic import BaseModel
4
  from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
5
 
6
- # βœ… Define a writable cache directory
7
- os.environ["TRANSFORMERS_CACHE"] = "/app/cache"
 
 
 
8
 
9
- # βœ… Ensure the cache directory exists
10
- os.makedirs("/app/cache", exist_ok=True)
11
 
12
- # βœ… Hugging Face model
13
  MODEL_NAME = "facebook/m2m100_418M"
14
 
15
- # βœ… Download the model manually before usage
16
  print("Downloading model...")
17
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, cache_dir="/app/cache")
18
- model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME, cache_dir="/app/cache")
19
  translator = pipeline("translation", model=model, tokenizer=tokenizer)
20
 
21
  print("Model loaded successfully!")
 
3
  from pydantic import BaseModel
4
  from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
5
 
6
+ # βœ… Set cache directory inside Hugging Face's allowed `/tmp/` folder
7
+ CACHE_DIR = "/tmp/huggingface_cache"
8
+ os.environ["TRANSFORMERS_CACHE"] = CACHE_DIR
9
+ os.environ["HF_HOME"] = CACHE_DIR
10
+ os.environ["HF_HUB_DISABLE_SYMLINKS"] = "1" # Fixes permission issue
11
 
12
+ # βœ… Ensure cache directory exists
13
+ os.makedirs(CACHE_DIR, exist_ok=True)
14
 
15
+ # βœ… Load the model
16
  MODEL_NAME = "facebook/m2m100_418M"
17
 
 
18
  print("Downloading model...")
19
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, cache_dir=CACHE_DIR)
20
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME, cache_dir=CACHE_DIR)
21
  translator = pipeline("translation", model=model, tokenizer=tokenizer)
22
 
23
  print("Model loaded successfully!")
requirements.txt CHANGED
@@ -1,5 +1,5 @@
1
- sentencepiece
2
  fastapi
3
  uvicorn
4
  transformers
5
  torch
 
 
 
1
  fastapi
2
  uvicorn
3
  transformers
4
  torch
5
+ huggingface_hub