HarshSanghavi's picture
Upload app.py
2edf61b verified
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'