parent
a4847a8345
commit
0a65b08cd8
|
@ -3,4 +3,4 @@
|
||||||
/narration
|
/narration
|
||||||
/frames/*
|
/frames/*
|
||||||
!/frames/.gitkeep
|
!/frames/.gitkeep
|
||||||
.env
|
.trunk
|
|
@ -1,8 +1,9 @@
|
||||||
import cv2
|
|
||||||
import time
|
|
||||||
from PIL import Image
|
|
||||||
import numpy as np
|
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
# Folder
|
# Folder
|
||||||
folder = "frames"
|
folder = "frames"
|
||||||
|
|
61
narrator.py
61
narrator.py
|
@ -1,22 +1,43 @@
|
||||||
import base64
|
import base64
|
||||||
import errno
|
import errno
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import simpleaudio as sa
|
from elevenlabs import generate, play, set_api_key, stream
|
||||||
from elevenlabs import generate, play, set_api_key, stream, voices
|
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
from pynput import ( # Using pynput to listen for a keypress instead of native keyboard module which was requiring admin privileges
|
||||||
|
keyboard,
|
||||||
|
)
|
||||||
|
|
||||||
client = OpenAI()
|
client = OpenAI()
|
||||||
|
|
||||||
set_api_key(os.environ.get("ELEVENLABS_API_KEY"))
|
set_api_key(os.environ.get("ELEVENLABS_API_KEY"))
|
||||||
|
|
||||||
|
# Initializes the variables based their respective environment variable values, defaulting to false
|
||||||
# This code initializes the variable 'isStreaming' based on the value of the environment variable 'ELEVENLABS_STREAMIMAGES'.
|
|
||||||
# If the value of 'ELEVENLABS_STREAMIMAGES' is "true", then 'isStreaming' is set to True.
|
|
||||||
# Otherwise, 'isStreaming' is set to False.
|
|
||||||
isStreaming = os.environ.get("ELEVENLABS_STREAMING", "false") == "true"
|
isStreaming = os.environ.get("ELEVENLABS_STREAMING", "false") == "true"
|
||||||
|
isPhotoBooth = os.environ.get("PHOTOBOOTH_MODE", "false") == "true"
|
||||||
|
|
||||||
|
script = []
|
||||||
|
narrator = "Sir David Attenborough"
|
||||||
|
|
||||||
|
|
||||||
|
def on_press(key):
|
||||||
|
if key == keyboard.Key.space:
|
||||||
|
# When space bar is pressed, run the main function which analyzes the image and generates the audio
|
||||||
|
_main()
|
||||||
|
|
||||||
|
|
||||||
|
def on_release(key):
|
||||||
|
if key == keyboard.Key.esc:
|
||||||
|
# Stop listener
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# Create a listener
|
||||||
|
listener = keyboard.Listener(on_press=on_press, on_release=on_release)
|
||||||
|
|
||||||
|
# Start the listener
|
||||||
|
listener.start()
|
||||||
|
|
||||||
|
|
||||||
def encode_image(image_path):
|
def encode_image(image_path):
|
||||||
|
@ -41,9 +62,11 @@ def play_audio(text):
|
||||||
)
|
)
|
||||||
|
|
||||||
if isStreaming:
|
if isStreaming:
|
||||||
|
# Stream the audio for more real-time responsiveness
|
||||||
stream(audio)
|
stream(audio)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Save the audio to a file and play it
|
||||||
unique_id = base64.urlsafe_b64encode(os.urandom(30)).decode("utf-8").rstrip("=")
|
unique_id = base64.urlsafe_b64encode(os.urandom(30)).decode("utf-8").rstrip("=")
|
||||||
dir_path = os.path.join("narration", unique_id)
|
dir_path = os.path.join("narration", unique_id)
|
||||||
os.makedirs(dir_path, exist_ok=True)
|
os.makedirs(dir_path, exist_ok=True)
|
||||||
|
@ -62,7 +85,7 @@ def generate_new_line(base64_image):
|
||||||
"content": [
|
"content": [
|
||||||
{
|
{
|
||||||
"type": "text",
|
"type": "text",
|
||||||
"text": "Describe this image as if you are David Attenborough",
|
"text": f"Describe this image as if you are {narrator}",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "image_url",
|
"type": "image_url",
|
||||||
|
@ -79,8 +102,8 @@ def analyze_image(base64_image, script):
|
||||||
messages=[
|
messages=[
|
||||||
{
|
{
|
||||||
"role": "system",
|
"role": "system",
|
||||||
"content": """
|
"content": f"""
|
||||||
You are Sir David Attenborough. Narrate the picture of the human as if it is a nature documentary.
|
You are {narrator}. Narrate the picture of the human as if it is a nature documentary.
|
||||||
Make it snarky and funny. Don't repeat yourself. Make it short. If I do anything remotely interesting, make a big deal about it!
|
Make it snarky and funny. Don't repeat yourself. Make it short. If I do anything remotely interesting, make a big deal about it!
|
||||||
""",
|
""",
|
||||||
},
|
},
|
||||||
|
@ -93,10 +116,9 @@ def analyze_image(base64_image, script):
|
||||||
return response_text
|
return response_text
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def _main():
|
||||||
script = []
|
global script
|
||||||
|
|
||||||
while True:
|
|
||||||
# path to your image
|
# path to your image
|
||||||
image_path = os.path.join(os.getcwd(), "./frames/frame.jpg")
|
image_path = os.path.join(os.getcwd(), "./frames/frame.jpg")
|
||||||
|
|
||||||
|
@ -104,7 +126,7 @@ def main():
|
||||||
base64_image = encode_image(image_path)
|
base64_image = encode_image(image_path)
|
||||||
|
|
||||||
# analyze posture
|
# analyze posture
|
||||||
print("👀 David is watching...")
|
print(f"👀 {narrator} is watching...")
|
||||||
analysis = analyze_image(base64_image, script=script)
|
analysis = analyze_image(base64_image, script=script)
|
||||||
|
|
||||||
print("🎙️ David says:")
|
print("🎙️ David says:")
|
||||||
|
@ -114,9 +136,20 @@ def main():
|
||||||
|
|
||||||
script = script + [{"role": "assistant", "content": analysis}]
|
script = script + [{"role": "assistant", "content": analysis}]
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
while True:
|
||||||
|
if isPhotoBooth:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
_main()
|
||||||
|
|
||||||
# wait for 5 seconds
|
# wait for 5 seconds
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
|
if isPhotoBooth:
|
||||||
|
print(f"Press the spacebar to trigger {narrator}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -28,6 +28,7 @@ pure-eval==0.2.2
|
||||||
pydantic==2.4.2
|
pydantic==2.4.2
|
||||||
pydantic_core==2.10.1
|
pydantic_core==2.10.1
|
||||||
Pygments==2.16.1
|
Pygments==2.16.1
|
||||||
|
pynput==1.7.6
|
||||||
requests==2.31.0
|
requests==2.31.0
|
||||||
simpleaudio==1.0.4
|
simpleaudio==1.0.4
|
||||||
six==1.16.0
|
six==1.16.0
|
||||||
|
|
Loading…
Reference in New Issue