Update app.py
Browse files
app.py
CHANGED
@@ -6,159 +6,100 @@ from gtts import gTTS
|
|
6 |
import cv2
|
7 |
import moviepy.editor as mp
|
8 |
import logging
|
9 |
-
from hercai import Hercai
|
10 |
import uuid
|
11 |
import time
|
12 |
import gradio as gr
|
13 |
import requests
|
14 |
-
|
15 |
-
# Configure
|
16 |
-
log_dir = os.getenv('LOG_DIRECTORY', './')
|
17 |
-
LOGGER_FILE_PATH = os.path.join(str(log_dir), 'utils.log')
|
18 |
|
19 |
logging.basicConfig(
|
20 |
filename=LOGGER_FILE_PATH,
|
21 |
-
filemode='a',
|
22 |
-
format='[%(asctime)s] [%(levelname)s] [%(filename)s] [%(lineno)s:%(funcName)s()] %(message)s',
|
23 |
-
datefmt='%Y-%b-%d %H:%M:%S'
|
24 |
)
|
25 |
-
LOGGER = logging.getLogger(__name__)
|
26 |
|
27 |
-
log_level_env = os.getenv('LOG_LEVEL', 'INFO')
|
28 |
-
log_level_dict = {
|
29 |
'DEBUG': logging.DEBUG,
|
30 |
'INFO': logging.INFO,
|
31 |
'WARNING': logging.WARNING,
|
32 |
'ERROR': logging.ERROR,
|
33 |
'CRITICAL': logging.CRITICAL
|
34 |
}
|
35 |
-
# Set the log level based on the environment variable or default to INFO
|
36 |
if log_level_env in log_level_dict:
|
37 |
log_level = log_level_dict[log_level_env]
|
38 |
else:
|
39 |
log_level = log_level_dict['INFO']
|
40 |
-
LOGGER.setLevel(log_level)
|
41 |
|
42 |
|
43 |
class Text2Video:
|
44 |
-
"""
|
45 |
-
A class to generate videos from text prompts, with detailed logging, model selection, and a user-friendly interface.
|
46 |
-
"""
|
47 |
|
48 |
def __init__(self) -> None:
|
49 |
"""
|
50 |
Initialize the Text2Video class.
|
|
|
|
|
51 |
"""
|
52 |
-
|
53 |
-
self.
|
54 |
-
|
|
|
55 |
|
56 |
-
def get_image(self, img_prompt: str
|
57 |
"""
|
58 |
-
Generate an image
|
59 |
-
|
60 |
Args:
|
61 |
-
img_prompt (str):
|
62 |
-
image_generator (str): The name of the AI image generation service (Hercai, Prodia, or Pollinations).
|
63 |
-
image_model (str): The specific model to use within the selected AI image generation service.
|
64 |
-
|
65 |
Returns:
|
66 |
-
str:
|
67 |
"""
|
68 |
-
LOGGER.info(f"Generating image for prompt: {img_prompt}")
|
69 |
try:
|
70 |
-
#
|
71 |
-
|
72 |
-
|
73 |
-
# Log the modified prompt
|
74 |
-
LOGGER.info(f"Modified prompt for {image_generator}: {modified_prompt}")
|
75 |
-
image_url = ""
|
76 |
-
|
77 |
-
if image_generator == "Hercai":
|
78 |
-
# Log the selected Hercai model
|
79 |
-
LOGGER.info(f"Using Hercai model: {image_model}")
|
80 |
-
|
81 |
-
# Generate the image using Hercai
|
82 |
-
image_result = self.herc.draw_image(model=image_model, prompt=modified_prompt, negative_prompt="Dark and gloomy")
|
83 |
-
# Extract the image URL from the result
|
84 |
-
image_url = image_result["url"]
|
85 |
-
|
86 |
-
elif image_generator == "Prodia":
|
87 |
-
# Log the selected Prodia model
|
88 |
-
LOGGER.info(f"Using Prodia model: {image_model}")
|
89 |
-
# Create the Prodia API call
|
90 |
-
api_url = "https://api.prodia.com/v1/generate"
|
91 |
-
payload = {
|
92 |
-
"model": image_model,
|
93 |
-
"prompt": modified_prompt,
|
94 |
-
"negative_prompt": "Dark and gloomy"
|
95 |
-
}
|
96 |
-
headers = {
|
97 |
-
"Authorization": "Bearer YOUR_PRODIA_API_KEY" # Replace YOUR_PRODIA_API_KEY with your actual Prodia API key
|
98 |
-
}
|
99 |
-
response = requests.post(api_url, json=payload, headers=headers)
|
100 |
-
if response.status_code == 200:
|
101 |
-
image_url = response.json()["url"]
|
102 |
-
# Log the generated image URL
|
103 |
-
LOGGER.info(f"Image generated successfully using Prodia: {image_url}")
|
104 |
-
else:
|
105 |
-
# Log an error if the Prodia API call failed
|
106 |
-
LOGGER.error(f"Error generating image using Prodia: {response.text}")
|
107 |
-
|
108 |
-
elif image_generator == "Pollinations":
|
109 |
-
# Log the selected Pollinations model
|
110 |
-
LOGGER.info(f"Using Pollinations model: {image_model}")
|
111 |
-
# Implement Pollinations API call here, similar to Prodia
|
112 |
-
# Replace the following placeholder with your Pollinations API call
|
113 |
-
# ...
|
114 |
-
|
115 |
-
# Log the generated image URL
|
116 |
-
LOGGER.info(f"Image generated successfully: {image_url}")
|
117 |
return image_url
|
118 |
|
119 |
except Exception as e:
|
120 |
# Log any errors encountered during image generation
|
121 |
-
LOGGER.error(f"Error generating image
|
122 |
return ""
|
123 |
|
124 |
def download_img_from_url(self, image_url: str, image_path: str) -> str:
|
125 |
"""
|
126 |
-
Download an image from a URL
|
127 |
-
|
128 |
Args:
|
129 |
-
image_url (str):
|
130 |
-
image_path (str):
|
131 |
-
|
132 |
Returns:
|
133 |
-
str:
|
134 |
"""
|
135 |
-
LOGGER.info(f"Downloading image from URL: {image_url}")
|
136 |
try:
|
137 |
-
# Download the image from the URL and save it to the specified path
|
138 |
urllib.request.urlretrieve(image_url, image_path)
|
139 |
-
|
140 |
-
LOGGER.info(f"Image downloaded and saved to: {image_path}")
|
141 |
-
return image_path
|
142 |
|
143 |
except Exception as e:
|
144 |
# Log any errors encountered during image download
|
145 |
-
LOGGER.error(f"Error downloading image from URL
|
146 |
-
return ""
|
147 |
|
148 |
def text_to_audio(self, img_prompt: str, audio_path: str) -> str:
|
149 |
"""
|
150 |
-
Convert text to speech
|
151 |
-
|
152 |
Args:
|
153 |
-
img_prompt (str):
|
154 |
-
audio_path (str):
|
155 |
-
|
156 |
Returns:
|
157 |
-
str:
|
158 |
"""
|
159 |
-
LOGGER.info(f"Converting text to audio: {img_prompt}")
|
160 |
try:
|
161 |
-
# Set the language for speech synthesis (English in this case)
|
162 |
language = 'en'
|
163 |
|
164 |
# Create a gTTS object to convert text to speech
|
@@ -167,41 +108,36 @@ class Text2Video:
|
|
167 |
# Save the audio file at the specified path
|
168 |
myobj.save(audio_path)
|
169 |
|
170 |
-
|
171 |
return audio_path
|
172 |
except Exception as e:
|
|
|
173 |
# Log any errors encountered during text-to-audio conversion
|
174 |
-
LOGGER.error(f"Error converting text
|
175 |
return ""
|
176 |
|
177 |
-
def get_images_and_audio(self, list_prompts: list
|
178 |
"""
|
179 |
-
Generate images and corresponding audio files
|
180 |
-
|
181 |
Args:
|
182 |
-
list_prompts (list):
|
183 |
-
image_generator (str): The name of the AI image generation service (Hercai, Prodia, or Pollinations).
|
184 |
-
image_model (str): The specific model to use within the selected AI image generation service.
|
185 |
-
|
186 |
Returns:
|
187 |
-
tuple: A tuple containing
|
188 |
"""
|
189 |
-
|
190 |
-
|
191 |
-
audio_paths = [] # List to store audio paths
|
192 |
for img_prompt in list_prompts:
|
193 |
-
LOGGER.info(f"Processing prompt: {img_prompt}")
|
194 |
try:
|
195 |
-
# Generate a unique identifier for
|
196 |
unique_id = uuid.uuid4().hex
|
197 |
|
198 |
# Construct the image path using the unique identifier
|
199 |
image_path = f"{img_prompt[:9]}_{unique_id}.png"
|
200 |
|
201 |
-
# Generate
|
202 |
-
img_url = self.get_image(img_prompt
|
203 |
|
204 |
-
# Download
|
205 |
image = self.download_img_from_url(img_url, image_path)
|
206 |
|
207 |
# Add the image path to the list
|
@@ -210,159 +146,184 @@ class Text2Video:
|
|
210 |
# Construct the audio path using the unique identifier
|
211 |
audio_path = f"{img_prompt[:9]}_{unique_id}.mp3"
|
212 |
|
213 |
-
# Convert
|
214 |
audio = self.text_to_audio(img_prompt, audio_path)
|
215 |
|
216 |
# Add the audio path to the list
|
217 |
audio_paths.append(audio)
|
218 |
|
219 |
except Exception as e:
|
220 |
-
|
221 |
-
LOGGER.error(f"Error processing prompt '{img_prompt}': {e}")
|
222 |
|
223 |
-
# Return
|
224 |
-
LOGGER.info("Images and audio generated successfully")
|
225 |
return img_list, audio_paths
|
226 |
|
227 |
def create_video_from_images_and_audio(self, image_files: list, audio_files: list, output_path: str) -> None:
|
228 |
"""
|
229 |
-
|
230 |
-
|
231 |
Args:
|
232 |
-
image_files (list):
|
233 |
-
audio_files (list):
|
234 |
-
output_path (str):
|
235 |
"""
|
236 |
-
LOGGER.info("Creating video from images and audio")
|
237 |
try:
|
238 |
-
# Check if the number of images
|
239 |
if len(image_files) != len(audio_files):
|
240 |
-
# Log an error if the number of image files and audio files don't match
|
241 |
LOGGER.error("Error: Number of images doesn't match the number of audio files.")
|
242 |
return
|
243 |
|
244 |
-
#
|
245 |
video_clips = []
|
246 |
|
247 |
-
# Loop through each image file and corresponding audio file
|
248 |
for image_file, audio_file in zip(image_files, audio_files):
|
249 |
-
LOGGER.info(f"Processing image: {image_file}, audio: {audio_file}")
|
250 |
|
251 |
-
# Read the image
|
252 |
frame = cv2.imread(image_file)
|
253 |
|
254 |
-
# Load the audio clip
|
255 |
audio_clip = mp.AudioFileClip(audio_file)
|
256 |
|
257 |
-
# Create
|
258 |
video_clip = mp.ImageClip(image_file).set_duration(audio_clip.duration)
|
259 |
|
260 |
-
# Set
|
261 |
video_clip = video_clip.set_audio(audio_clip)
|
262 |
|
263 |
-
# Append the video clip to the list
|
264 |
video_clips.append(video_clip)
|
265 |
|
266 |
-
# Concatenate all
|
267 |
final_clip = mp.concatenate_videoclips(video_clips)
|
268 |
|
269 |
-
# Write the final video
|
270 |
final_clip.write_videofile(output_path, codec='libx264', fps=24)
|
271 |
-
|
272 |
-
LOGGER.info(f"Video created successfully at: {output_path}")
|
273 |
|
274 |
except Exception as e:
|
275 |
# Log any errors encountered during video creation
|
276 |
LOGGER.error(f"Error creating video: {e}")
|
277 |
|
278 |
-
def generate_video(self, text:
|
279 |
"""
|
280 |
-
Generate a video from a
|
281 |
-
|
282 |
Args:
|
283 |
-
|
284 |
-
image_generator (str): The name of the AI image generation service (Hercai, Prodia, or Pollinations).
|
285 |
-
image_model (str): The specific model to use within the selected AI image generation service.
|
286 |
-
|
287 |
-
Returns:
|
288 |
-
str: The file path of the generated video file. Returns an empty string if an error occurred.
|
289 |
"""
|
290 |
-
LOGGER.info("Generating video from text")
|
291 |
try:
|
292 |
-
# Split the input text into a list of prompts
|
293 |
list_prompts = [sentence.strip() for sentence in text.split(",,") if sentence.strip()]
|
294 |
-
LOGGER.info(f"Prompts extracted from text: {list_prompts}")
|
295 |
|
296 |
-
#
|
297 |
-
output_path = "
|
298 |
|
299 |
-
# Generate images and corresponding audio files
|
300 |
-
img_list, audio_paths = self.get_images_and_audio(list_prompts
|
301 |
|
302 |
-
# Create
|
303 |
self.create_video_from_images_and_audio(img_list, audio_paths, output_path)
|
304 |
|
305 |
-
LOGGER.info(f"Video generated successfully: {output_path}")
|
306 |
return output_path
|
307 |
-
|
308 |
except Exception as e:
|
|
|
309 |
# Log any errors encountered during video generation
|
310 |
-
LOGGER.error(f"Error generating video
|
311 |
-
return ""
|
312 |
|
313 |
def gradio_interface(self):
|
314 |
-
"""
|
315 |
-
Creates a user-friendly Gradio interface for the video generation application.
|
316 |
-
"""
|
317 |
-
LOGGER.info("Launching Gradio interface")
|
318 |
-
with gr.Blocks(css="style.css", theme='abidlabs/dracula_revamped') as demo:
|
319 |
-
# Set the title of the application
|
320 |
-
gr.HTML("""
|
321 |
-
<center><h1 style="color:#fff">Comics Video Generator</h1></center>""")
|
322 |
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
|
328 |
-
|
329 |
-
|
330 |
-
image_generator = gr.Dropdown(label="Image Generator",
|
331 |
-
choices=["Hercai", "Prodia", "Pollinations"],
|
332 |
-
value="Hercai",
|
333 |
-
interactive=True)
|
334 |
|
335 |
-
# Create a dropdown menu for selecting the specific model within the chosen service
|
336 |
with gr.Row(elem_id="col-container"):
|
337 |
-
|
338 |
-
choices=["v1", "v2", "v3", "simurg", "animefy", "raava", "shonin"],
|
339 |
-
value="v3",
|
340 |
-
interactive=True)
|
341 |
|
342 |
-
# Create a button that triggers the video generation process
|
343 |
with gr.Row(elem_id="col-container"):
|
344 |
button = gr.Button("Generate Video")
|
345 |
|
346 |
-
# Create a component to display the generated video
|
347 |
with gr.Row(elem_id="col-container"):
|
348 |
output = gr.PlayableVideo()
|
349 |
|
350 |
-
# Provide an example to guide users on how to format their input
|
351 |
with gr.Row(elem_id="col-container"):
|
352 |
-
example_txt = """Once upon a time there was a village. It was a nice place to live, except for one thing. People did not like to share.,,
|
353 |
-
One day a visitor came to town. 'Hello. Does anybody have food to share?' He asked. 'No', said everyone.,,
|
354 |
-
'That's okay', said the visitor. 'I will make stone soup for everyone'. Then he took a stone and dropped it into a giant pot,,"""
|
355 |
example = gr.Examples([example_txt], input_text)
|
356 |
|
357 |
-
|
358 |
-
button.click(self.generate_video, [input_text, image_generator, image_model], output)
|
359 |
-
|
360 |
-
LOGGER.info("Gradio interface launched successfully")
|
361 |
-
# Launch the Gradio interface
|
362 |
demo.launch(debug=True)
|
363 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
364 |
|
365 |
if __name__ == "__main__":
|
366 |
-
|
367 |
-
text2video
|
368 |
-
text2video.gradio_interface() # Launch the Gradio interface
|
|
|
6 |
import cv2
|
7 |
import moviepy.editor as mp
|
8 |
import logging
|
|
|
9 |
import uuid
|
10 |
import time
|
11 |
import gradio as gr
|
12 |
import requests
|
13 |
+
from random import randint
|
14 |
+
# Configure logging
|
15 |
+
log_dir = os.getenv('LOG_DIRECTORY', './')
|
16 |
+
LOGGER_FILE_PATH = os.path.join(str(log_dir), 'utils.log')
|
17 |
|
18 |
logging.basicConfig(
|
19 |
filename=LOGGER_FILE_PATH,
|
20 |
+
filemode='a',
|
21 |
+
format='[%(asctime)s] [%(levelname)s] [%(filename)s] [%(lineno)s:%(funcName)s()] %(message)s',
|
22 |
+
datefmt='%Y-%b-%d %H:%M:%S'
|
23 |
)
|
24 |
+
LOGGER = logging.getLogger(__name__)
|
25 |
|
26 |
+
log_level_env = os.getenv('LOG_LEVEL', 'INFO')
|
27 |
+
log_level_dict = {
|
28 |
'DEBUG': logging.DEBUG,
|
29 |
'INFO': logging.INFO,
|
30 |
'WARNING': logging.WARNING,
|
31 |
'ERROR': logging.ERROR,
|
32 |
'CRITICAL': logging.CRITICAL
|
33 |
}
|
|
|
34 |
if log_level_env in log_level_dict:
|
35 |
log_level = log_level_dict[log_level_env]
|
36 |
else:
|
37 |
log_level = log_level_dict['INFO']
|
38 |
+
LOGGER.setLevel(log_level)
|
39 |
|
40 |
|
41 |
class Text2Video:
|
42 |
+
"""A class to generate videos from text prompts."""
|
|
|
|
|
43 |
|
44 |
def __init__(self) -> None:
|
45 |
"""
|
46 |
Initialize the Text2Video class.
|
47 |
+
Args:
|
48 |
+
file_path (str): Path to the configuration file.
|
49 |
"""
|
50 |
+
# Replace Azure OpenAI with Hercai
|
51 |
+
self.hercai = Hercai("") # Replace "" with your Hercai API key if you have one
|
52 |
+
self.prodia_model = "stable-diffusion-xl"
|
53 |
+
self.pollinations_model = None
|
54 |
|
55 |
+
def get_image(self, img_prompt: str) -> str:
|
56 |
"""
|
57 |
+
Generate an image based on the provided text prompt using Hercai's draw_image method.
|
|
|
58 |
Args:
|
59 |
+
img_prompt (str): Text prompt for generating the image.
|
|
|
|
|
|
|
60 |
Returns:
|
61 |
+
str: URL of the generated image.
|
62 |
"""
|
|
|
63 |
try:
|
64 |
+
# Generate image using Hercai's draw_image method
|
65 |
+
image_result = self.hercai.draw_image(model="v3", prompt=img_prompt, negative_prompt="")
|
66 |
+
image_url = image_result["url"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
return image_url
|
68 |
|
69 |
except Exception as e:
|
70 |
# Log any errors encountered during image generation
|
71 |
+
LOGGER.error(f"Error generating image: {e}")
|
72 |
return ""
|
73 |
|
74 |
def download_img_from_url(self, image_url: str, image_path: str) -> str:
|
75 |
"""
|
76 |
+
Download an image from a URL.
|
|
|
77 |
Args:
|
78 |
+
image_url (str): URL of the image to download.
|
79 |
+
image_path (str): Path to save the downloaded image.
|
|
|
80 |
Returns:
|
81 |
+
str: Path of the downloaded image.
|
82 |
"""
|
|
|
83 |
try:
|
84 |
+
# Download the image from the provided URL and save it to the specified path
|
85 |
urllib.request.urlretrieve(image_url, image_path)
|
86 |
+
return image_path # Return the path of the downloaded image if successful
|
|
|
|
|
87 |
|
88 |
except Exception as e:
|
89 |
# Log any errors encountered during image download
|
90 |
+
LOGGER.error(f"Error downloading image from URL: {e}")
|
91 |
+
return "" # Return an empty string if an error occurs
|
92 |
|
93 |
def text_to_audio(self, img_prompt: str, audio_path: str) -> str:
|
94 |
"""
|
95 |
+
Convert text to speech and save it as an audio file.
|
|
|
96 |
Args:
|
97 |
+
img_prompt (str): Text to convert to speech.
|
98 |
+
audio_path (str): Path to save the audio file.
|
|
|
99 |
Returns:
|
100 |
+
str: Path of the saved audio file.
|
101 |
"""
|
|
|
102 |
try:
|
|
|
103 |
language = 'en'
|
104 |
|
105 |
# Create a gTTS object to convert text to speech
|
|
|
108 |
# Save the audio file at the specified path
|
109 |
myobj.save(audio_path)
|
110 |
|
111 |
+
# Return the path of the saved audio file if successful
|
112 |
return audio_path
|
113 |
except Exception as e:
|
114 |
+
|
115 |
# Log any errors encountered during text-to-audio conversion
|
116 |
+
LOGGER.error(f"Error converting text to audio: {e}")
|
117 |
return ""
|
118 |
|
119 |
+
def get_images_and_audio(self, list_prompts: list) -> tuple:
|
120 |
"""
|
121 |
+
Generate images and corresponding audio files from a list of prompts.
|
|
|
122 |
Args:
|
123 |
+
list_prompts (list): List of text prompts.
|
|
|
|
|
|
|
124 |
Returns:
|
125 |
+
tuple: A tuple containing lists of image paths and audio paths.
|
126 |
"""
|
127 |
+
img_list = [] # Initialize an empty list to store image paths
|
128 |
+
audio_paths = [] # Initialize an empty list to store audio paths
|
|
|
129 |
for img_prompt in list_prompts:
|
|
|
130 |
try:
|
131 |
+
# Generate a unique identifier for this file
|
132 |
unique_id = uuid.uuid4().hex
|
133 |
|
134 |
# Construct the image path using the unique identifier
|
135 |
image_path = f"{img_prompt[:9]}_{unique_id}.png"
|
136 |
|
137 |
+
# Generate image URL based on the prompt
|
138 |
+
img_url = self.get_image(img_prompt)
|
139 |
|
140 |
+
# Download and save the image
|
141 |
image = self.download_img_from_url(img_url, image_path)
|
142 |
|
143 |
# Add the image path to the list
|
|
|
146 |
# Construct the audio path using the unique identifier
|
147 |
audio_path = f"{img_prompt[:9]}_{unique_id}.mp3"
|
148 |
|
149 |
+
# Convert text to audio and save it
|
150 |
audio = self.text_to_audio(img_prompt, audio_path)
|
151 |
|
152 |
# Add the audio path to the list
|
153 |
audio_paths.append(audio)
|
154 |
|
155 |
except Exception as e:
|
156 |
+
LOGGER.error(f"Error processing prompt: {img_prompt}, {e}")
|
|
|
157 |
|
158 |
+
# Return lists of image paths and audio paths as a tuple
|
|
|
159 |
return img_list, audio_paths
|
160 |
|
161 |
def create_video_from_images_and_audio(self, image_files: list, audio_files: list, output_path: str) -> None:
|
162 |
"""
|
163 |
+
Create a video from images and corresponding audio files.
|
|
|
164 |
Args:
|
165 |
+
image_files (list): List of image files.
|
166 |
+
audio_files (list): List of audio files.
|
167 |
+
output_path (str): Path to save the output video file.
|
168 |
"""
|
|
|
169 |
try:
|
170 |
+
# Check if the number of images matches the number of audio files
|
171 |
if len(image_files) != len(audio_files):
|
|
|
172 |
LOGGER.error("Error: Number of images doesn't match the number of audio files.")
|
173 |
return
|
174 |
|
175 |
+
# Initialize an empty list to store video clips
|
176 |
video_clips = []
|
177 |
|
|
|
178 |
for image_file, audio_file in zip(image_files, audio_files):
|
|
|
179 |
|
180 |
+
# Read the image frame
|
181 |
frame = cv2.imread(image_file)
|
182 |
|
183 |
+
# Load the audio clip
|
184 |
audio_clip = mp.AudioFileClip(audio_file)
|
185 |
|
186 |
+
# Create video clip with image
|
187 |
video_clip = mp.ImageClip(image_file).set_duration(audio_clip.duration)
|
188 |
|
189 |
+
# Set audio for the video clip
|
190 |
video_clip = video_clip.set_audio(audio_clip)
|
191 |
|
192 |
+
# Append the video clip to the list
|
193 |
video_clips.append(video_clip)
|
194 |
|
195 |
+
# Concatenate all video clips into a single clip
|
196 |
final_clip = mp.concatenate_videoclips(video_clips)
|
197 |
|
198 |
+
# Write the final video to the output path
|
199 |
final_clip.write_videofile(output_path, codec='libx264', fps=24)
|
200 |
+
print("Video created successfully.")
|
|
|
201 |
|
202 |
except Exception as e:
|
203 |
# Log any errors encountered during video creation
|
204 |
LOGGER.error(f"Error creating video: {e}")
|
205 |
|
206 |
+
def generate_video(self, text: list) -> None:
|
207 |
"""
|
208 |
+
Generate a video from a list of text prompts.
|
|
|
209 |
Args:
|
210 |
+
list_prompts (list): List of text prompts.
|
|
|
|
|
|
|
|
|
|
|
211 |
"""
|
|
|
212 |
try:
|
|
|
213 |
list_prompts = [sentence.strip() for sentence in text.split(",,") if sentence.strip()]
|
|
|
214 |
|
215 |
+
# Set the output path for the generated video
|
216 |
+
output_path = "output_video1.mp4"
|
217 |
|
218 |
+
# Generate images and corresponding audio files
|
219 |
+
img_list, audio_paths = self.get_images_and_audio(list_prompts)
|
220 |
|
221 |
+
# Create video from images and audio
|
222 |
self.create_video_from_images_and_audio(img_list, audio_paths, output_path)
|
223 |
|
|
|
224 |
return output_path
|
|
|
225 |
except Exception as e:
|
226 |
+
|
227 |
# Log any errors encountered during video generation
|
228 |
+
LOGGER.error(f"Error generating video: {e}")
|
|
|
229 |
|
230 |
def gradio_interface(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
|
232 |
+
with gr.Blocks(css="style.css", theme='abidlabs/dracula_revamped') as demo:
|
233 |
+
example_txt = """once upon a time there was a village. It was a nice place to live, except for one thing. people did not like to share.,, One day a visitor came to town.
|
234 |
+
'Hello. Does anybody have food to share?' He asked. 'No', said everyone.,,
|
235 |
+
That's okay', said the visitor. 'I will make stone soup for everyone'.Then he took a stone and dropped it into a giant pot,,"""
|
236 |
|
237 |
+
gr.HTML("""
|
238 |
+
<center><h1 style="color:#fff">Comics Video Generator</h1></center>""")
|
|
|
|
|
|
|
|
|
239 |
|
|
|
240 |
with gr.Row(elem_id="col-container"):
|
241 |
+
input_text = gr.Textbox(label="Comics Text", placeholder="Enter the comics by double comma separated")
|
|
|
|
|
|
|
242 |
|
|
|
243 |
with gr.Row(elem_id="col-container"):
|
244 |
button = gr.Button("Generate Video")
|
245 |
|
|
|
246 |
with gr.Row(elem_id="col-container"):
|
247 |
output = gr.PlayableVideo()
|
248 |
|
|
|
249 |
with gr.Row(elem_id="col-container"):
|
|
|
|
|
|
|
250 |
example = gr.Examples([example_txt], input_text)
|
251 |
|
252 |
+
button.click(self.generate_video, [input_text], output)
|
|
|
|
|
|
|
|
|
253 |
demo.launch(debug=True)
|
254 |
|
255 |
+
# --- Prodia & Pollinations Methods ---
|
256 |
+
def prodia_generate(self, model, prompt, output_file="prodia_output.png"):
|
257 |
+
s = requests.Session()
|
258 |
+
headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"}
|
259 |
+
|
260 |
+
resp = s.get(
|
261 |
+
"https://api.prodia.com/generate",
|
262 |
+
params={
|
263 |
+
"new": "true", "prompt": prompt, "model": model,
|
264 |
+
"negative_prompt": "verybadimagenegative_v1.3",
|
265 |
+
"steps": "20", "cfg": "7", "seed": randint(1, 10000),
|
266 |
+
"sample": "DPM++ 2M Karras", "aspect_ratio": "square"
|
267 |
+
},
|
268 |
+
headers=headers
|
269 |
+
)
|
270 |
+
|
271 |
+
job_id = resp.json()['job']
|
272 |
+
while True:
|
273 |
+
time.sleep(5)
|
274 |
+
status = s.get(f"https://api.prodia.com/job/{job_id}", headers=headers).json()
|
275 |
+
if status["status"] == "succeeded":
|
276 |
+
img_data = s.get(f"https://images.prodia.xyz/{job_id}.png?download=1", headers=headers).content
|
277 |
+
with open(output_file, 'wb') as f:
|
278 |
+
f.write(img_data)
|
279 |
+
return output_file
|
280 |
+
return None
|
281 |
+
|
282 |
+
def pollinations_generate(self, prompt, output_file="pollinations_output.png"):
|
283 |
+
response = requests.get(f"https://image.pollinations.ai/prompt/{prompt}{randint(1, 10000)}")
|
284 |
+
if response.status_code == 200:
|
285 |
+
with open(output_file, 'wb') as f:
|
286 |
+
f.write(response.content)
|
287 |
+
return output_file
|
288 |
+
return None
|
289 |
+
|
290 |
+
# --- Hercai Class ---
|
291 |
+
class Hercai:
|
292 |
+
def __init__(self, api_key=None):
|
293 |
+
self.api_key = api_key
|
294 |
+
|
295 |
+
def question(self, model="v3", content="", personality=None):
|
296 |
+
url = f"https://hercai.onrender.com/v3/hercai?question={content}&model={model}"
|
297 |
+
if personality:
|
298 |
+
url += f"&personality={personality}"
|
299 |
+
if self.api_key:
|
300 |
+
url += f"&key={self.api_key}"
|
301 |
+
response = requests.get(url)
|
302 |
+
return response.json()
|
303 |
+
|
304 |
+
def draw_image(self, model="v3", prompt="", negative_prompt=""):
|
305 |
+
url = f"https://hercai.onrender.com/v3/text2image?prompt={prompt}&model={model}&negative_prompt={negative_prompt}"
|
306 |
+
if self.api_key:
|
307 |
+
url += f"&key={self.api_key}"
|
308 |
+
response = requests.get(url)
|
309 |
+
return response.json()
|
310 |
+
|
311 |
+
def beta_question(self, model="v3", content="", personality=None):
|
312 |
+
url = f"https://hercai.onrender.com/beta/hercai?question={content}&model={model}"
|
313 |
+
if personality:
|
314 |
+
url += f"&personality={personality}"
|
315 |
+
if self.api_key:
|
316 |
+
url += f"&key={self.api_key}"
|
317 |
+
response = requests.get(url)
|
318 |
+
return response.json()
|
319 |
+
|
320 |
+
def beta_draw_image(self, model="v3", prompt="", negative_prompt=""):
|
321 |
+
url = f"https://hercai.onrender.com/beta/text2image?prompt={prompt}&model={model}&negative_prompt={negative_prompt}"
|
322 |
+
if self.api_key:
|
323 |
+
url += f"&key={self.api_key}"
|
324 |
+
response = requests.get(url)
|
325 |
+
return response.json()
|
326 |
|
327 |
if __name__ == "__main__":
|
328 |
+
text2video = Text2Video()
|
329 |
+
text2video.gradio_interface()
|
|