Spaces:
Sleeping
Sleeping
Commit
·
b729f12
1
Parent(s):
5fca79f
Fix Hugging Face cache issue and add NER endpoint
Browse files- Dockerfile +2 -0
- app.py +27 -14
Dockerfile
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
FROM python:3.10-slim
|
|
|
2 |
|
3 |
# Working directory
|
4 |
WORKDIR /app
|
@@ -14,3 +15,4 @@ EXPOSE 7860
|
|
14 |
|
15 |
# Command to run FastAPI app with uvicorn
|
16 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
1 |
FROM python:3.10-slim
|
2 |
+
RUN mkdir -p /tmp/huggingface
|
3 |
|
4 |
# Working directory
|
5 |
WORKDIR /app
|
|
|
15 |
|
16 |
# Command to run FastAPI app with uvicorn
|
17 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
18 |
+
|
app.py
CHANGED
@@ -2,16 +2,21 @@ from fastapi import FastAPI, HTTPException
|
|
2 |
from pydantic import BaseModel
|
3 |
from transformers import pipeline
|
4 |
import logging
|
|
|
5 |
|
6 |
logging.basicConfig(level=logging.INFO)
|
7 |
logger = logging.getLogger(__name__)
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
-
#
|
|
|
|
|
|
|
12 |
try:
|
13 |
ner_model = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
|
14 |
sentiment_model = pipeline("sentiment-analysis", model="ProsusAI/finbert")
|
|
|
15 |
except Exception as e:
|
16 |
logger.error(f"Model loading failed: {e}")
|
17 |
ner_model = None
|
@@ -28,25 +33,33 @@ def home():
|
|
28 |
def analyze_sentiment(req: TextRequest):
|
29 |
if not sentiment_model:
|
30 |
raise HTTPException(status_code=503, detail="Sentiment model not available")
|
31 |
-
text = req.text
|
32 |
if not text:
|
33 |
raise HTTPException(status_code=400, detail="Text cannot be empty")
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
39 |
|
40 |
@app.post("/ner")
|
41 |
def analyze_ner(req: TextRequest):
|
42 |
if not ner_model:
|
43 |
raise HTTPException(status_code=503, detail="NER model not available")
|
44 |
-
text = req.text
|
45 |
if not text:
|
46 |
raise HTTPException(status_code=400, detail="Text cannot be empty")
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
2 |
from pydantic import BaseModel
|
3 |
from transformers import pipeline
|
4 |
import logging
|
5 |
+
import os
|
6 |
|
7 |
logging.basicConfig(level=logging.INFO)
|
8 |
logger = logging.getLogger(__name__)
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
12 |
+
# Set transformers cache directory to a writable location to fix permission errors
|
13 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
|
14 |
+
|
15 |
+
# Load models once on startup with error handling
|
16 |
try:
|
17 |
ner_model = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
|
18 |
sentiment_model = pipeline("sentiment-analysis", model="ProsusAI/finbert")
|
19 |
+
logger.info("Models loaded successfully.")
|
20 |
except Exception as e:
|
21 |
logger.error(f"Model loading failed: {e}")
|
22 |
ner_model = None
|
|
|
33 |
def analyze_sentiment(req: TextRequest):
|
34 |
if not sentiment_model:
|
35 |
raise HTTPException(status_code=503, detail="Sentiment model not available")
|
36 |
+
text = req.text.strip()
|
37 |
if not text:
|
38 |
raise HTTPException(status_code=400, detail="Text cannot be empty")
|
39 |
+
try:
|
40 |
+
result = sentiment_model(text[:512])[0]
|
41 |
+
return {
|
42 |
+
"label": result["label"],
|
43 |
+
"score": round(result["score"] * 100, 2)
|
44 |
+
}
|
45 |
+
except Exception as e:
|
46 |
+
logger.error(f"Sentiment analysis error: {e}")
|
47 |
+
raise HTTPException(status_code=500, detail="Sentiment analysis failed")
|
48 |
|
49 |
@app.post("/ner")
|
50 |
def analyze_ner(req: TextRequest):
|
51 |
if not ner_model:
|
52 |
raise HTTPException(status_code=503, detail="NER model not available")
|
53 |
+
text = req.text.strip()
|
54 |
if not text:
|
55 |
raise HTTPException(status_code=400, detail="Text cannot be empty")
|
56 |
+
try:
|
57 |
+
entities = ner_model(text[:512])
|
58 |
+
# Filter relevant entity groups
|
59 |
+
relevant = [e['word'] for e in entities if e.get('entity_group') in ['ORG', 'PERSON', 'MISC', 'PRODUCT', 'GPE']]
|
60 |
+
# Remove duplicates preserving order, limit to 5 entities
|
61 |
+
unique_entities = list(dict.fromkeys(relevant))[:5]
|
62 |
+
return {"entities": unique_entities}
|
63 |
+
except Exception as e:
|
64 |
+
logger.error(f"NER analysis error: {e}")
|
65 |
+
raise HTTPException(status_code=500, detail="NER analysis failed")
|