File size: 2,049 Bytes
2edf61b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from chat import chat_conversations
from database_functions import create_new_session,save_mood_summary
import spacy
import os
import time
from config import settings
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
app = FastAPI()

templates = Jinja2Templates(directory="templates")

@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request):
    return templates.TemplateResponse("chatwidget.html", {"request": request})

@app.get("/chat", response_class=HTMLResponse)
async def read_root(request: Request):
    return templates.TemplateResponse("chatwidget.html", {"request": request})

@app.post("/chatwidget", response_class=HTMLResponse)
async def read_root(request: Request):
    start = time.time_ns()
    form_data = await request.json()
    query = form_data.get('query')
    user_id = form_data.get("user_id")
    response_text = chat_conversations(query,user_id)
    print("message is send after : ",time.time()- start)
    return response_text

@app.post("/start-session")
async def start_session(request: Request):
    start = time.time_ns()
    form_data = await request.json()
    # print("form data",form_data)
    user_id = form_data.get("user_id")
    # print("save user id ",user_id)
    response = create_new_session(user_id)
    print("session  is created after : ",time.time()- start)
    return response

@app.post("/mood-summary")
async def mood_summary(request:Request):
    form_data = await request.json()
    user_id = form_data.get("user_id")
    save_mood_summary(form_data,user_id)
    return "mood saved successfully..."

@app.get('/delete-history')
async def delete_history(request:Request):
    collection_list = settings.mongodb_db.list_collection_names()
    # Drop each collection
    for collection_name in collection_list:
      settings.mongodb_db[collection_name].drop()
    
    return 'history is cleared'