Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update narrator.py to reflect API updates #51

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions narrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import time
import simpleaudio as sa
import errno
from elevenlabs import generate, play, set_api_key, voices
from elevenlabs import play, Voice
from elevenlabs.client import ElevenLabs

client = OpenAI()
clientEL = ElevenLabs(
api_key=os.environ.get("ELEVENLABS_API_KEY") # Defaults to ELEVEN_API_KEY
)

set_api_key(os.environ.get("ELEVENLABS_API_KEY"))
clientOA = OpenAI()

def encode_image(image_path):
while True:
Expand All @@ -25,15 +28,22 @@ def encode_image(image_path):


def play_audio(text):
audio = generate(text, voice=os.environ.get("ELEVENLABS_VOICE_ID"))
audio = clientEL.generate(
text=text,
voice=Voice(
voice_id=os.environ.get("ELEVENLABS_VOICE_ID")
)
)

unique_id = base64.urlsafe_b64encode(os.urandom(30)).decode("utf-8").rstrip("=")
dir_path = os.path.join("narration", unique_id)
os.makedirs(dir_path, exist_ok=True)
file_path = os.path.join(dir_path, "audio.wav")

# Convert to bytes
audio_bytes = b''.join(audio)
with open(file_path, "wb") as f:
f.write(audio)
f.write(audio_bytes)

play(audio)

Expand All @@ -54,7 +64,7 @@ def generate_new_line(base64_image):


def analyze_image(base64_image, script):
response = client.chat.completions.create(
response = clientOA.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{
Expand Down