File size: 1,255 Bytes
c3f7401 5e04aff 7f7ea13 2af9c28 c3f7401 14131be c3f7401 7f7ea13 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, PlainTextResponse
from fastapi.templating import Jinja2Templates
import uvicorn
import subprocess
PACKAGES = ["transformers", "accelerate", "torch"]
for package in PACKAGES:
subprocess.run(["pip3", "install", package], check=True)
from transformers import pipeline
app = FastAPI()
templates = Jinja2Templates(directory="")
@app.get("/", response_class=HTMLResponse)
async def read_item(request: Request):
return templates.TemplateResponse("index.html", context={'request': request})
@app.get("/{content}", response_class=PlainTextResponse)
async def read_item(request: Request, content: str):
return analyze_output(content)
@app.post("/{content}", response_class=PlainTextResponse)
async def read_item(request: Request, content: str):
return analyze_output(content)
def analyze_output(input: str, pipe = pipeline("text-classification", model="Titeiiko/OTIS-Official-Spam-Model")):
x = pipe(input)[0]
if x["label"] == "LABEL_0":
return str({"type":"Not Spam", "probability":x["score"]})
else:
return str({"type":"Spam", "probability":x["score"]})
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) |