PierreBrunelle
commited on
Commit
•
c0fbf58
1
Parent(s):
bc20728
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import pixeltable as pxt
|
2 |
import os
|
3 |
import openai
|
@@ -8,14 +9,16 @@ from pixeltable.functions.video import extract_audio
|
|
8 |
from pixeltable.functions.audio import get_metadata
|
9 |
from pixeltable.functions import openai
|
10 |
|
|
|
|
|
11 |
if 'OPENAI_API_KEY' not in os.environ:
|
12 |
os.environ['OPENAI_API_KEY'] = getpass.getpass('Enter your OpenAI API key:')
|
13 |
|
14 |
-
|
|
|
15 |
pxt.drop_dir('directory', force=True)
|
16 |
pxt.create_dir('directory')
|
17 |
|
18 |
-
# Create a table to store video data
|
19 |
t = pxt.create_table(
|
20 |
'directory.video_table', {
|
21 |
"video": pxt.VideoType(nullable=True),
|
@@ -23,11 +26,10 @@ t = pxt.create_table(
|
|
23 |
}
|
24 |
)
|
25 |
|
26 |
-
# Create a view that automatically extracts frames from videos
|
27 |
frames_view = pxt.create_view(
|
28 |
"directory.frames",
|
29 |
t,
|
30 |
-
iterator=FrameIterator.create(video=t.video,
|
31 |
)
|
32 |
|
33 |
# Create computed columns to store transformations and persist outputs
|
@@ -36,19 +38,27 @@ t['metadata'] = get_metadata(t.audio)
|
|
36 |
t['transcription'] = openai.transcriptions(audio=t.audio, model='whisper-1')
|
37 |
t['transcription_text'] = t.transcription.text
|
38 |
|
39 |
-
|
40 |
-
|
|
|
41 |
@pxt.udf
|
42 |
def prompt(A: str, B: str) -> list[dict]:
|
|
|
|
|
43 |
return [
|
44 |
-
{'role': 'system', 'content':
|
45 |
-
{'role': 'user', 'content':
|
46 |
]
|
47 |
|
|
|
48 |
t['message'] = prompt(t.sm_type, t.transcription_text)
|
49 |
|
50 |
-
|
|
|
|
|
51 |
t['response'] = openai.chat_completions(messages=t.message, model='gpt-4o-mini-2024-07-18', max_tokens=500)
|
|
|
|
|
52 |
t['answer'] = t.response.choices[0].message.content
|
53 |
|
54 |
MAX_VIDEO_SIZE_MB = 35
|
@@ -77,7 +87,7 @@ def process_and_generate_post(video_file, social_media_type):
|
|
77 |
|
78 |
# Retrieve thumbnails
|
79 |
thumbnails = frames_view.select(frames_view.frame).tail(4)['frame']
|
80 |
-
|
81 |
# Retrieve Pixeltable Table containing all videos and stored data
|
82 |
df_output = t.collect().to_pandas()
|
83 |
|
@@ -139,7 +149,7 @@ def gradio_interface():
|
|
139 |
height='400px'
|
140 |
)
|
141 |
audio = gr.Audio()
|
142 |
-
|
143 |
df_output = gr.DataFrame(label="Pixeltable Table")
|
144 |
|
145 |
generate_btn.click(
|
@@ -160,4 +170,7 @@ def gradio_interface():
|
|
160 |
|
161 |
# Launch the Gradio interface
|
162 |
if __name__ == "__main__":
|
163 |
-
gradio_interface().launch(show_api=False)
|
|
|
|
|
|
|
|
1 |
+
|
2 |
import pixeltable as pxt
|
3 |
import os
|
4 |
import openai
|
|
|
9 |
from pixeltable.functions.audio import get_metadata
|
10 |
from pixeltable.functions import openai
|
11 |
|
12 |
+
"""## Store OpenAI API Key"""
|
13 |
+
|
14 |
if 'OPENAI_API_KEY' not in os.environ:
|
15 |
os.environ['OPENAI_API_KEY'] = getpass.getpass('Enter your OpenAI API key:')
|
16 |
|
17 |
+
"""## Create a Table, a View, and Computed Columns"""
|
18 |
+
|
19 |
pxt.drop_dir('directory', force=True)
|
20 |
pxt.create_dir('directory')
|
21 |
|
|
|
22 |
t = pxt.create_table(
|
23 |
'directory.video_table', {
|
24 |
"video": pxt.VideoType(nullable=True),
|
|
|
26 |
}
|
27 |
)
|
28 |
|
|
|
29 |
frames_view = pxt.create_view(
|
30 |
"directory.frames",
|
31 |
t,
|
32 |
+
iterator=FrameIterator.create(video=t.video, fps=.25)
|
33 |
)
|
34 |
|
35 |
# Create computed columns to store transformations and persist outputs
|
|
|
38 |
t['transcription'] = openai.transcriptions(audio=t.audio, model='whisper-1')
|
39 |
t['transcription_text'] = t.transcription.text
|
40 |
|
41 |
+
"""## Custom UDF for Generating Social Media Prompts"""
|
42 |
+
|
43 |
+
#Custom User-Defined Function (UDF) for Generating Social Media Prompts
|
44 |
@pxt.udf
|
45 |
def prompt(A: str, B: str) -> list[dict]:
|
46 |
+
system_msg = 'You are an expert in creating social media content and you generate effective post, based on user content. Respect the social media platform guidelines and constraints.'
|
47 |
+
user_msg = f'A: "{A}" \n B: "{B}"'
|
48 |
return [
|
49 |
+
{'role': 'system', 'content': system_msg},
|
50 |
+
{'role': 'user', 'content': user_msg}
|
51 |
]
|
52 |
|
53 |
+
# Apply the UDF to create a new column
|
54 |
t['message'] = prompt(t.sm_type, t.transcription_text)
|
55 |
|
56 |
+
"""## Generating Responses with OpenAI's GPT Model"""
|
57 |
+
|
58 |
+
# # Generate responses using OpenAI's chat completion API
|
59 |
t['response'] = openai.chat_completions(messages=t.message, model='gpt-4o-mini-2024-07-18', max_tokens=500)
|
60 |
+
|
61 |
+
## Extract the content of the response
|
62 |
t['answer'] = t.response.choices[0].message.content
|
63 |
|
64 |
MAX_VIDEO_SIZE_MB = 35
|
|
|
87 |
|
88 |
# Retrieve thumbnails
|
89 |
thumbnails = frames_view.select(frames_view.frame).tail(4)['frame']
|
90 |
+
|
91 |
# Retrieve Pixeltable Table containing all videos and stored data
|
92 |
df_output = t.collect().to_pandas()
|
93 |
|
|
|
149 |
height='400px'
|
150 |
)
|
151 |
audio = gr.Audio()
|
152 |
+
|
153 |
df_output = gr.DataFrame(label="Pixeltable Table")
|
154 |
|
155 |
generate_btn.click(
|
|
|
170 |
|
171 |
# Launch the Gradio interface
|
172 |
if __name__ == "__main__":
|
173 |
+
gradio_interface().launch(show_api=False)
|
174 |
+
|
175 |
+
"""
|
176 |
+
This example showcases how Pixeltable simplifies complex video processing workflows and integrates AI capabilities to create a powerful tool for generating social media content from video inputs."""
|