Joash commited on
Commit
e3a17dd
·
1 Parent(s): 2f11a3f

Fix token handling with proper git credentials

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -9,6 +9,10 @@ import json
9
  from typing import List, Dict
10
  import warnings
11
  import spaces
 
 
 
 
12
 
13
  # Filter out warnings
14
  warnings.filterwarnings('ignore')
@@ -18,12 +22,9 @@ logging.basicConfig(level=logging.INFO)
18
  logger = logging.getLogger(__name__)
19
 
20
  # Environment variables
21
- HF_TOKEN = os.getenv("HUGGING_FACE_TOKEN", "v4e-o3_7lUO4cn8T9m9p5GNSI") # Default to the token if not set in Space
22
  MODEL_NAME = os.getenv("MODEL_NAME", "google/gemma-2b-it")
23
 
24
- # Login to Hugging Face
25
- login(token=HF_TOKEN)
26
-
27
  # Create data directory for persistence
28
  DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
29
  os.makedirs(DATA_DIR, exist_ok=True)
@@ -113,6 +114,15 @@ class CodeReviewer:
113
  def initialize_model(self):
114
  """Initialize the model and tokenizer."""
115
  try:
 
 
 
 
 
 
 
 
 
116
  logger.info("Loading tokenizer...")
117
  self.tokenizer = AutoTokenizer.from_pretrained(
118
  MODEL_NAME,
 
9
  from typing import List, Dict
10
  import warnings
11
  import spaces
12
+ from dotenv import load_dotenv
13
+
14
+ # Load environment variables from .env file
15
+ load_dotenv()
16
 
17
  # Filter out warnings
18
  warnings.filterwarnings('ignore')
 
22
  logger = logging.getLogger(__name__)
23
 
24
  # Environment variables
25
+ HF_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
26
  MODEL_NAME = os.getenv("MODEL_NAME", "google/gemma-2b-it")
27
 
 
 
 
28
  # Create data directory for persistence
29
  DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
30
  os.makedirs(DATA_DIR, exist_ok=True)
 
114
  def initialize_model(self):
115
  """Initialize the model and tokenizer."""
116
  try:
117
+ # Login to Hugging Face with git credential
118
+ if HF_TOKEN:
119
+ try:
120
+ login(token=HF_TOKEN, add_to_git_credential=True)
121
+ logger.info("Successfully logged in to Hugging Face")
122
+ except Exception as e:
123
+ logger.error(f"Error logging in to Hugging Face: {e}")
124
+ return False
125
+
126
  logger.info("Loading tokenizer...")
127
  self.tokenizer = AutoTokenizer.from_pretrained(
128
  MODEL_NAME,