Spaces:
Sleeping
Sleeping
File size: 2,207 Bytes
75f48fa |
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 |
import os
from openai import OpenAI # type: ignore
import tempfile
# Lazily initialized OpenAI client to avoid import-time errors when the
# API key isn't configured. Previously this module attempted to create the
# client on import and raised a ``ValueError`` if ``OPENAI_API_KEY`` was
# missing, which prevented the rest of the application from running (and
# broke tests that don't require the API). The client is now created only
# when needed.
_client: OpenAI | None = None
def _get_client() -> OpenAI:
"""Return a cached OpenAI client instance.
Raises:
ValueError: If the ``OPENAI_API_KEY`` environment variable is not set.
"""
global _client
if _client is None:
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("OPENAI_API_KEY environment variable is required but not set")
_client = OpenAI(api_key=api_key)
return _client
def explain_detection(objects_list):
"""Send detected objects to OpenAI and return an explanation."""
if not objects_list:
return "No objects detected."
prompt = f"Explain these detected objects in simple terms: {objects_list}"
client = _get_client()
response = client.chat.completions.create(
model="gpt-4o-mini", # new lightweight chat model
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content
def generate_voice(text):
"""Generate voice narration using OpenAI's TTS service."""
try:
client = _get_client()
# Generate speech using OpenAI TTS
response = client.audio.speech.create(
model="tts-1",
voice="alloy", # You can change this to: alloy, echo, fable, onyx, nova, or shimmer
input=text,
response_format="mp3",
)
# Save the audio to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
temp_audio.write(response.content)
return temp_audio.name
except Exception as e:
print(f"Voice generation error: {e}")
return None
|