As everybody else, I wanted to see what ChatGPT could do.

I love finding recipes on YouTube, but sometimes, a good set of instruction is better than the whole story of the recipe. Especially when I want to do the same recipe again, I don’t want to watch again why, how, etc … did this recipe came to be.

So, let’s use ChatGPT and Whisper to take care of the process for me.

The video

The generated text

Recipe for Extra Crispy Oven-Fried Chicken Wings:

Ingredients:
– 2.5 pounds of chicken wings
– Kosher salt
– Baking powder
– Cornstarch (optional)

Instructions:
1. Air chill the chicken wings below 40 degrees for less retained moisture and drier skin.
2. Mix kosher salt, baking powder, and cornstarch in a ratio of one teaspoon for every pound of chicken. Add cornstarch optionally for crispiness.
3. Coat chicken wings in the mixture, refrigerate uncovered overnight for best results.
4. Preheat oven to 450 degrees with convection on.
5. Arrange chicken wings skin side up on a baking sheet.
6. Bake chicken wings for 20 minutes, flip them over, then bake for an additional 15 minutes.
7. While the chicken wings are baking, prepare ranch dressing and wing sauce using buttermilk, garlic, hot sauce, and butter.
8. Serve the chicken wings hot with the ranch dressing and wing sauce on the side. Enjoy!

The code

from pytube import YouTube
from moviepy.editor import *
import openai
import textwrap

openai.api_key = "YOUR KEY"
def download_youtube_video(url, output_path):
    yt = YouTube(url)
    video = yt.streams.filter(file_extension='mp4').first()
    video.download(output_path)


def extract_audio_from_video(video_path, audio_path):
    video = VideoFileClip(video_path)
    audio = video.audio
    audio.write_audiofile(audio_path)


def find_first_mp4(folder_path):
    files = os.listdir(folder_path)
    for file in files:
        if file.endswith('.mp4'):
            return os.path.join(folder_path, file)
    return None
def audio_to_text_whisper(audio_path):
    print("Calling trascribe from whisper...")
    with open(audio_path, "rb") as audio_file:
        transcript = openai.Audio.transcribe("whisper-1", audio_file)
    return transcript["text"]


def chunk_text(text, max_chars):
    return textwrap.wrap(text, width=max_chars)


def summarize_chunk(part, total_part, chunk):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system",
             "content": "You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible. You specialize in taking text from youtube video recipe in multiple part return a summary of each one."},
            {"role": "user", "content": f"Summarize chunk Part {part}/{total_part} text: {chunk}"}
        ]
    )
    summary = response['choices'][0]['message']['content']
    return summary


def summarize_text_chatgpt(text):
    max_chars = 2048  # Adjust this value according to your needs, keeping in mind the 4000 token limit
    text_chunks = chunk_text(text, max_chars)
    chunk_count = len(text_chunks)
    messages = [{"role": "system",
                 "content": "You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible. You specialize in taking text from youtube video recipe in as a summary of all its chunks and return a recipe at the end."}]

    summarized_text = text
    if chunk_count > 1:
        summarized_chunks = [summarize_chunk(i, chunk_count, chunk) for i, chunk in enumerate(text_chunks)]
        summarized_text = ' '.join(summarized_chunks)


    messages.append(
        {"role": "user",
         "content": f"Now, please summarize the following chunked summaries as a recipe with instructions to follow: {summarized_text}"})

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages
    )

    summary = response['choices'][0]['message']['content']
    return summary


# Download YouTube video
audio_path = './download/audio.mp3'
download_path = './download/'
url = 'YOUTUBE_VIDEO_URL'
#
download_youtube_video(url, download_path)
# Extract audio from the video
video_path = find_first_mp4(download_path)
extract_audio_from_video(video_path, audio_path)

# Convert audio to text using Whisper ASR API
text = audio_to_text_whisper(audio_path)
# Summarize text using OpenAI ChatGPT
summary = summarize_text_chatgpt(text)
print(summary)