Spaces:
Runtime error
Runtime error
aazizisoufiane
commited on
Commit
·
ad8fefa
1
Parent(s):
80a9bf5
first app
Browse files
app.py
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import Streamlit
|
2 |
+
import os
|
3 |
+
|
4 |
+
import openai
|
5 |
+
import streamlit as st
|
6 |
+
from streamlit_chat import message
|
7 |
+
|
8 |
+
from config import output_path_video, output_path_transcription
|
9 |
+
from keyword_retriever.keyword_retreiver import VideoRetriever
|
10 |
+
from logger import logger
|
11 |
+
from resource_loader.uploaded_video_loader import UploadedVideoLoader
|
12 |
+
from resource_loader.youtube_loader import YouTubeLoader
|
13 |
+
from summarization_service.summarizer import TranscriptSummary
|
14 |
+
from utils import check_file_exists, download_video, transcribe_video, load_transcription
|
15 |
+
|
16 |
+
st.set_page_config(page_title="Summary", layout="wide")
|
17 |
+
# from main import factory
|
18 |
+
|
19 |
+
# Initialize chat history
|
20 |
+
chat_history = []
|
21 |
+
|
22 |
+
logger.info(f"Build retriever")
|
23 |
+
|
24 |
+
|
25 |
+
def generate_response(prompt_input):
|
26 |
+
answer = transcript_summary.query_summary(prompt_input)
|
27 |
+
|
28 |
+
return answer
|
29 |
+
|
30 |
+
|
31 |
+
@st.cache_resource()
|
32 |
+
def factory_transcript(video_id, model):
|
33 |
+
ts = TranscriptSummary(doc_id=video_id, model=model)
|
34 |
+
logger.info("TranscriptSummary initialized")
|
35 |
+
return ts
|
36 |
+
|
37 |
+
|
38 |
+
@st.cache_resource()
|
39 |
+
def factory_video(video_id, top_k):
|
40 |
+
retriever = VideoRetriever(video_id=video_id, similarity_top_k=top_k)
|
41 |
+
logger.info("video_retriever initialized")
|
42 |
+
return retriever
|
43 |
+
|
44 |
+
|
45 |
+
with st.sidebar:
|
46 |
+
# Sidebar
|
47 |
+
st.title("Controls")
|
48 |
+
# Create a sidebar for the YouTube URL, search bar, and settings
|
49 |
+
# st.title("Settings")
|
50 |
+
# youtube_url = st.text_input("Enter YouTube URL:", value="https://www.youtube.com/watch?v=reUZRyXxUs4")
|
51 |
+
youtube_url = st.text_input("Enter YouTube URL:")
|
52 |
+
uploaded_video = st.file_uploader("Or upload a video...", type=['mp4', 'mov', 'avi', 'flv', 'mkv'])
|
53 |
+
if uploaded_video:
|
54 |
+
original_name = uploaded_video.name # Get the original name of the uploaded file
|
55 |
+
video_loader = UploadedVideoLoader(uploaded_video,
|
56 |
+
original_name) # youtube_url = st.text_input("Enter YouTube URL:")
|
57 |
+
|
58 |
+
# video_loader.download()
|
59 |
+
|
60 |
+
|
61 |
+
elif youtube_url:
|
62 |
+
# youtube_url = st.text_input("Enter YouTube URL:", value="https://www.youtube.com/watch?v=reUZRyXxUs4")
|
63 |
+
video_loader = YouTubeLoader(youtube_url, output_path_video)
|
64 |
+
|
65 |
+
similarity_top_k = st.number_input("Maximum Number of Results to Display", min_value=1, max_value=100, value=10)
|
66 |
+
|
67 |
+
chosen_LLM = st.selectbox("Choose Language Model", ["gpt-3.5-turbo", "default"])
|
68 |
+
api_key = st.text_input("OpenAI API Key", type="password")
|
69 |
+
|
70 |
+
if api_key:
|
71 |
+
openai.api_key = api_key
|
72 |
+
|
73 |
+
if youtube_url or uploaded_video:
|
74 |
+
video_file_path = os.path.join(output_path_video, f"{video_loader.video_id}.mp3")
|
75 |
+
transcription_file_path = os.path.join(output_path_transcription, f"{video_loader.video_id}.json")
|
76 |
+
|
77 |
+
if not check_file_exists(video_file_path):
|
78 |
+
download_video(video_loader, output_path_video)
|
79 |
+
else:
|
80 |
+
logger.info(f"Video already downloaded: {video_file_path}")
|
81 |
+
if not check_file_exists(transcription_file_path):
|
82 |
+
transcribe_video(video_loader, output_path_video, output_path_transcription)
|
83 |
+
else:
|
84 |
+
logger.info(f"Transcription already exists: {transcription_file_path}")
|
85 |
+
|
86 |
+
video_retriever = factory_video(video_loader.video_id, top_k=int(similarity_top_k))
|
87 |
+
transcript_summary = factory_transcript(video_loader.video_id, model=chosen_LLM)
|
88 |
+
|
89 |
+
docs = load_transcription(video_loader, output_path_transcription)
|
90 |
+
|
91 |
+
if 'current_video' not in st.session_state:
|
92 |
+
st.session_state.current_video = youtube_url
|
93 |
+
# Main Content - Top Section
|
94 |
+
col2, col3 = st.columns([3, 1])
|
95 |
+
|
96 |
+
# Main Content - Middle Section
|
97 |
+
video_slot = col2.empty()
|
98 |
+
|
99 |
+
with col2:
|
100 |
+
if isinstance(video_loader, UploadedVideoLoader):
|
101 |
+
video_slot.video(uploaded_video)
|
102 |
+
elif isinstance(video_loader, YouTubeLoader):
|
103 |
+
video_slot.video(youtube_url)
|
104 |
+
|
105 |
+
st.title("Summary")
|
106 |
+
# Display summary here
|
107 |
+
st.write(transcript_summary.get_document_summary())
|
108 |
+
# Initialize session_state for chat history if it doesn't exist
|
109 |
+
if 'chat_history' not in st.session_state:
|
110 |
+
st.session_state.chat_history = []
|
111 |
+
|
112 |
+
# Main Content - Bottom Section for Chat
|
113 |
+
st.title("Ask me")
|
114 |
+
|
115 |
+
with col3:
|
116 |
+
user_input = st.text_input("Search:")
|
117 |
+
if user_input:
|
118 |
+
|
119 |
+
if isinstance(video_loader, UploadedVideoLoader):
|
120 |
+
video_slot.video(uploaded_video)
|
121 |
+
elif isinstance(video_loader, YouTubeLoader):
|
122 |
+
video_slot.video(youtube_url)
|
123 |
+
|
124 |
+
raw_results = video_retriever.search(user_input)
|
125 |
+
for i, result in enumerate(raw_results):
|
126 |
+
text_content = result.node.text
|
127 |
+
start_time = int(result.node.metadata['start'])
|
128 |
+
|
129 |
+
full_youtube_url = f"{youtube_url}&t={start_time}s"
|
130 |
+
|
131 |
+
if st.button(text_content, key=f"button_{i}"):
|
132 |
+
st.session_state.current_video = full_youtube_url
|
133 |
+
if isinstance(video_loader, UploadedVideoLoader):
|
134 |
+
video_slot.video(uploaded_video, start_time=start_time)
|
135 |
+
elif isinstance(video_loader, YouTubeLoader):
|
136 |
+
video_slot.video(youtube_url, start_time=start_time)
|
137 |
+
|
138 |
+
with col2:
|
139 |
+
chat_placeholder = st.empty()
|
140 |
+
|
141 |
+
|
142 |
+
def on_btn_click():
|
143 |
+
del st.session_state.past[:]
|
144 |
+
del st.session_state.generated[:]
|
145 |
+
|
146 |
+
|
147 |
+
def on_input_change():
|
148 |
+
user_input = st.session_state.user_input
|
149 |
+
st.session_state.past.append(user_input)
|
150 |
+
st.session_state.generated.append("The messages from Bot\nWith new line")
|
151 |
+
|
152 |
+
|
153 |
+
if 'generated' not in st.session_state:
|
154 |
+
st.session_state['generated'] = []
|
155 |
+
if 'past' not in st.session_state:
|
156 |
+
st.session_state['past'] = []
|
157 |
+
|
158 |
+
with chat_placeholder.container():
|
159 |
+
for i in range(len(st.session_state['generated'])):
|
160 |
+
message(st.session_state['past'][i], is_user=True, key=f"{i}_user")
|
161 |
+
message(generate_response(st.session_state['past'][i]), key=f"{i}", allow_html=True, is_table=False)
|
162 |
+
|
163 |
+
st.button("Clear message", on_click=on_btn_click)
|
164 |
+
|
165 |
+
with st.container():
|
166 |
+
st.text_input("User Input:", on_change=on_input_change, key="user_input")
|