Spaces:
Sleeping
Sleeping
Przemyslaw Chachaj commited on
Commit ·
fc6f959
1
Parent(s): 15e9a2c
- app.py +9 -25
- modules/transcriber.py +24 -13
app.py
CHANGED
|
@@ -24,31 +24,15 @@ def load(uid:str)->dict:
|
|
| 24 |
# -------- BASE CONTRACT GEN --------
|
| 25 |
def create_contract(link:str,category:str="general",uid:Optional[str]=None)->dict:
|
| 26 |
uid = uid or f"{category}_{uuid.uuid4().hex[:8]}"
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
"Napisz krótką notatkę z efektów.",
|
| 37 |
-
"Zrób wersję X1.1 (trochę trudniejszą).",
|
| 38 |
-
"Pochwal się wynikiem komuś lub zapisz publicznie."
|
| 39 |
-
],
|
| 40 |
-
"current_index":0,
|
| 41 |
-
"history":[],
|
| 42 |
-
"created_at":datetime.datetime.utcnow().isoformat()+"Z"
|
| 43 |
-
}
|
| 44 |
-
save(contract)
|
| 45 |
-
return {
|
| 46 |
-
"uid":uid,
|
| 47 |
-
"goal":"Tiny habit based on reel",
|
| 48 |
-
"first_step":contract["steps"][0],
|
| 49 |
-
"total_steps":7
|
| 50 |
-
}
|
| 51 |
-
|
| 52 |
|
| 53 |
def next_step(uid:str)->dict:
|
| 54 |
c=load(uid)
|
|
|
|
| 24 |
# -------- BASE CONTRACT GEN --------
|
| 25 |
def create_contract(link:str,category:str="general",uid:Optional[str]=None)->dict:
|
| 26 |
uid = uid or f"{category}_{uuid.uuid4().hex[:8]}"
|
| 27 |
+
from modules.transcriber import transcribe_from_url
|
| 28 |
+
from modules.generate_plan import steps_from_transcript
|
| 29 |
+
|
| 30 |
+
txt = transcribe_from_url(link)
|
| 31 |
+
if txt.strip():
|
| 32 |
+
steps = steps_from_transcript(txt, category)
|
| 33 |
+
else:
|
| 34 |
+
steps = ["Obejrzyj wideo i zapisz 1 najważniejszy wniosek."]*7
|
| 35 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
def next_step(uid:str)->dict:
|
| 38 |
c=load(uid)
|
modules/transcriber.py
CHANGED
|
@@ -1,22 +1,33 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
ydl_opts = {"format": "bestaudio/best", "outtmpl": out}
|
| 15 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 16 |
ydl.download([url])
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
transcript = client.audio.transcriptions.create(
|
| 21 |
model="gpt-4o-transcribe",
|
| 22 |
file=f
|
|
|
|
| 1 |
+
def transcribe_from_url(url: str) -> str:
|
| 2 |
+
"""
|
| 3 |
+
Stable IG/TikTok/YT reel transcribe
|
| 4 |
+
→ forced audio extraction MP3
|
| 5 |
+
→ removes video-only DASH problem
|
| 6 |
+
"""
|
| 7 |
+
if not os.getenv("OPENAI_API_KEY"):
|
| 8 |
+
raise RuntimeError("Brak OPENAI_API_KEY")
|
| 9 |
|
| 10 |
+
tmp = f"/tmp/{uuid.uuid4().hex}.mp3"
|
| 11 |
|
| 12 |
+
ydl_opts = {
|
| 13 |
+
"format": "bestaudio/best", # nie mp4 — tylko audio
|
| 14 |
+
"postprocessors": [{
|
| 15 |
+
"key": "FFmpegExtractAudio",
|
| 16 |
+
"preferredcodec": "mp3",
|
| 17 |
+
"preferredquality": "192"
|
| 18 |
+
}],
|
| 19 |
+
"outtmpl": tmp.replace(".mp3", ".%(ext)s"),
|
| 20 |
+
"quiet": True,
|
| 21 |
+
}
|
| 22 |
|
|
|
|
| 23 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 24 |
ydl.download([url])
|
| 25 |
|
| 26 |
+
audio_file = tmp
|
| 27 |
+
if not os.path.exists(audio_file):
|
| 28 |
+
raise RuntimeError("Audio extraction failed – IG returned no audio stream")
|
| 29 |
+
|
| 30 |
+
with open(audio_file, "rb") as f:
|
| 31 |
transcript = client.audio.transcriptions.create(
|
| 32 |
model="gpt-4o-transcribe",
|
| 33 |
file=f
|