Update app.py
Browse files
app.py
CHANGED
@@ -1,125 +1,51 @@
|
|
1 |
import gradio as gr
|
2 |
import google.generativeai as genai
|
3 |
-
import cv2
|
4 |
-
import os
|
5 |
-
import shutil
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
FRAME_PREFIX = "_frame"
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
os.makedirs(output_dir)
|
18 |
-
else:
|
19 |
-
shutil.rmtree(output_dir)
|
20 |
-
os.makedirs(output_dir)
|
21 |
|
22 |
-
|
23 |
-
def extract_frame_from_video(video_file_path):
|
24 |
-
create_frame_output_dir(FRAME_EXTRACTION_DIRECTORY)
|
25 |
-
vidcap = cv2.VideoCapture(video_file_path)
|
26 |
-
fps = vidcap.get(cv2.CAP_PROP_FPS)
|
27 |
-
frame_duration = 1 / fps
|
28 |
-
output_file_prefix = os.path.basename(video_file_path).replace('.', '_')
|
29 |
-
frame_count = 0
|
30 |
-
count = 0
|
31 |
-
while vidcap.isOpened():
|
32 |
-
success, frame = vidcap.read()
|
33 |
-
if not success:
|
34 |
-
break
|
35 |
-
if int(count / fps) == frame_count:
|
36 |
-
min = frame_count // 60
|
37 |
-
sec = frame_count % 60
|
38 |
-
time_string = f"{min:02d}:{sec:02d}"
|
39 |
-
image_name = f"{output_file_prefix}{FRAME_PREFIX}{time_string}.jpg"
|
40 |
-
output_filename = os.path.join(FRAME_EXTRACTION_DIRECTORY, image_name)
|
41 |
-
cv2.imwrite(output_filename, frame)
|
42 |
-
frame_count += 1
|
43 |
-
count += 1
|
44 |
-
vidcap.release()
|
45 |
-
return frame_count
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
if display_name:
|
52 |
-
self.display_name = display_name
|
53 |
-
self.timestamp = self.get_timestamp(file_path)
|
54 |
|
55 |
-
|
56 |
-
|
|
|
57 |
|
58 |
-
|
59 |
-
parts = filename.split(FRAME_PREFIX)
|
60 |
-
if len(parts) != 2:
|
61 |
-
return None
|
62 |
-
return parts[1].split('.')[0]
|
63 |
|
64 |
-
#
|
65 |
-
|
66 |
-
uploaded_files = []
|
67 |
-
for file in files_to_upload:
|
68 |
-
response = genai.upload_file(path=file.file_path)
|
69 |
-
file.set_file_response(response)
|
70 |
-
uploaded_files.append(file)
|
71 |
-
return uploaded_files
|
72 |
|
73 |
-
#
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
request.append(file.timestamp)
|
80 |
-
request.append(file.response)
|
81 |
-
response = model.generate_content(request, request_options={"timeout": 600})
|
82 |
-
print(response)
|
83 |
-
return response
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
pass
|
88 |
-
"""
|
89 |
-
for file in uploaded_files:
|
90 |
-
genai.delete_file(file.response.name)
|
91 |
-
"""
|
92 |
-
# Gradio interface
|
93 |
-
def process_video(video_file):
|
94 |
-
try:
|
95 |
-
# Extract frames
|
96 |
-
frame_count = extract_frame_from_video(video_file.name)
|
97 |
-
|
98 |
-
# Prepare files for upload
|
99 |
-
files = os.listdir(FRAME_EXTRACTION_DIRECTORY)
|
100 |
-
files = sorted(files)
|
101 |
-
files_to_upload = []
|
102 |
-
for file in files:
|
103 |
-
files_to_upload.append(
|
104 |
-
File(file_path=os.path.join(FRAME_EXTRACTION_DIRECTORY, file))
|
105 |
-
)
|
106 |
|
107 |
-
|
108 |
-
uploaded_files = upload_files(files_to_upload)
|
109 |
-
|
110 |
-
# Generate description
|
111 |
-
description = generate_description(uploaded_files)
|
112 |
-
|
113 |
-
# Delete files from Gemini
|
114 |
-
delete_files(uploaded_files)
|
115 |
-
|
116 |
-
return f"Video processed successfully! Description:\n\n{description}"
|
117 |
except Exception as e:
|
118 |
-
return f"An error occurred: {
|
|
|
119 |
|
120 |
-
# Create Gradio interface
|
121 |
iface = gr.Interface(
|
122 |
-
fn=
|
123 |
inputs=gr.Video(),
|
124 |
outputs=gr.Textbox(),
|
125 |
title="Video Description with Gemini",
|
|
|
1 |
import gradio as gr
|
2 |
import google.generativeai as genai
|
|
|
|
|
|
|
3 |
|
4 |
+
# API key input
|
5 |
+
api_key = "AIzaSyDsrgHAnNWDJqWWzq3oFAbUy5W40cUT0dY"
|
6 |
+
genai.configure(api_key=api_key)
|
7 |
|
8 |
+
def describe_video(video_file):
|
9 |
+
try:
|
|
|
10 |
|
11 |
+
print(f"Uploading file...")
|
12 |
+
uploaded_video = genai.upload_file(path=video_file.name)
|
13 |
+
print(f"Completed upload: {uploaded_video.uri}")
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
while uploaded_video.state.name == "PROCESSING":
|
18 |
+
print("Waiting for video to be processed.")
|
19 |
+
time.sleep(10)
|
20 |
+
uploaded_video = genai.get_file(uploaded_video.name)
|
|
|
|
|
|
|
21 |
|
22 |
+
if uploaded_video.state.name == "FAILED":
|
23 |
+
raise ValueError(uploaded_video.state.name)
|
24 |
+
print(f"Video processing complete: " + uploaded_video.uri)
|
25 |
|
26 |
+
prompt = "Describe this video."
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
# Set the model to Gemini 1.5 Pro.
|
29 |
+
model = genai.GenerativeModel(model_name="models/gemini-1.5-flash-latest")
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
# Make the LLM request.
|
32 |
+
print("Making LLM inference request...")
|
33 |
+
response = model.generate_content(
|
34 |
+
[prompt, uploaded_video], request_options={"timeout": 600}
|
35 |
+
)
|
36 |
+
print(response.text)
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
genai.delete_file(uploaded_video.name)
|
39 |
+
print(f"Deleted file {uploaded_video.uri}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
return response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
except Exception as e:
|
43 |
+
return f"An error occurred: {e}"
|
44 |
+
|
45 |
|
46 |
+
# Create the Gradio interface
|
47 |
iface = gr.Interface(
|
48 |
+
fn=describe_video,
|
49 |
inputs=gr.Video(),
|
50 |
outputs=gr.Textbox(),
|
51 |
title="Video Description with Gemini",
|