File size: 1,535 Bytes
d1a66a2
 
06f7537
2f1cacb
10f0001
 
d1a66a2
edb525a
d1a66a2
06f7537
d1a66a2
06f7537
d1a66a2
 
027633d
 
 
0651c83
027633d
0f71d0f
5eafbee
027633d
edb525a
5f37b62
2f1cacb
 
e9d6536
 
 
1a6f950
2f1cacb
 
d1a66a2
2f1cacb
 
 
 
 
 
 
 
 
 
6ba6817
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
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

router = APIRouter(prefix="/websockets")

@router.websocket("/ws")
async def get_data(websocket:WebSocket):
    await websocket.accept()
    try:
        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,brand_name)