Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ from fastapi import FastAPI
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from transformers import BertTokenizer, BertForSequenceClassification
|
| 4 |
import torch
|
|
|
|
| 5 |
|
| 6 |
# --- Configuration ---
|
| 7 |
MODEL_PATH = "./BERT_Bullying_Detector_Model"
|
|
@@ -34,7 +35,7 @@ def read_root():
|
|
| 34 |
def predict_toxicity(input_data: TextInput):
|
| 35 |
if not model or not tokenizer:
|
| 36 |
return {"error": "Model not loaded."}
|
| 37 |
-
|
| 38 |
text = input_data.text
|
| 39 |
try:
|
| 40 |
encoding = tokenizer.encode_plus(
|
|
@@ -46,21 +47,21 @@ def predict_toxicity(input_data: TextInput):
|
|
| 46 |
return_tensors='pt',
|
| 47 |
truncation=True
|
| 48 |
)
|
| 49 |
-
|
| 50 |
input_ids = encoding['input_ids'].to(device)
|
| 51 |
attention_mask = encoding['attention_mask'].to(device)
|
| 52 |
|
| 53 |
with torch.no_grad():
|
| 54 |
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 55 |
_, prediction = torch.max(outputs.logits, dim=1)
|
| 56 |
-
|
| 57 |
label_map = {0: "Not Bullying", 1: "Bullying"}
|
| 58 |
result_label = label_map[prediction.item()]
|
| 59 |
-
|
| 60 |
return {"label": result_label, "score": prediction.item()}
|
| 61 |
except Exception as e:
|
| 62 |
return {"error": str(e)}
|
| 63 |
-
import uvicorn
|
| 64 |
|
|
|
|
| 65 |
if __name__ == "__main__":
|
| 66 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from transformers import BertTokenizer, BertForSequenceClassification
|
| 4 |
import torch
|
| 5 |
+
import uvicorn # <--- THIS IS THE FIX
|
| 6 |
|
| 7 |
# --- Configuration ---
|
| 8 |
MODEL_PATH = "./BERT_Bullying_Detector_Model"
|
|
|
|
| 35 |
def predict_toxicity(input_data: TextInput):
|
| 36 |
if not model or not tokenizer:
|
| 37 |
return {"error": "Model not loaded."}
|
| 38 |
+
|
| 39 |
text = input_data.text
|
| 40 |
try:
|
| 41 |
encoding = tokenizer.encode_plus(
|
|
|
|
| 47 |
return_tensors='pt',
|
| 48 |
truncation=True
|
| 49 |
)
|
| 50 |
+
|
| 51 |
input_ids = encoding['input_ids'].to(device)
|
| 52 |
attention_mask = encoding['attention_mask'].to(device)
|
| 53 |
|
| 54 |
with torch.no_grad():
|
| 55 |
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 56 |
_, prediction = torch.max(outputs.logits, dim=1)
|
| 57 |
+
|
| 58 |
label_map = {0: "Not Bullying", 1: "Bullying"}
|
| 59 |
result_label = label_map[prediction.item()]
|
| 60 |
+
|
| 61 |
return {"label": result_label, "score": prediction.item()}
|
| 62 |
except Exception as e:
|
| 63 |
return {"error": str(e)}
|
|
|
|
| 64 |
|
| 65 |
+
# --- Start the Server ---
|
| 66 |
if __name__ == "__main__":
|
| 67 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|