Spaces:
Sleeping
Sleeping
Przemyslaw Chachaj commited on
Commit ·
15e9a2c
1
Parent(s): 199032a
🔥 Switch to GPT-4o transcription (no pytorch needed)
Browse files- modules/transcriber.py +25 -0
modules/transcriber.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
import yt_dlp
|
| 6 |
+
|
| 7 |
+
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
| 8 |
+
|
| 9 |
+
def transcribe_from_url(url:str):
|
| 10 |
+
# pobranie audio/video przez yt-dlp do pliku tymczasowego
|
| 11 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp:
|
| 12 |
+
out = tmp.name
|
| 13 |
+
|
| 14 |
+
ydl_opts = {"format": "bestaudio/best", "outtmpl": out}
|
| 15 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 16 |
+
ydl.download([url])
|
| 17 |
+
|
| 18 |
+
# transkrypcja przez OpenAI - ZAWSZE działa
|
| 19 |
+
with open(out, "rb") as f:
|
| 20 |
+
transcript = client.audio.transcriptions.create(
|
| 21 |
+
model="gpt-4o-transcribe",
|
| 22 |
+
file=f
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
return transcript.text
|