Ouiam123 commited on
Commit
b619002
·
verified ·
1 Parent(s): 6fb7d33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
  from huggingface_hub import login
@@ -7,10 +8,9 @@ from dotenv import load_dotenv
7
  # Load environment variables
8
  load_dotenv()
9
 
10
- # Explicitly set a writable cache directory
11
- writable_cache_dir = "/tmp/huggingface_cache"
12
  os.environ["TRANSFORMERS_CACHE"] = writable_cache_dir
13
- os.makedirs(writable_cache_dir, exist_ok=True)
14
 
15
  # Retrieve the Hugging Face API token from environment variables
16
  api_token = os.getenv("ttt")
@@ -20,7 +20,7 @@ if not api_token:
20
 
21
  # Log in to Hugging Face with the token
22
  try:
23
- login(api_token) # Authenticate using the Hugging Face token
24
  print("Successfully logged in to Hugging Face.")
25
  except Exception as e:
26
  print(f"Failed to log in to Hugging Face: {e}")
@@ -39,8 +39,8 @@ try:
39
  # Load the model with 4-bit quantization
40
  model = AutoModelForCausalLM.from_pretrained(
41
  model_name,
42
- load_in_4bit=True, # Enable 4-bit quantization
43
- device_map="auto" # Automatically map to GPU or CPU
44
  )
45
 
46
  # Input text to the model
@@ -59,4 +59,4 @@ try:
59
  print("Response:", response)
60
 
61
  except Exception as e:
62
- print(f"An error occurred: {e}")
 
1
  import os
2
+ import tempfile
3
  import torch
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
5
  from huggingface_hub import login
 
8
  # Load environment variables
9
  load_dotenv()
10
 
11
+ # Create a temporary cache directory
12
+ writable_cache_dir = tempfile.mkdtemp()
13
  os.environ["TRANSFORMERS_CACHE"] = writable_cache_dir
 
14
 
15
  # Retrieve the Hugging Face API token from environment variables
16
  api_token = os.getenv("ttt")
 
20
 
21
  # Log in to Hugging Face with the token
22
  try:
23
+ login(api_token)
24
  print("Successfully logged in to Hugging Face.")
25
  except Exception as e:
26
  print(f"Failed to log in to Hugging Face: {e}")
 
39
  # Load the model with 4-bit quantization
40
  model = AutoModelForCausalLM.from_pretrained(
41
  model_name,
42
+ load_in_4bit=True,
43
+ device_map="auto"
44
  )
45
 
46
  # Input text to the model
 
59
  print("Response:", response)
60
 
61
  except Exception as e:
62
+ print(f"An error occurred: {e}")