1919 read_file ,
2020 save_response_as_file ,
2121)
22- from modules .persistance import SQL_DB , Transcript , Video , delete_video
22+ from modules .persistance import (
23+ SQL_DB ,
24+ LibraryEntry ,
25+ Transcript ,
26+ Video ,
27+ delete_video ,
28+ save_library_entry ,
29+ )
2330from modules .rag import (
2431 CHUNK_SIZE_TO_K_MAPPING ,
2532 embed_excerpts ,
6067# --- SQLite stuff ---
6168SQL_DB .connect (reuse_if_open = True )
6269# create tables if they don't already exist
63- SQL_DB .create_tables ([Video , Transcript ], safe = True )
70+ SQL_DB .create_tables ([Video , Transcript , LibraryEntry ], safe = True )
6471# --- end ---
6572
6673# --- Chroma ---
@@ -95,6 +102,26 @@ def refresh_page(message: str):
95102 st .rerun ()
96103
97104
105+ # variable for holding the Video object
106+ saved_video : None | Video = None
107+
108+
109+ def save_response_to_lib ():
110+ """Wrapper func for saving responses to the library."""
111+ try :
112+ save_library_entry (
113+ entry_type = "A" ,
114+ question_text = st .session_state .user_prompt ,
115+ response_text = st .session_state .response ,
116+ video = saved_video ,
117+ )
118+ except Exception as e :
119+ st .error ("Saving failed! If you are a developer, see logs for details!" )
120+ logging .error ("Error when saving library entry: %s" , e )
121+ else :
122+ st .success ("Saved answer to library successfully!" )
123+
124+
98125if (
99126 is_api_key_set ()
100127 and is_api_key_valid (st .session_state .openai_api_key )
@@ -129,7 +156,7 @@ def refresh_page(message: str):
129156 )
130157 # --- end ---
131158
132- # fetch saved videos from SQLite
159+ # fetch all saved videos from SQLite
133160 saved_videos = Video .select ()
134161
135162 # create columns
@@ -139,7 +166,10 @@ def refresh_page(message: str):
139166 selected_video_title = st .selectbox (
140167 label = "Select from already processed videos" ,
141168 placeholder = "Choose a video" ,
142- options = [video .title for video in saved_videos ],
169+ # only videos with an associated transcript can be selected
170+ options = [
171+ video .title for video in saved_videos if video .transcripts .count () != 0
172+ ],
143173 index = None ,
144174 key = "selected_video" ,
145175 help = get_default_config_value ("help_texts.selected_video" ),
@@ -148,7 +178,6 @@ def refresh_page(message: str):
148178 label = "Or enter the URL of a new video:" , disabled = is_video_selected ()
149179 )
150180
151- saved_video = None
152181 if is_video_selected ():
153182 saved_video = Video .get (Video .title == selected_video_title )
154183
@@ -218,16 +247,18 @@ def refresh_page(message: str):
218247 url = url_input ,
219248 )
220249
221- # 1. fetch transcript from youtube
250+ # 1. save video in the database
222251 saved_video = Video .create (
223252 yt_video_id = extract_youtube_video_id (url_input ),
224253 link = url_input ,
225254 title = video_metadata ["name" ],
226255 channel = video_metadata ["channel" ],
227256 saved_on = dt .now (),
228257 )
258+ # 2. fetch transcript from youtube
229259 original_transcript = fetch_youtube_transcript (url_input )
230260
261+ # 3. save transcript, ormore precisely, information about it, in the database
231262 saved_transcript = Transcript .create (
232263 video = saved_video ,
233264 original_token_num = num_tokens_from_string (
@@ -236,6 +267,7 @@ def refresh_page(message: str):
236267 ),
237268 )
238269
270+ # 4. get an already existing or create a new collection in ChromaDB
239271 collection = chroma_client .get_or_create_collection (
240272 name = randomname .get_name (),
241273 metadata = {
@@ -245,7 +277,7 @@ def refresh_page(message: str):
245277 },
246278 )
247279
248- # 2 . create excerpts. Either
280+ # 5 . create excerpts. Either
249281 # - from original transcript
250282 # - or from whisper transcription if transcription checkbox is checked
251283 if transcription_checkbox :
@@ -271,7 +303,7 @@ def refresh_page(message: str):
271303 len_func = "tokens" ,
272304 )
273305
274- # 3 . embed/index transcript excerpts
306+ # 6 . embed/index transcript excerpts
275307 Transcript .update (
276308 {
277309 Transcript .preprocessed : transcription_checkbox ,
@@ -322,10 +354,10 @@ def refresh_page(message: str):
322354
323355 prompt = st .chat_input (
324356 placeholder = "Ask a question or provide a topic covered in the video" ,
325- key = "user_prompt" ,
326357 )
327358
328359 if prompt :
360+ st .session_state .user_prompt = prompt
329361 with st .spinner ("Generating answer..." ):
330362 try :
331363 relevant_docs = find_relevant_documents (
@@ -340,13 +372,19 @@ def refresh_page(message: str):
340372 llm = openai_chat_model ,
341373 relevant_docs = relevant_docs ,
342374 )
375+ st .session_state .response = response
343376 except Exception as e :
344377 logging .error (
345378 "An unexpected error occurred: %s" , str (e ), exc_info = True
346379 )
347380 st .error (GENERAL_ERROR_MESSAGE )
348381 else :
349- st .write (response )
382+ st .write (st .session_state .response )
383+ st .button (
384+ label = "Save this response to your library" ,
385+ on_click = save_response_to_lib ,
386+ help = "Unfortunately, the response disappears in this view after saving it to the library. However, it will be visible on the 'Library' page!" ,
387+ )
350388 with st .expander (
351389 label = "Show chunks retrieved from index and provided to the model as context"
352390 ):
0 commit comments