File size: 1,619 Bytes
4cb1298
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from uuid import uuid4
import edge_tts
from groq import Groq
from dotenv import load_dotenv

load_dotenv()

client = Groq()

# ==================================================
# 🎙️ TEXT TO SPEECH (FIXED VOICE)
# ==================================================

DEFAULT_VOICE = "en-US-MichelleNeural"

async def TTS(

    text: str,

    output_dir: str = "tts_outputs",

    rate: str = "+0%",

    pitch: str = "+0Hz"

) -> str:

    if not text.strip():
        raise ValueError("Empty text")

    os.makedirs(output_dir, exist_ok=True)

    filename = f"{uuid4().hex}.mp3"
    output_path = os.path.join(output_dir, filename)

    communicate = edge_tts.Communicate(
        text=text,
        voice=DEFAULT_VOICE,
        rate=rate,
        pitch=pitch
    )

    await communicate.save(output_path)
    return output_path


# ==================================================
# 🎧 SPEECH TO TEXT
# ==================================================

async def STT(audio_file):
    os.makedirs("uploads", exist_ok=True)
    file_path = f"uploads/{uuid4().hex}.wav"

    with open(file_path, "wb") as f:
        f.write(await audio_file.read())

    with open(file_path, "rb") as f:
        transcription = client.audio.transcriptions.create(
            file=f,
            model="whisper-large-v3-turbo",
            response_format="verbose_json",
            temperature=0.0
        )

    return {
        "text": transcription.text,
        "segments": transcription.segments,
        "language": transcription.language
    }