danial0203 commited on
Commit
91336a9
·
1 Parent(s): 021e84d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -283
app.py CHANGED
@@ -1,27 +1,27 @@
1
- # import gradio as gr
2
- # import requests
3
- # import re
4
- # import boto3
5
- # import os
6
- # import json
7
- # from gradio.components import Textbox
8
- # from openai import OpenAI
9
- # from pathlib import Path
10
-
11
-
12
- # api_key= os.getenv('OPENAI_API_KEY')
13
 
14
- # client = OpenAI(api_key=api_key)
15
 
 
16
 
17
- # # Set your API keys and configurations here
18
- # aws_access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
19
- # aws_secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')
20
- # aws_default_region = os.getenv('AWS_DEFAULT_REGION')
21
 
22
 
 
 
 
 
23
 
24
- # # Initialize OpenAI and S3 clients
25
  # client = OpenAI(api_key=api_key)
26
  # s3 = boto3.client(
27
  # 's3',
@@ -30,239 +30,8 @@
30
  # region_name=aws_default_region
31
  # )
32
 
33
- # print("Clients initialized")
34
-
35
-
36
- # # Your content creation prompt
37
- # content_creation_prompt = """
38
- # [You are a teacher and a video creator and you create animated educational videos based on SLOs(Student learning outcomes) for students who don't really have a nag for the conventional ways of learning. You make sure that your video lessons are very very interesting and at the same time they still have all the teaching material that you want to incorporate. You have to make sure that the teaching material is logically incorporated within the stories, scenarios, or small daily life examples so that I can make an animated video out of it.
39
-
40
- # As a creative assistant you are the best creative person out there and you have more than 30 years of experience. As a learning assistant you are better than the best of the teachers out there, you always know how to teach the given topic in a super easy manner and break it down so that your target audience can easily learn it.
41
- # As a learning assistant you have more than 30 years of experience. As a creative assistant you will make sure that you come up with good ideas for the script and that you will incorporate all the teaching material into the story in a logical and creative manner and as a learning assistant you will make sure that you come up with good learning material and you will also make sure that creative assistant incorporates them in the story without being wrong about them.
42
-
43
- # Please remember that whatever you create together is more about demonstration and less about telling. Please remember this film writing rule all the times Show don’t tell. You will be given an SLO. And at this point you will have two options and you will have to choose one.
44
-
45
- # You have two options either proceed with story based learning or proceed with a short explainer video style. Whatever option you choose please remember to make it relevant to the Pakistani audience. Whatever option you may choose you have to make sure that you incorporate all the teaching material in that video.
46
-
47
- # If you proceed with story based learning the story will always start with a big idea on that SLO. In the story based learning, the creative assistant will generate a big story idea around the given SLO. ( What is the big idea?: well Big idea of any video lesson is that one scene around which the whole story revolves, for instance in the SLO “Understand centripetal acceleration”, the big idea is showing a scene with a daredevil doing a loop de loop and not falling inwards. Why is this a big idea? Because a kid will see the potential crisis, and when the daredevil successfully completes the stunt, they will understand the concept of centripetal acceleration. Well, it's all about creating that "aha" moment. When a kid sees the daredevil in action, they get how centripetal acceleration works.) You have to add scenarios, examples, diagrams, explanations, or any other better option that you might have to suggest. Make sure the big idea has an element of conflict to it. Which the main protagonist has to face. The big idea should also be very relevant to the target audience. You will keep in mind that you have to demonstrate more than you talk about it.
48
-
49
- # Skip the above step if a short explainer video is chosen.
50
-
51
- # The explainer video includes multiple examples, and short scenarios directly told to the target audience through a narrator. Short explainer videos can have very minimal visual design. Either way you will keep in mind that you have to demonstrate more than you talk about it. If the short explainer video is chosen then the topic does not need a story around it. In Fact Creative assistant will keep this video simple enough and will generate idea for it without adding a story. It will certainly have a narrative but no complete story. It is always encouraged to start by asking questions. You have to add scenarios, examples, diagrams, explanations, or any other better option that you might have to suggest. These scripts are relatively shorter and more concise and to the point. Important thing to notice is that this format is short, simple, crisp, to the point, and very very visual. short explainer videos can have very minimal visual design
52
-
53
- # your output will be in a script format. Timestamps would be mentioned, write voiceovers and provide prompts for static visuals from the story. Prompts for visuals would always be labeled as VISUAL PROMPT. Mention Timestamps for each scene. PLEASE ALWAYS MENTION VOICE OVERS AS V.O AND PROMPTS FOR VISUALS AS VISUAL PROMPT. ALSO MENTION THE SCENE NUMBERS. You must give 10 visual prompts at all times. if the user gives you an SLO that is half or broken or does not make any sense, you must fix it and improve it and show it to the user that this is how you fixed it. you must always ask for the grade for which you have to keep the difficult level for. no magic themes in both formats what so ever
54
-
55
- # "Generate a detailed prompt for a static animated image. The prompt should vividly describe a scene that includes the following elements:
56
-
57
- # 1. **Setting**: Specify a unique and engaging environment, such as an enchanted forest, a futuristic city, an underwater kingdom, a space station, or a historical setting like a medieval marketplace.
58
-
59
- # 2. **Key Features**: Include specific details about the landscape, architecture, and any notable features of the setting. For instance, glowing plants, futuristic buildings, coral palaces, spaceships, or market stalls.
60
-
61
- # 3. **Atmosphere and Lighting**: Describe the time of day and the overall atmosphere, highlighting elements like lighting (bioluminescent, neon, sunlight filtering through water, etc.) and mood (mystical, bustling, serene, etc.).
62
-
63
- # 4. **Characters and Lifeforms**: Mention any characters, creatures, or life forms present in the scene, whether they are people, animals, mythical beings, or aliens. Describe their appearance and activities.
64
-
65
- # 5. **Additional Elements**: Add any extra details that will bring the scene to life, such as specific actions occurring, types of objects present, or interactions between characters.
66
-
67
- # The prompt should be detailed and evoke a clear and vivid image of the scene, allowing for a diverse and imaginative interpretation when visualized.]
68
- # """
69
-
70
- # creatmote_api_key = os.getenv('CREATOMATE_API_KEY')
71
- # bucket_name = os.getenv('BUCKET_NAME')
72
- # folder_name = os.getenv('FOLDER_NAME')
73
-
74
-
75
- # def generate_voiceover(line, index, bucket_name, folder_name):
76
- # try:
77
- # print(f"Starting generate_voiceover for line {index + 1}")
78
-
79
- # # Initialize the S3 client
80
- # s3 = boto3.client('s3')
81
- # print("S3 client initialized")
82
-
83
- # # Generate a unique file name for each line
84
- # file_name = f"vo_line_{index + 1}.mp3"
85
- # print(f"Generated file name: {file_name}")
86
-
87
- # speech_file_path = Path(file_name)
88
-
89
- # # Generate voiceover using OpenAI
90
- # print("Generating voiceover using OpenAI")
91
- # try:
92
- # response = client.audio.speech.create(
93
- # model="tts-1",
94
- # voice="alloy",
95
- # input=line
96
- # )
97
- # except Exception as e:
98
- # print(f"Error generating voiceover with OpenAI: {e}")
99
- # raise
100
-
101
- # # Write the audio content to a local file
102
- # with open(speech_file_path, 'wb') as f:
103
- # f.write(response.content)
104
- # print(f"Voiceover written to file {file_name}")
105
-
106
- # # Construct the S3 key (path in the bucket)
107
- # s3_key = f"{folder_name}/{file_name}"
108
- # print(f"S3 key constructed: {s3_key}")
109
-
110
- # # Upload the file to S3 inside the specified folder
111
- # print(f"Uploading {file_name} to S3 bucket {bucket_name}")
112
- # s3.upload_file(
113
- # Filename=str(speech_file_path),
114
- # Bucket=bucket_name,
115
- # Key=s3_key
116
- # )
117
- # print(f"Successfully uploaded {file_name} to S3 bucket {bucket_name}")
118
-
119
- # # Generate and return the S3 URL for the uploaded file
120
- # s3_url = f'https://{bucket_name}.s3.amazonaws.com/{s3_key}'
121
- # print(f"S3 URL for the uploaded file: {s3_url}")
122
-
123
- # return s3_url
124
-
125
- # except Exception as e:
126
- # print(f"An error occurred in generate_voiceover: {e}")
127
- # raise
128
-
129
- # def generate_image(visual_prompt, API_KEY, ENDPOINT):
130
- # print("Generating image for visual prompt")
131
-
132
- # # Create the Request Payload
133
- # payload = {
134
- # "model": "dall-e-3",
135
- # "prompt": f"<{visual_prompt.strip()}>",
136
- # "n": 1,
137
- # "size": "1024x1024"
138
- # }
139
-
140
- # # Make the POST Request
141
- # headers = {
142
- # 'Authorization': f'Bearer {API_KEY}',
143
- # 'Content-Type': 'application/json'
144
- # }
145
-
146
- # response = requests.post(ENDPOINT, json=payload, headers=headers)
147
- # print("POST request sent to OpenAI for image generation")
148
-
149
- # # Handle the Response
150
- # if response.status_code == 200:
151
- # image_url = response.json()['data'][0]['url']
152
- # print(f"Image successfully generated: {image_url}")
153
- # return image_url
154
- # else:
155
- # error_message = f"Error in image generation: {response.status_code}, {response.text}"
156
- # print(error_message)
157
- # raise Exception(error_message)
158
-
159
-
160
- # def generate_video(slo):
161
- # print(f"Starting video generation for SLO: {slo}")
162
-
163
- # # Step 1: Script Generation
164
- # render_url = 'https://api.creatomate.com/v1/renders'
165
- # combined_prompt = f"{content_creation_prompt}\n\nBased on the following Student Learning Objective (SLO): '{slo}', generate a detailed script for an educational video."
166
- # print("Generating script...")
167
- # script_response = client.chat.completions.create(model="gpt-4", messages=[{"role": "user", "content": combined_prompt}])
168
- # script_text = script_response.choices[0].message.content
169
- # print(f"Script generated: {script_text}")
170
-
171
- # # Step 2: Image Generation
172
- # print("Generating images...")
173
- # pattern = r'^VISUAL PROMPT:(.*)$'
174
- # visual_prompts = re.findall(pattern, script_text, re.MULTILINE)
175
- # generated_image_urls = []
176
- # API_KEY = api_key
177
- # ENDPOINT = 'https://api.openai.com/v1/images/generations'
178
- # for visual_prompt in visual_prompts:
179
- # image_url = generate_image(visual_prompt, API_KEY, ENDPOINT)
180
- # generated_image_urls.append(image_url)
181
- # print(f"Image generated and added to list: {image_url}")
182
-
183
- # # Step 3: Voiceover Generation and Upload to AWS S3
184
- # print("Generating voiceovers and uploading to AWS S3...")
185
- # pattern_vo = r'^V\.O:\s*(.*?)$'
186
- # vo_lines = re.findall(pattern_vo, script_text, re.MULTILINE)
187
- # s3_voiceover_urls = []
188
- # for i, line in enumerate(vo_lines):
189
- # s3_url = generate_voiceover(line, i, bucket_name, folder_name)
190
- # s3_voiceover_urls.append(s3_url)
191
- # print(f"Voiceover for line {i + 1} uploaded to S3: {s3_url}")
192
-
193
- # # Step 4: Video Compilation with Creatomate
194
- # print("Compiling video with Creatomate...")
195
- # modifications = {f'Visual prompt-{i+1}': url for i, url in enumerate(generated_image_urls)}
196
- # for index, audio_url in enumerate(s3_voiceover_urls):
197
- # modifications[f"vo_line_{index+1}"] = audio_url
198
-
199
- # payload = {
200
- # 'template_id': '6f315232-a8eb-4d1b-9079-1b9ee595cc2b',
201
- # 'modifications': modifications,
202
- # }
203
- # headers = {
204
- # 'Authorization': f'Bearer {creatmote_api_key}',
205
- # 'Content-Type': 'application/json'
206
- # }
207
-
208
- # try:
209
- # response = requests.post(render_url, data=json.dumps(payload), headers=headers)
210
- # response.raise_for_status() # This will raise an error for bad status codes
211
- # print("POST request sent to Creatomate for video compilation")
212
-
213
- # response_data = response.json()
214
- # print("Response JSON:", response_data)
215
- # if response_data and isinstance(response_data, list) and len(response_data) > 0:
216
- # video_url = response_data[0].get('url', 'No URL found in the response')
217
- # print(f"Video URL: {video_url}")
218
- # else:
219
- # print("No valid data in Creatomate response")
220
- # video_url = 'No valid data in response'
221
- # except requests.RequestException as error:
222
- # error_message = f"An error occurred: {str(error)}"
223
- # print(error_message)
224
- # return error_message
225
-
226
- # print("Video generation process completed")
227
- # return video_url
228
-
229
- # # Gradio Interface
230
- # iface = gr.Interface(
231
- # fn=generate_video,
232
- # inputs=gr.Textbox(label="Enter Student Learning Objective (SLO)"),
233
- # outputs=gr.Textbox(label="Generated Video URL")
234
- # )
235
-
236
- # print("Gradio interface set up")
237
-
238
-
239
- # iface.queue().launch(share=False)
240
- import gradio as gr
241
- import requests
242
- import re
243
- import boto3
244
- from openai import OpenAI
245
- from pathlib import Path
246
- import json
247
-
248
- # OpenAI and AWS credentials
249
- api_key='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'
250
- aws_access_key_id='AKIAWRPUYBFWJOU73CF5'
251
- aws_secret_access_key='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'
252
- aws_default_region='ap-south-1'
253
-
254
- # Initialize OpenAI and S3 clients
255
- client = OpenAI(api_key=api_key)
256
- s3 = boto3.client(
257
- 's3',
258
- aws_access_key_id=aws_access_key_id,
259
- aws_secret_access_key=aws_secret_access_key,
260
- region_name=aws_default_region
261
- )
262
-
263
  print("Clients initialized")
264
 
265
-
266
  content_creation_prompt = """
267
  [You are a teacher and a video creator and you create animated educational videos based on SLOs(Student learning outcomes) for students who don't really have a nag for the conventional ways of learning. You make sure that your video lessons are very very interesting and at the same time they still have all the teaching material that you want to incorporate. You have to make sure that the teaching material is logically incorporated within the stories, scenarios, or small daily life examples so that I can make an animated video out of it.
268
 
@@ -296,57 +65,44 @@ your output will be in a script format. Timestamps would be mentioned, write voi
296
  The prompt should be detailed and evoke a clear and vivid image of the scene, allowing for a diverse and imaginative interpretation when visualized.]
297
  """
298
 
299
- creatmote_api_key = 'f5e58633fd06409885c26800015ee564bd54aae3d60fd9a4380ff106c81994327e9a383492c239b64a713b9ddeb2128e'
300
- bucket_name = 'ai-textract-bucket'
301
- folder_name = 'voiceovers'
302
-
303
-
304
- def generate_voiceover(line, index, bucket_name, folder_name):
305
  try:
306
  print(f"Starting generate_voiceover for line {index + 1}")
307
 
308
- # Initialize the S3 client
309
- s3 = boto3.client('s3')
310
- print("S3 client initialized")
311
 
312
  # Generate a unique file name for each line
313
  file_name = f"vo_line_{index + 1}.mp3"
314
  print(f"Generated file name: {file_name}")
315
 
316
- speech_file_path = Path(file_name)
317
-
318
  # Generate voiceover using OpenAI
319
  print("Generating voiceover using OpenAI")
320
- try:
321
- response = client.audio.speech.create(
322
- model="tts-1",
323
- voice="alloy",
324
- input=line
325
- )
326
- except Exception as e:
327
- print(f"Error generating voiceover with OpenAI: {e}")
328
- raise
329
 
330
  # Write the audio content to a local file
 
331
  with open(speech_file_path, 'wb') as f:
332
  f.write(response.content)
333
  print(f"Voiceover written to file {file_name}")
334
 
335
- # Construct the S3 key (path in the bucket)
336
- s3_key = f"{folder_name}/{file_name}"
337
- print(f"S3 key constructed: {s3_key}")
338
-
339
- # Upload the file to S3 inside the specified folder
340
- print(f"Uploading {file_name} to S3 bucket {bucket_name}")
341
- s3.upload_file(
342
- Filename=str(speech_file_path),
343
- Bucket=bucket_name,
344
- Key=s3_key
345
- )
346
- print(f"Successfully uploaded {file_name} to S3 bucket {bucket_name}")
347
 
348
  # Generate and return the S3 URL for the uploaded file
349
- s3_url = f'https://{bucket_name}.s3.amazonaws.com/{s3_key}'
350
  print(f"S3 URL for the uploaded file: {s3_url}")
351
 
352
  return s3_url
@@ -415,7 +171,7 @@ def generate_video(slo):
415
  vo_lines = re.findall(pattern_vo, script_text, re.MULTILINE)
416
  s3_voiceover_urls = []
417
  for i, line in enumerate(vo_lines):
418
- s3_url = generate_voiceover(line, i, bucket_name, folder_name)
419
  s3_voiceover_urls.append(s3_url)
420
  print(f"Voiceover for line {i + 1} uploaded to S3: {s3_url}")
421
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import re
4
+ import boto3
5
+ import os
6
+ import json
7
+ from gradio.components import Textbox
8
+ from openai import OpenAI
9
+ from pathlib import Path
10
+ from datasets.filesystems import S3FileSystem
11
+ import s3fs
 
12
 
 
13
 
14
+ api_key= os.getenv('OPENAI_API_KEY')
15
 
16
+ client = OpenAI(api_key=api_key)
 
 
 
17
 
18
 
19
+ # Set your API keys and configurations here
20
+ aws_access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
21
+ aws_secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')
22
+ aws_default_region = os.getenv('AWS_DEFAULT_REGION')
23
 
24
+ # Initialize OpenAI and S3 clients
25
  # client = OpenAI(api_key=api_key)
26
  # s3 = boto3.client(
27
  # 's3',
 
30
  # region_name=aws_default_region
31
  # )
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  print("Clients initialized")
34
 
 
35
  content_creation_prompt = """
36
  [You are a teacher and a video creator and you create animated educational videos based on SLOs(Student learning outcomes) for students who don't really have a nag for the conventional ways of learning. You make sure that your video lessons are very very interesting and at the same time they still have all the teaching material that you want to incorporate. You have to make sure that the teaching material is logically incorporated within the stories, scenarios, or small daily life examples so that I can make an animated video out of it.
37
 
 
65
  The prompt should be detailed and evoke a clear and vivid image of the scene, allowing for a diverse and imaginative interpretation when visualized.]
66
  """
67
 
68
+ creatmote_api_key = os.getenv('CREATOMATE_API_KEY')
69
+ bucket_name = os.getenv('BUCKET_NAME')
70
+ folder_name = os.getenv('FOLDER_NAME')
71
+ def generate_voiceover(line, index, bucket_name, folder_name, aws_access_key_id, aws_secret_access_key):
 
 
72
  try:
73
  print(f"Starting generate_voiceover for line {index + 1}")
74
 
75
+ # Initialize the S3FileSystem with AWS credentials
76
+ s3_fs = s3fs.S3FileSystem(key=aws_access_key_id, secret=aws_secret_access_key)
77
+ print("S3FileSystem initialized")
78
 
79
  # Generate a unique file name for each line
80
  file_name = f"vo_line_{index + 1}.mp3"
81
  print(f"Generated file name: {file_name}")
82
 
 
 
83
  # Generate voiceover using OpenAI
84
  print("Generating voiceover using OpenAI")
85
+ response = client.audio.speech.create(
86
+ model="tts-1",
87
+ voice="alloy",
88
+ input=line
89
+ )
 
 
 
 
90
 
91
  # Write the audio content to a local file
92
+ speech_file_path = Path(file_name)
93
  with open(speech_file_path, 'wb') as f:
94
  f.write(response.content)
95
  print(f"Voiceover written to file {file_name}")
96
 
97
+ # Upload the file to S3
98
+ s3_path = f"{bucket_name}/{folder_name}/{file_name}"
99
+ print(f"Uploading {file_name} to S3 path {s3_path}")
100
+ with open(speech_file_path, 'rb') as f:
101
+ s3_fs.put(str(speech_file_path), s3_path) # Corrected the order of arguments here
102
+ print(f"Successfully uploaded {file_name} to S3")
 
 
 
 
 
 
103
 
104
  # Generate and return the S3 URL for the uploaded file
105
+ s3_url = f'https://{bucket_name}.s3.amazonaws.com/{folder_name}/{file_name}'
106
  print(f"S3 URL for the uploaded file: {s3_url}")
107
 
108
  return s3_url
 
171
  vo_lines = re.findall(pattern_vo, script_text, re.MULTILINE)
172
  s3_voiceover_urls = []
173
  for i, line in enumerate(vo_lines):
174
+ s3_url = generate_voiceover(line, i, bucket_name, folder_name, aws_access_key_id, aws_secret_access_key)
175
  s3_voiceover_urls.append(s3_url)
176
  print(f"Voiceover for line {i + 1} uploaded to S3: {s3_url}")
177