Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
| 1 |
"""
|
| 2 |
-
π¬
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
-
|
| 13 |
"""
|
| 14 |
|
| 15 |
import gradio as gr
|
|
@@ -17,291 +17,263 @@ import torch
|
|
| 17 |
import random
|
| 18 |
import numpy as np
|
| 19 |
import cv2
|
| 20 |
-
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
|
| 21 |
import os
|
| 22 |
import shutil
|
| 23 |
import gc
|
| 24 |
import re
|
| 25 |
from typing import List, Tuple
|
| 26 |
|
|
|
|
| 27 |
from diffusers import StableDiffusionXLPipeline, DPMSolverMultistepScheduler
|
| 28 |
-
from
|
|
|
|
| 29 |
from pydub import AudioSegment
|
| 30 |
-
from pydub.generators import Sine, WhiteNoise
|
| 31 |
-
from pydub.effects import low_pass_filter, high_pass_filter
|
| 32 |
|
| 33 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 34 |
-
#
|
| 35 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"title": "The Recursive Apartment",
|
| 40 |
-
"script": """I moved into apartment 4B three months ago. Last Tuesday, I noticed a door I'd never seen before.
|
| 41 |
-
It was between my bedroom and bathroom. Old oak wood, brass handle, slightly warm to the touch.
|
| 42 |
-
I opened it. Behind the door was my apartment. Identical. Same furniture, same coffee stain on the carpet, same photos on the wall.
|
| 43 |
-
But the windows showed a city I didn't recognize. Taller buildings, darker sky, streets that curved wrong.
|
| 44 |
-
On my couch, someone was sleeping. Wearing my clothes. I stepped closer. They had my face.
|
| 45 |
-
I backed out quietly and closed the door. My hands were shaking. I checked again an hour later. The door was gone.
|
| 46 |
-
This morning I woke up on my couch. I don't remember falling asleep there. Through my window, I see that same unfamiliar city.
|
| 47 |
-
The buildings are taller now. The sky is darker. Behind me, I hear a door opening. A brass handle turning.
|
| 48 |
-
I turn around. Someone is standing in a doorway that wasn't there before. They look confused. They look like me.
|
| 49 |
-
They're looking at the couch where I'm sitting. I understand now. I understand what happened to the person who was sleeping.
|
| 50 |
-
I'm the one who was sleeping. And now someone else has opened the door.""",
|
| 51 |
-
"scenes": [
|
| 52 |
-
("apartment door, mysterious oak wood door, brass handle, dramatic lighting, horror atmosphere, cinematic, 4k", "zoom_slow"),
|
| 53 |
-
("identical living room through doorway, uncanny valley, perfect replica, eerie atmosphere, dramatic shadows, cinematic", "pan_right"),
|
| 54 |
-
("unfamiliar dystopian city through apartment window, towering buildings, dark ominous sky, sci-fi horror, cinematic", "zoom_in"),
|
| 55 |
-
("person sleeping on couch, back to camera, mysterious figure, dark room, horror aesthetic, moody lighting", "static_subtle"),
|
| 56 |
-
("brass door handle close up, hand reaching, dramatic lighting, suspenseful moment, horror movie, shallow depth", "zoom_in"),
|
| 57 |
-
("empty apartment interior, unsettling atmosphere, liminal space, modern furniture, eerie lighting, cinematic", "pan_left"),
|
| 58 |
-
("city skyline through window, apocalyptic atmosphere, wrong geometry, surreal architecture, horror sci-fi, dramatic", "zoom_slow"),
|
| 59 |
-
("doorway opening slowly, light spilling through, silhouette in doorway, dramatic backlight, suspense, cinematic", "zoom_in"),
|
| 60 |
-
("confused person in doorway, horror realization, dramatic lighting, emotional moment, cinematic composition", "static_subtle"),
|
| 61 |
-
("view of couch from doorway, someone sitting, back view, horror reveal, dramatic shadows, cinematic", "zoom_slow")
|
| 62 |
-
]
|
| 63 |
-
},
|
| 64 |
-
{
|
| 65 |
-
"title": "The Grandfather Paradox",
|
| 66 |
-
"script": """There's a staircase in the woods behind my house. Just standing there. No building, no structure. Just stairs leading up into nothing.
|
| 67 |
-
My grandfather spent his whole life warning me about it. 'Never climb those stairs,' he'd say. 'I climbed them once. In 1952. When I was your age.'
|
| 68 |
-
He told me when he reached the top, he found himself at the bottom. But everything was different. The trees were taller. The sky was a different shade of blue.
|
| 69 |
-
Most importantly, time had moved forward. He'd been gone for three days. But he'd only climbed for ten minutes.
|
| 70 |
-
Last week, my grandfather disappeared. He left a note in his handwriting. 'I'm going back up. I need to find out where I went. Don't follow me.'
|
| 71 |
-
The police searched for days. They found nothing. No trace. Like he'd never existed.
|
| 72 |
-
Today is exactly one week later. I'm standing at the bottom of the staircase. I shouldn't be here. I know I shouldn't.
|
| 73 |
-
At the top of the stairs, I see someone. They're young. Maybe twenty-five. They're waving at me. Beckoning me up.
|
| 74 |
-
I squint. They look familiar. I realize with ice in my veins, it's my grandfather. Young. The age he was in 1952.
|
| 75 |
-
Behind me, I hear footsteps. I hear breathing. I hear my own voice, sounding older, weathered. It's warning someone.
|
| 76 |
-
'Never climb those stairs,' my voice says. I turn around. There's a young person standing there, looking at me with wide eyes.
|
| 77 |
-
They look exactly like I did when I was twenty-five. Like I looked this morning in the mirror. I open my mouth to warn them.
|
| 78 |
-
But I already know. I've been here before. I'll be here again. The stairs don't lead up. They lead in circles.""",
|
| 79 |
-
"scenes": [
|
| 80 |
-
("mysterious wooden staircase in dark forest, freestanding, leading nowhere, fog, eerie atmosphere, cinematic, 4k", "zoom_slow"),
|
| 81 |
-
("old photograph 1952, sepia tone, young man at forest stairs, vintage photo, aged paper, historical, nostalgic", "static_subtle"),
|
| 82 |
-
("dark ancient forest, towering trees, different sky color, otherworldly atmosphere, surreal lighting, cinematic", "pan_right"),
|
| 83 |
-
("handwritten note on aged paper, urgent message, dramatic lighting, close-up, shallow depth of field, suspenseful", "zoom_in"),
|
| 84 |
-
("empty forest path, police search lights, night time, investigation scene, ominous atmosphere, cinematic", "pan_left"),
|
| 85 |
-
("bottom of mysterious stairs looking up, misty atmosphere, figure at top, horror atmosphere, dramatic perspective", "zoom_in"),
|
| 86 |
-
("silhouette of young man at top of stairs, 1950s clothing, waving, backlit, fog, eerie and inviting, cinematic", "static_subtle"),
|
| 87 |
-
("forest floor perspective, feet walking, footsteps, dramatic shadows, suspenseful moment, horror aesthetic", "zoom_slow"),
|
| 88 |
-
("young person looking up with realization, horror dawning, dramatic facial lighting, emotional intensity, cinematic", "zoom_in"),
|
| 89 |
-
("infinite staircase concept, circular time, surreal horror, impossible geometry, dramatic lighting, mind-bending", "pan_down")
|
| 90 |
-
]
|
| 91 |
-
},
|
| 92 |
-
{
|
| 93 |
-
"title": "The Two Second Delay",
|
| 94 |
-
"script": """I've lived in this apartment for six months. Every mirror has a two-second delay.
|
| 95 |
-
I wave at my reflection. Two seconds pass. Then my reflection waves back. Always exactly two seconds.
|
| 96 |
-
At first I thought it was charming. A quirk of the old building. Then I started testing it systematically.
|
| 97 |
-
I timed it with a stopwatch. Recorded it with my phone. Every mirror. Every reflective surface. Exactly two seconds. Never more, never less.
|
| 98 |
-
Three weeks ago, something changed. I was brushing my teeth before work. I looked up at the mirror.
|
| 99 |
-
My reflection was smiling. I wasn't smiling. I was tired, anxious about a presentation. But my reflection was grinning.
|
| 100 |
-
Then it spoke. I saw its lips move. Two seconds later, I heard the whisper behind me. Right behind me. 'I can see what you're about to do.'
|
| 101 |
-
I spun around. Nothing there. When I looked back at the mirror, I saw myself standing behind me. The other me. Watching me with my face.
|
| 102 |
-
I ran out of the bathroom. In the hallway mirror, I saw it again. Still watching. In every mirror in every room.
|
| 103 |
-
All showing me something two seconds before it happens. Showing me what I'm about to do before I do it.
|
| 104 |
-
Last night I conducted an experiment. I stood in front of the mirror for two hours. Completely still. Waiting.
|
| 105 |
-
For one hour and fifty-eight minutes, nothing happened. Then my reflection moved. It walked out of frame.
|
| 106 |
-
Two seconds later, I felt my legs moving. Not me moving them. They just moved. Walking me toward the mirror.
|
| 107 |
-
I'm standing in front of it now. My reflection shows me reaching forward. Touching the glass.
|
| 108 |
-
In two seconds, I'll know what happens when you touch it. In two seconds, I'll be on the other side.""",
|
| 109 |
-
"scenes": [
|
| 110 |
-
("bathroom mirror, foggy glass, reflection slightly off, dim atmospheric lighting, horror aesthetic, cinematic, 4k", "zoom_in"),
|
| 111 |
-
("person brushing teeth at mirror, tired expression, morning light, everyday horror, dramatic shadows, realistic", "static_subtle"),
|
| 112 |
-
("close-up of stopwatch timing, two seconds counting, dramatic lighting, suspenseful detail, shallow focus", "zoom_slow"),
|
| 113 |
-
("mirror reflection showing different expression, uncanny, creepy smile, unsettling mismatch, horror moment, cinematic", "zoom_in"),
|
| 114 |
-
("empty space behind person, dramatic shadows, nothing visible, tension, horror atmosphere, cinematic lighting", "pan_left"),
|
| 115 |
-
("hallway with multiple mirrors on walls, infinite reflections, eerie glow, liminal space, unsettling symmetry", "pan_right"),
|
| 116 |
-
("person standing completely still facing mirror, long exposure feel, waiting, tension building, dramatic lighting", "static_subtle"),
|
| 117 |
-
("reflection moving independently, walking out of frame, horror reveal, supernatural moment, cinematic, suspenseful", "pan_left"),
|
| 118 |
-
("legs moving against will, body horror, loss of control, dramatic lighting from below, unsettling perspective", "zoom_slow"),
|
| 119 |
-
("hand reaching toward mirror glass, about to touch, dramatic lighting, suspenseful moment, shallow depth of field", "zoom_in")
|
| 120 |
-
]
|
| 121 |
-
},
|
| 122 |
-
{
|
| 123 |
-
"title": "Security Floor 13",
|
| 124 |
-
"script": """I'm the night security manager for the Ashford Building. Thirty floors. Twelve cameras per floor. Three hundred and sixty cameras total.
|
| 125 |
-
Nothing ever happens. That's the point. The job is boring. I watch screens. I make my rounds. I go home at 6 AM.
|
| 126 |
-
Six months ago, I noticed camera 247 was offline. Floor 21, east hallway. I went to check it. The camera was fine.
|
| 127 |
-
But when I reviewed the footage, I saw myself walking down that hallway. Checking the camera. Exactly as I'd just done.
|
| 128 |
-
The timestamp said 3:47 AM. My watch said 3:52 AM. I'd checked the camera at 3:50 AM.
|
| 129 |
-
The footage showed me checking it two minutes before I actually did. That's impossible. That's not how time works.
|
| 130 |
-
I started checking the other cameras obsessively. Looking for anomalies. Glitches. Proof that the system was broken.
|
| 131 |
-
Last night I found something worse. Camera 156. Floor 13. Except we don't have a floor 13.
|
| 132 |
-
The building was constructed with twelve floors, then fourteen, fifteen, and so on. Superstition. Standard practice.
|
| 133 |
-
But camera 156 shows a floor. Empty offices. Dust-covered desks. Calendars on the walls showing dates from 1979.
|
| 134 |
-
On every screen, the same image. But camera 156 shows something else. Shows somewhere else. A floor that doesn't exist.
|
| 135 |
-
Tonight, I'm going to find it. The elevator only goes to floors 1 through 12, then 14 through 30. But there's a service shaft.
|
| 136 |
-
I'm in the shaft now. Between 12 and 14. There's a door here. It's been welded shut, then painted over. Someone didn't want it opened.
|
| 137 |
-
I'm forcing it open. It's giving way. I'm looking at floor 13. It looks exactly like camera 156 showed me.
|
| 138 |
-
Empty offices. Dust everywhere. Calendars showing 1979. But there's one difference.
|
| 139 |
-
On every desk, there's a security monitor. They're all turned on. They're all showing the security office.
|
| 140 |
-
They're all showing me. Right now. Sitting at my desk. Watching camera 156. I'm in two places at once.
|
| 141 |
-
I'm watching myself discover that I'm watching myself. The monitors on floor 13 show me standing on floor 13.
|
| 142 |
-
Which means I'm being watched from somewhere else. From some other floor that doesn't exist.""",
|
| 143 |
-
"scenes": [
|
| 144 |
-
("security office, wall of monitors, dark room, screens glowing, surveillance aesthetic, cinematic lighting, 4k", "zoom_slow"),
|
| 145 |
-
("empty office building hallway, security camera POV, fluorescent lights, corporate liminal space, eerie", "pan_right"),
|
| 146 |
-
("security camera monitor showing timestamp, close-up screen, grainy footage, time anomaly, dramatic lighting", "zoom_in"),
|
| 147 |
-
("floor number 13 on elevator panel, skipped floor, architectural superstition, dramatic shadows, suspenseful", "static_subtle"),
|
| 148 |
-
("abandoned office space, dust sheets over desks, 1979 calendars, time capsule, eerie atmosphere, cinematic", "pan_left"),
|
| 149 |
-
("elevator service shaft, industrial setting, between floors, claustrophobic space, dramatic lighting, suspenseful", "zoom_slow"),
|
| 150 |
-
("welded door being forced open, sparks, rust, hidden entrance, dramatic action, horror reveal, cinematic", "zoom_in"),
|
| 151 |
-
("empty office floor, multiple desks, monitors on every desk, all showing same image, uncanny, surreal horror", "pan_right"),
|
| 152 |
-
("security monitor showing recursive image, infinite loop, person watching themselves, mind-bending, dramatic", "zoom_in"),
|
| 153 |
-
("realization moment, horror dawning, person understanding impossible situation, dramatic lighting, emotional", "zoom_slow")
|
| 154 |
-
]
|
| 155 |
-
}
|
| 156 |
-
]
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
def
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
|
| 179 |
-
target_duration = 55000 # milliseconds
|
| 180 |
-
current_duration = len(audio)
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
audio = audio._spawn(audio.raw_data, overrides={
|
| 185 |
-
"frame_rate": int(audio.frame_rate * speed_factor)
|
| 186 |
-
}).set_frame_rate(44100)
|
| 187 |
|
| 188 |
-
#
|
| 189 |
-
|
| 190 |
-
reverb = audio - 18
|
| 191 |
-
audio = audio.overlay(reverb, position=60)
|
| 192 |
|
| 193 |
-
#
|
| 194 |
-
|
| 195 |
-
audio = high_pass_filter(audio, 80)
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
|
|
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
|
|
|
| 205 |
|
| 206 |
-
|
|
|
|
| 207 |
|
| 208 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
-
def
|
| 211 |
-
"""
|
| 212 |
-
|
| 213 |
-
duration_ms = int(duration_sec * 1000)
|
| 214 |
-
|
| 215 |
-
# Layer 1: Sub bass drone (fear frequency)
|
| 216 |
-
sub_bass = Sine(40).to_audio_segment(duration=duration_ms) - 20
|
| 217 |
-
|
| 218 |
-
# Layer 2: Mid drone (tension)
|
| 219 |
-
mid_drone = Sine(80).to_audio_segment(duration=duration_ms) - 22
|
| 220 |
-
|
| 221 |
-
# Layer 3: Harmonic drone
|
| 222 |
-
harmonic = Sine(120).to_audio_segment(duration=duration_ms) - 24
|
| 223 |
|
| 224 |
-
#
|
| 225 |
-
|
| 226 |
|
| 227 |
-
#
|
| 228 |
-
|
|
|
|
| 229 |
|
| 230 |
-
#
|
| 231 |
-
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
-
#
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
soundscape = soundscape.overlay(noise_white).overlay(rumble)
|
| 240 |
|
| 241 |
-
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
-
|
| 245 |
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
"""Apply professional horror color grading."""
|
| 250 |
|
| 251 |
-
|
| 252 |
-
enhancer = ImageEnhance.Color(image)
|
| 253 |
-
image = enhancer.enhance(0.2)
|
| 254 |
|
| 255 |
-
|
| 256 |
-
enhancer = ImageEnhance.Contrast(image)
|
| 257 |
-
image = enhancer.enhance(1.5)
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
-
#
|
| 264 |
-
|
| 265 |
-
grain = np.random.randint(-20, 20, arr.shape, dtype=np.int16)
|
| 266 |
-
arr = np.clip(arr.astype(np.int16) + grain, 0, 255).astype(np.uint8)
|
| 267 |
-
image = Image.fromarray(arr)
|
| 268 |
|
| 269 |
-
#
|
| 270 |
-
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
-
#
|
| 276 |
-
|
| 277 |
-
draw = ImageDraw.Draw(vignette)
|
| 278 |
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
|
| 283 |
-
|
|
|
|
| 284 |
|
| 285 |
-
#
|
| 286 |
-
|
| 287 |
-
image = Image.composite(image, dark_image, vignette)
|
| 288 |
|
| 289 |
-
#
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
arr[:,:,0] = np.clip(arr[:,:,0] * 0.95, 0, 255).astype(np.uint8) # Less red
|
| 293 |
-
image = Image.fromarray(arr)
|
| 294 |
|
| 295 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
_sdxl_pipe = None
|
| 298 |
|
| 299 |
-
def
|
| 300 |
-
"""Load SDXL for
|
| 301 |
global _sdxl_pipe
|
| 302 |
|
| 303 |
if _sdxl_pipe is None:
|
| 304 |
-
print("Loading
|
| 305 |
|
| 306 |
_sdxl_pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 307 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
|
@@ -310,47 +282,44 @@ def load_sdxl():
|
|
| 310 |
variant="fp16" if torch.cuda.is_available() else None
|
| 311 |
)
|
| 312 |
|
| 313 |
-
# Optimized scheduler for quality
|
| 314 |
_sdxl_pipe.scheduler = DPMSolverMultistepScheduler.from_config(
|
| 315 |
-
_sdxl_pipe.scheduler.config
|
| 316 |
-
use_karras_sigmas=True
|
| 317 |
)
|
| 318 |
|
| 319 |
if torch.cuda.is_available():
|
| 320 |
_sdxl_pipe.to("cuda")
|
| 321 |
_sdxl_pipe.enable_vae_slicing()
|
| 322 |
-
_sdxl_pipe.enable_xformers_memory_efficient_attention()
|
| 323 |
else:
|
| 324 |
_sdxl_pipe.enable_attention_slicing()
|
| 325 |
_sdxl_pipe.enable_vae_slicing()
|
| 326 |
|
| 327 |
-
print("SDXL ready
|
| 328 |
|
| 329 |
return _sdxl_pipe
|
| 330 |
|
| 331 |
-
def
|
| 332 |
-
"""Generate
|
| 333 |
|
| 334 |
-
pipe =
|
| 335 |
|
| 336 |
-
# Premium settings - quality over speed
|
| 337 |
image = pipe(
|
| 338 |
-
prompt=prompt + ",
|
| 339 |
-
negative_prompt="blurry, low quality,
|
| 340 |
-
num_inference_steps=
|
| 341 |
guidance_scale=7.5,
|
| 342 |
-
height=
|
| 343 |
-
width=
|
| 344 |
).images[0]
|
| 345 |
|
| 346 |
-
#
|
| 347 |
-
|
|
|
|
| 348 |
|
| 349 |
-
|
| 350 |
-
image =
|
| 351 |
|
| 352 |
-
|
| 353 |
-
image.
|
| 354 |
|
| 355 |
# Clear memory
|
| 356 |
if torch.cuda.is_available():
|
|
@@ -359,553 +328,431 @@ def generate_premium_image(prompt: str, index: int) -> Image.Image:
|
|
| 359 |
|
| 360 |
return image
|
| 361 |
|
| 362 |
-
|
| 363 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
|
| 367 |
|
| 368 |
h, w = arr.shape[:2]
|
| 369 |
-
total_frames = int(duration * fps)
|
| 370 |
frames = []
|
|
|
|
| 371 |
|
| 372 |
-
#
|
| 373 |
-
|
| 374 |
-
scaled = cv2.resize(arr, (int(w * scale_factor), int(h * scale_factor)), interpolation=cv2.INTER_LANCZOS4)
|
| 375 |
sh, sw = scaled.shape[:2]
|
| 376 |
|
| 377 |
for i in range(total_frames):
|
| 378 |
-
progress = i /
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
x = (temp_w - w) // 2
|
| 390 |
-
y = (temp_h - h) // 2
|
| 391 |
-
frame = zoomed[y:y+h, x:x+w]
|
| 392 |
-
|
| 393 |
-
elif movement == "zoom_in":
|
| 394 |
-
# More aggressive zoom
|
| 395 |
-
s = 1.0 + ease * 0.3
|
| 396 |
-
temp_w, temp_h = int(w * s), int(h * s)
|
| 397 |
-
zoomed = cv2.resize(arr, (temp_w, temp_h), interpolation=cv2.INTER_LANCZOS4)
|
| 398 |
-
x = (temp_w - w) // 2
|
| 399 |
-
y = (temp_h - h) // 2
|
| 400 |
-
frame = zoomed[y:y+h, x:x+w]
|
| 401 |
-
|
| 402 |
-
elif movement == "pan_right":
|
| 403 |
-
x = int((sw - w) * ease)
|
| 404 |
-
frame = scaled[0:h, x:x+w]
|
| 405 |
-
|
| 406 |
-
elif movement == "pan_left":
|
| 407 |
-
x = int((sw - w) * (1 - ease))
|
| 408 |
frame = scaled[0:h, x:x+w]
|
| 409 |
-
|
| 410 |
-
elif movement == "pan_down":
|
| 411 |
-
y = int((sh - h) * ease)
|
| 412 |
-
frame = scaled[y:y+h, 0:w]
|
| 413 |
-
|
| 414 |
-
elif movement == "static_subtle":
|
| 415 |
-
# Very slight movement for "static" shots
|
| 416 |
-
offset_x = int(np.sin(progress * np.pi) * 5)
|
| 417 |
-
offset_y = int(np.cos(progress * np.pi * 0.5) * 3)
|
| 418 |
-
x = (sw - w) // 2 + offset_x
|
| 419 |
-
y = (sh - h) // 2 + offset_y
|
| 420 |
-
frame = scaled[y:y+h, x:x+w]
|
| 421 |
-
|
| 422 |
-
else:
|
| 423 |
-
frame = arr
|
| 424 |
|
| 425 |
frames.append(frame)
|
| 426 |
|
| 427 |
return frames
|
| 428 |
|
| 429 |
-
def
|
| 430 |
-
"""Upscale
|
| 431 |
-
|
| 432 |
target_w, target_h = 1080, 1920
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
# Calculate scale
|
| 436 |
-
scale = max(target_w / current_w, target_h / current_h)
|
| 437 |
-
new_w = int(current_w * scale)
|
| 438 |
-
new_h = int(current_h * scale)
|
| 439 |
|
| 440 |
-
|
| 441 |
-
|
| 442 |
|
| 443 |
-
|
| 444 |
-
x = (new_w - target_w) // 2
|
| 445 |
-
y = (new_h - target_h) // 2
|
| 446 |
-
cropped = upscaled[y:y+target_h, x:x+target_w]
|
| 447 |
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
|
| 452 |
-
return
|
| 453 |
|
| 454 |
-
def
|
| 455 |
-
"""Add
|
| 456 |
-
|
| 457 |
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 458 |
pil_img = Image.fromarray(rgb)
|
| 459 |
draw = ImageDraw.Draw(pil_img)
|
| 460 |
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
for path in [
|
| 466 |
-
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
|
| 467 |
-
"/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",
|
| 468 |
-
"/System/Library/Fonts/Helvetica.ttc",
|
| 469 |
-
]:
|
| 470 |
-
try:
|
| 471 |
-
font = ImageFont.truetype(path, font_size)
|
| 472 |
-
break
|
| 473 |
-
except:
|
| 474 |
-
continue
|
| 475 |
-
|
| 476 |
-
if not font:
|
| 477 |
font = ImageFont.load_default()
|
| 478 |
|
| 479 |
-
#
|
| 480 |
words = text.split()
|
| 481 |
lines = []
|
| 482 |
-
|
| 483 |
-
max_width = 1000
|
| 484 |
|
| 485 |
for word in words:
|
| 486 |
-
test = ' '.join(
|
| 487 |
bbox = draw.textbbox((0, 0), test, font=font)
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
current_line.append(word)
|
| 491 |
else:
|
| 492 |
-
if
|
| 493 |
-
lines.append(' '.join(
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
lines.append(' '.join(current_line))
|
| 502 |
-
|
| 503 |
-
# Position subtitles in lower third
|
| 504 |
-
total_height = len(lines) * 75
|
| 505 |
-
start_y = 1720 - total_height
|
| 506 |
-
|
| 507 |
-
for line in lines:
|
| 508 |
bbox = draw.textbbox((0, 0), line, font=font)
|
| 509 |
-
|
| 510 |
-
x = (1080 - text_width) // 2
|
| 511 |
-
|
| 512 |
-
# Draw thick outline (broadcast standard)
|
| 513 |
-
outline_width = 6
|
| 514 |
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
draw.text((x + dx, start_y + dy), line, font=font, fill='black')
|
| 520 |
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
start_y += 75
|
| 525 |
|
| 526 |
return cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
|
| 527 |
|
| 528 |
-
def
|
| 529 |
-
"""Render
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
#
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
# Master to exactly 55 seconds
|
| 549 |
-
mixed = mixed[:55000]
|
| 550 |
-
|
| 551 |
-
# Export high quality
|
| 552 |
-
mixed.export("temp/mixed_master.mp3", format='mp3', bitrate="320k")
|
| 553 |
-
|
| 554 |
-
# Final encode with premium settings
|
| 555 |
-
cmd = (
|
| 556 |
-
f'ffmpeg -y -i {temp_video} -i temp/mixed_master.mp3 '
|
| 557 |
-
f'-c:v libx264 -preset slow -crf 18 ' # Near-lossless
|
| 558 |
-
f'-c:a aac -b:a 320k ' # Maximum audio quality
|
| 559 |
-
f'-pix_fmt yuv420p -movflags +faststart '
|
| 560 |
-
f'-t 55 ' # Exactly 55 seconds
|
| 561 |
-
f'{output} -loglevel error'
|
| 562 |
-
)
|
| 563 |
-
|
| 564 |
-
result = os.system(cmd)
|
| 565 |
-
|
| 566 |
-
if result != 0:
|
| 567 |
-
raise Exception("FFmpeg encoding failed")
|
| 568 |
|
| 569 |
return output
|
| 570 |
|
| 571 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 572 |
-
# MAIN
|
| 573 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 574 |
|
| 575 |
-
def
|
| 576 |
"""
|
| 577 |
-
|
| 578 |
-
Budget: 1 hour generation time.
|
| 579 |
"""
|
| 580 |
|
| 581 |
try:
|
| 582 |
setup_dirs()
|
| 583 |
|
| 584 |
-
#
|
| 585 |
-
progress(0.
|
| 586 |
-
|
|
|
|
|
|
|
| 587 |
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
|
| 592 |
-
|
| 593 |
-
progress(0.05, desc="π΅ Composing layered horror soundscape...")
|
| 594 |
-
ambient_path = create_layered_soundscape(55.0)
|
| 595 |
|
| 596 |
-
#
|
| 597 |
-
progress(0.
|
| 598 |
-
|
| 599 |
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 603 |
all_frames = []
|
| 604 |
|
| 605 |
-
|
| 606 |
-
|
|
|
|
|
|
|
| 607 |
|
| 608 |
-
|
| 609 |
-
image = generate_premium_image(prompt, i)
|
| 610 |
|
| 611 |
-
progress(
|
| 612 |
-
frames = create_professional_animation(image, seconds_per_scene, movement)
|
| 613 |
|
| 614 |
-
|
| 615 |
-
frames = [
|
| 616 |
|
| 617 |
all_frames.extend(frames)
|
| 618 |
|
| 619 |
-
|
| 620 |
-
del image, frames
|
| 621 |
gc.collect()
|
| 622 |
-
if torch.cuda.is_available():
|
| 623 |
-
torch.cuda.empty_cache()
|
| 624 |
-
|
| 625 |
-
# Step 6: Add professional subtitles
|
| 626 |
-
progress(0.88, desc="π Adding broadcast-quality subtitles...")
|
| 627 |
|
| 628 |
-
#
|
| 629 |
-
|
| 630 |
-
for sentence in re.split(r'(?<=[.!?])\s+', story['script']):
|
| 631 |
-
sentence = sentence.strip()
|
| 632 |
-
if sentence:
|
| 633 |
-
sentences.append(sentence)
|
| 634 |
|
| 635 |
-
|
| 636 |
-
|
| 637 |
|
| 638 |
final_frames = []
|
| 639 |
for i, frame in enumerate(all_frames):
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
output_path = render_premium_video(
|
| 647 |
-
final_frames,
|
| 648 |
-
voice_path,
|
| 649 |
-
ambient_path,
|
| 650 |
-
"output/premium_horror_short.mp4"
|
| 651 |
-
)
|
| 652 |
|
| 653 |
-
progress(1.0, desc="β
|
| 654 |
|
| 655 |
-
# Generate info
|
| 656 |
info = f"""
|
| 657 |
-
###
|
| 658 |
-
|
| 659 |
-
**
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
β
Voice processing with reverb & EQ
|
| 675 |
-
β
Film grain & vignette effects
|
| 676 |
-
β
Premium subtitle formatting
|
| 677 |
-
|
| 678 |
-
**Perfect for:**
|
| 679 |
-
YouTube Shorts, TikTok, Instagram Reels, Viral content
|
| 680 |
"""
|
| 681 |
|
| 682 |
-
return
|
| 683 |
|
| 684 |
except Exception as e:
|
| 685 |
-
|
| 686 |
-
print(
|
| 687 |
import traceback
|
| 688 |
traceback.print_exc()
|
| 689 |
-
return None,
|
| 690 |
|
| 691 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 692 |
# GRADIO INTERFACE
|
| 693 |
# ββββββββββββββββββοΏ½οΏ½οΏ½ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 694 |
|
| 695 |
-
with gr.Blocks(theme=gr.themes.
|
| 696 |
|
| 697 |
gr.Markdown("""
|
| 698 |
-
#
|
| 699 |
-
##
|
| 700 |
-
|
| 701 |
-
**Professional-grade viral horror content generator**
|
| 702 |
|
| 703 |
-
|
| 704 |
""")
|
| 705 |
|
| 706 |
with gr.Row():
|
| 707 |
with gr.Column(scale=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 708 |
generate_btn = gr.Button(
|
| 709 |
-
"
|
| 710 |
variant="primary",
|
| 711 |
size="lg"
|
| 712 |
)
|
| 713 |
|
| 714 |
gr.Markdown("""
|
| 715 |
-
###
|
| 716 |
-
|
| 717 |
-
**Duration & Format:**
|
| 718 |
-
- β±οΈ Exactly 55.0 seconds
|
| 719 |
-
- π± 1080x1920 (YouTube Shorts)
|
| 720 |
-
- ποΈ 30fps, 1,650 total frames
|
| 721 |
-
|
| 722 |
-
**Visual Quality:**
|
| 723 |
-
- π¨ 8-10 unique SDXL images
|
| 724 |
-
- πΌοΈ 4K generation β downscaled
|
| 725 |
-
- π₯ Cinematic camera movements
|
| 726 |
-
- π Professional color grading
|
| 727 |
-
- π¬ Film grain & vignette
|
| 728 |
-
- π CRF 18 (near-lossless)
|
| 729 |
-
|
| 730 |
-
**Audio Excellence:**
|
| 731 |
-
- ποΈ Premium TTS processing
|
| 732 |
-
- π΅ 7-layer soundscape
|
| 733 |
-
- π 320kbps AAC
|
| 734 |
-
- ποΈ Professional mixing
|
| 735 |
-
- β‘ Reverb & EQ effects
|
| 736 |
|
| 737 |
-
**
|
| 738 |
-
-
|
| 739 |
-
-
|
| 740 |
-
-
|
| 741 |
-
-
|
| 742 |
|
| 743 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 744 |
|
| 745 |
-
**
|
| 746 |
-
|
|
|
|
|
|
|
|
|
|
| 747 |
|
| 748 |
-
**
|
| 749 |
-
|
|
|
|
|
|
|
|
|
|
| 750 |
|
| 751 |
-
|
| 752 |
-
|
|
|
|
|
|
|
|
|
|
| 753 |
|
| 754 |
-
**
|
| 755 |
-
Surveillance horror - impossible spaces
|
| 756 |
|
| 757 |
-
###
|
| 758 |
-
|
| 759 |
-
-
|
| 760 |
-
-
|
| 761 |
-
-
|
| 762 |
-
-
|
| 763 |
-
|
| 764 |
-
**Total: 45-60 minutes**
|
| 765 |
-
|
| 766 |
-
### π‘ Why This Takes Time:
|
| 767 |
-
|
| 768 |
-
Each image uses **SDXL with 40 steps**
|
| 769 |
-
(industry standard for quality)
|
| 770 |
-
|
| 771 |
-
Premium > Speed
|
| 772 |
-
|
| 773 |
-
The result is worth it! π₯
|
| 774 |
""")
|
| 775 |
|
| 776 |
with gr.Column(scale=2):
|
| 777 |
video_output = gr.Video(
|
| 778 |
-
label="π¬
|
| 779 |
-
height=
|
| 780 |
)
|
| 781 |
|
| 782 |
script_output = gr.Textbox(
|
| 783 |
-
label="π
|
| 784 |
-
lines=
|
| 785 |
-
max_lines=20
|
| 786 |
)
|
| 787 |
|
| 788 |
-
info_output = gr.Markdown(label="π
|
| 789 |
|
| 790 |
generate_btn.click(
|
| 791 |
-
fn=
|
| 792 |
-
inputs=[],
|
| 793 |
outputs=[video_output, script_output, info_output]
|
| 794 |
)
|
| 795 |
|
| 796 |
gr.Markdown("""
|
| 797 |
---
|
| 798 |
|
| 799 |
-
## π
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
-
|
| 808 |
-
|
| 809 |
-
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
-
|
| 813 |
-
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
-
|
| 855 |
-
-
|
| 856 |
-
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
## π¨ Quality Comparison:
|
| 860 |
-
|
| 861 |
-
**This Generator vs Others:**
|
| 862 |
-
- β
SDXL (not SD 1.5) = 3x better detail
|
| 863 |
-
- β
40 steps (not 8-20) = professional smoothness
|
| 864 |
-
- β
4K downscale (not native 1080p) = crisp edges
|
| 865 |
-
- β
7-layer audio (not 2-3) = immersive sound
|
| 866 |
-
- β
Professional grading (not basic filters)
|
| 867 |
-
|
| 868 |
-
You're creating **broadcast quality** content. π¬
|
| 869 |
""")
|
| 870 |
|
| 871 |
if __name__ == "__main__":
|
| 872 |
-
demo.launch(
|
| 873 |
|
| 874 |
"""
|
| 875 |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 876 |
-
|
| 877 |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 878 |
|
| 879 |
-
|
| 880 |
-
torch>=2.0.0
|
| 881 |
-
diffusers>=0.25.0
|
| 882 |
-
transformers>=4.35.0
|
| 883 |
-
accelerate>=0.25.0
|
| 884 |
-
xformers
|
| 885 |
-
safetensors
|
| 886 |
-
gtts
|
| 887 |
-
pydub
|
| 888 |
-
opencv-python-headless
|
| 889 |
-
pillow>=10.0.0
|
| 890 |
-
numpy
|
| 891 |
-
invisible-watermark
|
| 892 |
-
scipy
|
| 893 |
-
|
| 894 |
-
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 895 |
-
π― QUICK START COMMANDS
|
| 896 |
-
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 897 |
|
| 898 |
-
|
| 899 |
-
|
| 900 |
-
|
|
|
|
| 901 |
|
| 902 |
-
|
| 903 |
-
1. Upload app.py and requirements.txt
|
| 904 |
-
2. Select GPU (A10G or T4)
|
| 905 |
-
3. Wait for build
|
| 906 |
-
4. Click "Generate Premium Horror Short"
|
| 907 |
-
5. Wait 45-60 minutes
|
| 908 |
-
6. Download your viral horror content!
|
| 909 |
|
|
|
|
| 910 |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 911 |
"""
|
|
|
|
| 1 |
"""
|
| 2 |
+
π¬ FULL AI PIPELINE HORROR SHORTS GENERATOR
|
| 3 |
+
Everything AI-Generated: Story β Speech β Images β Video
|
| 4 |
+
|
| 5 |
+
PIPELINE:
|
| 6 |
+
1. π€ LLM writes horror story (Mistral-7B)
|
| 7 |
+
2. ποΈ AI generates speech (Bark TTS)
|
| 8 |
+
3. π¨ AI creates images (Stable Diffusion XL)
|
| 9 |
+
4. π΅ AI generates ambient sound
|
| 10 |
+
5. π¬ Combines into final video
|
| 11 |
+
|
| 12 |
+
100% Free Hugging Face Models - No API Keys Needed
|
| 13 |
"""
|
| 14 |
|
| 15 |
import gradio as gr
|
|
|
|
| 17 |
import random
|
| 18 |
import numpy as np
|
| 19 |
import cv2
|
| 20 |
+
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
|
| 21 |
import os
|
| 22 |
import shutil
|
| 23 |
import gc
|
| 24 |
import re
|
| 25 |
from typing import List, Tuple
|
| 26 |
|
| 27 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 28 |
from diffusers import StableDiffusionXLPipeline, DPMSolverMultistepScheduler
|
| 29 |
+
from bark import SAMPLE_RATE, generate_audio, preload_models
|
| 30 |
+
from scipy.io.wavfile import write as write_wav
|
| 31 |
from pydub import AudioSegment
|
| 32 |
+
from pydub.generators import Sine, WhiteNoise
|
|
|
|
| 33 |
|
| 34 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 35 |
+
# STEP 1: AI STORY GENERATION (LLM)
|
| 36 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 37 |
|
| 38 |
+
_llm_model = None
|
| 39 |
+
_llm_tokenizer = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
def load_story_llm():
|
| 42 |
+
"""Load Mistral-7B for story generation."""
|
| 43 |
+
global _llm_model, _llm_tokenizer
|
| 44 |
+
|
| 45 |
+
if _llm_model is None:
|
| 46 |
+
print("Loading Mistral-7B for story generation...")
|
| 47 |
+
|
| 48 |
+
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 49 |
+
|
| 50 |
+
_llm_tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 51 |
+
_llm_model = AutoModelForCausalLM.from_pretrained(
|
| 52 |
+
model_name,
|
| 53 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 54 |
+
device_map="auto" if torch.cuda.is_available() else None,
|
| 55 |
+
low_cpu_mem_usage=True
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
print("Story LLM loaded!")
|
| 59 |
+
|
| 60 |
+
return _llm_model, _llm_tokenizer
|
| 61 |
|
| 62 |
+
def generate_horror_story_with_ai(theme: str = None) -> dict:
|
| 63 |
+
"""Use LLM to generate original horror story."""
|
| 64 |
+
|
| 65 |
+
model, tokenizer = load_story_llm()
|
| 66 |
+
|
| 67 |
+
# Themes for variety
|
| 68 |
+
themes = [
|
| 69 |
+
"liminal spaces and parallel dimensions",
|
| 70 |
+
"time loops and paradoxes",
|
| 71 |
+
"surveillance and being watched",
|
| 72 |
+
"mirrors and reflections",
|
| 73 |
+
"abandoned buildings with secrets",
|
| 74 |
+
"technology that behaves impossibly"
|
| 75 |
+
]
|
| 76 |
+
|
| 77 |
+
if theme is None:
|
| 78 |
+
theme = random.choice(themes)
|
| 79 |
+
|
| 80 |
+
# Prompt engineered for horror stories with loops
|
| 81 |
+
prompt = f"""[INST] You are a master horror writer specializing in creepypasta and internet horror.
|
| 82 |
|
| 83 |
+
Write a SHORT horror story (exactly 250-300 words) with these requirements:
|
| 84 |
+
|
| 85 |
+
THEME: {theme}
|
| 86 |
+
STYLE: First-person narration, present tense, internet creepypasta
|
| 87 |
+
STRUCTURE:
|
| 88 |
+
- Hook in first sentence
|
| 89 |
+
- Build tension gradually
|
| 90 |
+
- End with a twist that CONNECTS BACK to the beginning (looping narrative)
|
| 91 |
+
- The ending should make the reader want to re-read from the start
|
| 92 |
+
|
| 93 |
+
TONE: Unsettling, atmospheric, psychological horror (not gore)
|
| 94 |
+
AVOID: ClichΓ©s, explaining too much, happy endings
|
| 95 |
+
|
| 96 |
+
Write the story now (250-300 words): [/INST]
|
| 97 |
+
|
| 98 |
+
"""
|
| 99 |
|
| 100 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 101 |
+
if torch.cuda.is_available():
|
| 102 |
+
inputs = inputs.to("cuda")
|
| 103 |
+
|
| 104 |
+
outputs = model.generate(
|
| 105 |
+
**inputs,
|
| 106 |
+
max_new_tokens=400,
|
| 107 |
+
temperature=0.8,
|
| 108 |
+
top_p=0.9,
|
| 109 |
+
do_sample=True,
|
| 110 |
+
repetition_penalty=1.15
|
| 111 |
+
)
|
| 112 |
|
| 113 |
+
story = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
# Extract just the story (remove prompt)
|
| 116 |
+
story = story.split("[/INST]")[-1].strip()
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
# Clean up
|
| 119 |
+
story = re.sub(r'\n\n+', '\n\n', story)
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
# Generate title with AI
|
| 122 |
+
title_prompt = f"[INST] Give a 2-4 word creepy title for this horror story: {story[:100]}... [/INST] Title:"
|
|
|
|
| 123 |
|
| 124 |
+
title_inputs = tokenizer(title_prompt, return_tensors="pt")
|
| 125 |
+
if torch.cuda.is_available():
|
| 126 |
+
title_inputs = title_inputs.to("cuda")
|
| 127 |
|
| 128 |
+
title_outputs = model.generate(
|
| 129 |
+
**title_inputs,
|
| 130 |
+
max_new_tokens=10,
|
| 131 |
+
temperature=0.7
|
| 132 |
+
)
|
| 133 |
|
| 134 |
+
title = tokenizer.decode(title_outputs[0], skip_special_tokens=True)
|
| 135 |
+
title = title.split("Title:")[-1].strip().split("\n")[0]
|
| 136 |
+
title = re.sub(r'[^a-zA-Z0-9\s]', '', title)[:50]
|
| 137 |
|
| 138 |
+
# Generate scene descriptions
|
| 139 |
+
scene_prompts = generate_scene_descriptions_from_story(story)
|
| 140 |
|
| 141 |
+
return {
|
| 142 |
+
"title": title if title else "Untitled Horror",
|
| 143 |
+
"script": story,
|
| 144 |
+
"theme": theme,
|
| 145 |
+
"scene_prompts": scene_prompts
|
| 146 |
+
}
|
| 147 |
|
| 148 |
+
def generate_scene_descriptions_from_story(story: str) -> List[str]:
|
| 149 |
+
"""Extract key moments and generate visual prompts."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
# Split story into roughly 8-10 segments
|
| 152 |
+
sentences = [s.strip() for s in re.split(r'[.!?]+', story) if s.strip()]
|
| 153 |
|
| 154 |
+
# Group into scenes
|
| 155 |
+
scenes_per_segment = max(1, len(sentences) // 8)
|
| 156 |
+
scene_groups = [sentences[i:i+scenes_per_segment] for i in range(0, len(sentences), scenes_per_segment)]
|
| 157 |
|
| 158 |
+
# Generate visual prompts based on content
|
| 159 |
+
prompts = []
|
| 160 |
|
| 161 |
+
for group in scene_groups[:10]: # Max 10 scenes
|
| 162 |
+
text = ' '.join(group).lower()
|
| 163 |
+
|
| 164 |
+
# Keyword-based scene generation
|
| 165 |
+
if any(word in text for word in ['door', 'entrance', 'hallway']):
|
| 166 |
+
prompts.append("mysterious door in dark hallway, ominous atmosphere, cinematic lighting, horror aesthetic")
|
| 167 |
+
elif any(word in text for word in ['mirror', 'reflection', 'glass']):
|
| 168 |
+
prompts.append("eerie mirror reflection, bathroom, dim lighting, unsettling atmosphere, horror movie")
|
| 169 |
+
elif any(word in text for word in ['stair', 'stairs', 'staircase']):
|
| 170 |
+
prompts.append("dark staircase, shadows, ominous perspective, horror atmosphere, dramatic lighting")
|
| 171 |
+
elif any(word in text for word in ['window', 'outside', 'view']):
|
| 172 |
+
prompts.append("view through window, ominous sky, dramatic lighting, horror atmosphere, cinematic")
|
| 173 |
+
elif any(word in text for word in ['room', 'apartment', 'house']):
|
| 174 |
+
prompts.append("empty room, liminal space, eerie atmosphere, dramatic shadows, horror aesthetic")
|
| 175 |
+
elif any(word in text for word in ['forest', 'woods', 'trees']):
|
| 176 |
+
prompts.append("dark forest, fog, mysterious atmosphere, horror movie lighting, cinematic")
|
| 177 |
+
elif any(word in text for word in ['camera', 'footage', 'monitor']):
|
| 178 |
+
prompts.append("security camera footage, grainy, CCTV aesthetic, surveillance horror, dramatic")
|
| 179 |
+
elif any(word in text for word in ['elevator', 'floor']):
|
| 180 |
+
prompts.append("elevator interior, flickering lights, claustrophobic, horror atmosphere, cinematic")
|
| 181 |
+
else:
|
| 182 |
+
prompts.append("dark atmospheric horror scene, liminal space, eerie lighting, unsettling, cinematic")
|
| 183 |
|
| 184 |
+
# Ensure we have at least 8 prompts
|
| 185 |
+
while len(prompts) < 8:
|
| 186 |
+
prompts.append("abstract horror atmosphere, darkness, shadows, eerie mood, cinematic lighting")
|
|
|
|
| 187 |
|
| 188 |
+
return prompts[:10]
|
| 189 |
+
|
| 190 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 191 |
+
# STEP 2: AI SPEECH GENERATION (BARK TTS)
|
| 192 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 193 |
+
|
| 194 |
+
def load_bark_tts():
|
| 195 |
+
"""Load Bark TTS model."""
|
| 196 |
+
print("Loading Bark TTS...")
|
| 197 |
+
preload_models()
|
| 198 |
+
print("Bark TTS ready!")
|
| 199 |
+
|
| 200 |
+
def generate_ai_speech(text: str, target_duration: float = 55.0) -> Tuple[str, float]:
|
| 201 |
+
"""Generate speech with Bark AI TTS."""
|
| 202 |
|
| 203 |
+
load_bark_tts()
|
| 204 |
|
| 205 |
+
# Bark works best with shorter segments
|
| 206 |
+
# Split text into chunks
|
| 207 |
+
sentences = [s.strip() + '.' for s in re.split(r'[.!?]+', text) if s.strip()]
|
|
|
|
| 208 |
|
| 209 |
+
audio_segments = []
|
|
|
|
|
|
|
| 210 |
|
| 211 |
+
print(f"Generating speech for {len(sentences)} sentences...")
|
|
|
|
|
|
|
| 212 |
|
| 213 |
+
for i, sentence in enumerate(sentences):
|
| 214 |
+
print(f" Generating sentence {i+1}/{len(sentences)}...")
|
| 215 |
+
|
| 216 |
+
# Generate audio with Bark
|
| 217 |
+
# Use a creepy voice preset
|
| 218 |
+
audio_array = generate_audio(
|
| 219 |
+
sentence,
|
| 220 |
+
history_prompt="v2/en_speaker_6", # Deeper, more ominous voice
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
# Convert to AudioSegment
|
| 224 |
+
temp_path = f"temp/bark_segment_{i}.wav"
|
| 225 |
+
write_wav(temp_path, SAMPLE_RATE, audio_array)
|
| 226 |
+
|
| 227 |
+
segment = AudioSegment.from_wav(temp_path)
|
| 228 |
+
audio_segments.append(segment)
|
| 229 |
+
|
| 230 |
+
# Cleanup
|
| 231 |
+
os.remove(temp_path)
|
| 232 |
|
| 233 |
+
# Combine all segments
|
| 234 |
+
full_audio = sum(audio_segments)
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
+
# Adjust speed to hit target duration
|
| 237 |
+
current_duration = len(full_audio) / 1000.0
|
| 238 |
|
| 239 |
+
if abs(current_duration - target_duration) > 2:
|
| 240 |
+
speed_factor = current_duration / target_duration
|
| 241 |
+
full_audio = full_audio._spawn(
|
| 242 |
+
full_audio.raw_data,
|
| 243 |
+
overrides={"frame_rate": int(full_audio.frame_rate * speed_factor)}
|
| 244 |
+
).set_frame_rate(SAMPLE_RATE)
|
| 245 |
|
| 246 |
+
# Horror audio processing
|
| 247 |
+
full_audio = full_audio - 2 # Slight reduction
|
|
|
|
| 248 |
|
| 249 |
+
# Add reverb
|
| 250 |
+
reverb = full_audio - 20
|
| 251 |
+
full_audio = full_audio.overlay(reverb, position=70)
|
| 252 |
|
| 253 |
+
# Fades
|
| 254 |
+
full_audio = full_audio.fade_in(300).fade_out(500)
|
| 255 |
|
| 256 |
+
# Force to exactly target duration
|
| 257 |
+
full_audio = full_audio[:int(target_duration * 1000)]
|
|
|
|
| 258 |
|
| 259 |
+
# Export
|
| 260 |
+
output_path = "temp/ai_voice.mp3"
|
| 261 |
+
full_audio.export(output_path, format='mp3', bitrate="192k")
|
|
|
|
|
|
|
| 262 |
|
| 263 |
+
return output_path, len(full_audio) / 1000.0
|
| 264 |
+
|
| 265 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 266 |
+
# STEP 3: AI IMAGE GENERATION (SDXL)
|
| 267 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 268 |
|
| 269 |
_sdxl_pipe = None
|
| 270 |
|
| 271 |
+
def load_image_generator():
|
| 272 |
+
"""Load SDXL for image generation."""
|
| 273 |
global _sdxl_pipe
|
| 274 |
|
| 275 |
if _sdxl_pipe is None:
|
| 276 |
+
print("Loading Stable Diffusion XL...")
|
| 277 |
|
| 278 |
_sdxl_pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 279 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
|
|
|
| 282 |
variant="fp16" if torch.cuda.is_available() else None
|
| 283 |
)
|
| 284 |
|
|
|
|
| 285 |
_sdxl_pipe.scheduler = DPMSolverMultistepScheduler.from_config(
|
| 286 |
+
_sdxl_pipe.scheduler.config
|
|
|
|
| 287 |
)
|
| 288 |
|
| 289 |
if torch.cuda.is_available():
|
| 290 |
_sdxl_pipe.to("cuda")
|
| 291 |
_sdxl_pipe.enable_vae_slicing()
|
|
|
|
| 292 |
else:
|
| 293 |
_sdxl_pipe.enable_attention_slicing()
|
| 294 |
_sdxl_pipe.enable_vae_slicing()
|
| 295 |
|
| 296 |
+
print("SDXL ready!")
|
| 297 |
|
| 298 |
return _sdxl_pipe
|
| 299 |
|
| 300 |
+
def generate_ai_image(prompt: str, index: int) -> Image.Image:
|
| 301 |
+
"""Generate image with AI."""
|
| 302 |
|
| 303 |
+
pipe = load_image_generator()
|
| 304 |
|
|
|
|
| 305 |
image = pipe(
|
| 306 |
+
prompt=prompt + ", cinematic, dramatic lighting, horror atmosphere, high quality, professional",
|
| 307 |
+
negative_prompt="blurry, low quality, text, watermark, bright, cheerful, cartoon",
|
| 308 |
+
num_inference_steps=25,
|
| 309 |
guidance_scale=7.5,
|
| 310 |
+
height=1024,
|
| 311 |
+
width=768,
|
| 312 |
).images[0]
|
| 313 |
|
| 314 |
+
# Apply horror grading
|
| 315 |
+
enhancer = ImageEnhance.Color(image)
|
| 316 |
+
image = enhancer.enhance(0.4)
|
| 317 |
|
| 318 |
+
enhancer = ImageEnhance.Contrast(image)
|
| 319 |
+
image = enhancer.enhance(1.4)
|
| 320 |
|
| 321 |
+
enhancer = ImageEnhance.Brightness(image)
|
| 322 |
+
image = enhancer.enhance(0.75)
|
| 323 |
|
| 324 |
# Clear memory
|
| 325 |
if torch.cuda.is_available():
|
|
|
|
| 328 |
|
| 329 |
return image
|
| 330 |
|
| 331 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 332 |
+
# STEP 4: VIDEO ASSEMBLY
|
| 333 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 334 |
+
|
| 335 |
+
def setup_dirs():
|
| 336 |
+
for folder in ['output', 'temp', 'images']:
|
| 337 |
+
if os.path.exists(folder):
|
| 338 |
+
shutil.rmtree(folder)
|
| 339 |
+
os.makedirs(folder)
|
| 340 |
+
|
| 341 |
+
def create_ambient_sound(duration: float) -> str:
|
| 342 |
+
"""Generate AI-like ambient sound."""
|
| 343 |
+
duration_ms = int(duration * 1000)
|
| 344 |
+
|
| 345 |
+
# Multi-layer ambient
|
| 346 |
+
drone1 = Sine(55).to_audio_segment(duration=duration_ms) - 20
|
| 347 |
+
drone2 = Sine(110).to_audio_segment(duration=duration_ms) - 23
|
| 348 |
+
tension = Sine(8000).to_audio_segment(duration=duration_ms) - 30
|
| 349 |
+
noise = WhiteNoise().to_audio_segment(duration=duration_ms) - 35
|
| 350 |
|
| 351 |
+
ambient = drone1.overlay(drone2).overlay(tension).overlay(noise)
|
| 352 |
+
ambient = ambient.fade_in(3000).fade_out(3000)
|
| 353 |
+
|
| 354 |
+
ambient.export("temp/ambient.mp3", format='mp3')
|
| 355 |
+
return "temp/ambient.mp3"
|
| 356 |
+
|
| 357 |
+
def animate_image(img: Image.Image, duration: float, movement: str) -> List[np.ndarray]:
|
| 358 |
+
"""Create animation from image."""
|
| 359 |
+
arr = np.array(img)
|
| 360 |
arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
|
| 361 |
|
| 362 |
h, w = arr.shape[:2]
|
|
|
|
| 363 |
frames = []
|
| 364 |
+
total_frames = int(duration * 30)
|
| 365 |
|
| 366 |
+
# Scale for movement
|
| 367 |
+
scaled = cv2.resize(arr, (int(w*1.3), int(h*1.3)), interpolation=cv2.INTER_LINEAR)
|
|
|
|
| 368 |
sh, sw = scaled.shape[:2]
|
| 369 |
|
| 370 |
for i in range(total_frames):
|
| 371 |
+
progress = i / total_frames
|
| 372 |
+
ease = progress * progress * (3.0 - 2.0 * progress)
|
| 373 |
+
|
| 374 |
+
if movement == 'zoom':
|
| 375 |
+
s = 1.0 + ease * 0.2
|
| 376 |
+
temp = cv2.resize(arr, (int(w*s), int(h*s)), interpolation=cv2.INTER_LINEAR)
|
| 377 |
+
th, tw = temp.shape[:2]
|
| 378 |
+
x, y = (tw-w)//2, (th-h)//2
|
| 379 |
+
frame = temp[y:y+h, x:x+w]
|
| 380 |
+
else: # pan
|
| 381 |
+
x = int((sw-w) * ease)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
frame = scaled[0:h, x:x+w]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
frames.append(frame)
|
| 385 |
|
| 386 |
return frames
|
| 387 |
|
| 388 |
+
def upscale_frame(frame: np.ndarray) -> np.ndarray:
|
| 389 |
+
"""Upscale to 1080x1920."""
|
|
|
|
| 390 |
target_w, target_h = 1080, 1920
|
| 391 |
+
h, w = frame.shape[:2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
|
| 393 |
+
scale = max(target_w/w, target_h/h)
|
| 394 |
+
new_size = (int(w*scale), int(h*scale))
|
| 395 |
|
| 396 |
+
upscaled = cv2.resize(frame, new_size, interpolation=cv2.INTER_LANCZOS4)
|
|
|
|
|
|
|
|
|
|
| 397 |
|
| 398 |
+
uh, uw = upscaled.shape[:2]
|
| 399 |
+
x = (uw - target_w) // 2
|
| 400 |
+
y = (uh - target_h) // 2
|
| 401 |
|
| 402 |
+
return upscaled[y:y+target_h, x:x+target_w]
|
| 403 |
|
| 404 |
+
def add_subtitles(frame: np.ndarray, text: str) -> np.ndarray:
|
| 405 |
+
"""Add subtitles to frame."""
|
|
|
|
| 406 |
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 407 |
pil_img = Image.fromarray(rgb)
|
| 408 |
draw = ImageDraw.Draw(pil_img)
|
| 409 |
|
| 410 |
+
try:
|
| 411 |
+
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 55)
|
| 412 |
+
except:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
font = ImageFont.load_default()
|
| 414 |
|
| 415 |
+
# Word wrap
|
| 416 |
words = text.split()
|
| 417 |
lines = []
|
| 418 |
+
current = []
|
|
|
|
| 419 |
|
| 420 |
for word in words:
|
| 421 |
+
test = ' '.join(current + [word])
|
| 422 |
bbox = draw.textbbox((0, 0), test, font=font)
|
| 423 |
+
if bbox[2] - bbox[0] <= 980:
|
| 424 |
+
current.append(word)
|
|
|
|
| 425 |
else:
|
| 426 |
+
if current:
|
| 427 |
+
lines.append(' '.join(current))
|
| 428 |
+
current = [word]
|
| 429 |
+
if current:
|
| 430 |
+
lines.append(' '.join(current))
|
| 431 |
+
|
| 432 |
+
# Draw
|
| 433 |
+
y = 1700
|
| 434 |
+
for line in lines[:2]: # Max 2 lines
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
bbox = draw.textbbox((0, 0), line, font=font)
|
| 436 |
+
x = (1080 - (bbox[2] - bbox[0])) // 2
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
|
| 438 |
+
# Outline
|
| 439 |
+
for dx in [-4, 0, 4]:
|
| 440 |
+
for dy in [-4, 0, 4]:
|
| 441 |
+
draw.text((x+dx, y+dy), line, font=font, fill='black')
|
|
|
|
| 442 |
|
| 443 |
+
draw.text((x, y), line, font=font, fill='white')
|
| 444 |
+
y += 70
|
|
|
|
|
|
|
| 445 |
|
| 446 |
return cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
|
| 447 |
|
| 448 |
+
def render_video(frames: List[np.ndarray], voice: str, ambient: str, output: str) -> str:
|
| 449 |
+
"""Render final video."""
|
| 450 |
+
temp_vid = "temp/video.mp4"
|
| 451 |
+
|
| 452 |
+
out = cv2.VideoWriter(temp_vid, cv2.VideoWriter_fourcc(*'mp4v'), 30, (1080, 1920))
|
| 453 |
+
for f in frames:
|
| 454 |
+
out.write(f)
|
| 455 |
+
out.release()
|
| 456 |
+
|
| 457 |
+
# Mix audio
|
| 458 |
+
v = AudioSegment.from_mp3(voice)
|
| 459 |
+
a = AudioSegment.from_mp3(ambient)
|
| 460 |
+
mixed = v.overlay(a - 15)
|
| 461 |
+
mixed = mixed[:55000] # Exactly 55s
|
| 462 |
+
mixed.export("temp/audio.mp3", format='mp3')
|
| 463 |
+
|
| 464 |
+
# Combine
|
| 465 |
+
cmd = f'ffmpeg -y -i {temp_vid} -i temp/audio.mp3 -c:v libx264 -preset medium -crf 20 -c:a aac -b:a 192k -t 55 -shortest {output} -loglevel error'
|
| 466 |
+
os.system(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
|
| 468 |
return output
|
| 469 |
|
| 470 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 471 |
+
# MAIN PIPELINE
|
| 472 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 473 |
|
| 474 |
+
def generate_full_ai_pipeline(selected_theme: str = "Random", progress=gr.Progress()):
|
| 475 |
"""
|
| 476 |
+
Complete AI pipeline: Story β Speech β Images β Video
|
|
|
|
| 477 |
"""
|
| 478 |
|
| 479 |
try:
|
| 480 |
setup_dirs()
|
| 481 |
|
| 482 |
+
# STEP 1: AI writes story
|
| 483 |
+
progress(0.05, desc="π€ AI writing horror story...")
|
| 484 |
+
|
| 485 |
+
theme = None if selected_theme == "Random" else selected_theme
|
| 486 |
+
story_data = generate_horror_story_with_ai(theme)
|
| 487 |
|
| 488 |
+
title = story_data['title']
|
| 489 |
+
script = story_data['script']
|
| 490 |
+
scene_prompts = story_data['scene_prompts']
|
| 491 |
|
| 492 |
+
progress(0.15, desc=f"β
Story complete: '{title}'")
|
|
|
|
|
|
|
| 493 |
|
| 494 |
+
# STEP 2: AI generates speech
|
| 495 |
+
progress(0.20, desc="ποΈ AI generating speech with Bark...")
|
| 496 |
+
voice_path, duration = generate_ai_speech(script, 55.0)
|
| 497 |
|
| 498 |
+
progress(0.35, desc=f"β
Speech generated ({duration:.1f}s)")
|
| 499 |
+
|
| 500 |
+
# STEP 3: Generate ambient
|
| 501 |
+
progress(0.40, desc="π΅ Creating ambient soundscape...")
|
| 502 |
+
ambient_path = create_ambient_sound(55.0)
|
| 503 |
+
|
| 504 |
+
# STEP 4: AI generates images
|
| 505 |
+
progress(0.45, desc="π¨ Loading image AI...")
|
| 506 |
+
load_image_generator()
|
| 507 |
+
|
| 508 |
+
num_scenes = min(len(scene_prompts), 8)
|
| 509 |
+
sec_per_scene = 55.0 / num_scenes
|
| 510 |
all_frames = []
|
| 511 |
|
| 512 |
+
movements = ['zoom', 'pan'] * 5
|
| 513 |
+
|
| 514 |
+
for i in range(num_scenes):
|
| 515 |
+
progress(0.45 + (i * 0.05), desc=f"π¨ AI generating image {i+1}/{num_scenes}...")
|
| 516 |
|
| 517 |
+
img = generate_ai_image(scene_prompts[i], i)
|
|
|
|
| 518 |
|
| 519 |
+
progress(0.45 + (i * 0.05) + 0.02, desc=f"ποΈ Animating scene {i+1}/{num_scenes}...")
|
|
|
|
| 520 |
|
| 521 |
+
frames = animate_image(img, sec_per_scene, movements[i])
|
| 522 |
+
frames = [upscale_frame(f) for f in frames]
|
| 523 |
|
| 524 |
all_frames.extend(frames)
|
| 525 |
|
| 526 |
+
del img, frames
|
|
|
|
| 527 |
gc.collect()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
|
| 529 |
+
# STEP 5: Add subtitles
|
| 530 |
+
progress(0.90, desc="π Adding subtitles...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 531 |
|
| 532 |
+
sentences = [s.strip() + '.' for s in re.split(r'[.!?]+', script) if s.strip()]
|
| 533 |
+
frames_per_sub = len(all_frames) // len(sentences)
|
| 534 |
|
| 535 |
final_frames = []
|
| 536 |
for i, frame in enumerate(all_frames):
|
| 537 |
+
sub_idx = min(i // frames_per_sub, len(sentences) - 1)
|
| 538 |
+
final_frames.append(add_subtitles(frame, sentences[sub_idx]))
|
| 539 |
+
|
| 540 |
+
# STEP 6: Render
|
| 541 |
+
progress(0.95, desc="π¬ Rendering final video...")
|
| 542 |
+
output = render_video(final_frames, voice_path, ambient_path, "output/ai_horror_short.mp4")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 543 |
|
| 544 |
+
progress(1.0, desc="β
Complete!")
|
| 545 |
|
|
|
|
| 546 |
info = f"""
|
| 547 |
+
### π€ Full AI Generation Complete!
|
| 548 |
+
|
| 549 |
+
**Title:** {title}
|
| 550 |
+
|
| 551 |
+
**AI Pipeline:**
|
| 552 |
+
1. β
Story written by: Mistral-7B-Instruct
|
| 553 |
+
2. β
Speech by: Bark TTS (Suno AI)
|
| 554 |
+
3. β
Images by: Stable Diffusion XL
|
| 555 |
+
4. β
Assembled automatically
|
| 556 |
+
|
| 557 |
+
**Stats:**
|
| 558 |
+
- Duration: 55.0 seconds
|
| 559 |
+
- Scenes: {num_scenes}
|
| 560 |
+
- Frames: {len(final_frames)}
|
| 561 |
+
- Theme: {story_data['theme']}
|
| 562 |
+
|
| 563 |
+
**Everything created by AI - zero human writing!**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 564 |
"""
|
| 565 |
|
| 566 |
+
return output, script, info
|
| 567 |
|
| 568 |
except Exception as e:
|
| 569 |
+
error = f"β Error: {str(e)}"
|
| 570 |
+
print(error)
|
| 571 |
import traceback
|
| 572 |
traceback.print_exc()
|
| 573 |
+
return None, error, error
|
| 574 |
|
| 575 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 576 |
# GRADIO INTERFACE
|
| 577 |
# ββββββββββββββββββοΏ½οΏ½οΏ½ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 578 |
|
| 579 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="slate")) as demo:
|
| 580 |
|
| 581 |
gr.Markdown("""
|
| 582 |
+
# π€ Full AI Horror Shorts Pipeline
|
| 583 |
+
## Every Step Generated by AI - Story to Final Video
|
|
|
|
|
|
|
| 584 |
|
| 585 |
+
**100% AI-Generated Content Using Free Hugging Face Models**
|
| 586 |
""")
|
| 587 |
|
| 588 |
with gr.Row():
|
| 589 |
with gr.Column(scale=1):
|
| 590 |
+
|
| 591 |
+
theme_dropdown = gr.Dropdown(
|
| 592 |
+
choices=[
|
| 593 |
+
"Random",
|
| 594 |
+
"liminal spaces and parallel dimensions",
|
| 595 |
+
"time loops and paradoxes",
|
| 596 |
+
"surveillance and being watched",
|
| 597 |
+
"mirrors and reflections",
|
| 598 |
+
"abandoned buildings with secrets",
|
| 599 |
+
"technology that behaves impossibly"
|
| 600 |
+
],
|
| 601 |
+
value="Random",
|
| 602 |
+
label="π Story Theme"
|
| 603 |
+
)
|
| 604 |
+
|
| 605 |
generate_btn = gr.Button(
|
| 606 |
+
"π€ Generate Full AI Horror Short",
|
| 607 |
variant="primary",
|
| 608 |
size="lg"
|
| 609 |
)
|
| 610 |
|
| 611 |
gr.Markdown("""
|
| 612 |
+
### π AI Pipeline Steps:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 613 |
|
| 614 |
+
**1. Story Generation** π€
|
| 615 |
+
- Model: Mistral-7B-Instruct
|
| 616 |
+
- Writes original 250-300 word story
|
| 617 |
+
- Creates looping narrative
|
| 618 |
+
- Generates title
|
| 619 |
|
| 620 |
+
**2. Speech Synthesis** ποΈ
|
| 621 |
+
- Model: Bark TTS (Suno AI)
|
| 622 |
+
- Natural-sounding voice
|
| 623 |
+
- Horror audio processing
|
| 624 |
+
- Exactly 55 seconds
|
| 625 |
|
| 626 |
+
**3. Image Generation** π¨
|
| 627 |
+
- Model: Stable Diffusion XL
|
| 628 |
+
- 8 unique horror scenes
|
| 629 |
+
- Cinematic color grading
|
| 630 |
+
- High resolution
|
| 631 |
|
| 632 |
+
**4. Video Assembly** π¬
|
| 633 |
+
- Animated camera movements
|
| 634 |
+
- Professional subtitles
|
| 635 |
+
- Layered ambient sound
|
| 636 |
+
- 1080x1920 output
|
| 637 |
|
| 638 |
+
### β±οΈ Generation Time:
|
| 639 |
+
- Story: 1-2 min
|
| 640 |
+
- Speech: 3-5 min
|
| 641 |
+
- Images: 20-30 min (8 scenes)
|
| 642 |
+
- Assembly: 2-3 min
|
| 643 |
|
| 644 |
+
**Total: 30-40 minutes**
|
|
|
|
| 645 |
|
| 646 |
+
### π‘ Features:
|
| 647 |
+
- β
Zero pre-written content
|
| 648 |
+
- β
Every story is unique
|
| 649 |
+
- β
Free HuggingFace models
|
| 650 |
+
- β
No API keys needed
|
| 651 |
+
- β
Looping narratives
|
| 652 |
+
- β
Professional quality
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 653 |
""")
|
| 654 |
|
| 655 |
with gr.Column(scale=2):
|
| 656 |
video_output = gr.Video(
|
| 657 |
+
label="π¬ AI-Generated Horror Short",
|
| 658 |
+
height=750
|
| 659 |
)
|
| 660 |
|
| 661 |
script_output = gr.Textbox(
|
| 662 |
+
label="π AI-Written Story",
|
| 663 |
+
lines=15
|
|
|
|
| 664 |
)
|
| 665 |
|
| 666 |
+
info_output = gr.Markdown(label="π Generation Info")
|
| 667 |
|
| 668 |
generate_btn.click(
|
| 669 |
+
fn=generate_full_ai_pipeline,
|
| 670 |
+
inputs=[theme_dropdown],
|
| 671 |
outputs=[video_output, script_output, info_output]
|
| 672 |
)
|
| 673 |
|
| 674 |
gr.Markdown("""
|
| 675 |
---
|
| 676 |
|
| 677 |
+
## π Models Used (All Free from Hugging Face):
|
| 678 |
+
|
| 679 |
+
1. **Mistral-7B-Instruct-v0.2** - Story generation
|
| 680 |
+
- 7 billion parameters
|
| 681 |
+
- Instruction-tuned for creative writing
|
| 682 |
+
- Excellent at horror narratives
|
| 683 |
+
|
| 684 |
+
2. **Bark TTS** - Speech synthesis
|
| 685 |
+
- By Suno AI
|
| 686 |
+
- Natural prosody and emotion
|
| 687 |
+
- Multiple voice options
|
| 688 |
+
|
| 689 |
+
3. **Stable Diffusion XL** - Image generation
|
| 690 |
+
- State-of-the-art image quality
|
| 691 |
+
- 1024px native resolution
|
| 692 |
+
- Excellent at atmospheric scenes
|
| 693 |
+
|
| 694 |
+
## π¦ Requirements:
|
| 695 |
+
|
| 696 |
+
```
|
| 697 |
+
gradio
|
| 698 |
+
torch
|
| 699 |
+
transformers
|
| 700 |
+
diffusers
|
| 701 |
+
accelerate
|
| 702 |
+
bark
|
| 703 |
+
scipy
|
| 704 |
+
pydub
|
| 705 |
+
opencv-python-headless
|
| 706 |
+
pillow
|
| 707 |
+
numpy
|
| 708 |
+
```
|
| 709 |
+
|
| 710 |
+
## π― Best Practices:
|
| 711 |
+
|
| 712 |
+
- Use GPU for reasonable speed (30-40 min)
|
| 713 |
+
- CPU will work but take 2-3 hours
|
| 714 |
+
- First run downloads models (~15GB total)
|
| 715 |
+
- Subsequent runs use cached models
|
| 716 |
+
|
| 717 |
+
## π° Cost:
|
| 718 |
+
|
| 719 |
+
**$0** - Completely free!
|
| 720 |
+
- All models from Hugging Face
|
| 721 |
+
- No API keys or subscriptions
|
| 722 |
+
- Run on free GPU (Google Colab, HF Spaces)
|
| 723 |
+
|
| 724 |
+
## π¨ Why This Is Special:
|
| 725 |
+
|
| 726 |
+
Most "AI video generators" use:
|
| 727 |
+
- Pre-written scripts β
|
| 728 |
+
- Pre-recorded voice β
|
| 729 |
+
- Stock images β
|
| 730 |
+
|
| 731 |
+
This uses:
|
| 732 |
+
- AI-written stories β
|
| 733 |
+
- AI-generated speech β
|
| 734 |
+
- AI-generated images β
|
| 735 |
+
|
| 736 |
+
**Every single element created by AI!**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 737 |
""")
|
| 738 |
|
| 739 |
if __name__ == "__main__":
|
| 740 |
+
demo.launch()
|
| 741 |
|
| 742 |
"""
|
| 743 |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 744 |
+
π€ FULL AI PIPELINE - NO HUMAN INPUT REQUIRED
|
| 745 |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 746 |
|
| 747 |
+
This is a TRUE end-to-end AI content generation pipeline.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 748 |
|
| 749 |
+
STEP 1: LLM writes story (Mistral-7B)
|
| 750 |
+
STEP 2: TTS creates speech (Bark)
|
| 751 |
+
STEP 3: Diffusion creates images (SDXL)
|
| 752 |
+
STEP 4: Assembly creates video
|
| 753 |
|
| 754 |
+
Everything automated. Every video unique. Zero templates.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 755 |
|
| 756 |
+
Deploy on HuggingFace Spaces with GPU for best results!
|
| 757 |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 758 |
"""
|