initial commit
This commit is contained in:
37
backend/app/services/voice_service.py
Normal file
37
backend/app/services/voice_service.py
Normal file
@@ -0,0 +1,37 @@
|
||||
"""FluentGerman.ai — Voice service (API provider + browser fallback)."""
|
||||
|
||||
import io
|
||||
|
||||
import openai
|
||||
|
||||
from app.config import get_settings
|
||||
|
||||
|
||||
async def transcribe(audio_bytes: bytes, filename: str = "audio.webm") -> str:
|
||||
"""Transcribe audio to text using OpenAI Whisper API."""
|
||||
settings = get_settings()
|
||||
client = openai.AsyncOpenAI(api_key=settings.llm_api_key)
|
||||
|
||||
audio_file = io.BytesIO(audio_bytes)
|
||||
audio_file.name = filename
|
||||
|
||||
transcript = await client.audio.transcriptions.create(
|
||||
model=settings.stt_model,
|
||||
file=audio_file,
|
||||
)
|
||||
return transcript.text
|
||||
|
||||
|
||||
async def synthesize(text: str) -> bytes:
|
||||
"""Synthesize text to speech using OpenAI TTS API."""
|
||||
settings = get_settings()
|
||||
client = openai.AsyncOpenAI(api_key=settings.llm_api_key)
|
||||
|
||||
response = await client.audio.speech.create(
|
||||
model=settings.tts_model,
|
||||
voice=settings.tts_voice,
|
||||
input=text,
|
||||
response_format="mp3",
|
||||
)
|
||||
|
||||
return response.content
|
||||
Reference in New Issue
Block a user