File size: 1,856 Bytes
dcd68c4
8d83939
3229324
8d83939
 
dcd68c4
0fd4a4d
8d83939
 
0fd4a4d
 
 
dcd68c4
 
 
 
 
0fd4a4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd68c4
 
932db78
 
0fd4a4d
 
 
 
 
 
932db78
8d83939
 
 
 
 
 
 
 
 
 
 
 
dcd68c4
 
 
cf64ca3
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
from fastapi import FastAPI
from fastapi.responses import Response
import uvicorn
import numpy as np
import io
from sentence_transformers import SentenceTransformer
from pymilvus import Collection
import soundfile as sf
from bark import SAMPLE_RATE

from db.db_connect import connect, disconnect
from db.query_db import query

model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')

app = FastAPI()

def get_wait_responses():
    with open('db/wait_responses.txt', 'r') as file:
        content = file.read()
        contents = content.split('\n\n')

    wait_embeddings = model.encode(contents)
    return wait_embeddings


WAIT_RESPONSES_EMBEDDINGS = get_wait_responses()

#TODO raise exception
def insert_response_to_generate_for_audio(text, embeddings):
    connect()
    collection = Collection("Response")
    data = [
        [text],
        embeddings
    ]
    collection.insert(data)
    collection.flush()
    disconnect()

@app.post('/tts')
async def transcribe(text: str):
    embeddings = model.encode([text])

    audio = await query(embeddings, threshold=0.8)
    # if does not exist then store to response
    # store this text as response to be used to generate audio
    if audio is None:
        insert_response_to_generate_for_audio(text, embeddings)
        audio = await query(WAIT_RESPONSES_EMBEDDINGS, threshold=0.8)

    # convert audio bytes to appropriate format to return
    audio_file = io.BytesIO(np.frombuffer(audio, dtype=np.int16))
    audio, sample_rate = sf.read(audio_file)

    audio_file = io.BytesIO()
    sf.write(audio_file, audio, sample_rate, format='wav')
    audio_file.seek(0)

    return Response(
        content=audio_file.read(),
        media_type="audio/wav",  # Same as the Content-Type header
        )


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