shreyasiv commited on
Commit
ab4b7d4
·
verified ·
1 Parent(s): 6eb8621

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -174
app.py DELETED
@@ -1,174 +0,0 @@
1
- import cv2
2
- import streamlit as st
3
- import tempfile
4
- import base64
5
- import os
6
- from dotenv import load_dotenv
7
- from openai import OpenAI
8
- import assemblyai as aai
9
- from moviepy.editor import *
10
-
11
-
12
-
13
- # Load environment variables
14
- load_dotenv()
15
- aai.settings.api_key = os.getenv("ASSEMBLYAI_API_KEY")
16
- OpenAI.api_key = os.getenv("OPENAI_API_KEY")
17
- client = OpenAI()
18
-
19
- def main():
20
- st.title('Insightly Video Content Moderation')
21
-
22
- # Video upload section
23
- uploaded_video = st.file_uploader('Upload a video', type=["mp4", "avi", "mov"])
24
-
25
- if uploaded_video is not None:
26
- # Save the video to a temp file
27
- tfile = tempfile.NamedTemporaryFile(delete=False)
28
- tfile.write(uploaded_video.read())
29
- video_file_path = tfile.name
30
- tfile.close()
31
-
32
- transcriber = aai.Transcriber()
33
- transcript = transcriber.transcribe(tfile.name)
34
-
35
- # Process the video and display frames in a grid layout
36
- base64_frames = video_to_base64_frames(video_file_path)
37
- display_frame_grid(base64_frames[::30]) # Display every 30th frame in a 3-column grid
38
-
39
- st.write("Actions:") # Header for the actions/buttons section
40
-
41
- # Creating four columns to align the buttons
42
- col1, col2, col3, col4 = st.columns(4)
43
-
44
- with col1:
45
- if st.button("Description"):
46
- st.session_state['description'] = generate_description(base64_frames) if 'description' not in st.session_state else st.session_state['description']
47
-
48
- with col2:
49
- if st.button("Frame Description"):
50
- st.session_state['frame_description'] = generate_frame_description(base64_frames) if 'frame_description' not in st.session_state else st.session_state['frame_description']
51
-
52
- with col3:
53
- if st.button("Generate Transcript"):
54
- st.session_state['transcript'] = transcript.text if 'transcript' not in st.session_state else st.session_state['transcript']
55
-
56
- with col4:
57
- if st.button("Category of Video"):
58
- st.session_state['category'] = generate_category(base64_frames) if 'category' not in st.session_state else st.session_state['category']
59
-
60
- # If any value exists in session state then display it
61
- if 'description' in st.session_state and st.session_state['description']:
62
- st.subheader("Video Description")
63
- st.write(st.session_state['description'])
64
-
65
- if 'frame_description' in st.session_state and st.session_state['frame_description']:
66
- st.subheader("Frame Description")
67
- st.write(st.session_state['frame_description'])
68
-
69
- if 'transcript' in st.session_state and st.session_state['transcript']:
70
- st.subheader("Video Transcript")
71
- st.write(st.session_state['transcript'])
72
-
73
- if 'category' in st.session_state and st.session_state['category']:
74
- st.subheader("Video Category")
75
- st.write(st.session_state['category'])
76
-
77
-
78
-
79
-
80
-
81
-
82
- def video_to_base64_frames(video_file_path):
83
- # Logic to extract all frames from the video and convert them to base64
84
- video = cv2.VideoCapture(video_file_path)
85
- base64_frames = []
86
-
87
- while video.isOpened():
88
- success, frame = video.read()
89
- if not success:
90
- break
91
-
92
- _, buffer = cv2.imencode('.jpg', frame)
93
- base64_frame = base64.b64encode(buffer).decode('utf-8')
94
- base64_frames.append(base64_frame)
95
-
96
- video.release()
97
- return base64_frames
98
-
99
- #########################################
100
- #Generate Video description
101
- def generate_description(base64_frames):
102
- prompt_messages = [
103
- {
104
- "role": "user",
105
- "content": [
106
- "1. Generate a description for this sequence of video frames in about 90 words.\
107
- Return the following : 1. List of objects in the video 2. Any restrictive content or sensitive content and if so which frame ",
108
- *map(lambda x: {"image": x, "resize": 428}, base64_frames[0::30]),
109
- ],
110
- },
111
- ]
112
- response = client.chat.completions.create(
113
- model="gpt-4-vision-preview",
114
- messages=prompt_messages,
115
- max_tokens=3000,
116
- )
117
- return response.choices[0].message.content
118
-
119
- #Generate frame description
120
- def generate_frame_description(base64_frames):
121
- prompt_messages = [
122
- {
123
- "role": "user",
124
- "content": [
125
- "Describe what is happening in each frame.",
126
- *map(lambda x: {"image": x, "resize": 428}, base64_frames[0::30]),
127
- ],
128
- },
129
- ]
130
- response = client.chat.completions.create(
131
- model="gpt-4-vision-preview",
132
- messages=prompt_messages,
133
- max_tokens=3000,
134
- )
135
- return response.choices[0].message.content
136
-
137
-
138
-
139
- #Generate Category of Video
140
- def generate_category(base64_frames):
141
- prompt_messages = [
142
- {
143
- "role": "user",
144
- "content": [
145
- "What category can this video be tagged to?",
146
- *map(lambda x: {"image": x, "resize": 428}, base64_frames[0::30]),
147
- ],
148
- },
149
- ]
150
- response = client.chat.completions.create(
151
- model="gpt-4-vision-preview",
152
- messages=prompt_messages,
153
- max_tokens=3000,
154
- )
155
- return response.choices[0].message.content
156
-
157
-
158
-
159
-
160
- ########################
161
- def display_frame_grid(base64_frames):
162
- cols_per_row = 3
163
- n_frames = len(base64_frames)
164
- for idx in range(0, n_frames, cols_per_row):
165
- cols = st.columns(cols_per_row)
166
- for col_index in range(cols_per_row):
167
- frame_idx = idx + col_index
168
- if frame_idx < n_frames:
169
- with cols[col_index]:
170
- frame = base64_frames[frame_idx]
171
- st.image(base64.b64decode(frame), caption=f'Frame {frame_idx * 30 + 1}', width=200)
172
-
173
- if __name__ == '__main__':
174
- main()