From 64dc8d2517910fe2522c82c34b14299c6fb78921 Mon Sep 17 00:00:00 2001 From: bladeclara42 <71927457+bladeclara42@users.noreply.github.com> Date: Mon, 30 Jun 2025 09:57:40 +0700 Subject: [PATCH] add voice in openai api (not tested) --- .gitignore | 3 +++ app/core/config.py | 1 + app/core/openai_voice_client.py | 39 +++++++++++++++++++++++++++++++++ app/main.py | 4 ++-- app/services/voice.py | 7 ++++++ 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 app/core/openai_voice_client.py create mode 100644 app/services/voice.py diff --git a/.gitignore b/.gitignore index 9c937d6..0f4a9ac 100644 --- a/.gitignore +++ b/.gitignore @@ -85,3 +85,6 @@ dmypy.json _build/ docs/_build/ ".env" + +# Ignore all files in the pycache folder +__pycache__ diff --git a/app/core/config.py b/app/core/config.py index b63258c..dd11377 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -11,4 +11,5 @@ OPENAI_API_BASE = os.getenv("OPENAI_API_BASE", "https://api.openai.com/v1") DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY", "") DEEPSEEK_API_BASE = os.getenv("DEEPSEEK_API_BASE", "https://api.deepseek.com/v1") DEEPSEEK_MODEL = os.getenv("DEEPSEEK_MODEL", "deepseek-chat") +OPENAI_AUDIO_MODEL = os.getenv("OPENAI_AUDIO_MODEL", "") diff --git a/app/core/openai_voice_client.py b/app/core/openai_voice_client.py new file mode 100644 index 0000000..820f9fa --- /dev/null +++ b/app/core/openai_voice_client.py @@ -0,0 +1,39 @@ +import openai +from openai import OpenAI +from openai import OpenAIError +from app.core.config import OPENAI_API_KEY, OPENAI_AUDIO_MODEL, OPENAI_API_BASE + +# Ensure the API key is properly set +if not OPENAI_API_KEY: + raise ValueError("OPENAI_API_KEY is not set in environment variables") + +# Initialize the client with proper configuration +client = OpenAI( + api_key=OPENAI_API_KEY, + base_url=OPENAI_API_BASE +) + +async def generate_voice(messages: list): + if not messages: + raise ValueError("Messages list cannot be empty") + + try: + response = client.chat.completions.create( + model=OPENAI_AUDIO_MODEL, + response_format="mp3", + messages=messages, + ) + + if not response.choices or not response.choices[0].message.content: + return "No response content from the model" + + return response.choices[0].message.content + + except OpenAIError as e: + error_msg = f"OpenAI API Error: {str(e)}" + print(error_msg) + raise Exception(error_msg) from e + except Exception as e: + error_msg = f"Unexpected error: {str(e)}" + print(error_msg) + raise Exception(error_msg) from e \ No newline at end of file diff --git a/app/main.py b/app/main.py index a4ac1a4..9f77a86 100644 --- a/app/main.py +++ b/app/main.py @@ -1,9 +1,9 @@ from fastapi import FastAPI from app.api.v1 import translate -# from app.api.v1 import voice +from app.api.v1 import voice app = FastAPI() # Include your routes app.include_router(translate.router, prefix="/api/v1/translate", tags=["translate"]) -# app.include_router(voice.router, prefix="/api/v1/voice", tags=["voice"]) \ No newline at end of file +app.include_router(voice.router, prefix="/api/v1/voice", tags=["voice"]) \ No newline at end of file diff --git a/app/services/voice.py b/app/services/voice.py new file mode 100644 index 0000000..87a64db --- /dev/null +++ b/app/services/voice.py @@ -0,0 +1,7 @@ +from app.core.openai_voice_client import generate_voice + +async def generate_voice(text: str) -> str: + voice = await generate_voice(text) + return voice + +