Nauman J Qazi commited on
Commit
4609d8b
·
1 Parent(s): 5ec4c01

Add Chat in Demo

Browse files
Files changed (3) hide show
  1. .gitignore +1 -1
  2. app.py +25 -15
  3. utility.py +16 -22
.gitignore CHANGED
@@ -4,5 +4,5 @@ __pycache__
4
  .env
5
  .venv
6
  .github
7
- shared_data/.lancedb
8
  shared_data/videos/yt_video/blackholes101nationalgeographic/audio.mp3
 
4
  .env
5
  .venv
6
  .github
7
+ shared_data/.lancedb/
8
  shared_data/videos/yt_video/blackholes101nationalgeographic/audio.mp3
app.py CHANGED
@@ -133,7 +133,8 @@ def chat_response_llvm(instruction):
133
  return result
134
  """
135
 
136
- def return_top_k_most_similar_docs(vid_table_name, query, max_docs=2, use_llm=False, caption_file_path=None):
 
137
  # ask to return top 3 most similar documents
138
  # Creating a LanceDB vector store
139
  print("Querying ", vid_table_name)
@@ -155,16 +156,15 @@ def return_top_k_most_similar_docs(vid_table_name, query, max_docs=2, use_llm=Fa
155
  with open(file_path, 'r', encoding='utf-8') as f:
156
  content = f.read()
157
  return content
158
- captions = read_vtt_file(caption_file_path)
 
 
 
159
  prompt = "Answer this query : " + query + " from the content " + captions
160
-
161
- # Get folder path from results metadata and read captions
162
- video_folder = os.path.dirname(results[0].metadata['extracted_frame_path'])
163
- captions_path = os.path.join(video_folder, 'captions.vtt')
164
- captions_content = read_vtt_file(captions_path)
165
-
166
  # Combine captions with prompt for LLM
167
- all_page_content = lvlm_inference_with_ollama(prompt + captions_content)
168
  else:
169
  all_page_content = "\n\n".join([result.page_content for result in results])
170
 
@@ -188,19 +188,21 @@ def process_url_and_init(youtube_url, from_gen=False):
188
  submit_btn2 = gr.update(visible=True)
189
  frame1 = gr.update(visible=True)
190
  frame2 = gr.update(visible=False)
 
191
  vid_filepath, vid_table_name = get_metadata_of_yt_video_with_captions(youtube_url, from_gen)
192
  video = gr.Video(vid_filepath,render=True)
193
- return url_input, submit_btn, video, vid_table_name, chatbox,submit_btn2, frame1, frame2
194
 
195
  def init_ui():
196
  with gr.Blocks() as demo:
 
197
  gr.Markdown("Welcome to video chat demo - Initial processing can take up to 2 minutes, and responses may be slow. Please be patient and avoid clicking repeatedly.")
198
  url_input = gr.Textbox(label="Enter YouTube URL", visible=False, elem_id='url-inp',value="https://www.youtube.com/watch?v=kOEDG3j1bjs", interactive=True)
199
  vid_table_name = gr.Textbox(label="Enter Table Name", visible=False, interactive=False)
200
  video = gr.Video()
201
  with gr.Row():
202
  submit_btn = gr.Button("Process Video By Download Subtitles")
203
- submit_btn_gen = gr.Button("Process Video By Generating Audio & Subtitles By AI")
204
 
205
  with gr.Row():
206
  chatbox = gr.Textbox(label="Enter the keyword/s and AI will get related captions and images", visible=False, value="event horizan", scale=4)
@@ -214,11 +216,19 @@ def init_ui():
214
  with gr.Row():
215
  frame1 = gr.Image(visible=False, interactive=False, scale=2)
216
  frame2 = gr.Image(visible=False, interactive=False, scale=2)
217
- submit_btn.click(fn=process_url_and_init, inputs=[url_input], outputs=[url_input, submit_btn, video, vid_table_name, chatbox,submit_btn_whisper, frame1, frame2])
218
- submit_btn_gen.click(fn=lambda x: process_url_and_init(x, from_gen=True), inputs=[url_input], outputs=[url_input, submit_btn, video, vid_table_name, chatbox,submit_btn_whisper, frame1, frame2])
219
  submit_btn_whisper.click(fn=return_top_k_most_similar_docs, inputs=[vid_table_name, chatbox], outputs=[response, frame1, frame2])
220
- caption_file = 'shared_data/videos/yt_video/' + vid_table_name + '/captions.vtt'
221
- submit_btn_chat.click(fn=lambda x: return_top_k_most_similar_docs(x, use_llm=True, caption_file_path=caption_file ), inputs=[vid_table_name, chatbox_llm], outputs=[response, frame1, frame2])
 
 
 
 
 
 
 
 
222
  reset_btn = gr.Button("Reload Page")
223
  reset_btn.click(None, js="() => { location.reload(); }")
224
  return demo
 
133
  return result
134
  """
135
 
136
+ def return_top_k_most_similar_docs(vid_table_name, query, use_llm=False):
137
+ max_docs=2
138
  # ask to return top 3 most similar documents
139
  # Creating a LanceDB vector store
140
  print("Querying ", vid_table_name)
 
156
  with open(file_path, 'r', encoding='utf-8') as f:
157
  content = f.read()
158
  return content
159
+ vid_table_name = vid_table_name.split('_table')[0]
160
+ caption_file = 'shared_data/videos/yt_video/' + vid_table_name + '/captions.vtt'
161
+ print("Caption file path ", caption_file)
162
+ captions = read_vtt_file(caption_file)
163
  prompt = "Answer this query : " + query + " from the content " + captions
164
+ print("Prompt ", prompt)
165
+
 
 
 
 
166
  # Combine captions with prompt for LLM
167
+ all_page_content = lvlm_inference_with_ollama(prompt)
168
  else:
169
  all_page_content = "\n\n".join([result.page_content for result in results])
170
 
 
188
  submit_btn2 = gr.update(visible=True)
189
  frame1 = gr.update(visible=True)
190
  frame2 = gr.update(visible=False)
191
+ chatbox_llm, submit_btn_chat = gr.update(visible=True), gr.update(visible=True)
192
  vid_filepath, vid_table_name = get_metadata_of_yt_video_with_captions(youtube_url, from_gen)
193
  video = gr.Video(vid_filepath,render=True)
194
+ return url_input, submit_btn, video, vid_table_name, chatbox,submit_btn2, frame1, frame2, chatbox_llm, submit_btn_chat
195
 
196
  def init_ui():
197
  with gr.Blocks() as demo:
198
+
199
  gr.Markdown("Welcome to video chat demo - Initial processing can take up to 2 minutes, and responses may be slow. Please be patient and avoid clicking repeatedly.")
200
  url_input = gr.Textbox(label="Enter YouTube URL", visible=False, elem_id='url-inp',value="https://www.youtube.com/watch?v=kOEDG3j1bjs", interactive=True)
201
  vid_table_name = gr.Textbox(label="Enter Table Name", visible=False, interactive=False)
202
  video = gr.Video()
203
  with gr.Row():
204
  submit_btn = gr.Button("Process Video By Download Subtitles")
205
+ submit_btn_gen = gr.Button("Process Video By Generating Subtitles")
206
 
207
  with gr.Row():
208
  chatbox = gr.Textbox(label="Enter the keyword/s and AI will get related captions and images", visible=False, value="event horizan", scale=4)
 
216
  with gr.Row():
217
  frame1 = gr.Image(visible=False, interactive=False, scale=2)
218
  frame2 = gr.Image(visible=False, interactive=False, scale=2)
219
+ submit_btn.click(fn=process_url_and_init, inputs=[url_input], outputs=[url_input, submit_btn, video, vid_table_name, chatbox,submit_btn_whisper, frame1, frame2, chatbox_llm, submit_btn_chat])
220
+ submit_btn_gen.click(fn=lambda x: process_url_and_init(x, from_gen=True), inputs=[url_input], outputs=[url_input, submit_btn, video, vid_table_name, chatbox,submit_btn_whisper, frame1, frame2,chatbox_llm, submit_btn_chat])
221
  submit_btn_whisper.click(fn=return_top_k_most_similar_docs, inputs=[vid_table_name, chatbox], outputs=[response, frame1, frame2])
222
+
223
+ submit_btn_chat.click(
224
+ fn=lambda table_name, query: return_top_k_most_similar_docs(
225
+ vid_table_name=table_name,
226
+ query=query,
227
+ use_llm=True
228
+ ),
229
+ inputs=[vid_table_name, chatbox_llm],
230
+ outputs=[response, frame1, frame2]
231
+ )
232
  reset_btn = gr.Button("Reload Page")
233
  reset_btn.click(None, js="() => { location.reload(); }")
234
  return demo
utility.py CHANGED
@@ -572,28 +572,18 @@ def lvlm_inference_with_conversation(conversation, max_tokens: int = 200, temper
572
  )
573
  return response['choices'][-1]['message']['content']
574
 
575
- def lvlm_inference_with_ollama(conversation, max_tokens: int = 200, temperature: float = 0.95, top_p: float = 0.1, top_k: int = 10):
576
-
577
-
578
-
579
- # Send the request to the local Ollama server
580
- #response = requests.post("http://localhost:8000/api/v1/completions", json=payload)
581
-
582
- stream = chat(
583
- model="llava-1.5-7b-hf",
584
- messages= conversation,
585
- stream=True,
586
- temperature=temperature,
587
- max_tokens=max_tokens,
588
- top_p=top_p,
589
- top_k=top_k
590
  )
591
-
592
- response_data = ''
593
- for chunk in stream:
594
- response_data += chunk['message']['content']
595
-
596
- return response_data
597
 
598
  # function `extract_and_save_frames_and_metadata``:
599
  # receives as input a video and its transcript
@@ -738,4 +728,8 @@ def extract_and_save_frames_and_metadata_with_fps(
738
  metadatas_path = osp.join(path_to_save_metadatas,'metadatas.json')
739
  with open(metadatas_path, 'w') as outfile:
740
  json.dump(metadatas, outfile)
741
- return metadatas
 
 
 
 
 
572
  )
573
  return response['choices'][-1]['message']['content']
574
 
575
+ def lvlm_inference_with_ollama(prompt):
576
+ # Remove temperature or use correct parameters for Ollama client
577
+ response = chat(
578
+ model="phi3", # or your chosen model
579
+ messages=[
580
+ {
581
+ "role": "user",
582
+ "content": prompt
583
+ }
584
+ ]
 
 
 
 
 
585
  )
586
+ return response['message']['content']
 
 
 
 
 
587
 
588
  # function `extract_and_save_frames_and_metadata``:
589
  # receives as input a video and its transcript
 
728
  metadatas_path = osp.join(path_to_save_metadatas,'metadatas.json')
729
  with open(metadatas_path, 'w') as outfile:
730
  json.dump(metadatas, outfile)
731
+ return metadatas
732
+
733
+ if __name__ == "__main__":
734
+ res = lvlm_inference_with_ollama("Tell me a story")
735
+ print(res)