File size: 1,568 Bytes
9a3c8f5
 
932db78
 
9a3c8f5
 
932db78
 
 
9a3c8f5
932db78
 
9a3c8f5
 
932db78
 
 
 
 
 
 
9a3c8f5
 
932db78
 
 
 
 
 
 
 
 
9a3c8f5
 
 
 
932db78
 
9a3c8f5
 
932db78
 
9a3c8f5
 
932db78
 
 
 
9a3c8f5
 
 
932db78
 
9a3c8f5
 
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
import io

from pymilvus import Collection
import asyncio
from bark import SAMPLE_RATE, generate_audio, preload_models
from scipy.io.wavfile import write
from db_connect import connect

# for audio storage
from audio_db.is3.is3 import StagedObject

connect()
preload_models()


async def generate_audio():
    response = Collection("Response")
    audio_response = Collection("AudioResponse")

    data = []

    response_iterator = response.query_iterator(batch_size=1, output_fields=['text', 'embeddings'])
    ids_to_delete = []
    while True:
        res = response_iterator.next()
        if len(res) == 0:
            print("query iteration finished, close")
            # close the iterator
            response_iterator.close()
            break

        # generate audio
        audio_array = generate_audio(res[0]['text'], history_prompt="en_speaker_3")
        bytes_io = io.BytesIO()
        write(bytes_io, SAMPLE_RATE, audio_array)
        audio_bytes = bytes_io.read()

        # store the audio
        obj = StagedObject(obj=audio_bytes, name='audio')
        uploaded_object = await obj.upload()

        # save the audio record to AudioResponse
        data.append([res[0]['text'], uploaded_object.obj_id, res[0]['embeddings']])
        ids_to_delete.append(res[0]['id'])

    audio_response.insert(list(zip(*data)))
    audio_response.flush()

    # delete text to generate audio
    response.delete(expr=f"id in {str(ids_to_delete)}")


if __name__ == '__main__':
    loop = asyncio.new_event_loop()
    loop.run_until_complete(generate_audio())