Spaces:
Sleeping
Sleeping
Add ReportMessagesModel
Browse files- app.py +22 -4
- models/models.py +14 -5
app.py
CHANGED
|
@@ -4,8 +4,11 @@ import sys
|
|
| 4 |
import re
|
| 5 |
|
| 6 |
import httpx
|
| 7 |
-
|
|
|
|
| 8 |
from fastapi.responses import JSONResponse, FileResponse
|
|
|
|
|
|
|
| 9 |
from transformers import pipeline
|
| 10 |
from phishing_datasets import submit_entry
|
| 11 |
from url_tools import extract_urls, resolve_short_url, extract_domain_from_url
|
|
@@ -13,7 +16,7 @@ from urlscan_client import UrlscanClient
|
|
| 13 |
import requests
|
| 14 |
from mnemonic_attack import find_confusable_brand
|
| 15 |
|
| 16 |
-
from models.models import MessageModel, QueryModel, AppModel, InputModel, OutputModel,
|
| 17 |
from models.enums import ActionModel, SubActionModel
|
| 18 |
|
| 19 |
app = FastAPI()
|
|
@@ -168,5 +171,20 @@ def predict(model: InputModel) -> OutputModel:
|
|
| 168 |
return OutputModel(action=action, sub_action=SubActionModel.NONE)
|
| 169 |
|
| 170 |
@app.post("/report")
|
| 171 |
-
def report(model:
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import re
|
| 5 |
|
| 6 |
import httpx
|
| 7 |
+
|
| 8 |
+
from fastapi import FastAPI, Request, status
|
| 9 |
from fastapi.responses import JSONResponse, FileResponse
|
| 10 |
+
from fastapi.exceptions import RequestValidationError
|
| 11 |
+
|
| 12 |
from transformers import pipeline
|
| 13 |
from phishing_datasets import submit_entry
|
| 14 |
from url_tools import extract_urls, resolve_short_url, extract_domain_from_url
|
|
|
|
| 16 |
import requests
|
| 17 |
from mnemonic_attack import find_confusable_brand
|
| 18 |
|
| 19 |
+
from models.models import MessageModel, QueryModel, AppModel, InputModel, OutputModel, ReportMessagesModel, ReportInputModel
|
| 20 |
from models.enums import ActionModel, SubActionModel
|
| 21 |
|
| 22 |
app = FastAPI()
|
|
|
|
| 171 |
return OutputModel(action=action, sub_action=SubActionModel.NONE)
|
| 172 |
|
| 173 |
@app.post("/report")
|
| 174 |
+
def report(model: ReportInputModel):
|
| 175 |
+
logging.info(f"[REPORT] {model.classification.messages}")
|
| 176 |
+
|
| 177 |
+
for msg in model.classification.messages:
|
| 178 |
+
logging.info(
|
| 179 |
+
f"[REPORT] {msg.timestamp=} {msg.sender=} {msg.message=}"
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
return {"status": "received", "count": len(model.classification.messages)}
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
@app.exception_handler(RequestValidationError)
|
| 186 |
+
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
| 187 |
+
exc_str = f'{exc}'.replace('\n', ' ').replace(' ', ' ')
|
| 188 |
+
logging.error(f"{request}: {exc_str}")
|
| 189 |
+
content = {'status_code': 10422, 'message': exc_str, 'data': None}
|
| 190 |
+
return JSONResponse(content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
|
models/models.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from pydantic import BaseModel
|
| 2 |
-
from
|
| 3 |
|
| 4 |
class MessageModel(BaseModel):
|
| 5 |
text: str
|
|
@@ -17,9 +17,18 @@ class InputModel(BaseModel):
|
|
| 17 |
app: AppModel
|
| 18 |
|
| 19 |
class OutputModel(BaseModel):
|
| 20 |
-
action:
|
| 21 |
-
sub_action:
|
| 22 |
|
| 23 |
-
class
|
|
|
|
| 24 |
sender: str
|
| 25 |
-
message: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from pydantic import BaseModel
|
| 2 |
+
from models.enums import ActionModel, SubActionModel
|
| 3 |
|
| 4 |
class MessageModel(BaseModel):
|
| 5 |
text: str
|
|
|
|
| 17 |
app: AppModel
|
| 18 |
|
| 19 |
class OutputModel(BaseModel):
|
| 20 |
+
action: ActionModel
|
| 21 |
+
sub_action: SubActionModel
|
| 22 |
|
| 23 |
+
class ReportMessageModel(BaseModel):
|
| 24 |
+
timestamp: int
|
| 25 |
sender: str
|
| 26 |
+
message: str
|
| 27 |
+
|
| 28 |
+
class ReportMessagesModel(BaseModel):
|
| 29 |
+
messages: list[ReportMessageModel]
|
| 30 |
+
|
| 31 |
+
class ReportInputModel(BaseModel):
|
| 32 |
+
_version: int
|
| 33 |
+
classification: ReportMessagesModel
|
| 34 |
+
app: AppModel
|