Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
+
from fastapi.responses import HTMLResponse, PlainTextResponse
|
3 |
+
from fastapi.staticfiles import StaticFiles
|
4 |
+
from fastapi.templating import Jinja2Templates
|
5 |
+
import uvicorn
|
6 |
+
from transformers import pipeline
|
7 |
+
|
8 |
+
|
9 |
+
app = FastAPI()
|
10 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
11 |
+
templates = Jinja2Templates(directory="templates")
|
12 |
+
|
13 |
+
@app.get("/", response_class=HTMLResponse)
|
14 |
+
async def read_item(request: Request):
|
15 |
+
return templates.TemplateResponse("index.html", context={'request': request})
|
16 |
+
|
17 |
+
@app.get("/{content}", response_class=PlainTextResponse)
|
18 |
+
async def read_item(request: Request, content: str):
|
19 |
+
return analyze_output(content)
|
20 |
+
|
21 |
+
@app.post("/{content}", response_class=PlainTextResponse)
|
22 |
+
async def read_item(request: Request, content: str):
|
23 |
+
return analyze_output(content)
|
24 |
+
|
25 |
+
def analyze_output(input: str, pipe = pipeline("text-classification", model="Titeiiko/OTIS-Official-Spam-Model")):
|
26 |
+
x = pipe(input)[0]
|
27 |
+
if x["label"] == "LABEL_0":
|
28 |
+
return str({"type":"Not Spam", "probability":x["score"]})
|
29 |
+
else:
|
30 |
+
return str({"type":"Spam", "probability":x["score"]})
|
31 |
+
|
32 |
+
if __name__ == "__main__":
|
33 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|