tsi-org commited on
Commit
b240f73
1 Parent(s): af9a186

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -1
app.py CHANGED
@@ -9,7 +9,109 @@ import os
9
  import requests
10
  import tempfile
11
 
12
- # Assuming other function definitions remain the same...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  def main():
15
  st.set_page_config(page_title="AI Voiceover", page_icon="🔮")
 
9
  import requests
10
  import tempfile
11
 
12
+ # Load environment variables from .env.local
13
+ load_dotenv('.env.local')
14
+
15
+ def check_password():
16
+ correct_password = os.getenv('PASSWORD')
17
+ if correct_password is None:
18
+ st.error("Password is not set in .env.local")
19
+ return False
20
+
21
+ user_password = st.text_input("Enter the password to proceed", type="password")
22
+ if user_password == correct_password:
23
+ return True
24
+ else:
25
+ if st.button("Check Password"):
26
+ st.error("Incorrect password")
27
+ return False
28
+
29
+ def video_to_frames(video_file, frame_sampling_rate=1):
30
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
31
+ tmpfile.write(video_file.read())
32
+ video_filename = tmpfile.name
33
+
34
+ video_clip = VideoFileClip(video_filename)
35
+ video_duration = video_clip.duration
36
+ fps = video_clip.fps
37
+ frames_to_skip = int(fps * frame_sampling_rate)
38
+
39
+ video = cv2.VideoCapture(video_filename)
40
+ base64Frame = []
41
+ current_frame = 0
42
+
43
+ while video.isOpened():
44
+ success, frame = video.read()
45
+ if not success:
46
+ break
47
+ if current_frame % frames_to_skip == 0:
48
+ _, buffer = cv2.imencode('.jpg', frame)
49
+ base64Frame.append(base64.b64encode(buffer).decode("utf-8"))
50
+ current_frame += 1
51
+
52
+ video.release()
53
+ print(f"{len(base64Frame)} frames read at a sampling rate of {frame_sampling_rate} second(s) per frame.")
54
+ return base64Frame, video_filename, video_duration
55
+
56
+ def frames_to_story(base64Frames, prompt, api_key):
57
+ PROMPT_MESSAGES = [
58
+ {
59
+ "role": "user",
60
+ "content": [
61
+ prompt,
62
+ *map(lambda x: {"image": x, "resize": 768}, base64Frames[0::50]),
63
+ ],
64
+ },
65
+ ]
66
+ params = {
67
+ "model": "gpt-4-vision-preview",
68
+ "messages": PROMPT_MESSAGES,
69
+ "api_key": api_key,
70
+ "headers": {"Openai-Version": "2020-11-07"},
71
+ "max_tokens": 1000,
72
+ }
73
+ result = openai.ChatCompletion.create(**params)
74
+ print(result.choices[0].message.content)
75
+ return result.choices[0].message.content
76
+
77
+ def text_to_audio(text, api_key, voice):
78
+ response = requests.post(
79
+ "https://api.openai.com/v1/audio/speech",
80
+ headers={
81
+ "Authorization": f"Bearer {api_key}",
82
+ },
83
+ json={
84
+ "model": "tts-1",
85
+ "input": text,
86
+ "voice": voice,
87
+ },
88
+ )
89
+
90
+ if response.status_code != 200:
91
+ raise Exception("Request failed with status code")
92
+
93
+ audio_bytes_io = io.BytesIO()
94
+ for chunk in response.iter_content(chunk_size=1024*1024):
95
+ audio_bytes_io.write(chunk)
96
+ audio_bytes_io.seek(0)
97
+
98
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmpfile:
99
+ for chunk in response.iter_content(chunk_size=1024*1024):
100
+ tmpfile.write(chunk)
101
+ audio_filename = tmpfile.name
102
+
103
+ return audio_filename, audio_bytes_io
104
+
105
+ def merge_audio_video(video_filename, audio_filename, output_filename):
106
+ print("Merging audio and video ...")
107
+ video_clip = VideoFileClip(video_filename)
108
+ audio_clip = AudioFileClip(audio_filename)
109
+ final_clip = video_clip.set_audio(audio_clip)
110
+ final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
111
+ video_clip.close()
112
+ audio_clip.close()
113
+ return output_filename
114
+
115
 
116
  def main():
117
  st.set_page_config(page_title="AI Voiceover", page_icon="🔮")