Update app.py
Browse files
app.py
CHANGED
@@ -11,9 +11,11 @@ from pixeltable.functions import openai
|
|
11 |
if 'OPENAI_API_KEY' not in os.environ:
|
12 |
os.environ['OPENAI_API_KEY'] = getpass.getpass('Enter your OpenAI API key:')
|
13 |
|
|
|
14 |
pxt.drop_dir('directory', force=True)
|
15 |
pxt.create_dir('directory')
|
16 |
|
|
|
17 |
t = pxt.create_table(
|
18 |
'directory.video_table', {
|
19 |
"video": pxt.VideoType(nullable=True),
|
@@ -21,6 +23,7 @@ t = pxt.create_table(
|
|
21 |
}
|
22 |
)
|
23 |
|
|
|
24 |
frames_view = pxt.create_view(
|
25 |
"directory.frames",
|
26 |
t,
|
@@ -33,6 +36,8 @@ t['metadata'] = get_metadata(t.audio)
|
|
33 |
t['transcription'] = openai.transcriptions(audio=t.audio, model='whisper-1')
|
34 |
t['transcription_text'] = t.transcription.text
|
35 |
|
|
|
|
|
36 |
@pxt.udf
|
37 |
def prompt(A: str, B: str) -> list[dict]:
|
38 |
return [
|
@@ -42,6 +47,7 @@ def prompt(A: str, B: str) -> list[dict]:
|
|
42 |
|
43 |
t['message'] = prompt(t.sm_type, t.transcription_text)
|
44 |
|
|
|
45 |
t['response'] = openai.chat_completions(messages=t.message, model='gpt-4o-mini-2024-07-18', max_tokens=500)
|
46 |
t['answer'] = t.response.choices[0].message.content
|
47 |
|
@@ -57,7 +63,7 @@ def process_and_generate_post(video_file, social_media_type):
|
|
57 |
if video_size > MAX_VIDEO_SIZE_MB:
|
58 |
return f"The video file is larger than {MAX_VIDEO_SIZE_MB} MB. Please upload a smaller file.", None
|
59 |
|
60 |
-
# Insert video
|
61 |
t.insert([{
|
62 |
"video": video_file,
|
63 |
"sm_type": social_media_type
|
@@ -67,8 +73,7 @@ def process_and_generate_post(video_file, social_media_type):
|
|
67 |
social_media_post = t.select(t.answer).tail(1)['answer'][0]
|
68 |
|
69 |
# Retrieve thumbnails
|
70 |
-
|
71 |
-
thumbnails = [frame['frame'] for frame in frames]
|
72 |
|
73 |
#Display content
|
74 |
return social_media_post, thumbnails
|
|
|
11 |
if 'OPENAI_API_KEY' not in os.environ:
|
12 |
os.environ['OPENAI_API_KEY'] = getpass.getpass('Enter your OpenAI API key:')
|
13 |
|
14 |
+
# Create a Pixeltable directory to organize related tables
|
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 |
}
|
24 |
)
|
25 |
|
26 |
+
# Create a view that automatically extracts frames from videos
|
27 |
frames_view = pxt.create_view(
|
28 |
"directory.frames",
|
29 |
t,
|
|
|
36 |
t['transcription'] = openai.transcriptions(audio=t.audio, model='whisper-1')
|
37 |
t['transcription_text'] = t.transcription.text
|
38 |
|
39 |
+
# Create a user-defined function (UDF) to construct the prompt
|
40 |
+
# This shows how Pixeltable allows users to extend functionality with custom Python code
|
41 |
@pxt.udf
|
42 |
def prompt(A: str, B: str) -> list[dict]:
|
43 |
return [
|
|
|
47 |
|
48 |
t['message'] = prompt(t.sm_type, t.transcription_text)
|
49 |
|
50 |
+
# Import a function from Pixeltable's built-in library for OpenAI
|
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 |
|
|
|
63 |
if video_size > MAX_VIDEO_SIZE_MB:
|
64 |
return f"The video file is larger than {MAX_VIDEO_SIZE_MB} MB. Please upload a smaller file.", None
|
65 |
|
66 |
+
# # Insert a video into the table. Pixeltable supports referencing external data sources like URLs
|
67 |
t.insert([{
|
68 |
"video": video_file,
|
69 |
"sm_type": social_media_type
|
|
|
73 |
social_media_post = t.select(t.answer).tail(1)['answer'][0]
|
74 |
|
75 |
# Retrieve thumbnails
|
76 |
+
thumbnails = frames_view.select(frames_view.frame).tail(4)['frame']
|
|
|
77 |
|
78 |
#Display content
|
79 |
return social_media_post, thumbnails
|