Update ai_config.py
Browse files- ai_config.py +22 -6
ai_config.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
from langchain_openai import ChatOpenAI
|
2 |
from openai import OpenAI
|
3 |
import tiktoken
|
@@ -7,7 +9,8 @@ def n_of_questions():
|
|
7 |
n_of_questions = 25
|
8 |
return n_of_questions
|
9 |
|
10 |
-
openai_api_key = os.environ.get("openai_api_key")
|
|
|
11 |
|
12 |
model = "gpt-4o-mini"
|
13 |
|
@@ -22,20 +25,33 @@ def load_model(openai_api_key):
|
|
22 |
# Initialize the OpenAI client with the API key
|
23 |
client = OpenAI(api_key=openai_api_key)
|
24 |
|
|
|
|
|
25 |
|
26 |
-
def convert_text_to_speech(text,
|
27 |
try:
|
28 |
# Convert the final text to speech
|
29 |
response = client.audio.speech.create(model="tts-1", voice="alloy", input=text)
|
30 |
|
31 |
-
|
|
|
32 |
for chunk in response.iter_bytes():
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
except Exception as e:
|
36 |
print(f"An error occurred: {e}")
|
|
|
37 |
response = client.audio.speech.create(model="tts-1", voice="alloy", input='Here is my Report!')
|
38 |
|
39 |
-
|
40 |
for chunk in response.iter_bytes():
|
41 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
from io import BytesIO
|
2 |
+
|
3 |
from langchain_openai import ChatOpenAI
|
4 |
from openai import OpenAI
|
5 |
import tiktoken
|
|
|
9 |
n_of_questions = 25
|
10 |
return n_of_questions
|
11 |
|
12 |
+
#openai_api_key = os.environ.get("openai_api_key")
|
13 |
+
openai_api_key = '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'
|
14 |
|
15 |
model = "gpt-4o-mini"
|
16 |
|
|
|
25 |
# Initialize the OpenAI client with the API key
|
26 |
client = OpenAI(api_key=openai_api_key)
|
27 |
|
28 |
+
import os
|
29 |
+
|
30 |
|
31 |
+
def convert_text_to_speech(text, output):
|
32 |
try:
|
33 |
# Convert the final text to speech
|
34 |
response = client.audio.speech.create(model="tts-1", voice="alloy", input=text)
|
35 |
|
36 |
+
if isinstance(output, BytesIO):
|
37 |
+
# If output is a BytesIO object, write directly to it
|
38 |
for chunk in response.iter_bytes():
|
39 |
+
output.write(chunk)
|
40 |
+
else:
|
41 |
+
# If output is a file path, open and write to it
|
42 |
+
with open(output, 'wb') as f:
|
43 |
+
for chunk in response.iter_bytes():
|
44 |
+
f.write(chunk)
|
45 |
|
46 |
except Exception as e:
|
47 |
print(f"An error occurred: {e}")
|
48 |
+
# Fallback in case of error
|
49 |
response = client.audio.speech.create(model="tts-1", voice="alloy", input='Here is my Report!')
|
50 |
|
51 |
+
if isinstance(output, BytesIO):
|
52 |
for chunk in response.iter_bytes():
|
53 |
+
output.write(chunk)
|
54 |
+
else:
|
55 |
+
with open(output, 'wb') as f:
|
56 |
+
for chunk in response.iter_bytes():
|
57 |
+
f.write(chunk)
|