File size: 9,131 Bytes
4ae5311 3b31d0b 4ae5311 3b31d0b 4ae5311 3b31d0b ff73331 3b31d0b 49f4a30 885164c 3b31d0b 49f4a30 4ae5311 ff73331 49f4a30 ff73331 49f4a30 ff73331 4ae5311 3b31d0b 885164c 3b31d0b 49f4a30 3b31d0b 4ae5311 885164c 49f4a30 3fdfe7d 49f4a30 885164c ff73331 885164c 49f4a30 4ae5311 885164c ff73331 4ae5311 3b31d0b 49f4a30 4ae5311 49f4a30 885164c 49f4a30 3b31d0b 4ae5311 49f4a30 ff73331 49f4a30 4ae5311 49f4a30 885164c 49f4a30 885164c 49f4a30 885164c 49f4a30 ff73331 49f4a30 4ae5311 49f4a30 885164c 49f4a30 885164c 49f4a30 885164c 49f4a30 885164c 49f4a30 885164c 49f4a30 885164c 49f4a30 885164c 49f4a30 885164c ff73331 4ae5311 3b31d0b 49f4a30 885164c 49f4a30 3b31d0b 49f4a30 3b31d0b 885164c 3b31d0b 885164c 49f4a30 3b31d0b 885164c 3b31d0b 49f4a30 885164c 3b31d0b ff73331 82bc15e 62ead92 885164c 49f4a30 885164c 49f4a30 3b31d0b 885164c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
import os
import json
import urllib.request
from PIL import Image
from gtts import gTTS
import cv2
import moviepy.editor as mp
import logging
from hercai import Hercai
import uuid
import time
import gradio as gr
from typing import Tuple, List
import numpy as np
# Configure logging with console output
logging.basicConfig(
level=logging.INFO,
format='[%(asctime)s] %(message)s',
handlers=[
logging.FileHandler('app.log'),
logging.StreamHandler() # This will print to console
]
)
LOGGER = logging.getLogger(__name__)
class Text2Video:
def __init__(self) -> None:
"""Initialize the Text2Video class."""
LOGGER.info("Initializing Text2Video application...")
self.herc = Hercai()
LOGGER.info("Hercai API initialized successfully")
def get_image(self, img_prompt: str) -> str:
"""Generate an image based on the provided text prompt."""
try:
LOGGER.info(f"π¨ Starting image generation for prompt: {img_prompt}")
# Enhanced prompt for better comic-style results
comic_style_prompt = (
f"{img_prompt}, comic book style, full scene composition, "
"vibrant colors, clear speech bubbles with text, "
"dramatic lighting, high contrast, detailed backgrounds, "
"comic book panel layout, professional illustration"
)
LOGGER.info("π Enhanced prompt with comic style elements")
LOGGER.info(f"π Sending request to Hercai API...")
image_result = self.herc.draw_image(
model="v3",
prompt=comic_style_prompt,
negative_prompt="blurry, cropped, low quality, dark, gloomy"
)
image_url = image_result["url"]
LOGGER.info(f"β
Image generated successfully: {image_url}")
return image_url
except Exception as e:
LOGGER.error(f"β Error generating image: {str(e)}")
raise
def download_img_from_url(self, image_url: str, image_path: str) -> str:
"""Download and process image from URL."""
try:
urllib.request.urlretrieve(image_url, image_path)
# Image processing for consistent quality
img = Image.open(image_path)
target_size = (1792, 1024)
img = img.resize(target_size, Image.Resampling.LANCZOS)
img.save(image_path, quality=95)
LOGGER.info(f"Successfully downloaded and processed image: {image_path}")
return image_path
except Exception as e:
LOGGER.error(f"Error downloading image: {e}")
raise
def text_to_audio(self, img_prompt: str, audio_path: str) -> str:
"""Convert text to speech with enhanced quality."""
try:
LOGGER.info(f"π Converting text to audio: {img_prompt}")
# Create audio with enhanced parameters
tts = gTTS(text=img_prompt, lang='en', slow=False)
LOGGER.info("π Audio conversion complete")
# Save audio file
tts.save(audio_path)
LOGGER.info(f"β
Audio saved to: {audio_path}")
return audio_path
except Exception as e:
LOGGER.error(f"β Error in audio conversion: {str(e)}")
raise
def get_images_and_audio(self, list_prompts: List[str]) -> Tuple[List[str], List[str]]:
"""Process multiple prompts to generate images and audio."""
img_list = []
audio_paths = []
LOGGER.info(f"π¬ Starting batch processing of {len(list_prompts)} prompts")
for idx, img_prompt in enumerate(list_prompts, 1):
try:
LOGGER.info(f"π Processing prompt {idx}/{len(list_prompts)}")
# Generate unique identifier
unique_id = uuid.uuid4().hex[:8]
# Process image
image_path = f"scene_{idx}_{unique_id}.png"
img_url = self.get_image(img_prompt)
image = self.download_img_from_url(img_url, image_path)
img_list.append(image)
# Process audio
audio_path = f"audio_{idx}_{unique_id}.mp3"
audio = self.text_to_audio(img_prompt, audio_path)
audio_paths.append(audio)
LOGGER.info(f"β
Completed processing prompt {idx}")
except Exception as e:
LOGGER.error(f"β Error processing prompt {idx}: {str(e)}")
raise
return img_list, audio_paths
def create_video_from_images_and_audio(self, image_files: List[str],
audio_files: List[str],
output_path: str) -> None:
"""Create final video with enhanced quality."""
try:
LOGGER.info("π₯ Starting video creation process")
if len(image_files) != len(audio_files):
raise ValueError("Number of images and audio files don't match")
video_clips = []
for idx, (image_file, audio_file) in enumerate(zip(image_files, audio_files), 1):
LOGGER.info(f"π Processing scene {idx}/{len(image_files)}")
# Load audio and create video clip
audio_clip = mp.AudioFileClip(audio_file)
video_clip = mp.ImageClip(image_file).set_duration(audio_clip.duration)
video_clip = video_clip.set_audio(audio_clip)
video_clips.append(video_clip)
LOGGER.info(f"β
Scene {idx} processed successfully")
LOGGER.info("π Concatenating all scenes")
final_clip = mp.concatenate_videoclips(video_clips)
LOGGER.info("πΎ Writing final video file")
final_clip.write_videofile(
output_path,
codec='libx264',
fps=24,
audio_codec='aac',
audio_bitrate='192k',
preset='medium'
)
LOGGER.info("β
Video created successfully")
except Exception as e:
LOGGER.error(f"β Error in video creation: {str(e)}")
raise
def generate_video(self, text: str) -> str:
"""Main function to generate video from text."""
try:
LOGGER.info("π¬ Starting video generation process")
# Split text into prompts
list_prompts = [sentence.strip() for sentence in text.split(",,") if sentence.strip()]
LOGGER.info(f"π Processed {len(list_prompts)} scenes from input text")
output_path = f"comic_video_{uuid.uuid4().hex[:8]}.mp4"
# Generate images and audio
img_list, audio_paths = self.get_images_and_audio(list_prompts)
# Create final video
self.create_video_from_images_and_audio(img_list, audio_paths, output_path)
LOGGER.info(f"β
Video generation completed: {output_path}")
return output_path
except Exception as e:
LOGGER.error(f"β Error in video generation: {str(e)}")
raise
def gradio_interface(self):
"""Create Gradio interface."""
LOGGER.info("π Initializing Gradio interface")
with gr.Blocks(theme='abidlabs/dracula_revamped') as demo:
gr.HTML("""
<center><h1 style="color:#fff">Comic Video Generator</h1></center>
""")
with gr.Row():
input_text = gr.Textbox(
label="Comic Script",
placeholder="Enter your story (separate scenes with ,,)"
)
with gr.Row():
generate_btn = gr.Button("π¬ Generate Video")
with gr.Row():
output = gr.Video(label="Generated Comic Video")
# Example Story
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.
'Hello. Does anybody have food to share?' He asked. 'No', said everyone.,,
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,,"""
gr.Examples([[example_txt]], [input_text])
generate_btn.click(self.generate_video, inputs=[input_text], outputs=[output])
LOGGER.info("β
Gradio interface initialized")
demo.launch(debug=True)
if __name__ == "__main__":
text2video = Text2Video()
text2video.gradio_interface() |