Behavior_Changer / modules /transcriber.py
Przemyslaw Chachaj
d
7a2ad2e
Raw
History Blame Contribute Delete
1.45 kB
import yt_dlp
import os
import uuid
from openai import OpenAI
USE_OPENAI = False # True → OpenAI Whisper, False → Nebius
if USE_OPENAI:
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
STT_MODEL = "gpt-4o-transcribe"
else:
client = OpenAI(
base_url="https://api.tokenfactory.nebius.com/v1/",
api_key=os.environ["NEBIUS_API_KEY"]
)
STT_MODEL = "gpt-whisper-1"
def transcribe_from_url(url: str) -> str:
"""
Stable IG/TikTok/YT reel transcribe
→ forced audio extraction MP3
→ removes video-only DASH problem
"""
if not os.getenv("OPENAI_API_KEY"):
raise RuntimeError("Brak OPENAI_API_KEY")
tmp = f"/tmp/{uuid.uuid4().hex}.mp3"
ydl_opts = {
"format": "bestaudio/best", # nie mp4 — tylko audio
"postprocessors": [{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": "192"
}],
"outtmpl": tmp.replace(".mp3", ".%(ext)s"),
"quiet": True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
audio_file = tmp
if not os.path.exists(audio_file):
raise RuntimeError("Audio extraction failed – IG returned no audio stream")
with open(audio_file, "rb") as f:
transcript = client.audio.transcriptions.create(
model="gpt-4o-transcribe",
file=f
)
return transcript.text