Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,296 +1,58 @@
|
|
| 1 |
# main.py
|
| 2 |
import streamlit as st
|
| 3 |
-
import os
|
| 4 |
-
import tempfile
|
| 5 |
-
import whisper
|
| 6 |
-
import yt_dlp
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from config import Config
|
| 9 |
from transcript_utils import extract_video_id, fetch_transcript
|
| 10 |
from vector_utils import build_vector_store, get_retriever
|
| 11 |
from qa_pipeline import run_qa
|
| 12 |
-
from youtube_transcript_api import YouTubeTranscriptApi
|
| 13 |
-
from youtube_transcript_api._errors import TranscriptsDisabled, NoTranscriptFound, VideoUnavailable
|
| 14 |
|
| 15 |
# Load environment variables
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
st.set_page_config(page_title="YouTube Video Chat-QA", layout="centered")
|
| 19 |
-
st.title("
|
| 20 |
|
| 21 |
# Initialize session state
|
| 22 |
if 'ready' not in st.session_state:
|
| 23 |
st.session_state.ready = False
|
| 24 |
if 'messages' not in st.session_state:
|
| 25 |
st.session_state.messages = []
|
| 26 |
-
if 'transcript_method' not in st.session_state:
|
| 27 |
-
st.session_state.transcript_method = None
|
| 28 |
-
|
| 29 |
-
@st.cache_resource
|
| 30 |
-
def load_whisper_model():
|
| 31 |
-
"""Load Whisper model with caching for better performance"""
|
| 32 |
-
try:
|
| 33 |
-
return whisper.load_model("base")
|
| 34 |
-
except Exception as e:
|
| 35 |
-
st.error(f"Failed to load Whisper model: {str(e)}")
|
| 36 |
-
return None
|
| 37 |
-
|
| 38 |
-
def download_audio_from_youtube(video_id):
|
| 39 |
-
"""Download audio from YouTube video using yt-dlp with better error handling"""
|
| 40 |
-
try:
|
| 41 |
-
# Create temporary directory
|
| 42 |
-
temp_dir = tempfile.mkdtemp()
|
| 43 |
-
output_path = os.path.join(temp_dir, f"{video_id}.%(ext)s")
|
| 44 |
-
|
| 45 |
-
ydl_opts = {
|
| 46 |
-
'format': 'bestaudio/best',
|
| 47 |
-
'outtmpl': output_path,
|
| 48 |
-
'extractaudio': True,
|
| 49 |
-
'audioformat': 'mp3',
|
| 50 |
-
'noplaylist': True,
|
| 51 |
-
'no_warnings': True,
|
| 52 |
-
'quiet': True,
|
| 53 |
-
# Add user agent to avoid bot detection
|
| 54 |
-
'http_headers': {
|
| 55 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 56 |
-
},
|
| 57 |
-
# Add retries for network issues
|
| 58 |
-
'retries': 3,
|
| 59 |
-
'socket_timeout': 30,
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 63 |
-
url = f"https://www.youtube.com/watch?v={video_id}"
|
| 64 |
-
info = ydl.extract_info(url, download=True)
|
| 65 |
-
|
| 66 |
-
# Find the downloaded file
|
| 67 |
-
for file in os.listdir(temp_dir):
|
| 68 |
-
if file.startswith(video_id):
|
| 69 |
-
return os.path.join(temp_dir, file)
|
| 70 |
-
|
| 71 |
-
return None
|
| 72 |
-
except Exception as e:
|
| 73 |
-
st.error(f"Audio download failed: {str(e)}")
|
| 74 |
-
return None
|
| 75 |
-
|
| 76 |
-
def transcribe_with_whisper(video_id):
|
| 77 |
-
"""Fallback transcription using Whisper when YouTube transcript fails"""
|
| 78 |
-
try:
|
| 79 |
-
# Load Whisper model
|
| 80 |
-
model = load_whisper_model()
|
| 81 |
-
if not model:
|
| 82 |
-
return None
|
| 83 |
-
|
| 84 |
-
# Download audio
|
| 85 |
-
audio_file = download_audio_from_youtube(video_id)
|
| 86 |
-
if not audio_file:
|
| 87 |
-
return None
|
| 88 |
-
|
| 89 |
-
# Transcribe using Whisper
|
| 90 |
-
with st.spinner("🤖 Transcribing audio using AI (this may take a few minutes)..."):
|
| 91 |
-
result = model.transcribe(audio_file)
|
| 92 |
-
|
| 93 |
-
# Clean up audio file
|
| 94 |
-
try:
|
| 95 |
-
os.remove(audio_file)
|
| 96 |
-
os.rmdir(os.path.dirname(audio_file))
|
| 97 |
-
except:
|
| 98 |
-
pass
|
| 99 |
-
|
| 100 |
-
# Convert to transcript format compatible with your existing code
|
| 101 |
-
if result and result.get("text"):
|
| 102 |
-
transcript = [{
|
| 103 |
-
"text": result["text"],
|
| 104 |
-
"start": 0,
|
| 105 |
-
"duration": len(result["text"].split()) * 0.5
|
| 106 |
-
}]
|
| 107 |
-
return transcript
|
| 108 |
-
|
| 109 |
-
return None
|
| 110 |
-
|
| 111 |
-
except Exception as e:
|
| 112 |
-
st.error(f"Whisper transcription failed: {str(e)}")
|
| 113 |
-
return None
|
| 114 |
-
|
| 115 |
-
def enhanced_fetch_transcript(video_id, languages=None):
|
| 116 |
-
"""Enhanced transcript fetching with multiple fallback methods"""
|
| 117 |
-
transcript = None
|
| 118 |
-
method_used = None
|
| 119 |
-
|
| 120 |
-
# Method 1: Try YouTube Transcript API first (CORRECTED)
|
| 121 |
-
try:
|
| 122 |
-
# CORRECT: Call static method directly, not as instance
|
| 123 |
-
transcript = YouTubeTranscriptApi.get_transcript(
|
| 124 |
-
video_id,
|
| 125 |
-
languages=languages or ['en', 'hi', 'auto-generated']
|
| 126 |
-
)
|
| 127 |
-
method_used = "YouTube API"
|
| 128 |
-
st.success("✅ Transcript fetched from YouTube")
|
| 129 |
-
|
| 130 |
-
except (TranscriptsDisabled, NoTranscriptFound, VideoUnavailable) as e:
|
| 131 |
-
st.warning("⚠️ YouTube transcript not available. Trying alternative method...")
|
| 132 |
-
|
| 133 |
-
# Method 2: Try with different language codes
|
| 134 |
-
try:
|
| 135 |
-
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
|
| 136 |
-
method_used = "YouTube API (English only)"
|
| 137 |
-
st.success("✅ English transcript found")
|
| 138 |
-
except:
|
| 139 |
-
pass
|
| 140 |
-
|
| 141 |
-
except Exception as e:
|
| 142 |
-
st.warning(f"⚠️ YouTube API error: {str(e)}")
|
| 143 |
-
|
| 144 |
-
# Method 3: Fallback to Whisper transcription (only if network allows)
|
| 145 |
-
if not transcript:
|
| 146 |
-
st.info("🔄 Attempting AI transcription as fallback...")
|
| 147 |
-
transcript = transcribe_with_whisper(video_id)
|
| 148 |
-
if transcript:
|
| 149 |
-
method_used = "Whisper AI"
|
| 150 |
-
st.success("✅ Transcript generated using AI")
|
| 151 |
-
|
| 152 |
-
return transcript, method_used
|
| 153 |
|
| 154 |
# Setup section for video link
|
| 155 |
-
with st.expander("
|
| 156 |
-
st.
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
else:
|
| 165 |
-
vid = extract_video_id(url)
|
| 166 |
-
if not vid:
|
| 167 |
-
st.error("❌ Invalid YouTube URL. Please check and try again.")
|
| 168 |
-
else:
|
| 169 |
-
st.session_state.video_id = vid
|
| 170 |
-
st.session_state.ready = True
|
| 171 |
-
st.session_state.messages = []
|
| 172 |
-
st.rerun()
|
| 173 |
-
|
| 174 |
-
with col2:
|
| 175 |
-
if st.button("🔄 Reset"):
|
| 176 |
-
for key in ['ready', 'video_id', 'messages', 'input_url', 'transcript_method']:
|
| 177 |
-
if key in st.session_state:
|
| 178 |
-
del st.session_state[key]
|
| 179 |
-
st.rerun()
|
| 180 |
|
| 181 |
# Chat interface after video is ready
|
| 182 |
def chat_interface():
|
| 183 |
-
st.
|
| 184 |
-
|
| 185 |
-
# Display video
|
| 186 |
-
try:
|
| 187 |
-
st.video(f"https://www.youtube.com/watch?v={st.session_state.video_id}")
|
| 188 |
-
except Exception as e:
|
| 189 |
-
st.error(f"Error displaying video: {str(e)}")
|
| 190 |
-
|
| 191 |
-
# Show transcript method if available
|
| 192 |
-
if st.session_state.get('transcript_method'):
|
| 193 |
-
st.info(f"📝 Transcript source: {st.session_state.transcript_method}")
|
| 194 |
-
|
| 195 |
-
# Display chat history first
|
| 196 |
-
for msg in st.session_state.messages:
|
| 197 |
-
with st.chat_message(msg['role']):
|
| 198 |
-
st.write(msg['content'])
|
| 199 |
-
|
| 200 |
# Input new message
|
| 201 |
-
new_q = st.chat_input("
|
| 202 |
-
|
| 203 |
if new_q:
|
| 204 |
-
# Add user message
|
| 205 |
st.session_state.messages.append({'role': 'user', 'content': new_q})
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
)
|
| 219 |
-
|
| 220 |
-
# Store the method used
|
| 221 |
-
if method_used:
|
| 222 |
-
st.session_state.transcript_method = method_used
|
| 223 |
-
|
| 224 |
-
if not transcript:
|
| 225 |
-
error_msg = """
|
| 226 |
-
❌ **Unable to obtain transcript for this video.**
|
| 227 |
-
|
| 228 |
-
This could be due to:
|
| 229 |
-
- Network connectivity issues in the deployment environment
|
| 230 |
-
- YouTube's restrictions on cloud platforms (common on Hugging Face Spaces)
|
| 231 |
-
- Video has no available transcripts/captions
|
| 232 |
-
- The video is private or restricted
|
| 233 |
-
|
| 234 |
-
**Suggestions:**
|
| 235 |
-
- Try a different video with verified captions
|
| 236 |
-
- Use videos from educational channels (usually have transcripts)
|
| 237 |
-
- Test locally if possible
|
| 238 |
-
"""
|
| 239 |
-
st.markdown(error_msg)
|
| 240 |
-
st.session_state.messages.append({
|
| 241 |
-
'role': 'assistant',
|
| 242 |
-
'content': error_msg
|
| 243 |
-
})
|
| 244 |
-
else:
|
| 245 |
-
try:
|
| 246 |
-
# Build vector store and get answer
|
| 247 |
-
vector_store = build_vector_store(transcript, None, Config.EMBEDDING_MODEL)
|
| 248 |
-
retriever = get_retriever(vector_store)
|
| 249 |
-
answer = run_qa(retriever, new_q)
|
| 250 |
-
|
| 251 |
-
# Display answer
|
| 252 |
-
st.write(answer)
|
| 253 |
-
st.session_state.messages.append({'role': 'assistant', 'content': answer})
|
| 254 |
-
|
| 255 |
-
except Exception as e:
|
| 256 |
-
error_msg = f"❌ Error processing your question: {str(e)}"
|
| 257 |
-
st.error(error_msg)
|
| 258 |
-
st.session_state.messages.append({
|
| 259 |
-
'role': 'assistant',
|
| 260 |
-
'content': error_msg
|
| 261 |
-
})
|
| 262 |
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
# Welcome message
|
| 268 |
-
st.markdown("""
|
| 269 |
-
### 👋 Welcome to YouTube Video Chat-QA!
|
| 270 |
-
|
| 271 |
-
This app allows you to:
|
| 272 |
-
- 📺 Load any YouTube video
|
| 273 |
-
- 💬 Ask questions about the video content
|
| 274 |
-
- 🤖 Get AI-powered answers based on the video transcript
|
| 275 |
-
|
| 276 |
-
**⚠️ Note for Hugging Face Spaces users:**
|
| 277 |
-
Due to network restrictions, some features may be limited. The app works best with:
|
| 278 |
-
- Educational videos with clear captions
|
| 279 |
-
- Popular videos from established channels
|
| 280 |
-
- Videos with manual (not auto-generated) transcripts
|
| 281 |
-
""")
|
| 282 |
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
st.markdown("### ℹ️ App Status")
|
| 286 |
-
st.markdown("""
|
| 287 |
-
**Network Status:**
|
| 288 |
-
- ⚠️ Limited connectivity (Cloud environment)
|
| 289 |
-
- ✅ YouTube Transcript API (when available)
|
| 290 |
-
- ❌ Whisper AI (Network restricted)
|
| 291 |
-
""")
|
| 292 |
-
|
| 293 |
-
if st.session_state.ready:
|
| 294 |
-
st.markdown(f"**Current Video ID:** `{st.session_state.video_id}`")
|
| 295 |
-
if st.session_state.get('transcript_method'):
|
| 296 |
-
st.markdown(f"**Transcript Method:** {st.session_state.transcript_method}")
|
|
|
|
| 1 |
# main.py
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from config import Config
|
| 5 |
from transcript_utils import extract_video_id, fetch_transcript
|
| 6 |
from vector_utils import build_vector_store, get_retriever
|
| 7 |
from qa_pipeline import run_qa
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Load environment variables
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
st.set_page_config(page_title="YouTube Video Chat-QA", layout="centered")
|
| 13 |
+
st.title("YouTube Video Chat-QA App")
|
| 14 |
|
| 15 |
# Initialize session state
|
| 16 |
if 'ready' not in st.session_state:
|
| 17 |
st.session_state.ready = False
|
| 18 |
if 'messages' not in st.session_state:
|
| 19 |
st.session_state.messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Setup section for video link
|
| 22 |
+
with st.expander("Setup Video", expanded=not st.session_state.ready):
|
| 23 |
+
url = st.text_input("YouTube Video Link", key='input_url')
|
| 24 |
+
if st.button("Submit Link"):
|
| 25 |
+
vid = extract_video_id(url)
|
| 26 |
+
if not vid:
|
| 27 |
+
st.error("Invalid YouTube URL.")
|
| 28 |
+
else:
|
| 29 |
+
st.session_state.video_id = vid
|
| 30 |
+
st.session_state.ready = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Chat interface after video is ready
|
| 33 |
def chat_interface():
|
| 34 |
+
st.video(f"https://www.youtube.com/watch?v={st.session_state.video_id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Input new message
|
| 36 |
+
new_q = st.chat_input("Ask a question about this video...")
|
|
|
|
| 37 |
if new_q:
|
|
|
|
| 38 |
st.session_state.messages.append({'role': 'user', 'content': new_q})
|
| 39 |
+
transcript = fetch_transcript(st.session_state.video_id, languages=["en", "hi"])
|
| 40 |
+
if not transcript:
|
| 41 |
+
st.session_state.messages.append({'role': 'assistant', 'content': "Transcript not available."})
|
| 42 |
+
else:
|
| 43 |
+
vector_store = build_vector_store(transcript, None, Config.EMBEDDING_MODEL)
|
| 44 |
+
retriever = get_retriever(vector_store)
|
| 45 |
+
answer = run_qa(retriever, new_q)
|
| 46 |
+
st.session_state.messages.append({'role': 'assistant', 'content': answer})
|
| 47 |
+
|
| 48 |
+
# Display chat history
|
| 49 |
+
for msg in st.session_state.messages:
|
| 50 |
+
st.chat_message(msg['role']).write(msg['content'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
if st.button("Exit Chat"):
|
| 53 |
+
for key in ['ready', 'video_id', 'messages', 'input_url']:
|
| 54 |
+
if key in st.session_state:
|
| 55 |
+
del st.session_state[key]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
if st.session_state.ready:
|
| 58 |
+
chat_interface()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|