-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
172 lines (154 loc) Β· 5.44 KB
/
app.py
File metadata and controls
172 lines (154 loc) Β· 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import streamlit as st
from youtube_rag import YouTubeRAG
import os
import re
import time
# Custom CSS
st.markdown("""
<style>
header {display: none !important;}
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
.stApp {
background: linear-gradient(135deg, #2c3e50 0%, #1a1a2e 100%);
}
.main {
padding: 2rem;
max-width: 1200px;
margin: 0 auto;
}
.stButton>button {
background: linear-gradient(45deg, #2196F3, #21CBF3);
color: white;
border: none;
border-radius: 10px;
padding: 10px 20px;
font-weight: bold;
transition: all 0.3s ease;
}
.stButton>button:hover {
transform: scale(1.05);
box-shadow: 0 5px 15px rgba(33, 203, 243, 0.4);
}
.stTextInput>div>div>input {
border-radius: 10px;
border: 2px solid #2196F3;
background: rgba(255, 255, 255, 0.1);
color: #FF0000 !important;
max-width: 800px;
}
.chat-message {
padding: 1rem;
border-radius: 10px;
margin: 0.5rem 0;
background: rgba(255, 255, 255, 0.1);
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.user-message {
background: rgba(227, 242, 253, 0.2);
}
.assistant-message {
background: rgba(245, 245, 245, 0.2);
}
h1, h2, h3, p {
color: #ffffff;
}
.stMarkdown {
color: #ffffff;
}
.block-container {
max-width: 1400px;
padding-top: 2rem !important;
padding-right: 2rem !important;
padding-left: 2rem !important;
margin: 0 auto !important;
}
[data-testid="stVideo"] {
width: 100% !important;
max-width: 800px !important;
margin: 0 auto;
}
[data-testid="stVideo"] > div {
width: 100% !important;
}
[data-testid="stVideo"] video {
width: 100% !important;
}
div[data-testid="stHorizontalBlock"] > div {
padding: 0 1rem;
}
div[data-testid="stHorizontalBlock"] {
gap: 2rem;
}
</style>
""", unsafe_allow_html=True)
# Initialize session state
if "messages" not in st.session_state:
st.session_state.messages = []
if "rag" not in st.session_state:
st.session_state.rag = YouTubeRAG()
if "current_video_id" not in st.session_state:
st.session_state.current_video_id = None
if "processing" not in st.session_state:
st.session_state.processing = False
def extract_video_id(youtube_link: str) -> str:
"""Extract video ID from a YouTube URL."""
video_id_pattern = r'(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})'
match = re.match(video_id_pattern, youtube_link)
if match:
return match.group(1)
return None
# Title and description with animation
st.markdown("""
<div style='text-align: center;'>
<h1 style='color: #2196F3;'>YouTube RAGbot π₯</h1>
<p style='color: #666;'>Your AI-powered video analysis companion</p>
</div>
""", unsafe_allow_html=True)
# Create two columns for video and chat
col1, col2 = st.columns([1, 1])
# YouTube link input and video display in first column
with col1:
st.markdown("### π¬ Enter YouTube Link")
youtube_link = st.text_input("", key="youtube_link", placeholder="Paste your YouTube link here...")
if youtube_link:
video_id = extract_video_id(youtube_link)
if video_id:
st.session_state.current_video_id = video_id
st.video(f"https://www.youtube.com/watch?v={video_id}")
if st.button("β¨ Get Transcript", key="process_button"):
st.session_state.processing = True
with st.spinner("π Processing video transcript..."):
# Create embeddings directory
os.makedirs("db", exist_ok=True)
# Load video
if st.session_state.rag.load_video(youtube_link):
st.success("β
Video transcript loaded! You can now ask questions about the content.")
# Clear previous chat history when loading new video
st.session_state.messages = []
else:
st.error("β Failed to load video. Please check if the video has captions available.")
st.session_state.processing = False
else:
st.error("β Invalid YouTube link. Please enter a valid YouTube URL.")
# Chat interface in second column
with col2:
st.markdown("### π¬ Chat")
# Display chat messages with animations
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Chat input with custom styling
if prompt := st.chat_input("Ask a question about the video...", key="chat_input"):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message
with st.chat_message("user"):
st.markdown(prompt)
# Generate response
with st.chat_message("assistant"):
with st.spinner("π€ Thinking..."):
response = st.session_state.rag.chat(prompt)
st.markdown(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})