Last active
March 13, 2024 10:44
-
-
Save Belphemur/e92057776c8c375c6bcd213b122623dd to your computer and use it in GitHub Desktop.
Summarize Youtube Recipe video into text intruction with OpenAI ChatGPT and Whisper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From Youtube: https://www.youtube.com/watch?v=mh2AXh1eRmE | |
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! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment