Spaces:
Running
Running
Update app/sentiment.py
Browse files- app/sentiment.py +12 -14
app/sentiment.py
CHANGED
@@ -1,49 +1,47 @@
|
|
1 |
"""
|
2 |
-
|
3 |
-
Hugging Face Transformers-based sentiment analysis
|
4 |
-
β
Fixed for Hugging Face Spaces: avoids /.cache write errors
|
5 |
"""
|
6 |
|
|
|
7 |
from transformers import pipeline
|
8 |
from functools import lru_cache
|
9 |
import hashlib
|
10 |
import logging
|
11 |
-
import os
|
12 |
|
13 |
-
#
|
14 |
-
# Set model cache path to a writable directory
|
15 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
|
16 |
os.makedirs("/tmp/huggingface", exist_ok=True)
|
17 |
|
18 |
-
# Load sentiment
|
19 |
_sentiment = pipeline(
|
20 |
"sentiment-analysis",
|
21 |
model="distilbert-base-uncased-finetuned-sst-2-english"
|
22 |
)
|
23 |
|
24 |
class SentimentCache:
|
25 |
-
"""
|
26 |
latest_id: int = 0
|
27 |
latest_result: dict = {}
|
28 |
|
29 |
@classmethod
|
30 |
def _hash(cls, text: str) -> str:
|
31 |
-
"""Hash text to
|
32 |
return hashlib.sha256(text.encode()).hexdigest()
|
33 |
|
34 |
@classmethod
|
35 |
@lru_cache(maxsize=128)
|
36 |
def _analyze(cls, text: str):
|
37 |
-
"""
|
38 |
return _sentiment(text)[0]
|
39 |
|
40 |
@classmethod
|
41 |
def compute(cls, text: str):
|
42 |
-
"""
|
43 |
-
|
44 |
cls.latest_id += 1
|
45 |
cls.latest_result = {
|
46 |
"text": text,
|
47 |
-
|
|
|
48 |
}
|
49 |
-
logging.info("
|
|
|
1 |
"""
|
2 |
+
Sentiment analysis module using Hugging Face Transformers with cache redirection for HF Spaces.
|
|
|
|
|
3 |
"""
|
4 |
|
5 |
+
import os
|
6 |
from transformers import pipeline
|
7 |
from functools import lru_cache
|
8 |
import hashlib
|
9 |
import logging
|
|
|
10 |
|
11 |
+
# π§ Redirect HF cache to writable /tmp directory (important for Hugging Face Spaces)
|
|
|
12 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
|
13 |
os.makedirs("/tmp/huggingface", exist_ok=True)
|
14 |
|
15 |
+
# π§ Load sentiment model
|
16 |
_sentiment = pipeline(
|
17 |
"sentiment-analysis",
|
18 |
model="distilbert-base-uncased-finetuned-sst-2-english"
|
19 |
)
|
20 |
|
21 |
class SentimentCache:
|
22 |
+
"""Handles in-memory caching and streaming of sentiment results."""
|
23 |
latest_id: int = 0
|
24 |
latest_result: dict = {}
|
25 |
|
26 |
@classmethod
|
27 |
def _hash(cls, text: str) -> str:
|
28 |
+
"""Hash input text to use as a cache key."""
|
29 |
return hashlib.sha256(text.encode()).hexdigest()
|
30 |
|
31 |
@classmethod
|
32 |
@lru_cache(maxsize=128)
|
33 |
def _analyze(cls, text: str):
|
34 |
+
"""Run inference on text, cached for performance."""
|
35 |
return _sentiment(text)[0]
|
36 |
|
37 |
@classmethod
|
38 |
def compute(cls, text: str):
|
39 |
+
"""Trigger inference and update latest result."""
|
40 |
+
result = cls._analyze(text)
|
41 |
cls.latest_id += 1
|
42 |
cls.latest_result = {
|
43 |
"text": text,
|
44 |
+
"label": result["label"],
|
45 |
+
"score": round(result["score"], 4)
|
46 |
}
|
47 |
+
logging.info("β
Sentiment computed: %s", cls.latest_result)
|