Update app.py
Browse files
app.py
CHANGED
|
@@ -358,7 +358,69 @@ def save_video(video_file):
|
|
| 358 |
f.write(video_file.getbuffer())
|
| 359 |
return video_file.name
|
| 360 |
|
| 361 |
-
def process_video(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
base64Frames = []
|
| 363 |
base_video_path, _ = os.path.splitext(video_path)
|
| 364 |
video = cv2.VideoCapture(video_path)
|
|
|
|
| 358 |
f.write(video_file.getbuffer())
|
| 359 |
return video_file.name
|
| 360 |
|
| 361 |
+
def process_video(video_input, user_prompt):
|
| 362 |
+
SaveNewFile=True
|
| 363 |
+
video_file_name=''
|
| 364 |
+
if isinstance(video_input, str):
|
| 365 |
+
video_file_name = video_input
|
| 366 |
+
with open(video_input, "rb") as video_file:
|
| 367 |
+
video_input = video_file.read()
|
| 368 |
+
SaveNewFile=False # file is there and this is just prompt inference
|
| 369 |
+
else:
|
| 370 |
+
video_file_name = video_input.name
|
| 371 |
+
video_input = video_input.read()
|
| 372 |
+
SaveNewFile=True
|
| 373 |
+
|
| 374 |
+
st.markdown('Processing video: ' + video_file_name)
|
| 375 |
+
|
| 376 |
+
base64Frames, audio_path = process_video(video_file_name, seconds_per_frame=1)
|
| 377 |
+
|
| 378 |
+
# Get the transcript for the video model call
|
| 379 |
+
transcript = process_audio_for_video(video_input)
|
| 380 |
+
|
| 381 |
+
# Generate a summary with visual and audio
|
| 382 |
+
response = client.chat.completions.create(
|
| 383 |
+
model=MODEL,
|
| 384 |
+
messages=[
|
| 385 |
+
{"role": "system", "content": """You are generating a video summary. Create a summary of the provided video and its transcript. Respond in Markdown"""},
|
| 386 |
+
{"role": "user", "content": [
|
| 387 |
+
"These are the frames from the video.",
|
| 388 |
+
*map(lambda x: {"type": "image_url",
|
| 389 |
+
"image_url": {"url": f'data:image/jpg;base64,{x}', "detail": "low"}}, base64Frames),
|
| 390 |
+
{"type": "text", "text": f"The audio transcription is: {transcript}"},
|
| 391 |
+
{"type": "text", "text": user_prompt}
|
| 392 |
+
]},
|
| 393 |
+
],
|
| 394 |
+
temperature=0,
|
| 395 |
+
)
|
| 396 |
+
video_response = response.choices[0].message.content
|
| 397 |
+
st.markdown(video_response)
|
| 398 |
+
|
| 399 |
+
# Save markdown on video AI output from gpt4o
|
| 400 |
+
filename_md = generate_filename(video_file_name + '- ' + video_response, "md")
|
| 401 |
+
# Save markdown on video AI output from gpt4o
|
| 402 |
+
filename_mp4 = filename_md.replace('.md', '.' + video_file_name.split('.')[-1])
|
| 403 |
+
|
| 404 |
+
create_file(filename_md, video_response, '', True)
|
| 405 |
+
|
| 406 |
+
with open(filename_md, "w", encoding="utf-8") as f:
|
| 407 |
+
f.write(video_response)
|
| 408 |
+
|
| 409 |
+
# Extract boldface terms from video_response then autoname save file
|
| 410 |
+
boldface_terms = extract_title(video_response).replace(':','')
|
| 411 |
+
filename_stem, extension = os.path.splitext(video_file_name)
|
| 412 |
+
filename_video = f"{filename_stem} {''.join(boldface_terms)}{extension}"
|
| 413 |
+
if SaveNewFile:
|
| 414 |
+
newfilename = save_video(video_input, filename_video)
|
| 415 |
+
filename_md = newfilename.replace('.mp4', '.md')
|
| 416 |
+
create_file(filename_md, '', video_response, True)
|
| 417 |
+
else:
|
| 418 |
+
filename = generate_filename(filename_md, "md")
|
| 419 |
+
create_file(filename, video_file_name, video_response, should_save)
|
| 420 |
+
|
| 421 |
+
return video_response
|
| 422 |
+
|
| 423 |
+
def process_video_old(video_path, seconds_per_frame=2):
|
| 424 |
base64Frames = []
|
| 425 |
base_video_path, _ = os.path.splitext(video_path)
|
| 426 |
video = cv2.VideoCapture(video_path)
|