rajtilakjee / keepyourmouthshut Goto Github PK
View Code? Open in Web Editor NEWAcid Reflux for your Ears!
License: GNU General Public License v3.0
Acid Reflux for your Ears!
License: GNU General Public License v3.0
I have a function that used ElevenLabs API which worked fine. However, it incurred a lot of cost even during prototyping. So I thought it would be best to go for Edge-TTS. I am getting an error message:
UnboundLocalError: cannot access local variable 'audio_segment' where it is not associated with a value
Here's the ElevenLabs API code:
from elevenlabs import generate, set_api_key, Voice, VoiceSettings
from pydub import AudioSegment
import io
import os
import hashlib
from utils.os_stuff import get_env_var_or_fail
import logging
set_api_key(get_env_var_or_fail('ELEVEN_LABS_API_KEY'))
HOST_VOICE = Voice(
voice_id="21m00Tcm4TlvDq8ikWAM",
name="Rachel",
category="premade",
settings=VoiceSettings(stability=0.35, similarity_boost=0.9),
)
ADS_VOICE = Voice(
voice_id="TxGEqnHWrfWFTfGW9XjX",
name="Josh",
category="premade",
settings=VoiceSettings(stability=0.35, similarity_boost=0.9),
)
def load_audio_bytes(audio_bytes):
audio_file = io.BytesIO(audio_bytes)
audio_segment = AudioSegment.from_file(audio_file, format='mp3')
return audio_segment
def convert_text_to_mp3(text, voice):
# Generate the cache key by MD5 hashing the text
cache_key = hashlib.md5(text.encode()).hexdigest()
# Check if the file already exists in cache
cache_dir = ".eleven_labs_cache"
cache_file = os.path.join(cache_dir, f"{cache_key}.mp3")
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
if os.path.exists(cache_file):
# If it does exist, load and return as an AudioSegment
audio_segment = AudioSegment.from_mp3(cache_file)
else:
# If it does not exist, call the API, create, save, and return as an AudioSegment
char_count = len(text)
logging.info(f'Calling eleven labs for {char_count} chars...')
section_1_voice_over = load_audio_bytes(generate(
text=text,
voice=voice
))
section_1_voice_over.export(cache_file, format='mp3')
audio_segment = section_1_voice_over
return audio_segment
And here's the Edge-TTS code:
import asyncio
import edge_tts
from pydub import AudioSegment
VOICE = "en-GB-SoniaNeural"
def convert_text_to_mp3(text):
loop = asyncio.get_event_loop_policy().get_event_loop()
try:
audio_segment = loop.run_until_complete(edge_tts.Communicate(text, VOICE))
finally:
loop.close()
return audio_segment
Describe the bug
Crash on clicking "Generate"
(keepyourmouthshut) alan@ziggy:~/Source/rajtilakjee/keepyourmouthshut$ python app/keepyourmouthshut.py
* Serving Flask app 'keepyourmouthshut'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:64215
* Running on http://10.0.25.12:64215
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 103-517-491
127.0.0.1 - - [26/Mar/2024 18:45:20] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [26/Mar/2024 18:45:20] "GET /static/kyms-logo.png HTTP/1.1" 200 -
127.0.0.1 - - [26/Mar/2024 18:45:21] "GET /static/kyms-logo.png HTTP/1.1" 304 -
127.0.0.1 - - [26/Mar/2024 18:46:17] "POST /generate HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/alan/Source/rajtilakjee/keepyourmouthshut/lib/python3.11/site-packages/flask/app.py", line 1478, in __call__
return self.wsgi_app(environ, start_response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/alan/Source/rajtilakjee/keepyourmouthshut/lib/python3.11/site-packages/flask/app.py", line 1458, in wsgi_app
response = self.handle_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/alan/Source/rajtilakjee/keepyourmouthshut/lib/python3.11/site-packages/flask/app.py", line 1455, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/alan/Source/rajtilakjee/keepyourmouthshut/lib/python3.11/site-packages/flask/app.py", line 869, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/alan/Source/rajtilakjee/keepyourmouthshut/lib/python3.11/site-packages/flask/app.py", line 867, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/alan/Source/rajtilakjee/keepyourmouthshut/lib/python3.11/site-packages/flask/app.py", line 852, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/alan/Source/rajtilakjee/keepyourmouthshut/app/keepyourmouthshut.py", line 49, in generate
mp3_file, txt_file = generate_podcast(name, desc, topics, adverts)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/alan/Source/rajtilakjee/keepyourmouthshut/app/generate_podcast.py", line 50, in generate_podcast
script_segment = llmOS_stuff.generate_response(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/alan/Source/rajtilakjee/keepyourmouthshut/app/utils/llmOS_stuff.py", line 49, in generate_response
result = answer["choices"][0]["message"]["content"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'choices'
To Reproduce
Steps to reproduce the behavior:
Crash pasted above
Expected behavior
A viral podcast episode to be generated! :D
Screenshots
Desktop (please complete the following information):
Additional context
I cloned the repo, and did the pip install of requirements (which is incorrect in the readme, by the way, as requirements is in the app folder, not the root).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.