Thao Pham commited on
Commit
5de0912
Β·
1 Parent(s): 527422b

Change input to upload video

Browse files
Files changed (2) hide show
  1. app.py +31 -33
  2. video_utils.py +1 -1
app.py CHANGED
@@ -5,6 +5,7 @@ import video_utils
5
  import utils
6
  import embed
7
  import rag
 
8
  import os
9
  import uuid
10
  import numpy as np
@@ -18,7 +19,7 @@ from dotenv import load_dotenv
18
  load_dotenv() # Load from .env
19
 
20
  UPLOAD_FOLDER = 'uploads'
21
- video_name = None
22
 
23
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
24
  PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
@@ -38,7 +39,11 @@ pc = Pinecone(
38
  # Connect to an index
39
  index_name = "multimodal-minilm"
40
  if index_name not in pc.list_indexes().names():
41
- pinecone.create_index(index_name, dimension=384, metric="cosine")
 
 
 
 
42
  INDEX = pc.Index(index_name)
43
 
44
  MODEL_STACK = [TEXT_MODEL, VISION_MODEL, VISION_MODEL_PROCESSOR, VLM, VLM_PROCESSOR]
@@ -81,41 +86,36 @@ def check_exist_before_upsert(index, video_path):
81
 
82
  def chat(message, history):
83
  image_input_path = None
 
 
 
 
84
  if len(message['files']) > 0:
85
  assert len(message['files']) == 1
86
- image_input_path = message['files'][0]
 
 
 
 
 
87
 
88
  message = message['text']
89
 
90
  if history is None:
91
  history = []
92
 
93
- if message.startswith("https://"):
94
- # Check valid URL
95
- history.append((message, f"Checking if your provided URL at {message} is valid..."))
96
- yield history
97
-
98
- valid = is_valid_youtube_url(message)
99
- if not valid:
100
- history.append((None, "❌ Invalid YouTube URL. Please try again."))
101
- yield history
102
- return
103
-
104
  # Check metadata
105
- history.append((None, "βœ… URL is valid! Fetching video metadata..."))
106
  yield history
107
 
108
- video_metadata = video_utils.get_video_metdata(message)
109
- history.append((None, f"The video you want to process is: \nTitle: {video_metadata['title']} published by {video_metadata['author']} on {video_metadata['publish_date']}."))
110
- yield history
111
 
112
- history.append((None, "⏳ Downloading video..."))
113
- yield history
114
 
115
- output_folder_path = os.path.join(UPLOAD_FOLDER, video_metadata['title'])
116
- path_to_video = os.path.join(output_folder_path, f"video.mp4")
117
  if not os.path.exists(path_to_video):
118
- path_to_video = utils.download_video(message, path=output_folder_path)
119
 
120
  history.append((None, "⏳ Transcribing video..."))
121
  yield history
@@ -155,17 +155,17 @@ def chat(message, history):
155
  with open(os.path.join(output_folder_path, "summary.txt"), "w") as f:
156
  f.write(video_summary)
157
 
158
- history.append((None, f"Video processing complete! You can now ask me questions about the video {video_metadata['title']}!"))
159
  yield history
160
 
161
- global video_name
162
- video_name = video_metadata['title']
163
  else:
164
  history.append((message, None))
165
  yield history
166
 
167
- if video_name is None:
168
- history.append((None, "You need to insert video URL before asking questions."))
169
  yield history
170
  return
171
 
@@ -186,18 +186,16 @@ def chat(message, history):
186
  yield history
187
 
188
  def clear_chat(history):
189
- # return []
190
  history = []
191
- history.append((None, "Please input a Youtube URL to get started!"))
192
- # yield history
193
  return history
194
 
195
  def main():
196
- initial_messages = [(None, "Please input a Youtube URL to get started!")]
197
 
198
  with gr.Blocks() as demo:
199
  chatbot = gr.Chatbot(value=initial_messages)
200
- msg = gr.MultimodalTextbox(file_types=['image'], sources=['upload'])
201
 
202
  with gr.Row():
203
  with gr.Column():
 
5
  import utils
6
  import embed
7
  import rag
8
+ import shutil
9
  import os
10
  import uuid
11
  import numpy as np
 
19
  load_dotenv() # Load from .env
20
 
21
  UPLOAD_FOLDER = 'uploads'
22
+ global_video_name = None
23
 
24
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
25
  PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
 
39
  # Connect to an index
40
  index_name = "multimodal-minilm"
41
  if index_name not in pc.list_indexes().names():
42
+ pc.create_index(index_name, dimension=384, metric="cosine",
43
+ spec=ServerlessSpec(
44
+ cloud="aws",
45
+ region="us-east-1"
46
+ ))
47
  INDEX = pc.Index(index_name)
48
 
49
  MODEL_STACK = [TEXT_MODEL, VISION_MODEL, VISION_MODEL_PROCESSOR, VLM, VLM_PROCESSOR]
 
86
 
87
  def chat(message, history):
88
  image_input_path = None
89
+
90
+ # print(message['files'])
91
+
92
+ video_name, video_input_path = None, None
93
  if len(message['files']) > 0:
94
  assert len(message['files']) == 1
95
+
96
+ if message['files'][0].endswith('.jpg'):
97
+ image_input_path = message['files'][0]
98
+ elif message['files'][0].endswith('.mp4'):
99
+ video_input_path = message['files'][0]
100
+ video_name = os.path.basename(video_input_path).split('.mp4')[0]
101
 
102
  message = message['text']
103
 
104
  if history is None:
105
  history = []
106
 
107
+ if video_name is not None:
 
 
 
 
 
 
 
 
 
 
108
  # Check metadata
109
+ history.append((None, f"βœ… Video uploaded succesfully! Your video's title is {video_name}..."))
110
  yield history
111
 
112
+ output_folder_path = os.path.join(UPLOAD_FOLDER, video_name)
113
+ os.makedirs(output_folder_path, exist_ok=True)
 
114
 
115
+ path_to_video = os.path.join(output_folder_path, "video.mp4")
 
116
 
 
 
117
  if not os.path.exists(path_to_video):
118
+ shutil.move(video_input_path, path_to_video)
119
 
120
  history.append((None, "⏳ Transcribing video..."))
121
  yield history
 
155
  with open(os.path.join(output_folder_path, "summary.txt"), "w") as f:
156
  f.write(video_summary)
157
 
158
+ history.append((None, f"Video processing complete! You can now ask me questions about the video {video_name}!"))
159
  yield history
160
 
161
+ global global_video_name
162
+ global_video_name = video_name
163
  else:
164
  history.append((message, None))
165
  yield history
166
 
167
+ if global_video_name is None:
168
+ history.append((None, "You need to upload a video before asking questions."))
169
  yield history
170
  return
171
 
 
186
  yield history
187
 
188
  def clear_chat(history):
 
189
  history = []
190
+ history.append((None, "Please upload a video to get started!"))
 
191
  return history
192
 
193
  def main():
194
+ initial_messages = [(None, "Please upload a video to get started!")]
195
 
196
  with gr.Blocks() as demo:
197
  chatbot = gr.Chatbot(value=initial_messages)
198
+ msg = gr.MultimodalTextbox(file_types=['image', '.mp4'], sources=['upload'])
199
 
200
  with gr.Row():
201
  with gr.Column():
video_utils.py CHANGED
@@ -22,7 +22,7 @@ def extract_audio(path_to_video:str, output_folder:str):
22
  video_name = os.path.basename(path_to_video).replace('.mp4', '')
23
 
24
  # declare where to save .mp3 audio
25
- path_to_extracted_audio_file = os.path.join(output_folder, f'{video_name}.mp3')
26
 
27
  # extract mp3 audio file from mp4 video video file
28
  clip = VideoFileClip(path_to_video)
 
22
  video_name = os.path.basename(path_to_video).replace('.mp4', '')
23
 
24
  # declare where to save .mp3 audio
25
+ path_to_extracted_audio_file = os.path.join(output_folder, 'audio.mp3')
26
 
27
  # extract mp3 audio file from mp4 video video file
28
  clip = VideoFileClip(path_to_video)