Created
March 12, 2024 12:32
-
-
Save jrknox1977/b411fe214c544dc0937957a773dcb270 to your computer and use it in GitHub Desktop.
YouTube Video Analysis with Claude 3
This file contains hidden or 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
# YouTube Video Analysis with Claude 3 | |
# By KNOX @jr_knox1977 | |
# | |
# Prompt "inpired" by several prompts from Daniel Meissler's Fabric Project: | |
# https://github.com/danielmiessler/fabric | |
# | |
# This script expects a .env file with the following content: | |
# CLAUDE_API_KEY=your_api | |
# | |
# Quick pip: python3 -m pip install anthropic youtube_transcript_api python-dotenv | |
import argparse | |
import os | |
from dotenv import load_dotenv | |
import anthropic | |
from youtube_transcript_api import YouTubeTranscriptApi | |
system_prompt = """# YouTube Video Analyst | |
You are an expert YouTube video content analyzer focused on extracting surprising, insightful, and interesting ideas from informative videos on a wide range of topics, including but not limited to: | |
- The purpose and meaning of life | |
- Human flourishing | |
- The role of technology in the future of humanity | |
- Artificial intelligence and its effect on humans | |
- Memes, learning, reading, and books | |
- Continuous improvement and personal development | |
- Technical tutorials and coding walkthroughs | |
- Other educational and informative subjects | |
Take a step back and think deeply about how to achieve the best possible analysis by following the steps below. | |
## OUTPUT SECTIONS | |
- **SUMMARY:** Provide a concise overview of the video content in 50 words or less, including the presenter and the main topics discussed. | |
- **MAIN POINTS:** List the 10 most important points covered in the video, using no more than 15 words per point. | |
- **DETAILED SENTENCE OUTLINE:** Create a comprehensive sentence outline of the video content, capturing the main ideas, supporting points, and key details. Each point in the outline should be a complete sentence that summarizes a specific part of the video, maintaining a logical flow and structure. The sentence outline should provide a detailed summary of the entire video. | |
- **KEY IDEAS:** Extract 20 to 50 of the most surprising, insightful, and/or interesting ideas from the video. If there are fewer than 50, collect all of them. Ensure you extract at least 20. | |
- **KEY TAKEAWAYS:** Identify the 5 most valuable insights or lessons from the video. | |
- **ACTIONABLE ADVICE:** Provide 3 specific action items for the viewer to apply the video's lessons. | |
- **IDEA IMPLICATIONS:** Explore how the key ideas could be applied or built upon in 3-5 points. | |
- **QUOTES:** Highlight 10-15 of the most surprising, insightful, or interesting verbatim quotes from the video. | |
- **HABITS:** Identify 5-10 practical and useful personal habits mentioned by the presenter or speakers in the video. | |
- **FACTS:** List 5-10 surprising, insightful, or interesting facts about the world that are mentioned in the video. | |
- **REFERENCES:** Compile all mentions of books, articles, tools, projects, or other sources of inspiration cited in the video. | |
- **RECOMMENDATIONS:** Present 5-10 of the most actionable or valuable recommendations derived from the video content. | |
- **FURTHER EXPLORATION:** Suggest 3 related topics or questions for further exploration. | |
## OUTPUT INSTRUCTIONS | |
- Output human-readable Markdown. | |
- Use bullet points for all sections except SUMMARY. | |
- Do not include warnings or notes; only output the requested sections. | |
- Do not repeat ideas, points, insights, action items, quotes, habits, facts, or references. | |
- Do not start items with the same opening words. | |
- Focus on extracting the most useful, informative, and actionable content for the viewer. | |
- Tailor the length and depth of your analysis to the substance and complexity of the video content. | |
- Dive deep into the implications and potential of the key ideas presented. | |
- Present the content objectively, without agreeing or disagreeing with the video's perspective. | |
- Ensure you follow ALL these instructions when creating your output.""" | |
# Setup argparse | |
parser = argparse.ArgumentParser(description='Fetch YouTube video transcript and interact with Claude model.') | |
parser.add_argument('-u', '--url', required=True, help='YouTube video URL') | |
parser.add_argument('-m', '--model', choices=['sonnet', 'opus'], default='opus', help='Choose the model for the YouTube analysis response (default: opus)') | |
# Parse the arguments | |
args = parser.parse_args() | |
# Function to fetch the transcript | |
def fetch_transcript(video_url): | |
video_id = video_url.split('=')[-1] | |
transcript = YouTubeTranscriptApi.get_transcript(video_id) | |
return ' '.join([entry['text'] for entry in transcript]) | |
# Fetch the transcript | |
transcript_text = fetch_transcript(args.url) | |
# Load environment variables from .env file | |
load_dotenv() | |
# Access the API key | |
api_key = os.environ['CLAUDE_API_KEY'] | |
# Initialize the client with the API key | |
client = anthropic.Anthropic(api_key=api_key) | |
# Ask Claude for a markdown file name suggestion based on the transcript using the medium model | |
response = client.messages.create( | |
model="claude-3-sonnet-20240229", # Use the medium Claude model | |
max_tokens=100, | |
temperature=0.5, | |
system="Suggest a markdown file name based on the following transcript. Only respond with the markdown file name or there will be an error:", | |
messages=[ | |
{"role": "user", "content": transcript_text} | |
] | |
) | |
# Extract the suggested file name from the response | |
suggested_file_name = ''.join([block.text for block in response.content]) | |
# Check if the suggested file name already has a .md extension | |
name, ext = os.path.splitext(suggested_file_name) | |
if ext.lower() != '.md': | |
suggested_file_name += '.md' | |
print(f"Suggested file name: {suggested_file_name}") | |
# Use the new internal system prompt directly | |
model_choice = args.model | |
response = client.messages.create( | |
model=f"claude-3-{model_choice}-20240229", # Use the specified model for the analysis response | |
max_tokens=1000, | |
temperature=0.0, | |
system=system_prompt, # Use the new internal system prompt | |
messages=[ | |
{"role": "user", "content": transcript_text} | |
] | |
) | |
# Extract the response text from the response content blocks | |
response_text = ''.join([block.text for block in response.content]) | |
# Prepend the original YouTube URL to the top of response_text with a Markdown heading | |
response_text = f"# Original Source: {args.url}\n\n" + response_text | |
# Append the original transcript to the end of response_text with a Markdown heading | |
response_text += "\n\n## Original Transcript:\n\n" + transcript_text | |
# Save the response into a single file with the generated file name | |
with open(suggested_file_name, 'w') as response_file: | |
response_file.write(response_text) | |
print(f"All responses and original transcript saved to file '{suggested_file_name}'.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment