Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,6 +19,11 @@ logger = logging.getLogger(__name__)
|
|
| 19 |
|
| 20 |
class EnhancedContentGenerator:
|
| 21 |
def __init__(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
self.output_dir = Path("generated_content")
|
| 23 |
self.output_dir.mkdir(exist_ok=True)
|
| 24 |
|
|
@@ -35,9 +40,13 @@ class EnhancedContentGenerator:
|
|
| 35 |
self.pipe.enable_attention_slicing()
|
| 36 |
|
| 37 |
# Initialize Groq client
|
| 38 |
-
if not (self.api_key := os.getenv("GROQ_API_KEY")):
|
| 39 |
-
raise ValueError("GROQ_API_KEY not found in environment variables")
|
| 40 |
self.groq_client = groq.Groq(api_key=self.api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def generate_cartoon_frame(self, prompt, style="cartoon"):
|
| 43 |
"""Generate a single cartoon frame with specified style"""
|
|
@@ -101,7 +110,7 @@ class EnhancedContentGenerator:
|
|
| 101 |
def enhance_audio(self, audio_path, style="cartoon"):
|
| 102 |
"""Add effects to the audio based on style"""
|
| 103 |
try:
|
| 104 |
-
audio = AudioFileClip(audio_path)
|
| 105 |
|
| 106 |
if style == "cartoon":
|
| 107 |
# Speed up slightly for cartoon effect
|
|
@@ -111,12 +120,12 @@ class EnhancedContentGenerator:
|
|
| 111 |
echo = audio.set_start(0.1)
|
| 112 |
audio = CompositeVideoClip([audio, echo.volumex(0.3)])
|
| 113 |
|
| 114 |
-
enhanced_path = audio_path.replace('.wav', '_enhanced.wav')
|
| 115 |
audio.write_audiofile(enhanced_path)
|
| 116 |
return enhanced_path
|
| 117 |
except Exception as e:
|
| 118 |
logger.error(f"Error enhancing audio: {str(e)}")
|
| 119 |
-
return audio_path
|
| 120 |
|
| 121 |
def generate_comedy_animation(self, prompt):
|
| 122 |
"""Generate enhanced comedy animation"""
|
|
@@ -130,29 +139,30 @@ class EnhancedContentGenerator:
|
|
| 130 |
- Scene descriptions in (parentheses)
|
| 131 |
Keep it family-friendly and around 3-4 scenes."""
|
| 132 |
|
| 133 |
-
|
| 134 |
messages=[
|
| 135 |
{"role": "system", "content": "You are a professional cartoon comedy writer."},
|
| 136 |
{"role": "user", "content": script_prompt}
|
| 137 |
],
|
| 138 |
model="mixtral-8x7b-32768",
|
| 139 |
temperature=0.7
|
| 140 |
-
)
|
|
|
|
| 141 |
|
| 142 |
# Generate frames with cartoon style
|
| 143 |
frames = self.generate_video_sequence(script, style="cartoon")
|
| 144 |
|
| 145 |
# Generate and enhance audio
|
| 146 |
-
speech_path =
|
| 147 |
-
self.tts.tts_to_file(text=script, file_path=speech_path)
|
| 148 |
enhanced_speech = self.enhance_audio(speech_path, "cartoon")
|
| 149 |
|
| 150 |
# Create video with enhanced frames
|
| 151 |
-
video_path =
|
| 152 |
frames_tensor = torch.from_numpy(np.array(frames)).permute(0, 3, 1, 2)
|
| 153 |
-
write_video(video_path, frames_tensor, fps=12) # Higher FPS for smoother animation
|
| 154 |
|
| 155 |
-
return script, video_path, enhanced_speech
|
| 156 |
|
| 157 |
except Exception as e:
|
| 158 |
logger.error(f"Error in comedy animation generation: {str(e)}")
|
|
@@ -170,29 +180,30 @@ class EnhancedContentGenerator:
|
|
| 170 |
- (Action descriptions) for animation
|
| 171 |
Make it upbeat and memorable!"""
|
| 172 |
|
| 173 |
-
|
| 174 |
messages=[
|
| 175 |
{"role": "system", "content": "You are a children's music composer."},
|
| 176 |
{"role": "user", "content": lyrics_prompt}
|
| 177 |
],
|
| 178 |
model="mixtral-8x7b-32768",
|
| 179 |
temperature=0.7
|
| 180 |
-
)
|
|
|
|
| 181 |
|
| 182 |
# Generate frames with kids' style
|
| 183 |
frames = self.generate_video_sequence(lyrics, style="kids", num_frames=36)
|
| 184 |
|
| 185 |
# Generate and enhance audio
|
| 186 |
-
speech_path =
|
| 187 |
-
self.tts.tts_to_file(text=lyrics, file_path=speech_path)
|
| 188 |
enhanced_speech = self.enhance_audio(speech_path, "kids")
|
| 189 |
|
| 190 |
# Create video with enhanced frames
|
| 191 |
-
video_path =
|
| 192 |
frames_tensor = torch.from_numpy(np.array(frames)).permute(0, 3, 1, 2)
|
| 193 |
-
write_video(video_path, frames_tensor, fps=15) # Smooth animation for kids
|
| 194 |
|
| 195 |
-
return lyrics, video_path, enhanced_speech
|
| 196 |
|
| 197 |
except Exception as e:
|
| 198 |
logger.error(f"Error in kids music animation generation: {str(e)}")
|
|
@@ -243,4 +254,4 @@ def create_interface():
|
|
| 243 |
|
| 244 |
if __name__ == "__main__":
|
| 245 |
app = create_interface()
|
| 246 |
-
app.launch()
|
|
|
|
| 19 |
|
| 20 |
class EnhancedContentGenerator:
|
| 21 |
def __init__(self):
|
| 22 |
+
# Check for API key
|
| 23 |
+
self.api_key = os.getenv("GROQ_API_KEY")
|
| 24 |
+
if not self.api_key:
|
| 25 |
+
raise ValueError("GROQ_API_KEY not found in environment variables")
|
| 26 |
+
|
| 27 |
self.output_dir = Path("generated_content")
|
| 28 |
self.output_dir.mkdir(exist_ok=True)
|
| 29 |
|
|
|
|
| 40 |
self.pipe.enable_attention_slicing()
|
| 41 |
|
| 42 |
# Initialize Groq client
|
|
|
|
|
|
|
| 43 |
self.groq_client = groq.Groq(api_key=self.api_key)
|
| 44 |
+
|
| 45 |
+
# Create output directories if they don't exist
|
| 46 |
+
self.audio_dir = self.output_dir / "audio"
|
| 47 |
+
self.video_dir = self.output_dir / "video"
|
| 48 |
+
self.audio_dir.mkdir(exist_ok=True)
|
| 49 |
+
self.video_dir.mkdir(exist_ok=True)
|
| 50 |
|
| 51 |
def generate_cartoon_frame(self, prompt, style="cartoon"):
|
| 52 |
"""Generate a single cartoon frame with specified style"""
|
|
|
|
| 110 |
def enhance_audio(self, audio_path, style="cartoon"):
|
| 111 |
"""Add effects to the audio based on style"""
|
| 112 |
try:
|
| 113 |
+
audio = AudioFileClip(str(audio_path))
|
| 114 |
|
| 115 |
if style == "cartoon":
|
| 116 |
# Speed up slightly for cartoon effect
|
|
|
|
| 120 |
echo = audio.set_start(0.1)
|
| 121 |
audio = CompositeVideoClip([audio, echo.volumex(0.3)])
|
| 122 |
|
| 123 |
+
enhanced_path = str(audio_path).replace('.wav', '_enhanced.wav')
|
| 124 |
audio.write_audiofile(enhanced_path)
|
| 125 |
return enhanced_path
|
| 126 |
except Exception as e:
|
| 127 |
logger.error(f"Error enhancing audio: {str(e)}")
|
| 128 |
+
return str(audio_path)
|
| 129 |
|
| 130 |
def generate_comedy_animation(self, prompt):
|
| 131 |
"""Generate enhanced comedy animation"""
|
|
|
|
| 139 |
- Scene descriptions in (parentheses)
|
| 140 |
Keep it family-friendly and around 3-4 scenes."""
|
| 141 |
|
| 142 |
+
completion = self.groq_client.chat.completions.create(
|
| 143 |
messages=[
|
| 144 |
{"role": "system", "content": "You are a professional cartoon comedy writer."},
|
| 145 |
{"role": "user", "content": script_prompt}
|
| 146 |
],
|
| 147 |
model="mixtral-8x7b-32768",
|
| 148 |
temperature=0.7
|
| 149 |
+
)
|
| 150 |
+
script = completion.choices[0].message.content
|
| 151 |
|
| 152 |
# Generate frames with cartoon style
|
| 153 |
frames = self.generate_video_sequence(script, style="cartoon")
|
| 154 |
|
| 155 |
# Generate and enhance audio
|
| 156 |
+
speech_path = self.audio_dir / f"speech_{hash(script)}.wav"
|
| 157 |
+
self.tts.tts_to_file(text=script, file_path=str(speech_path))
|
| 158 |
enhanced_speech = self.enhance_audio(speech_path, "cartoon")
|
| 159 |
|
| 160 |
# Create video with enhanced frames
|
| 161 |
+
video_path = self.video_dir / f"video_{hash(prompt)}.mp4"
|
| 162 |
frames_tensor = torch.from_numpy(np.array(frames)).permute(0, 3, 1, 2)
|
| 163 |
+
write_video(str(video_path), frames_tensor, fps=12) # Higher FPS for smoother animation
|
| 164 |
|
| 165 |
+
return script, str(video_path), enhanced_speech
|
| 166 |
|
| 167 |
except Exception as e:
|
| 168 |
logger.error(f"Error in comedy animation generation: {str(e)}")
|
|
|
|
| 180 |
- (Action descriptions) for animation
|
| 181 |
Make it upbeat and memorable!"""
|
| 182 |
|
| 183 |
+
completion = self.groq_client.chat.completions.create(
|
| 184 |
messages=[
|
| 185 |
{"role": "system", "content": "You are a children's music composer."},
|
| 186 |
{"role": "user", "content": lyrics_prompt}
|
| 187 |
],
|
| 188 |
model="mixtral-8x7b-32768",
|
| 189 |
temperature=0.7
|
| 190 |
+
)
|
| 191 |
+
lyrics = completion.choices[0].message.content
|
| 192 |
|
| 193 |
# Generate frames with kids' style
|
| 194 |
frames = self.generate_video_sequence(lyrics, style="kids", num_frames=36)
|
| 195 |
|
| 196 |
# Generate and enhance audio
|
| 197 |
+
speech_path = self.audio_dir / f"music_{hash(lyrics)}.wav"
|
| 198 |
+
self.tts.tts_to_file(text=lyrics, file_path=str(speech_path))
|
| 199 |
enhanced_speech = self.enhance_audio(speech_path, "kids")
|
| 200 |
|
| 201 |
# Create video with enhanced frames
|
| 202 |
+
video_path = self.video_dir / f"video_{hash(theme)}.mp4"
|
| 203 |
frames_tensor = torch.from_numpy(np.array(frames)).permute(0, 3, 1, 2)
|
| 204 |
+
write_video(str(video_path), frames_tensor, fps=15) # Smooth animation for kids
|
| 205 |
|
| 206 |
+
return lyrics, str(video_path), enhanced_speech
|
| 207 |
|
| 208 |
except Exception as e:
|
| 209 |
logger.error(f"Error in kids music animation generation: {str(e)}")
|
|
|
|
| 254 |
|
| 255 |
if __name__ == "__main__":
|
| 256 |
app = create_interface()
|
| 257 |
+
app.launch()
|