Merge pull request #76 from Madhuvod/chat-youtube

fix: using youtube-transcript-api
This commit is contained in:
Shubham Saboo 2025-01-04 12:27:44 -06:00 committed by GitHub
commit 8ddd85b02f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 12 deletions

View file

@ -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

View file

@ -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)
try:
answer = app.chat(prompt)
st.write(answer)
except Exception as e:
st.error(f"Error chatting with the video: {e}")

View file

@ -1,2 +1,3 @@
streamlit
embedchain[youtube]
embedchain[youtube]
youtube-transcript-api==0.6.3