Spaces:
Sleeping
Sleeping
from fastapi import APIRouter , Request ,HTTPException , WebSocket | |
from controllers import websocket_controller as wc | |
from controllers import ws_controller as w | |
from controllers import ws_controller_1 as w1 | |
from controllers import ner_ai_controller as ai | |
from services.chat_client_NER import ChatClient | |
import logging | |
import aiohttp | |
import time | |
from models import supabase_models as sp | |
router = APIRouter(prefix="/websockets") | |
#-----New Code -----# | |
# @router.websocket("/ws") | |
# async def get_data(websocket:WebSocket): | |
# await websocket.accept() | |
# # -------------#-------------#-------------- | |
# try: | |
# json = await websocket.receive_text() | |
# json = js.loads(json) | |
# apitoken = json['supabase_authorisation'] | |
# result = sp.AuthUser_Validator(apitoken) | |
# print(result) | |
# if result == "Exceptional error": | |
# await websocket.send_text("user unautheticated!") | |
# await websocket.close() | |
# else: | |
# start = time.time() | |
# # try: | |
# # # json = await websocket.receive_text() | |
# # # json = js.loads(json) | |
# # json = await websocket.receive_json() | |
# # except Exception as e: | |
# # logging.info(f"Error {e}") | |
# # print(f"Error {e}") | |
# # await websocket.send_text(f"There is some error !!! {e}") | |
# # return | |
# access_token = json['access_token'] | |
# logging.info(f"access_token:{access_token}") | |
# user_query = json['brand_name'] if json.get('brand_name') is not None else None | |
# logging.info(f"brand_name: {user_query}") | |
# if access_token is None: | |
# await websocket.send_text("Access Token Invalid OR NULL !!!") | |
# websocket.close() | |
# # access_token = await websocket.receive_text() | |
# brand_name = "" | |
# logging.info(f"brand_name: f{user_query}") | |
# logging.info(f"access_token : {access_token}") | |
# if user_query is not None: | |
# chat = ChatClient().create(conversation=[]) | |
# response = chat.send_message(content=f"{user_query}", stream=False) | |
# if response.text == 'others': | |
# brand_name = None | |
# else: | |
# brand_name = response.text | |
# await w.websocket_main(access_token ,websocket,start,brand_name) | |
# except HTTPException as e: | |
# print("Inside exception") | |
# await websocket.send_text(f"user unautheticated!{e.detail}") | |
#-----#---- | |
async def get_data(websocket:WebSocket): | |
await websocket.accept() | |
#-------------#-------------#-------------- | |
# try: | |
# result = AuthUser_Validator(apitoken) | |
# if result == "Exceptional error": | |
# await websocket.send_text("user unautheticated!") | |
# else: | |
# start = time.time() | |
# try: | |
# # json = await websocket.receive_text() | |
# # json = js.loads(json) | |
# json = await websocket.receive_json() | |
# except Exception as e: | |
# logging.info(f"Error {e}") | |
# print(f"Error {e}") | |
# await websocket.send_text(f"There is some error !!! {e}") | |
# return | |
# access_token = json['access_token'] | |
# logging.info(f"access_token:{access_token}") | |
# user_query = json['brand_name'] if json.get('brand_name') is not None else None | |
# logging.info(f"brand_name: {user_query}") | |
# if access_token is None: | |
# await websocket.send_text("Access Token Invalid OR NULL !!!") | |
# websocket.close() | |
# # access_token = await websocket.receive_text() | |
# brand_name = "" | |
# logging.info(f"brand_name: f{user_query}") | |
# logging.info(f"access_token : {access_token}") | |
# if user_query is not None: | |
# chat = ChatClient().create(conversation=[]) | |
# response = chat.send_message(content=f"{user_query}", stream=False) | |
# if response.text == 'others': | |
# brand_name = None | |
# else: | |
# brand_name = response.text | |
# await w.websocket_main(access_token ,websocket,start,brand_name) | |
# except HTTPException as e: | |
# print("Inside exception") | |
# await websocket.send_text(f"user unautheticated!{e.detail}") | |
#----------------#----------#------------ | |
start = time.time() | |
try: | |
#json = await websocket.receive_text() | |
#json = js.loads(json) | |
json = await websocket.receive_json() | |
except Exception as e: | |
logging.info(f"Error {e}") | |
print(f"Error {e}") | |
await websocket.send_text(f"There is some error !!! {e}") | |
return | |
access_token = json['access_token'] | |
logging.info(f"access_token:{access_token}") | |
user_query = json['brand_name'] if json.get('brand_name') is not None else None | |
logging.info(f"brand_name: {user_query}") | |
if access_token is None: | |
await websocket.send_text("Access Token Invalid OR NULL !!!") | |
websocket.close() | |
# access_token = await websocket.receive_text() | |
brand_name = "" | |
logging.info(f"brand_name: f{user_query}") | |
logging.info(f"access_token : {access_token}") | |
if user_query is not None: | |
chat = ChatClient().create(conversation=[]) | |
response = chat.send_message(content=f"{user_query}", stream=False) | |
if response.text == 'others': | |
brand_name = None | |
else: | |
brand_name = response.text | |
await w.websocket_main(access_token ,websocket,start,brand_name) | |
# await w1.websocket_main(access_token , websocket , start , brand_name) | |