File size: 12,085 Bytes
9680844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175d877
 
 
 
9680844
 
 
175d877
9680844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6f7c33
9680844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175d877
9680844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from operator import itemgetter
import os
from datetime import datetime
import uvicorn
from typing import Any, Optional, Tuple, Dict, TypedDict
from urllib import parse
from uuid import uuid4
import logging
from fastapi.logger import logger as fastapi_logger
import sys

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi import APIRouter, Body, Request, status
from pymongo import MongoClient
from dotenv import dotenv_values
from routes import router as api_router
from contextlib import asynccontextmanager
import requests

from typing import List
from datetime import date
from mongodb.operations.calls import *
from mongodb.operations.users import *
from mongodb.models.calls import UserCall, UpdateCall
# from mongodb.endpoints.calls import *

from transformers import AutoProcessor, SeamlessM4Tv2Model

# from seamless_communication.inference import Translator
from Client import Client
import numpy as np
import torch
import socketio

# Configure logger
gunicorn_error_logger = logging.getLogger("gunicorn.error")
gunicorn_logger = logging.getLogger("gunicorn")
uvicorn_access_logger = logging.getLogger("uvicorn.access")

gunicorn_error_logger.propagate = True
gunicorn_logger.propagate = True
uvicorn_access_logger.propagate = True

uvicorn_access_logger.handlers = gunicorn_error_logger.handlers
fastapi_logger.handlers = gunicorn_error_logger.handlers

# sio is the main socket.io entrypoint
sio = socketio.AsyncServer(
    async_mode="asgi",
    cors_allowed_origins="*",
    logger=gunicorn_logger,
    engineio_logger=gunicorn_logger,
)
# sio.logger.setLevel(logging.DEBUG)
socketio_app = socketio.ASGIApp(sio)
# app.mount("/", socketio_app)

config = dotenv_values(".env")

# Read connection string from environment vars
# uri = os.environ['MONGODB_URI']

# Read connection string from .env file
uri = config['MONGODB_URI']


# MongoDB Connection Lifespan Events
@asynccontextmanager
async def lifespan(app: FastAPI):
    # startup logic
    app.mongodb_client = MongoClient(uri)
    app.database = app.mongodb_client['IT-Cluster1'] #connect to interpretalk primary db
    try:
        app.mongodb_client.admin.command('ping')
        print("MongoDB Connection Established...")
    except Exception as e:
        print(e)
    
    yield

    # shutdown logic
    print("Closing MongoDB Connection...")
    app.mongodb_client.close()

app = FastAPI(lifespan=lifespan, logger=gunicorn_logger)

# New CORS funcitonality
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], # configured node app port
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(api_router) # include routers for user, calls and transcripts operations

DEBUG = True

ESCAPE_HATCH_SERVER_LOCK_RELEASE_NAME = "remove_server_lock"

TARGET_SAMPLING_RATE = 16000
MAX_BYTES_BUFFER = 960_000

print("")
print("")
print("=" * 18 + " Interpretalk is starting... " + "=" * 18)

###############################################
# Configure socketio server
###############################################

# TODO PM - change this to the actual path
# seamless remnant code
CLIENT_BUILD_PATH = "../streaming-react-app/dist/"
static_files = {
    "/": CLIENT_BUILD_PATH,
    "/assets/seamless-db6a2555.svg": {
        "filename": CLIENT_BUILD_PATH + "assets/seamless-db6a2555.svg",
        "content_type": "image/svg+xml",
    },
}
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
processor = AutoProcessor.from_pretrained("facebook/seamless-m4t-v2-large")

# PM - hardcoding temporarily as my GPU doesnt have enough vram
model = SeamlessM4Tv2Model.from_pretrained("facebook/seamless-m4t-v2-large").to(device)


bytes_data = bytearray()
model_name = "seamlessM4T_v2_large"
vocoder_name = "vocoder_v2" if model_name == "seamlessM4T_v2_large" else "vocoder_36langs"

clients = {}
rooms = {}


def get_collection_users():
    return app.database["user_records"]

def get_collection_calls():
    return app.database["call_records"]


@app.get("/home/", response_description="Welcome User")
def test():
    return {"message": "Welcome to InterpreTalk!"}



async def send_translated_text(client_id, username, original_text, translated_text, room_id):
    # print(rooms) # Debugging
    # print(clients) # Debugging

    data = {
        "author_id": str(client_id),
        "author_username": str(client_id),
        "original_text": str(original_text),
        "translated_text": str(translated_text),
        "timestamp": str(datetime.now())
    }
    gunicorn_logger.info("SENDING TRANSLATED TEXT TO CLIENT")
    await sio.emit("translated_text", data, room=room_id)
    gunicorn_logger.info("SUCCESSFULLY SEND AUDIO TO FRONTEND")


@sio.on("connect")
async def connect(sid, environ):
    print(f"📥 [event: connected] sid={sid}")
    query_params = dict(parse.parse_qsl(environ["QUERY_STRING"]))

    client_id = query_params.get("client_id")
    gunicorn_logger.info(f"📥 [event: connected] sid={sid}, client_id={client_id}")

    # get username to Client Object from DB
    username = find_name_from_id(get_collection_users(), username)

    # sid = socketid, client_id = client specific ID ,always the same for same user
    clients[sid] = Client(sid, client_id, username)
    print(clients[sid].username)
    gunicorn_logger.warning(f"Client connected: {sid}")
    gunicorn_logger.warning(clients)


@sio.on("disconnect")
async def disconnect(sid):
    gunicorn_logger.debug(f"📤 [event: disconnected] sid={sid}")

    call_id = clients[sid].call_id
    user_id = clients[sid].client_id
    target_language = clients[sid].target_language

    clients.pop(sid, None)
    
    # Perform Key Term Extraction and summarisation
    try:
        # Get combined caption field for call record based on call_id
        key_terms = term_extraction(get_collection_calls(), call_id, user_id, target_language)    
        
        # Perform summarisation based on target language
        summary_result = summarise(get_collection_calls(), call_id, user_id, target_language)
    
    except:
        gunicorn_logger.error(f"📤 [event: term_extraction/summarisation request error] sid={sid}, call={call_id}")


@sio.on("target_language")
async def target_language(sid, target_lang):
    gunicorn_logger.info(f"📥 [event: target_language] sid={sid}, target_lang={target_lang}")
    clients[sid].target_language = target_lang


@sio.on("call_user")
async def call_user(sid, call_id):
    clients[sid].call_id = call_id
    gunicorn_logger.info(f"CALL {sid}: entering room {call_id}")
    rooms[call_id] = rooms.get(call_id, [])
    if sid not in rooms[call_id] and len(rooms[call_id]) < 2:
        rooms[call_id].append(sid)
        sio.enter_room(sid, call_id)
    else:
        gunicorn_logger.info(f"CALL {sid}: room {call_id} is full")
        # await sio.emit("room_full", room=call_id, to=sid)

    # BO - Get call id from dictionary created during socketio connection
    client_id = clients[sid].client_id
    
    gunicorn_logger.warning(f"NOW TRYING TO CREATE DB RECORD FOR Caller with ID: {client_id} for call: {call_id}")
    # BO -> Create Call Record with Caller and call_id field (None for callee, duration, terms..)
    request_data = {
        "call_id": str(call_id),
        "caller_id": str(client_id),
        "creation_date": str(datetime.now())
    }

    response =  create_calls(get_collection_calls(), request_data)
    print(response) # BO - print created db call record


@sio.on("audio_config")
async def audio_config(sid, sample_rate):
    clients[sid].original_sr = sample_rate


@sio.on("answer_call")
async def answer_call(sid, call_id):

    clients[sid].call_id = call_id
    gunicorn_logger.info(f"ANSWER {sid}: entering room {call_id}")
    rooms[call_id] = rooms.get(call_id, [])
    if sid not in rooms[call_id] and len(rooms[call_id]) < 2:
        rooms[call_id].append(sid)
        sio.enter_room(sid, call_id)
    else:
        gunicorn_logger.info(f"ANSWER {sid}: room {call_id} is full")
        # await sio.emit("room_full", room=call_id, to=sid)


    # BO - Get call id from dictionary created during socketio connection
    client_id = clients[sid].client_id
    
    # BO -> Update Call Record with Callee field based on call_id
    gunicorn_logger.warning(f"NOW UPDATING MongoDB RECORD FOR Caller with ID: {client_id} for call: {call_id}")
    # BO -> Create Call Record with callee_id field (None for callee, duration, terms..)
    request_data = {
        "callee_id": client_id
    }

    response =  update_calls(get_collection_calls(), call_id, request_data)
    print(response) # BO - print created db call record
    

@sio.on("incoming_audio")
async def incoming_audio(sid, data, call_id):
    try:
        clients[sid].add_bytes(data)

        if clients[sid].get_length() >= MAX_BYTES_BUFFER:
            gunicorn_logger.info('Buffer full, now outputting...')
            output_path = clients[sid].output_path
            resampled_audio = clients[sid].resample_and_clear()
            vad_result = clients[sid].vad_analyse(resampled_audio)
            # source lang is speakers tgt language 😃
            src_lang = clients[sid].target_language

            if vad_result:
                gunicorn_logger.info('Speech detected, now processing audio.....')
                tgt_sid = next(id for id in rooms[call_id] if id != sid)
                tgt_lang = clients[tgt_sid].target_language
                # following example from https://github.com/facebookresearch/seamless_communication/blob/main/docs/m4t/README.md#transformers-usage
                output_tokens = processor(audios=resampled_audio, src_lang=src_lang, return_tensors="pt", sampling_rate=TARGET_SAMPLING_RATE).to(device)
                model_output = model.generate(**output_tokens, tgt_lang=src_lang, generate_speech=False)[0].tolist()[0]
                asr_text = processor.decode(model_output, skip_special_tokens=True)
                print(f"ASR TEXT = {asr_text}")
                # ASR TEXT => ORIGINAL TEXT

                if src_lang != tgt_lang:
                    t2t_tokens = processor(text=asr_text, src_lang=src_lang, tgt_lang=tgt_lang, return_tensors="pt").to(device)
                    translated_data = model.generate(**t2t_tokens, tgt_lang=tgt_lang, generate_speech=False)[0].tolist()[0]
                    translated_text = processor.decode(translated_data, skip_special_tokens=True)
                    print(f"TRANSLATED TEXT = {translated_text}")
                else:
                    # PM - both users have same language selected, no need to translate
                    translated_text = asr_text

                # PM - text_output is a list with 1 string
                await send_translated_text(clients[sid].client_id, clients[sid].username, asr_text, translated_text, call_id)

                # BO -> send translated_text to mongodb as caption record update based on call_id
                await send_captions(clients[sid].client_id, clients[sid].username, asr_text, translated_text, call_id)

    except Exception as e:
        gunicorn_logger.error(f"Error in incoming_audio: {e.with_traceback()}")
    

async def send_captions(client_id, username, original_text, translated_text, call_id):
    # BO -> Update Call Record with Callee field based on call_id    
    print(f"Now updating Caption field in call record for Caller with ID: {client_id} for call: {call_id}")

    data = {
        "author_id": str(client_id),
        "author_username": str(username),
        "original_text": str(original_text),
        "translated_text": str(translated_text),
        "timestamp": str(datetime.now())
    }

    response = update_captions(get_collection_calls(), get_collection_users(), call_id, data)
    return response


app.mount("/", socketio_app)


if __name__ == '__main__':
    uvicorn.run("main:app", host='0.0.0.0', port=7860, log_level="info")


# Running in Docker Container
if __name__ != "__main__":
    fastapi_logger.setLevel(gunicorn_logger.level)
else:
    fastapi_logger.setLevel(logging.DEBUG)