Spaces:
Sleeping
Sleeping
Commit ·
2d8d612
1
Parent(s): d9c89fb
Final production optimizations: pre-download BERT and explicit model status detection
Browse files- Dockerfile +5 -0
- hf_api.py +12 -0
Dockerfile
CHANGED
|
@@ -20,6 +20,11 @@ COPY hf_requirements.txt .
|
|
| 20 |
RUN pip install --upgrade pip && \
|
| 21 |
pip install -r hf_requirements.txt
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Copy the rest of the application code
|
| 24 |
COPY . .
|
| 25 |
|
|
|
|
| 20 |
RUN pip install --upgrade pip && \
|
| 21 |
pip install -r hf_requirements.txt
|
| 22 |
|
| 23 |
+
# Pre-download the BERT base model during build to speed up startup
|
| 24 |
+
RUN python -c "from transformers import BertTokenizer, BertForSequenceClassification; \
|
| 25 |
+
BertTokenizer.from_pretrained('bert-base-uncased'); \
|
| 26 |
+
BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)"
|
| 27 |
+
|
| 28 |
# Copy the rest of the application code
|
| 29 |
COPY . .
|
| 30 |
|
hf_api.py
CHANGED
|
@@ -68,6 +68,10 @@ def analyze(request: AnalysisRequest):
|
|
| 68 |
if not content:
|
| 69 |
return {"error": "Could not retrieve content for this article."}
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
if request.action == "get_summary":
|
| 72 |
summary = service.summarize_content(content[:3000])
|
| 73 |
return {
|
|
@@ -86,6 +90,14 @@ def analyze(request: AnalysisRequest):
|
|
| 86 |
# 2. Analyze sentences in a single batch
|
| 87 |
batch_results = service.rate_bias_batch(analysis_sentences)
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
# 3. Get Summary
|
| 90 |
summary = service.summarize_content(content)
|
| 91 |
|
|
|
|
| 68 |
if not content:
|
| 69 |
return {"error": "Could not retrieve content for this article."}
|
| 70 |
|
| 71 |
+
# Ensure model is loaded (pre-load should have handled this, but be safe)
|
| 72 |
+
if not service.bias_model:
|
| 73 |
+
service.load_local_bias_model()
|
| 74 |
+
|
| 75 |
if request.action == "get_summary":
|
| 76 |
summary = service.summarize_content(content[:3000])
|
| 77 |
return {
|
|
|
|
| 90 |
# 2. Analyze sentences in a single batch
|
| 91 |
batch_results = service.rate_bias_batch(analysis_sentences)
|
| 92 |
|
| 93 |
+
# Check if all results are "Offline" or "Error"
|
| 94 |
+
is_offline = all(res.get("label") in ["Offline", "Error"] for res in batch_results)
|
| 95 |
+
if is_offline:
|
| 96 |
+
return {
|
| 97 |
+
"error": "The bias detection model is currently offline or failing to load.",
|
| 98 |
+
"details": batch_results[0].get("reasoning", "Unknown error")
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
# 3. Get Summary
|
| 102 |
summary = service.summarize_content(content)
|
| 103 |
|