Skip to content

100+ AI Agents, Agent Skills and RAG Apps - Free and Open Source.

resolved the issues - using youtube-transcript-api

Madhu7238820ad968
diff --git a/chat_with_X_tutorials/chat_with_youtube_videos/README.md b/chat_with_X_tutorials/chat_with_youtube_videos/README.md
index 69e8d8ee..281fd2e1 100644
--- a/chat_with_X_tutorials/chat_with_youtube_videos/README.md
+++ b/chat_with_X_tutorials/chat_with_youtube_videos/README.md
@@ -1,6 +1,6 @@
## 📽️ Chat with YouTube Videos
-LLM app with RAG to chat with YouTube Videos in just 30 lines of Python Code. The app uses Retrieval Augmented Generation (RAG) to provide accurate answers to questions based on the content of the uploaded video.
+LLM app with RAG to chat with YouTube Videos with OpenAI's gpt-4o, mem0/embedchain as memory and the youtube-transcript-api. The app uses Retrieval Augmented Generation (RAG) to provide accurate answers to questions based on the content of the uploaded video.
### Features
diff --git a/chat_with_X_tutorials/chat_with_youtube_videos/chat_youtube.py b/chat_with_X_tutorials/chat_with_youtube_videos/chat_youtube.py
index 7246e462..297176a7 100644
--- a/chat_with_X_tutorials/chat_with_youtube_videos/chat_youtube.py
+++ b/chat_with_X_tutorials/chat_with_youtube_videos/chat_youtube.py
@@ -1,18 +1,36 @@
-# Import the required libraries
import tempfile
import streamlit as st
from embedchain import App
+from youtube_transcript_api import YouTubeTranscriptApi
+from typing import Tuple
-# Define the embedchain_bot function
-def embedchain_bot(db_path, api_key):
+def embedchain_bot(db_path: str, api_key: str) -> App:
return App.from_config(
config={
- "llm": {"provider": "openai", "config": {"model": "gpt-4o", "temperature": 0.5, "api_key": api_key}},
+ "llm": {"provider": "openai", "config": {"model": "gpt-4", "temperature": 0.5, "api_key": api_key}},
"vectordb": {"provider": "chroma", "config": {"dir": db_path}},
"embedder": {"provider": "openai", "config": {"api_key": api_key}},
}
)
+def extract_video_id(video_url: str) -> str:
+ if "youtube.com/watch?v=" in video_url:
+ return video_url.split("v=")[-1].split("&")[0]
+ elif "youtube.com/shorts/" in video_url:
+ return video_url.split("/shorts/")[-1].split("?")[0]
+ else:
+ raise ValueError("Invalid YouTube URL")
+
+def fetch_video_data(video_url: str) -> Tuple[str, str]:
+ try:
+ video_id = extract_video_id(video_url)
+ transcript = YouTubeTranscriptApi.get_transcript(video_id)
+ transcript_text = " ".join([entry["text"] for entry in transcript])
+ return "Unknown", transcript_text # Title is set to "Unknown" since we're not fetching it
+ except Exception as e:
+ st.error(f"Error fetching transcript: {e}")
+ return "Unknown", "No transcript available for this video."
+
# Create Streamlit app
st.title("Chat with YouTube Video 📺")
st.caption("This app allows you to chat with a YouTube video using OpenAI API")
@@ -30,13 +48,21 @@ if openai_access_token:
video_url = st.text_input("Enter YouTube Video URL", type="default")
# Add the video to the knowledge base
if video_url:
- app.add(video_url, data_type="youtube_video")
- st.success(f"Added {video_url} to knowledge base!")
+ try:
+ title, transcript = fetch_video_data(video_url)
+ if transcript != "No transcript available for this video.":
+ app.add(transcript, data_type="text", metadata={"title": title, "url": video_url})
+ st.success(f"Added video '{title}' to knowledge base!")
+ else:
+ st.warning(f"No transcript available for video '{title}'. Cannot add to knowledge base.")
+ except Exception as e:
+ st.error(f"Error adding video: {e}")
# Ask a question about the video
prompt = st.text_input("Ask any question about the YouTube Video")
# Chat with the video
if prompt:
- answer = app.chat(prompt)
- st.write(answer)
-
-
\ No newline at end of file
+ try:
+ answer = app.chat(prompt)
+ st.write(answer)
+ except Exception as e:
+ st.error(f"Error chatting with the video: {e}")
\ No newline at end of file
diff --git a/chat_with_X_tutorials/chat_with_youtube_videos/requirements.txt b/chat_with_X_tutorials/chat_with_youtube_videos/requirements.txt
index 3d083f65..298d034b 100644
--- a/chat_with_X_tutorials/chat_with_youtube_videos/requirements.txt
+++ b/chat_with_X_tutorials/chat_with_youtube_videos/requirements.txt
@@ -1,2 +1,3 @@
streamlit
-embedchain[youtube]
\ No newline at end of file
+embedchain[youtube]
+youtube-transcript-api==0.6.3
\ No newline at end of file