Skip to content

Commit

Permalink
using configuration url to do stuff now
Browse files Browse the repository at this point in the history
Signed-off-by: Rishi <[email protected]>
  • Loading branch information
rshrc committed Oct 25, 2023
1 parent af418a0 commit e4d6528
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
14 changes: 12 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class UpdateInfo:
class Memory:
context_limit: int

@dataclass
class Voice:
url: str

@dataclass
class DeviceConfig:
user_info: UserInfo
Expand All @@ -53,6 +57,7 @@ class DeviceConfig:
google_cloud_security_file: GoogleCloudSecurityFile
update_info: UpdateInfo
memory: Memory
voice: Voice

@classmethod
def from_dict(cls, data: dict):
Expand All @@ -64,7 +69,9 @@ def from_dict(cls, data: dict):
api_limits=APILimits(**data['api_limits']),
google_cloud_security_file=GoogleCloudSecurityFile(**data['google_cloud_security_file']),
update_info=UpdateInfo(**data['update_info']),
memory=Memory(**data['memory'])
memory=Memory(**data['memory']),
voice=Voice(**data['voice'])

)

def to_dict(self):
Expand All @@ -74,10 +81,13 @@ def to_dict(self):
"api_limits": asdict(self.api_limits),
"google_cloud_security_file": asdict(self.google_cloud_security_file),
"update_info": asdict(self.update_info),
"memory": asdict(self.memory)
"memory": asdict(self.memory),
"voice": asdict(self.voice)

}



def get_configuration(url: str, device_uuid: Union[str, None]) -> Union[str, DeviceConfig]:
if device_uuid is None:
return "No Device Set Yet!"
Expand Down
8 changes: 6 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def increase_volume():
'X-Device-Serial': get_serial_number()
}

tts = TextToSpeechPlayer()

load_dotenv()

Expand All @@ -68,6 +67,11 @@ def increase_volume():
# Configure Device etc
configuration = get_configuration(BASE_URL, test_device_uuid)

print(f"Line 71 : {configuration}")

# sys.exit()
tts = TextToSpeechPlayer(configuration.voice.url)

MEMORY_CONTEXT = 0

if isinstance(configuration, str):
Expand Down Expand Up @@ -215,7 +219,7 @@ async def process_input(recognized_text):
# print(f"Response : {conversation}")
display_controller.render_text_threaded_v2(speech)
# await output_voice(speech)
tts.text_to_speech(speech, "output.mp3")
tts.play(speech, headers)


def voice_filler():
Expand Down
29 changes: 27 additions & 2 deletions voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import threading
import time
from tempfile import TemporaryFile

import requests as r
from google.cloud import texttospeech
from gtts import gTTS
from measure import timing
Expand All @@ -15,8 +15,9 @@


class TextToSpeechPlayer:
def __init__(self):
def __init__(self, url):
self.client = texttospeech.TextToSpeechClient()
self.url = url

def synthesize_speech(self, text, output_filename):
start_time = time.time()
Expand Down Expand Up @@ -84,6 +85,30 @@ def cues_to_audio_files(self, cue_dict, folder_name):

print(f"Saved '{cue}' to {filepath}")


def play(self, text, headers):
# Make a request to the Django API endpoint to get synthesized audio
response = r.post(
self.url,
json={"text": text},
headers=headers,
)

# Check if the request was successful
if response.status_code == 200:
output_filename = "output.mp3"

# Write the audio data to a temporary file
with open(output_filename, "wb") as file:
file.write(response.content)

# Load and play the audio
self.load_and_play(output_filename)

else:
print(f"Failed to get audio. Status code: {response.status_code}. Message: {response.text}")


@timing
async def output_voicev2(text: str, expect_return=False):
tts = gTTS(text, lang='en')
Expand Down

0 comments on commit e4d6528

Please sign in to comment.