Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,58 @@
|
|
| 1 |
-
from fastapi import FastAPI, Request
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
-
from fastapi.templating import Jinja2Templates
|
| 4 |
-
from chat import chat_conversations
|
| 5 |
-
from database_functions import create_new_session,save_mood_summary
|
| 6 |
-
import spacy
|
| 7 |
-
import os
|
| 8 |
-
import time
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from fastapi.templating import Jinja2Templates
|
| 4 |
+
from chat import chat_conversations
|
| 5 |
+
from database_functions import create_new_session,save_mood_summary
|
| 6 |
+
import spacy
|
| 7 |
+
import os
|
| 8 |
+
import time
|
| 9 |
+
from config import settings
|
| 10 |
+
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
templates = Jinja2Templates(directory="templates")
|
| 14 |
+
|
| 15 |
+
@app.get("/", response_class=HTMLResponse)
|
| 16 |
+
async def read_root(request: Request):
|
| 17 |
+
return templates.TemplateResponse("chatwidget.html", {"request": request})
|
| 18 |
+
|
| 19 |
+
@app.get("/chat", response_class=HTMLResponse)
|
| 20 |
+
async def read_root(request: Request):
|
| 21 |
+
return templates.TemplateResponse("chatwidget.html", {"request": request})
|
| 22 |
+
|
| 23 |
+
@app.post("/chatwidget", response_class=HTMLResponse)
|
| 24 |
+
async def read_root(request: Request):
|
| 25 |
+
start = time.time_ns()
|
| 26 |
+
form_data = await request.json()
|
| 27 |
+
query = form_data.get('query')
|
| 28 |
+
user_id = form_data.get("user_id")
|
| 29 |
+
response_text = chat_conversations(query,user_id)
|
| 30 |
+
print("message is send after : ",time.time()- start)
|
| 31 |
+
return response_text
|
| 32 |
+
|
| 33 |
+
@app.post("/start-session")
|
| 34 |
+
async def start_session(request: Request):
|
| 35 |
+
start = time.time_ns()
|
| 36 |
+
form_data = await request.json()
|
| 37 |
+
# print("form data",form_data)
|
| 38 |
+
user_id = form_data.get("user_id")
|
| 39 |
+
# print("save user id ",user_id)
|
| 40 |
+
response = create_new_session(user_id)
|
| 41 |
+
print("session is created after : ",time.time()- start)
|
| 42 |
+
return response
|
| 43 |
+
|
| 44 |
+
@app.post("/mood-summary")
|
| 45 |
+
async def mood_summary(request:Request):
|
| 46 |
+
form_data = await request.json()
|
| 47 |
+
user_id = form_data.get("user_id")
|
| 48 |
+
save_mood_summary(form_data,user_id)
|
| 49 |
+
return "mood saved successfully..."
|
| 50 |
+
|
| 51 |
+
@app.get('/delete-history')
|
| 52 |
+
async def delete_history(request:Request):
|
| 53 |
+
collection_list = settings.mongodb_db.list_collection_names()
|
| 54 |
+
# Drop each collection
|
| 55 |
+
for collection_name in collection_list:
|
| 56 |
+
settings.mongodb_db[collection_name].drop()
|
| 57 |
+
|
| 58 |
+
return 'history is cleared'
|