File size: 828 Bytes
199032a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import yt_dlp
from openai import OpenAI

client = OpenAI(
    base_url="https://api.studio.nebius.ai/v1/",
    api_key=os.environ.get("NEBIUS_API_KEY")
)

def transcribe_from_url(link:str) -> str:
    tmp = "/tmp/reel_audio.m4a"

    ydl_opts = {
        "format": "m4a/bestaudio/best",
        "outtmpl": tmp,
        "quiet": True,
    }

    try:
        yt_dlp.YoutubeDL(ydl_opts).download([link])
    except Exception:
        return ""

    # Nebius ASR
    try:
        with open(tmp, "rb") as f:
            r = client.chat.completions.create(
                model="openai/whisper-1",
                messages=[{"role":"user","content":"Transcribe the audio"}],
                audio={"file":f,"format":"wav"}
            )
            return r.choices[0].message.content.strip()
    except:
        return ""