Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
-
from transformers import pipeline
|
| 5 |
from langdetect import detect, DetectorFactory
|
| 6 |
|
| 7 |
# Ensure consistent language detection results
|
|
@@ -9,12 +9,21 @@ DetectorFactory.seed = 0
|
|
| 9 |
|
| 10 |
# Set Hugging Face cache directory to a writable location
|
| 11 |
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 12 |
-
os.makedirs(os.environ["HF_HOME"], exist_ok=True)
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
|
| 16 |
-
# Load
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
english_model = pipeline("sentiment-analysis", model="siebert/sentiment-roberta-large-english")
|
| 19 |
|
| 20 |
class SentimentRequest(BaseModel):
|
|
@@ -29,7 +38,7 @@ class SentimentResponse(BaseModel):
|
|
| 29 |
def detect_language(text):
|
| 30 |
try:
|
| 31 |
return detect(text)
|
| 32 |
-
except:
|
| 33 |
return "unknown"
|
| 34 |
|
| 35 |
@app.get("/")
|
|
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
+
from transformers import pipeline, AutoTokenizer
|
| 5 |
from langdetect import detect, DetectorFactory
|
| 6 |
|
| 7 |
# Ensure consistent language detection results
|
|
|
|
| 9 |
|
| 10 |
# Set Hugging Face cache directory to a writable location
|
| 11 |
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 12 |
+
os.makedirs(os.environ["HF_HOME"], exist_ok=True)
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
|
| 16 |
+
# Load the original tokenizer from the base model
|
| 17 |
+
original_tokenizer = AutoTokenizer.from_pretrained("tabularisai/multilingual-sentiment-analysis")
|
| 18 |
+
|
| 19 |
+
# Load the fine-tuned model and pass the tokenizer explicitly
|
| 20 |
+
multilingual_model = pipeline(
|
| 21 |
+
"sentiment-analysis",
|
| 22 |
+
model="models/",
|
| 23 |
+
tokenizer=original_tokenizer
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# English model remains unchanged
|
| 27 |
english_model = pipeline("sentiment-analysis", model="siebert/sentiment-roberta-large-english")
|
| 28 |
|
| 29 |
class SentimentRequest(BaseModel):
|
|
|
|
| 38 |
def detect_language(text):
|
| 39 |
try:
|
| 40 |
return detect(text)
|
| 41 |
+
except Exception:
|
| 42 |
return "unknown"
|
| 43 |
|
| 44 |
@app.get("/")
|