Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,20 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import torch
|
|
|
|
| 3 |
from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
print("Loading tokenizer and model from
|
|
|
|
| 10 |
|
| 11 |
-
# Load tokenizer & model from
|
| 12 |
-
tokenizer = DistilBertTokenizerFast.from_pretrained(
|
| 13 |
-
model = DistilBertForSequenceClassification.from_pretrained(
|
| 14 |
model.eval()
|
| 15 |
|
| 16 |
@app.route("/predict", methods=["POST"])
|
|
@@ -20,6 +24,7 @@ def predict():
|
|
| 20 |
results = []
|
| 21 |
|
| 22 |
for item in data:
|
|
|
|
| 23 |
input_text = f"{item['category']} - {item['subcategory']} in {item['area']}. {item.get('comments', '')}"
|
| 24 |
inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
|
| 25 |
|
|
@@ -30,6 +35,7 @@ def predict():
|
|
| 30 |
results.append({"priority_score": predicted_class})
|
| 31 |
|
| 32 |
return jsonify(results)
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
return jsonify({"error": str(e)}), 500
|
| 35 |
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import torch
|
| 3 |
+
import os
|
| 4 |
from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
# Hugging Face repo and token
|
| 9 |
+
REPO = "aaronmrls/distilBERT-maintenance-priority-scorer"
|
| 10 |
+
HF_TOKEN = os.environ.get("HUGGINGFACE_TOKEN") # optional (only needed if repo is private)
|
| 11 |
|
| 12 |
+
print("Loading tokenizer and model from Hugging Face Hub...")
|
| 13 |
+
print("Using Hugging Face token:", "Yes" if HF_TOKEN else "No")
|
| 14 |
|
| 15 |
+
# Load tokenizer & model directly from Hub
|
| 16 |
+
tokenizer = DistilBertTokenizerFast.from_pretrained(REPO, use_auth_token=HF_TOKEN)
|
| 17 |
+
model = DistilBertForSequenceClassification.from_pretrained(REPO, use_auth_token=HF_TOKEN)
|
| 18 |
model.eval()
|
| 19 |
|
| 20 |
@app.route("/predict", methods=["POST"])
|
|
|
|
| 24 |
results = []
|
| 25 |
|
| 26 |
for item in data:
|
| 27 |
+
# Build input text (you can adjust formatting if needed)
|
| 28 |
input_text = f"{item['category']} - {item['subcategory']} in {item['area']}. {item.get('comments', '')}"
|
| 29 |
inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
|
| 30 |
|
|
|
|
| 35 |
results.append({"priority_score": predicted_class})
|
| 36 |
|
| 37 |
return jsonify(results)
|
| 38 |
+
|
| 39 |
except Exception as e:
|
| 40 |
return jsonify({"error": str(e)}), 500
|
| 41 |
|