Update video_generation.py
Browse files- video_generation.py +96 -32
video_generation.py
CHANGED
|
@@ -18,32 +18,52 @@ class FreeVideoGenerator:
|
|
| 18 |
self.hf_token = hf_token or os.getenv('HF_TOKEN', '')
|
| 19 |
|
| 20 |
# Available free models
|
| 21 |
-
self.
|
| 22 |
"cerspense/zeroscope_v2_576w",
|
| 23 |
"damo-vilab/text-to-video-ms-1.7b"
|
| 24 |
]
|
| 25 |
|
| 26 |
-
self.current_model = self.
|
| 27 |
self.timeout = 120
|
| 28 |
self.max_retries = 2
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def enhance_prompt_with_context(self, prompt: str) -> str:
|
| 31 |
"""Enhance video prompts with cinematic context"""
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
"cinematic, 8k, ultra detailed, high quality, masterpiece",
|
| 34 |
"epic, dramatic lighting, film grain, cinematic shot, professional",
|
| 35 |
"beautiful, stunning, visually striking, vivid colors, trending",
|
| 36 |
"high resolution, detailed, sharp focus, studio quality"
|
| 37 |
]
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
return enhanced
|
| 43 |
|
| 44 |
def generate_text_to_video(self, prompt: str) -> Optional[str]:
|
| 45 |
"""
|
| 46 |
-
Generate video from text prompt
|
| 47 |
"""
|
| 48 |
try:
|
| 49 |
enhanced_prompt = self.enhance_prompt_with_context(prompt)
|
|
@@ -55,12 +75,13 @@ class FreeVideoGenerator:
|
|
| 55 |
payload = {
|
| 56 |
"inputs": enhanced_prompt,
|
| 57 |
"parameters": {
|
| 58 |
-
"num_frames":
|
| 59 |
"num_inference_steps": 25,
|
| 60 |
"guidance_scale": 7.5,
|
| 61 |
-
"fps":
|
| 62 |
-
"height":
|
| 63 |
-
"width":
|
|
|
|
| 64 |
}
|
| 65 |
}
|
| 66 |
|
|
@@ -91,44 +112,87 @@ class FreeVideoGenerator:
|
|
| 91 |
except requests.exceptions.Timeout:
|
| 92 |
logger.warning(f"⏰ Request timeout, attempt {attempt + 1}")
|
| 93 |
continue
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
-
logger.error(f"Video generation
|
| 97 |
|
| 98 |
return None
|
| 99 |
|
| 100 |
def create_cultural_video(self, theme: str, style: str = "animated") -> Optional[str]:
|
| 101 |
"""
|
| 102 |
-
Create videos with cultural themes
|
| 103 |
"""
|
|
|
|
| 104 |
cultural_themes = {
|
| 105 |
-
"safari":
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
}
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
"realistic": "realistic, documentary style, cinematic, natural lighting"
|
| 118 |
-
}
|
| 119 |
|
| 120 |
-
|
|
|
|
| 121 |
|
| 122 |
return self.generate_text_to_video(full_prompt)
|
| 123 |
|
| 124 |
def get_video_info(self) -> dict:
|
| 125 |
-
"""Get information about video generation"""
|
| 126 |
return {
|
| 127 |
-
"available_models": self.
|
| 128 |
"current_model": self.current_model,
|
| 129 |
-
"resolution": "
|
| 130 |
-
"fps":
|
| 131 |
-
"max_frames":
|
| 132 |
-
"max_duration": "
|
| 133 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
}
|
|
|
|
| 18 |
self.hf_token = hf_token or os.getenv('HF_TOKEN', '')
|
| 19 |
|
| 20 |
# Available free models
|
| 21 |
+
self.models = [
|
| 22 |
"cerspense/zeroscope_v2_576w",
|
| 23 |
"damo-vilab/text-to-video-ms-1.7b"
|
| 24 |
]
|
| 25 |
|
| 26 |
+
self.current_model = self.models[0]
|
| 27 |
self.timeout = 120
|
| 28 |
self.max_retries = 2
|
| 29 |
|
| 30 |
+
# Video settings
|
| 31 |
+
self.width = 576
|
| 32 |
+
self.height = 320
|
| 33 |
+
self.fps = 8
|
| 34 |
+
self.frames = 24
|
| 35 |
+
|
| 36 |
def enhance_prompt_with_context(self, prompt: str) -> str:
|
| 37 |
"""Enhance video prompts with cinematic context"""
|
| 38 |
+
|
| 39 |
+
# Cinematic enhancements
|
| 40 |
+
cinematic = [
|
| 41 |
"cinematic, 8k, ultra detailed, high quality, masterpiece",
|
| 42 |
"epic, dramatic lighting, film grain, cinematic shot, professional",
|
| 43 |
"beautiful, stunning, visually striking, vivid colors, trending",
|
| 44 |
"high resolution, detailed, sharp focus, studio quality"
|
| 45 |
]
|
| 46 |
|
| 47 |
+
# Cultural enhancements for African themes
|
| 48 |
+
if any(word in prompt.lower() for word in ['africa', 'kenya', 'tanzania', 'safari', 'wildlife', 'cultural']):
|
| 49 |
+
cultural = [
|
| 50 |
+
"African style, vibrant colors, cultural elements, traditional",
|
| 51 |
+
"East African landscape, warm colors, cultural symbolism",
|
| 52 |
+
"African documentary style, natural lighting, authentic",
|
| 53 |
+
"Traditional African art style, symbolic, meaningful"
|
| 54 |
+
]
|
| 55 |
+
enhanced = f"{prompt}, {random.choice(cinematic)}, {random.choice(cultural)}"
|
| 56 |
+
else:
|
| 57 |
+
enhanced = f"{prompt}, {random.choice(cinematic)}"
|
| 58 |
+
|
| 59 |
+
# Add technical specs
|
| 60 |
+
enhanced += f", {self.width}x{self.height} resolution, {self.fps} fps"
|
| 61 |
|
| 62 |
return enhanced
|
| 63 |
|
| 64 |
def generate_text_to_video(self, prompt: str) -> Optional[str]:
|
| 65 |
"""
|
| 66 |
+
Generate video from text prompt using Hugging Face API
|
| 67 |
"""
|
| 68 |
try:
|
| 69 |
enhanced_prompt = self.enhance_prompt_with_context(prompt)
|
|
|
|
| 75 |
payload = {
|
| 76 |
"inputs": enhanced_prompt,
|
| 77 |
"parameters": {
|
| 78 |
+
"num_frames": self.frames,
|
| 79 |
"num_inference_steps": 25,
|
| 80 |
"guidance_scale": 7.5,
|
| 81 |
+
"fps": self.fps,
|
| 82 |
+
"height": self.height,
|
| 83 |
+
"width": self.width,
|
| 84 |
+
"negative_prompt": "blurry, low quality, distorted, watermark, text"
|
| 85 |
}
|
| 86 |
}
|
| 87 |
|
|
|
|
| 112 |
except requests.exceptions.Timeout:
|
| 113 |
logger.warning(f"⏰ Request timeout, attempt {attempt + 1}")
|
| 114 |
continue
|
| 115 |
+
except Exception as e:
|
| 116 |
+
logger.error(f"Video generation error: {e}")
|
| 117 |
+
break
|
| 118 |
|
| 119 |
except Exception as e:
|
| 120 |
+
logger.error(f"Video generation failed: {e}")
|
| 121 |
|
| 122 |
return None
|
| 123 |
|
| 124 |
def create_cultural_video(self, theme: str, style: str = "animated") -> Optional[str]:
|
| 125 |
"""
|
| 126 |
+
Create videos with African cultural themes
|
| 127 |
"""
|
| 128 |
+
# Cultural themes database
|
| 129 |
cultural_themes = {
|
| 130 |
+
"safari": {
|
| 131 |
+
"prompt": "African safari sunset with elephants and giraffes walking, majestic savanna landscape, acacia trees, warm colors",
|
| 132 |
+
"styles": {
|
| 133 |
+
"animated": "animated, cartoon style, smooth motion, vibrant colors",
|
| 134 |
+
"realistic": "realistic, documentary style, cinematic, natural lighting"
|
| 135 |
+
}
|
| 136 |
+
},
|
| 137 |
+
"dance": {
|
| 138 |
+
"prompt": "Traditional Maasai warriors dancing, vibrant colors, cultural celebration, energetic movement, community",
|
| 139 |
+
"styles": {
|
| 140 |
+
"animated": "animated, lively motion, colorful, celebratory",
|
| 141 |
+
"realistic": "realistic, documentary footage, authentic, cultural"
|
| 142 |
+
}
|
| 143 |
+
},
|
| 144 |
+
"market": {
|
| 145 |
+
"prompt": "Busy African market scene, vibrant colors, people trading goods, lively atmosphere, traditional clothing",
|
| 146 |
+
"styles": {
|
| 147 |
+
"animated": "animated, bustling market, colorful stalls, lively",
|
| 148 |
+
"realistic": "realistic, documentary style, authentic market scene"
|
| 149 |
+
}
|
| 150 |
+
},
|
| 151 |
+
"coastal": {
|
| 152 |
+
"prompt": "Swahili coast with traditional dhows sailing, Indian Ocean waves, beach scenery, palm trees, traditional architecture",
|
| 153 |
+
"styles": {
|
| 154 |
+
"animated": "animated, ocean waves, sailing dhows, coastal life",
|
| 155 |
+
"realistic": "realistic, coastal documentary, ocean scenery"
|
| 156 |
+
}
|
| 157 |
+
},
|
| 158 |
+
"wildlife": {
|
| 159 |
+
"prompt": "African wildlife documentary style, lions hunting on savanna, dramatic nature scene, wildlife behavior",
|
| 160 |
+
"styles": {
|
| 161 |
+
"animated": "animated, wildlife cartoon, animal movement",
|
| 162 |
+
"realistic": "realistic, nature documentary, wildlife footage"
|
| 163 |
+
}
|
| 164 |
+
},
|
| 165 |
+
"village": {
|
| 166 |
+
"prompt": "Traditional African village life, community activities, sunset over huts, daily life, cultural activities",
|
| 167 |
+
"styles": {
|
| 168 |
+
"animated": "animated, village life, community activities",
|
| 169 |
+
"realistic": "realistic, documentary style, authentic village"
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
}
|
| 173 |
|
| 174 |
+
# Get theme data
|
| 175 |
+
theme_data = cultural_themes.get(theme, cultural_themes["safari"])
|
| 176 |
+
base_prompt = theme_data["prompt"]
|
| 177 |
+
style_enhancement = theme_data["styles"].get(style, "animated, vibrant colors")
|
|
|
|
|
|
|
| 178 |
|
| 179 |
+
# Combine prompt
|
| 180 |
+
full_prompt = f"{base_prompt}, {style_enhancement}, cultural, authentic, East African"
|
| 181 |
|
| 182 |
return self.generate_text_to_video(full_prompt)
|
| 183 |
|
| 184 |
def get_video_info(self) -> dict:
|
| 185 |
+
"""Get information about video generation capabilities"""
|
| 186 |
return {
|
| 187 |
+
"available_models": self.models,
|
| 188 |
"current_model": self.current_model,
|
| 189 |
+
"resolution": f"{self.width}x{self.height}",
|
| 190 |
+
"fps": self.fps,
|
| 191 |
+
"max_frames": self.frames,
|
| 192 |
+
"max_duration": f"{self.frames/self.fps:.1f} seconds",
|
| 193 |
+
"cultural_themes": ["safari", "dance", "market", "coastal", "wildlife", "village"],
|
| 194 |
+
"styles": ["animated", "realistic"],
|
| 195 |
+
"free": True,
|
| 196 |
+
"provider": "Hugging Face Inference API",
|
| 197 |
+
"creator": "Stanley Samwel Owino"
|
| 198 |
}
|