File size: 5,943 Bytes
d1a66a2
 
06f7537
2f1cacb
10f0001
d1a66a2
edb525a
b8cc90f
5bfb5e0
 
06f7537
d1a66a2
eb44025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06f7537
d1a66a2
 
5bfb5e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8cc90f
027633d
342cfd9
 
 
027633d
0651c83
027633d
0f71d0f
5eafbee
027633d
edb525a
5f37b62
2f1cacb
 
e9d6536
 
 
1a6f950
2f1cacb
 
d1a66a2
2f1cacb
 
 
 
 
 
 
 
 
 
b8cc90f
d1a66a2
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from fastapi import APIRouter , Request ,HTTPException , WebSocket
from controllers import websocket_controller as wc
from controllers import ws_controller as w
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}")
#-----#----

@router.websocket("/ws")
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)