Malruwan commited on
Commit
e3e0ddf
·
verified ·
1 Parent(s): bc948f4

Update sentiment_api.py

Browse files
Files changed (1) hide show
  1. sentiment_api.py +11 -7
sentiment_api.py CHANGED
@@ -1,20 +1,25 @@
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI, Request
2
  from pydantic import BaseModel
3
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
  import torch
5
- import os
6
-
7
- # Set a custom cache directory
8
- os.environ["TRANSFORMERS_CACHE"] = "./hf_cache"
9
 
10
- # Load model and tokenizer once at startup
11
  model_name = "tabularisai/multilingual-sentiment-analysis"
12
  tokenizer = AutoTokenizer.from_pretrained(model_name)
13
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
14
 
15
  app = FastAPI()
16
 
17
- # Sentiment map
18
  sentiment_map = {
19
  0: "Very Negative",
20
  1: "Negative",
@@ -23,7 +28,6 @@ sentiment_map = {
23
  4: "Very Positive"
24
  }
25
 
26
- # Request body schema
27
  class ReviewRequest(BaseModel):
28
  text: str
29
 
 
1
+ import os
2
+
3
+ # Force cache inside local folder
4
+ os.environ["TRANSFORMERS_CACHE"] = "./hf_cache"
5
+ os.environ["HF_HOME"] = "./hf_cache"
6
+ os.environ["XDG_CACHE_HOME"] = "./hf_cache"
7
+ os.environ["TORCH_HOME"] = "./hf_cache"
8
+ os.environ["HF_DATASETS_CACHE"] = "./hf_cache"
9
+ os.environ["SAFE_TENSORS_CACHE"] = "./hf_cache"
10
+
11
  from fastapi import FastAPI, Request
12
  from pydantic import BaseModel
13
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
14
  import torch
 
 
 
 
15
 
16
+ # Load model
17
  model_name = "tabularisai/multilingual-sentiment-analysis"
18
  tokenizer = AutoTokenizer.from_pretrained(model_name)
19
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
20
 
21
  app = FastAPI()
22
 
 
23
  sentiment_map = {
24
  0: "Very Negative",
25
  1: "Negative",
 
28
  4: "Very Positive"
29
  }
30
 
 
31
  class ReviewRequest(BaseModel):
32
  text: str
33