-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
92 lines (73 loc) · 2.42 KB
/
main.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from fastapi import FastAPI, HTTPException, Request, APIRouter
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import uvicorn
from pydantic import BaseModel
from api.SmartBookmark import evaluateText, evaluateUrl, labels
from starlette.middleware.cors import CORSMiddleware
app = FastAPI(
title="HypeLinks API: A Smart Bookmark API",
version="1.0.0-beta03",
description="Classifies the sites into various different categories",
)
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://127.0.0.1:8000",
"http://0.0.0.0:8000",
"http://localhost",
"http://localhost:8000",
"http://localhost:3000",
"http://localhost:0000",
"http://smart-bookmark-api.iamyajat.co",
"https://smart-bookmark-api.iamyajat.co",
"http://smart-bookmark-api.azurewebsites.net",
"https://smart-bookmark-api.azurewebsites.net",
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["Access-Control-Allow-Origin", "*"],
)
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
@app.get("/")
async def index(request: Request):
return templates.TemplateResponse("homepage.html", {"request": request, "id": 1})
@app.get("/status")
async def index():
return {"status": "online"}
@app.get("/list")
async def index():
return {
"categories": [{"category_id": k, "category": v} for k, v in labels.items()]
}
class InputUrl(BaseModel):
url: str
class InputText(BaseModel):
text: str
@app.post("/classify/url")
async def get_item(input: InputUrl):
try:
title, test_preds, cat_id = evaluateUrl(input.url)
return {
"url": input.url,
"title": title[0],
"category": test_preds,
"category_id": int(cat_id),
}
except:
raise HTTPException(status_code=400, detail="Enter a valid URL!")
@app.post("/classify/text")
async def get_item(input: InputText):
try:
title, test_preds, cat_id = evaluateText([input.text])
return {
"title": title[0],
"category": test_preds,
"category_id": int(cat_id),
}
except:
raise HTTPException(status_code=400, detail="Error")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=5000)