-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
55 lines (42 loc) · 1.36 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
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel
# NLU.
from backend.intent_classifier import classify_user_input
from backend.agent import response_handler
app = FastAPI()
templates = Jinja2Templates(directory="frontend/templates")
app.mount("/static", StaticFiles(directory="frontend/static"), name="static")
@app.get("/")
def homepage(request: Request):
return templates.TemplateResponse("homepage.html", context={
"request": request
})
class Chat(BaseModel):
"""
Validating the POST sent from client.
"""
user_input: str
@app.post("/chat")
def chat_with_bot(chat: Chat):
"""
Generates Bot Responses.
"""
user_input = chat.user_input
user_input_tokens, intent_detected, confidence, unk_percent = classify_user_input(user_input=user_input)
response = response_handler(intent_detected=intent_detected,
user_input_tokens=user_input_tokens,
confidence=confidence,
unk_percent=unk_percent)
return {
"bot_response": response
}
@app.get("/about")
def about(request: Request):
"""
Displays About page.
"""
return templates.TemplateResponse("about.html", {
"request": request
})