Spaces:
Runtime error
Runtime error
File size: 3,034 Bytes
f534c35 |
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import pyktok as pyk
pyk.specify_browser("chrome") # Specify the browser to use for accessing TikTok
import os
import moviepy.editor as mp
from openai import OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
import time
from pathlib import Path
from openai import OpenAI
import os
import argparse
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"),
)
# def download_video(url):
# pyk.save_tiktok(url, save_video=True, filename="tiktok_video.mp4")
def download_video(url, outpath=Path('.')):
pyk.save_tiktok(url, True, str(outpath / 'tiktok_video.mp4'))
print('Downloaded video wait...', end='')
time.sleep(5)
print('Done')
def extract_audio(path):
outpath = Path(path).parent / "tiktok_audio.mp3"
print('Extracting audio...', flush=True)
video = mp.VideoFileClip(path)
video.audio.write_audiofile(str(outpath))
print('Audio extracted', flush=True)
return outpath
# def transcribe_audio():
# model = whisper.load_model("base")
# result = model.transcribe("tiktok_audio.mp3")
# return result["text"]
def transcribe_audio(audio_file_path):
print('sending for transcription...', flush=True)
with open(audio_file_path, 'rb') as audio_file:
transcription = client.audio.transcriptions.create(model="whisper-1", file=audio_file,
response_format="text")
print('Transcription done', flush=True)
return transcription
def format_recipe(transcript):
prompt = f"Please format the following recipe transcript into a nicely formatted recipe:\n\n{transcript}"
response = client.completions.create(engine="text-davinci-003",
prompt=prompt,
max_tokens=500,
n=1,
stop=None,
temperature=0.7)
return response.choices[0].text.strip()
def main():
# tiktok_url = input("Enter the TikTok video URL: ")
# tiktok_url = "https://www.tiktok.com/t/ZTLjYBSpt/"
tiktok_url = "https://www.tiktok.com/@emmaaaaaaam_/video/7348493781961886981"
_, user, _, video_id = tiktok_url.strip('https://').strip('http://').strip('/').split("/")
outpath = Path(video_id)
outpath.mkdir(parents=True, exist_ok=True)
download_video(tiktok_url, outpath)
video_path = [p for p in outpath.parent.rglob('*.mp4') if video_id in p.name][0]
video_path.rename(outpath / video_path.name)
video_path = str(outpath / video_path.name)
audio_path = extract_audio(video_path)
transcript_path = outpath / 'transcript.txt'
if transcript_path.exists():
with transcript_path.open() as f:
transcript = f.read()
else:
transcript = transcribe_audio(audio_path)
with transcript_path.open('w') as f:
f.write(transcript)
formatted_recipe = format_recipe(transcript)
print("Formatted Recipe:")
print(formatted_recipe)
# # Clean up temporary files
# os.remove("tiktok_video.mp4")
# os.remove("tiktok_audio.mp3")
if __name__ == "__main__":
main() |