Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,5 +1,5 @@
|
|
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
|
@@ -12,22 +12,35 @@ 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 |
app = FastAPI()
|
16 |
|
17 |
# Model names
|
18 |
-
|
19 |
-
|
20 |
|
21 |
-
# Load
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
|
29 |
# Load English sentiment model
|
30 |
-
|
|
|
|
|
|
|
31 |
|
32 |
class SentimentRequest(BaseModel):
|
33 |
text: str
|
@@ -39,6 +52,7 @@ class SentimentResponse(BaseModel):
|
|
39 |
confidence_score: float
|
40 |
|
41 |
def detect_language(text):
|
|
|
42 |
try:
|
43 |
return detect(text)
|
44 |
except Exception:
|
@@ -50,7 +64,11 @@ def home():
|
|
50 |
|
51 |
@app.post("/analyze/", response_model=SentimentResponse)
|
52 |
def analyze_sentiment(request: SentimentRequest):
|
53 |
-
text = request.text
|
|
|
|
|
|
|
|
|
54 |
language = detect_language(text)
|
55 |
|
56 |
# Choose the appropriate model based on detected language
|
@@ -62,4 +80,4 @@ def analyze_sentiment(request: SentimentRequest):
|
|
62 |
language_detected=language,
|
63 |
sentiment=result[0]["label"].lower(),
|
64 |
confidence_score=result[0]["score"],
|
65 |
-
)
|
|
|
1 |
import os
|
2 |
+
from fastapi import FastAPI, HTTPException
|
3 |
from pydantic import BaseModel
|
4 |
from transformers import pipeline, AutoTokenizer
|
5 |
from langdetect import detect, DetectorFactory
|
|
|
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 |
+
|
21 |
app = FastAPI()
|
22 |
|
23 |
# Model names
|
24 |
+
MULTILINGUAL_MODEL_NAME = "johndoee/sentiment"
|
25 |
+
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 |
|
45 |
class SentimentRequest(BaseModel):
|
46 |
text: str
|
|
|
52 |
confidence_score: float
|
53 |
|
54 |
def detect_language(text):
|
55 |
+
"""Detect the language of the given text."""
|
56 |
try:
|
57 |
return detect(text)
|
58 |
except Exception:
|
|
|
64 |
|
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
|
|
|
80 |
language_detected=language,
|
81 |
sentiment=result[0]["label"].lower(),
|
82 |
confidence_score=result[0]["score"],
|
83 |
+
)
|