-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_app.py
142 lines (104 loc) · 3.08 KB
/
main_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import base64
import requests
import dotenv
import json
import os
import time
import logging
import datetime
from playsound import playsound
from openai import OpenAI
logger = logging.getLogger(__name__)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("main_app.log"),
logging.StreamHandler()
]
)
IMAGE_PATH = "./media/CURRENT.jpg"
PROMPT = "You're task is to create a narration to this image. What is the person doing? What are their intentions? You can speculate freely."
# Elevenlabs configs
VOICE_ID = "7ocq291fMmdXFngd68ml"
dotenv.load_dotenv()
API_KEY_OPENAI = os.getenv("API_OPENAI")
client = OpenAI(
api_key=API_KEY_OPENAI
)
API_KEY_ELEVENLABS = os.getenv("API_ELEVENLABS")
if API_KEY_OPENAI is None or API_KEY_ELEVENLABS is None:
raise Exception("Api keys not set or env file is not loaded")
# OpenAI API Key
# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Getting the base64 strin
def get_description(image_path = IMAGE_PATH):
base64_image = encode_image(image_path)
response = client.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": PROMPT},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}",
},
},
],
}
],
max_tokens=100,
)
timestamp = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S_")
filename = f"./media/descriptions/description_{timestamp}.txt"
with open(filename, 'w') as f:
f.write(response.choices[0].message.content)
return response.choices[0].message.content
def call_elevenlabs_api(text, api_key=API_KEY_ELEVENLABS):
CHUNK_SIZE = 1024
url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}"
headers = {
"Accept": "audio/mpeg",
"Content-Type": "application/json",
"xi-api-key": api_key
}
data = {
"text": text,
"model_id": "eleven_monolingual_v1",
"voice_settings": {
"stability": 0.5,
"similarity_boost": 0.5
}
}
response = requests.post(url, json=data, headers=headers)
timestamp = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S_")
filename = f"./media/audio/{timestamp}.mp3"
with open(filename, 'wb') as f:
for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
if chunk:
f.write(chunk)
return filename
def main():
interval = 5
loop = 0
max_loop = 10
while True:
logger.info("Getting description")
description = get_description()
logger.info("Calling Elevenlabs API")
path_to_sound = call_elevenlabs_api(description)
logger.info("Playing sound")
playsound(path_to_sound)
logger.info(f"Sleeping for {interval} seconds")
time.sleep(interval)
loop += 1
if loop > max_loop:
break
if __name__ == "__main__":
main()