parth parekh
commited on
Commit
·
a404f18
1
Parent(s):
0dc96d7
added bert again better outputs
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
import torch
|
4 |
-
from transformers import
|
5 |
from torch.nn.functional import softmax
|
6 |
|
7 |
app = FastAPI(
|
@@ -14,8 +14,8 @@ app = FastAPI(
|
|
14 |
class ContactDetector:
|
15 |
def __init__(self):
|
16 |
cache_dir = "/app/model_cache"
|
17 |
-
self.tokenizer =
|
18 |
-
self.model =
|
19 |
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
20 |
self.model.to(self.device)
|
21 |
self.model.eval()
|
@@ -27,7 +27,7 @@ class ContactDetector:
|
|
27 |
probabilities = softmax(outputs.logits, dim=1)
|
28 |
return probabilities[0][1].item() # Probability of contact info
|
29 |
|
30 |
-
def is_contact_info(self, text, threshold=0.
|
31 |
return self.detect_contact_info(text) > threshold
|
32 |
|
33 |
detector = ContactDetector()
|
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
import torch
|
4 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
5 |
from torch.nn.functional import softmax
|
6 |
|
7 |
app = FastAPI(
|
|
|
14 |
class ContactDetector:
|
15 |
def __init__(self):
|
16 |
cache_dir = "/app/model_cache"
|
17 |
+
self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', cache_dir=cache_dir)
|
18 |
+
self.model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2, cache_dir=cache_dir)
|
19 |
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
20 |
self.model.to(self.device)
|
21 |
self.model.eval()
|
|
|
27 |
probabilities = softmax(outputs.logits, dim=1)
|
28 |
return probabilities[0][1].item() # Probability of contact info
|
29 |
|
30 |
+
def is_contact_info(self, text, threshold=0.45):
|
31 |
return self.detect_contact_info(text) > threshold
|
32 |
|
33 |
detector = ContactDetector()
|