Ehrii commited on
Commit
604a2b6
·
1 Parent(s): f41c1d7

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -9
main.py CHANGED
@@ -10,11 +10,14 @@ DetectorFactory.seed = 0
10
  # Set Hugging Face cache directory to a writable location
11
  os.environ["HF_HOME"] = "/tmp/huggingface"
12
  os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
13
- os.makedirs(os.environ["HF_HOME"], exist_ok=True)
 
 
 
 
14
 
15
  # Retrieve Hugging Face token from environment variable
16
  HF_TOKEN = os.getenv("HF_TOKEN")
17
-
18
  if not HF_TOKEN:
19
  raise RuntimeError("Hugging Face token is missing! Please set the HF_TOKEN environment variable.")
20
 
@@ -26,19 +29,29 @@ ENGLISH_MODEL_NAME = "siebert/sentiment-roberta-large-english"
26
 
27
  # Load multilingual sentiment model
28
  try:
29
- multilingual_tokenizer = AutoTokenizer.from_pretrained(MULTILINGUAL_MODEL_NAME, use_auth_token=HF_TOKEN)
 
 
 
 
30
  multilingual_model = pipeline(
31
  "sentiment-analysis",
32
  model=MULTILINGUAL_MODEL_NAME,
33
  tokenizer=multilingual_tokenizer,
34
- use_auth_token=HF_TOKEN
 
35
  )
36
  except Exception as e:
37
  raise RuntimeError(f"Failed to load multilingual model: {e}")
38
 
39
  # Load English sentiment model
40
  try:
41
- english_model = pipeline("sentiment-analysis", model=ENGLISH_MODEL_NAME, use_auth_token=HF_TOKEN)
 
 
 
 
 
42
  except Exception as e:
43
  raise RuntimeError(f"Failed to load English sentiment model: {e}")
44
 
@@ -65,16 +78,14 @@ def home():
65
  @app.post("/analyze/", response_model=SentimentResponse)
66
  def analyze_sentiment(request: SentimentRequest):
67
  text = request.text.strip()
68
-
69
  if not text:
70
  raise HTTPException(status_code=400, detail="Text input cannot be empty.")
71
-
72
  language = detect_language(text)
73
-
74
  # Choose the appropriate model based on detected language
75
  model = english_model if language == "en" else multilingual_model
76
  result = model(text)
77
-
78
  return SentimentResponse(
79
  original_text=text,
80
  language_detected=language,
 
10
  # Set Hugging Face cache directory to a writable location
11
  os.environ["HF_HOME"] = "/tmp/huggingface"
12
  os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
13
+
14
+ # Create cache directory with proper permissions
15
+ cache_dir = os.environ["HF_HOME"]
16
+ os.makedirs(cache_dir, exist_ok=True)
17
+ os.chmod(cache_dir, 0o755) # Set read/write/execute permissions for owner
18
 
19
  # Retrieve Hugging Face token from environment variable
20
  HF_TOKEN = os.getenv("HF_TOKEN")
 
21
  if not HF_TOKEN:
22
  raise RuntimeError("Hugging Face token is missing! Please set the HF_TOKEN environment variable.")
23
 
 
29
 
30
  # Load multilingual sentiment model
31
  try:
32
+ multilingual_tokenizer = AutoTokenizer.from_pretrained(
33
+ MULTILINGUAL_MODEL_NAME,
34
+ token=HF_TOKEN, # Use 'token' instead of deprecated 'use_auth_token'
35
+ cache_dir=cache_dir
36
+ )
37
  multilingual_model = pipeline(
38
  "sentiment-analysis",
39
  model=MULTILINGUAL_MODEL_NAME,
40
  tokenizer=multilingual_tokenizer,
41
+ token=HF_TOKEN, # Use 'token' instead of deprecated 'use_auth_token'
42
+ cache_dir=cache_dir
43
  )
44
  except Exception as e:
45
  raise RuntimeError(f"Failed to load multilingual model: {e}")
46
 
47
  # Load English sentiment model
48
  try:
49
+ english_model = pipeline(
50
+ "sentiment-analysis",
51
+ model=ENGLISH_MODEL_NAME,
52
+ token=HF_TOKEN, # Use 'token' instead of deprecated 'use_auth_token'
53
+ cache_dir=cache_dir
54
+ )
55
  except Exception as e:
56
  raise RuntimeError(f"Failed to load English sentiment model: {e}")
57
 
 
78
  @app.post("/analyze/", response_model=SentimentResponse)
79
  def analyze_sentiment(request: SentimentRequest):
80
  text = request.text.strip()
 
81
  if not text:
82
  raise HTTPException(status_code=400, detail="Text input cannot be empty.")
83
+
84
  language = detect_language(text)
 
85
  # Choose the appropriate model based on detected language
86
  model = english_model if language == "en" else multilingual_model
87
  result = model(text)
88
+
89
  return SentimentResponse(
90
  original_text=text,
91
  language_detected=language,