minor update
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import pixeltable as pxt
|
3 |
import os
|
@@ -7,33 +8,31 @@ from pixeltable.functions import openai as pxop
|
|
7 |
import openai
|
8 |
|
9 |
|
10 |
-
#
|
11 |
db_directory = "video_db"
|
12 |
table_name = "video_table"
|
13 |
|
14 |
-
# constants
|
15 |
-
|
16 |
MAX_VIDEO_SIZE_MB = 35
|
17 |
GPT_MODEL = "gpt-4o-mini-2024-07-18"
|
18 |
MAX_TOKENS = 500
|
19 |
WHISPER_MODEL = "whisper-1"
|
20 |
|
21 |
-
# Set
|
22 |
if "OPENAI_API_KEY" not in os.environ:
|
23 |
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
|
24 |
|
25 |
-
|
26 |
pxt.drop_dir("video_db", force=True)
|
27 |
if table_name in pxt.list_tables():
|
28 |
pxt.drop_table("video_db.video_table")
|
29 |
|
30 |
-
#
|
31 |
if db_directory not in pxt.list_dirs():
|
32 |
pxt.create_dir(db_directory)
|
33 |
else:
|
34 |
print(f"Directory {db_directory} already exists. Using the existing directory.")
|
35 |
|
36 |
-
# Check if the table exists, if not, create it
|
37 |
if table_name not in pxt.list_tables():
|
38 |
t = pxt.create_table(
|
39 |
f"{db_directory}.{table_name}",
|
@@ -44,12 +43,10 @@ if table_name not in pxt.list_tables():
|
|
44 |
"sm_post": pxt.StringType(),
|
45 |
},
|
46 |
)
|
47 |
-
|
48 |
else:
|
49 |
t = pxt.load_table(f"{db_directory}.{table_name}")
|
50 |
print(f"Table {table_name} already exists. Using the existing table.")
|
51 |
|
52 |
-
|
53 |
# Function to generate social media post using OpenAI GPT-4 API
|
54 |
def generate_social_media_post(transcript_text, social_media_type):
|
55 |
response = openai.chat.completions.create(
|
@@ -68,50 +65,32 @@ def generate_social_media_post(transcript_text, social_media_type):
|
|
68 |
)
|
69 |
return response.choices[0].message.content
|
70 |
|
71 |
-
|
72 |
# Function to process the uploaded video and generate the post
|
73 |
def process_and_generate_post(video_file, social_media_type):
|
74 |
if video_file:
|
75 |
try:
|
|
|
76 |
video_size = os.path.getsize(video_file) / (1024 * 1024) # Convert to MB
|
77 |
-
|
78 |
if video_size > MAX_VIDEO_SIZE_MB:
|
79 |
return f"The video file is larger than {MAX_VIDEO_SIZE_MB} MB. Please upload a smaller file."
|
80 |
|
81 |
video_filename = os.path.basename(video_file)
|
82 |
tr_audio_gen_flag = True
|
83 |
sm_gen_flag = True
|
84 |
-
print(
|
85 |
-
"##################\nthe video file and social media are..."
|
86 |
-
+ video_file
|
87 |
-
+ "....."
|
88 |
-
+ social_media_type
|
89 |
-
)
|
90 |
-
video_df = t.where(t.video_filename == video_filename).tail(1)
|
91 |
|
|
|
|
|
92 |
if t.select().where(t.video_filename == video_filename).count() >= 1:
|
93 |
-
# print('Video Exists')
|
94 |
tr_audio_gen_flag = False
|
95 |
|
96 |
-
# Check if video and
|
97 |
video_type_df = t.where(
|
98 |
(t.video_filename == video_filename) & (t.sm_type == social_media_type)
|
99 |
).tail(1)
|
100 |
-
|
101 |
if video_type_df:
|
102 |
-
# print('Video & Type Exists')
|
103 |
sm_gen_flag = False
|
104 |
|
105 |
-
#
|
106 |
-
|
107 |
-
# print('both the cases....')
|
108 |
-
|
109 |
-
# print(video_df and not video_type_df)
|
110 |
-
|
111 |
-
# print(t.select().where(t.video_filename == video_filename).count() >=1 )
|
112 |
-
|
113 |
-
# print(t.select().where((t.video_filename == video_filename) & (t.sm_type == social_media_type)).count() >=1 )
|
114 |
-
|
115 |
if (
|
116 |
(t.count() < 1)
|
117 |
or not (
|
@@ -119,7 +98,6 @@ def process_and_generate_post(video_file, social_media_type):
|
|
119 |
)
|
120 |
or (video_df and not video_type_df)
|
121 |
):
|
122 |
-
# Insert video into PixelTable
|
123 |
t.insert(
|
124 |
[
|
125 |
{
|
@@ -131,9 +109,8 @@ def process_and_generate_post(video_file, social_media_type):
|
|
131 |
]
|
132 |
)
|
133 |
|
|
|
134 |
if tr_audio_gen_flag:
|
135 |
-
# Extract audio from video
|
136 |
-
|
137 |
if not t.get_column(name="audio"):
|
138 |
t["audio"] = extract_audio(t.video, format="mp3")
|
139 |
else:
|
@@ -141,7 +118,6 @@ def process_and_generate_post(video_file, social_media_type):
|
|
141 |
|
142 |
print("########### processing transcription #############")
|
143 |
|
144 |
-
# Transcribe audio using OpenAI Whisper API
|
145 |
if not t.get_column(name="transcription"):
|
146 |
t["transcription"] = pxop.transcriptions(
|
147 |
t.audio, model=WHISPER_MODEL
|
@@ -149,8 +125,7 @@ def process_and_generate_post(video_file, social_media_type):
|
|
149 |
else:
|
150 |
t.transcription = pxop.transcriptions(t.audio, model=WHISPER_MODEL)
|
151 |
|
152 |
-
#
|
153 |
-
|
154 |
filtered_df = t.where(
|
155 |
(t.video_filename == video_filename) & (t.sm_type == social_media_type)
|
156 |
).tail(1)
|
@@ -161,11 +136,7 @@ def process_and_generate_post(video_file, social_media_type):
|
|
161 |
cur_video_df = filtered_df[0]
|
162 |
plain_text = cur_video_df["transcription"]["text"]
|
163 |
|
164 |
-
#
|
165 |
-
|
166 |
-
# print(t.show())
|
167 |
-
# print('status of social media type')
|
168 |
-
# print(t.select().where((t.video_filename == video_filename) & (t.sm_type == social_media_type)).count() >=1)
|
169 |
if (
|
170 |
t.select()
|
171 |
.where(
|
@@ -176,7 +147,6 @@ def process_and_generate_post(video_file, social_media_type):
|
|
176 |
.count()
|
177 |
>= 1
|
178 |
):
|
179 |
-
|
180 |
print("retrieving existing social media post")
|
181 |
social_media_post = (
|
182 |
t.select(t.sm_post)
|
@@ -186,10 +156,7 @@ def process_and_generate_post(video_file, social_media_type):
|
|
186 |
)
|
187 |
.collect()["sm_post"]
|
188 |
)
|
189 |
-
return social_media_post
|
190 |
-
|
191 |
else:
|
192 |
-
|
193 |
print("generating new social media post")
|
194 |
social_media_post = generate_social_media_post(
|
195 |
plain_text, social_media_type
|
@@ -197,8 +164,6 @@ def process_and_generate_post(video_file, social_media_type):
|
|
197 |
if sm_gen_flag:
|
198 |
cur_video_df.update({"sm_post": social_media_post})
|
199 |
|
200 |
-
# print(t.show())
|
201 |
-
|
202 |
return cur_video_df["sm_post"]
|
203 |
|
204 |
except Exception as e:
|
@@ -206,10 +171,10 @@ def process_and_generate_post(video_file, social_media_type):
|
|
206 |
else:
|
207 |
return "Please upload a video file."
|
208 |
|
209 |
-
|
210 |
# Gradio Interface
|
211 |
def gradio_interface():
|
212 |
with gr.Blocks(theme=gr.themes.Glass()) as demo:
|
|
|
213 |
gr.Markdown(
|
214 |
"""<center><font size=12>Video to Social Media Post Generator</center>"""
|
215 |
)
|
@@ -236,14 +201,13 @@ def gradio_interface():
|
|
236 |
)
|
237 |
generate_btn = gr.Button("Generate Post", interactive= True)
|
238 |
|
239 |
-
#video_input.upload(lambda : gr.Button(interactive=True), None, generate_btn)
|
240 |
-
|
241 |
output = gr.Textbox(label="Generated Social Media Post", show_copy_button=True)
|
242 |
|
243 |
examples = gr.Examples(
|
244 |
[["example1.mp4"], ["example2.mp4"]], inputs=[video_input]
|
245 |
)
|
246 |
|
|
|
247 |
generate_btn.click(
|
248 |
fn=process_and_generate_post,
|
249 |
inputs=[video_input, social_media_type],
|
@@ -252,5 +216,5 @@ def gradio_interface():
|
|
252 |
|
253 |
return demo
|
254 |
|
255 |
-
|
256 |
-
gradio_interface().launch(show_api=False)
|
|
|
1 |
+
# Import necessary libraries
|
2 |
import gradio as gr
|
3 |
import pixeltable as pxt
|
4 |
import os
|
|
|
8 |
import openai
|
9 |
|
10 |
|
11 |
+
# Set up Pixeltable database and table
|
12 |
db_directory = "video_db"
|
13 |
table_name = "video_table"
|
14 |
|
15 |
+
# Define constants
|
|
|
16 |
MAX_VIDEO_SIZE_MB = 35
|
17 |
GPT_MODEL = "gpt-4o-mini-2024-07-18"
|
18 |
MAX_TOKENS = 500
|
19 |
WHISPER_MODEL = "whisper-1"
|
20 |
|
21 |
+
# Set OpenAI API key
|
22 |
if "OPENAI_API_KEY" not in os.environ:
|
23 |
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
|
24 |
|
25 |
+
# Clean up existing database and table if they exist
|
26 |
pxt.drop_dir("video_db", force=True)
|
27 |
if table_name in pxt.list_tables():
|
28 |
pxt.drop_table("video_db.video_table")
|
29 |
|
30 |
+
# Create or use existing directory and table
|
31 |
if db_directory not in pxt.list_dirs():
|
32 |
pxt.create_dir(db_directory)
|
33 |
else:
|
34 |
print(f"Directory {db_directory} already exists. Using the existing directory.")
|
35 |
|
|
|
36 |
if table_name not in pxt.list_tables():
|
37 |
t = pxt.create_table(
|
38 |
f"{db_directory}.{table_name}",
|
|
|
43 |
"sm_post": pxt.StringType(),
|
44 |
},
|
45 |
)
|
|
|
46 |
else:
|
47 |
t = pxt.load_table(f"{db_directory}.{table_name}")
|
48 |
print(f"Table {table_name} already exists. Using the existing table.")
|
49 |
|
|
|
50 |
# Function to generate social media post using OpenAI GPT-4 API
|
51 |
def generate_social_media_post(transcript_text, social_media_type):
|
52 |
response = openai.chat.completions.create(
|
|
|
65 |
)
|
66 |
return response.choices[0].message.content
|
67 |
|
|
|
68 |
# Function to process the uploaded video and generate the post
|
69 |
def process_and_generate_post(video_file, social_media_type):
|
70 |
if video_file:
|
71 |
try:
|
72 |
+
# Check video file size
|
73 |
video_size = os.path.getsize(video_file) / (1024 * 1024) # Convert to MB
|
|
|
74 |
if video_size > MAX_VIDEO_SIZE_MB:
|
75 |
return f"The video file is larger than {MAX_VIDEO_SIZE_MB} MB. Please upload a smaller file."
|
76 |
|
77 |
video_filename = os.path.basename(video_file)
|
78 |
tr_audio_gen_flag = True
|
79 |
sm_gen_flag = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
+
# Check if video already exists in the table
|
82 |
+
video_df = t.where(t.video_filename == video_filename).tail(1)
|
83 |
if t.select().where(t.video_filename == video_filename).count() >= 1:
|
|
|
84 |
tr_audio_gen_flag = False
|
85 |
|
86 |
+
# Check if video and social media type combination exists
|
87 |
video_type_df = t.where(
|
88 |
(t.video_filename == video_filename) & (t.sm_type == social_media_type)
|
89 |
).tail(1)
|
|
|
90 |
if video_type_df:
|
|
|
91 |
sm_gen_flag = False
|
92 |
|
93 |
+
# Insert video into PixelTable if it doesn't exist or if it's a new social media type
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
if (
|
95 |
(t.count() < 1)
|
96 |
or not (
|
|
|
98 |
)
|
99 |
or (video_df and not video_type_df)
|
100 |
):
|
|
|
101 |
t.insert(
|
102 |
[
|
103 |
{
|
|
|
109 |
]
|
110 |
)
|
111 |
|
112 |
+
# Extract audio and transcribe if needed
|
113 |
if tr_audio_gen_flag:
|
|
|
|
|
114 |
if not t.get_column(name="audio"):
|
115 |
t["audio"] = extract_audio(t.video, format="mp3")
|
116 |
else:
|
|
|
118 |
|
119 |
print("########### processing transcription #############")
|
120 |
|
|
|
121 |
if not t.get_column(name="transcription"):
|
122 |
t["transcription"] = pxop.transcriptions(
|
123 |
t.audio, model=WHISPER_MODEL
|
|
|
125 |
else:
|
126 |
t.transcription = pxop.transcriptions(t.audio, model=WHISPER_MODEL)
|
127 |
|
128 |
+
# Get the current video data
|
|
|
129 |
filtered_df = t.where(
|
130 |
(t.video_filename == video_filename) & (t.sm_type == social_media_type)
|
131 |
).tail(1)
|
|
|
136 |
cur_video_df = filtered_df[0]
|
137 |
plain_text = cur_video_df["transcription"]["text"]
|
138 |
|
139 |
+
# Generate or retrieve social media post
|
|
|
|
|
|
|
|
|
140 |
if (
|
141 |
t.select()
|
142 |
.where(
|
|
|
147 |
.count()
|
148 |
>= 1
|
149 |
):
|
|
|
150 |
print("retrieving existing social media post")
|
151 |
social_media_post = (
|
152 |
t.select(t.sm_post)
|
|
|
156 |
)
|
157 |
.collect()["sm_post"]
|
158 |
)
|
|
|
|
|
159 |
else:
|
|
|
160 |
print("generating new social media post")
|
161 |
social_media_post = generate_social_media_post(
|
162 |
plain_text, social_media_type
|
|
|
164 |
if sm_gen_flag:
|
165 |
cur_video_df.update({"sm_post": social_media_post})
|
166 |
|
|
|
|
|
167 |
return cur_video_df["sm_post"]
|
168 |
|
169 |
except Exception as e:
|
|
|
171 |
else:
|
172 |
return "Please upload a video file."
|
173 |
|
|
|
174 |
# Gradio Interface
|
175 |
def gradio_interface():
|
176 |
with gr.Blocks(theme=gr.themes.Glass()) as demo:
|
177 |
+
# Set up the UI components
|
178 |
gr.Markdown(
|
179 |
"""<center><font size=12>Video to Social Media Post Generator</center>"""
|
180 |
)
|
|
|
201 |
)
|
202 |
generate_btn = gr.Button("Generate Post", interactive= True)
|
203 |
|
|
|
|
|
204 |
output = gr.Textbox(label="Generated Social Media Post", show_copy_button=True)
|
205 |
|
206 |
examples = gr.Examples(
|
207 |
[["example1.mp4"], ["example2.mp4"]], inputs=[video_input]
|
208 |
)
|
209 |
|
210 |
+
# Connect the generate button to the processing function
|
211 |
generate_btn.click(
|
212 |
fn=process_and_generate_post,
|
213 |
inputs=[video_input, social_media_type],
|
|
|
216 |
|
217 |
return demo
|
218 |
|
219 |
+
# Launch the Gradio interface
|
220 |
+
gradio_interface().launch(show_api=False)
|