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

load_dotenv()

client = Groq()

# ==================================================
# 🎧 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
        )
    
    # Optional: cleanup the uploaded file after processing
    # os.remove(file_path)

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


# ==================================================
# 🗣️ TEXT TO SPEECH
# ==================================================

async def TTS(text: str, voice: str = "en-US-AriaNeural") -> str:
    """

    Converts text to speech and saves it to a file.

    Returns the path to the generated audio file.

    """
    os.makedirs("outputs", exist_ok=True)
    filename = f"outputs/{uuid4().hex}.mp3"
    
    communicate = edge_tts.Communicate(text, voice)
    await communicate.save(filename)
    
    return filename