Spaces:
Sleeping
Sleeping
from gtts import gTTS | |
import base64 | |
import os | |
from initialize import init_doc_store, define_components, define_pipeline | |
API_Key = os.environ['API_KEY'] | |
def init_pipeline(): | |
path = 'RAG Files/' | |
files = os.listdir(path) | |
document_Store = init_doc_store(path, files) | |
retreiver, prompt_builder, generator = define_components(document_Store, API_Key) | |
pipeline = define_pipeline(retreiver, prompt_builder, generator) | |
return pipeline | |
def generate_response(question, pipeline): | |
response = pipeline.run({ | |
'retriever':{'query':question}, | |
'prompt_builder':{'question':question} | |
}) | |
response = response['llm']['replies'][0] | |
return response | |
def audio_response(response): | |
audio_stream="response_audio.mp3" | |
tts = gTTS(response) | |
tts.save(audio_stream) | |
with open(audio_stream, 'rb') as file: | |
audio_data = file.read() | |
audio_base64 = base64.b64encode(audio_data).decode('utf-8') | |
audio_tag = f'<audio autoplay="true" src="data:audio/mp3;base64,{audio_base64}">' | |
return audio_tag | |