File size: 10,497 Bytes
d65b1bc 3a6cb04 d65b1bc 24a53c2 d65b1bc 3a6cb04 61a58c3 3a6cb04 61a58c3 3a6cb04 61a58c3 3a6cb04 d65b1bc 61a58c3 d65b1bc 00689f9 d65b1bc 00689f9 d65b1bc 0682a8f d65b1bc a5737e1 d65b1bc 6dcb753 d65b1bc 6dcb753 0f248e4 6dcb753 0f248e4 d65b1bc 6dcb753 61a58c3 d65b1bc 61a58c3 d65b1bc 61a58c3 0b92995 61a58c3 d65b1bc d0a3189 f2ad651 d0a3189 d65b1bc 24a53c2 d65b1bc |
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
import databases
import orm
import asyncio, os
import uuid, random
from pydub import AudioSegment
from .DescriptAPI import Speak
from .Vercel import AsyncImageGenerator
import aiohttp
from typing import List
from pydantic import BaseModel
import json
database_url = f"sqlite+aiosqlite:///{str(uuid.uuid4())}.db"
database = databases.Database(database_url)
models = orm.ModelRegistry(database=database)
class WordAlignment(BaseModel):
text: str
alignedWord: str
start: float
end: float
hasFailedAlignment: bool
@classmethod
def from_old_format(cls, data: dict, offset: float = 0.0):
return cls(
text=data["word"],
alignedWord=data["alignedWord"],
start=data["startTime"] + offset,
end=data["endTime"] + offset,
hasFailedAlignment=data["hasFailedAlignment"],
)
def transform_alignment_data(data: List[dict], offset: float = 0.0) -> List[dict]:
return [WordAlignment.from_old_format(item, offset).model_dump() for item in data]
class Project(orm.Model):
tablename = "projects"
start = 0
registry = models
fields = {
"id": orm.Integer(primary_key=True),
"name": orm.String(max_length=10_000),
"aspect_ratio": orm.Float(allow_null=True, default=0),
"transcript": orm.JSON(allow_null=True, default=[]),
"duration": orm.Integer(allow_null=True, default=0),
"assets": orm.JSON(allow_null=True, default=[]),
"links": orm.JSON(allow_null=True, default=[]),
"constants": orm.JSON(allow_null=True, default={}),
}
async def get_all_scenes(self):
return await Scene.objects.filter(project=self).all()
async def generate_json(self):
project_scenes: List[Scene] = await self.get_all_scenes()
self.links = []
self.assets = []
image_assets = []
video_assets = []
audio_assets = []
text_stream = []
transitions = [
# "WaveRight_transparent.webm",
# "WaveLeft_transparent.webm",
# "WaveBlue_transparent.webm",
# "Wave_transparent.webm",
# "Swirl_transparent.webm",
# "Snow_transparent.webm",
# "Likes_transparent.webm",
# "Lightning_transparent.webm",
"Happy_transparent.webm",
# "Fire_transparent.webm",
# "CurlingWave_transparent.webm",
# "Cloud_transparent.webm",
]
self.links.append(
{
"file_name": "sfx_1.mp3",
"link": "https://dm0qx8t0i9gc9.cloudfront.net/previews/audio/BsTwCwBHBjzwub4i4/click-match_My50GP4u_NWM.mp3?type=preview&origin=AUDIOBLOCKS×tamp_ms=1715843203035&publicKey=kUhrS9sKVrQMTvByQMAGMM0jwRbJ4s31HTPVkfDGmwGhYqzmWJHsjIw5fZCkI7ba&organizationId=105711&apiVersion=2.0&stockItemId=28820&resolution=&endUserId=414d29f16694d76c58e7998200a8dcf6f28dc165&projectId=f734c6d7-e39d-4c1d-8f41-417f94cd37ce&searchId=adb77624-5919-41ee-84c6-58e7af098a6d&searchPageId=9124f65b-3e21-47ac-af6b-81387328b7b5",
}
)
for scene in project_scenes:
_, file_name = os.path.split(scene.narration_path)
self.duration += scene.narration_duration ## added one for spaces
self.links.append({"file_name": file_name, "link": scene.narration_link})
# generate transcripts
temp = await scene.generate_scene_transcript(offset=self.start)
end_word = temp[-1]
# narration of the story
audio_assets.append(
{
"type": "audio",
"name": file_name,
"start": self.start,
"end": end_word["start"],
"props": {
"startFrom": 0,
"endAt": end_word["start"] * 30,
# "volume": 5,
},
}
)
text_stream.extend(temp[:-1])
## images and transitions
for image in scene.images:
file_name = str(uuid.uuid4()) + ".png"
self.links.append({"file_name": file_name, "link": image})
image_assets.append(
{
"type": "image",
"name": file_name,
"start": self.start,
"end": self.start + scene.image_duration,
}
)
self.start = self.start + scene.image_duration
## transitions between images
video_assets.append(
{
"type": "video",
"name": "Effects/" + random.choice(transitions),
"start": self.start - 1,
"end": self.start + 2,
"props": {
"startFrom": 1 * 30,
"endAt": 3 * 30,
"volume": 0,
},
}
)
self.assets.append({"type": "audio", "sequence": audio_assets})
## add the images to assets
self.assets.append({"type": "image", "sequence": image_assets})
self.assets.append(
{"type": "video", "sequence": video_assets},
)
self.constants = {
"duration": self.duration * 30,
"height": 1920,
"width": 1080,
}
self.assets.append({"type": "text", "sequence": text_stream})
await self.update(**self.__dict__)
return {"links": self.links, "assets": self.assets, "constants": self.constants}
class Scene(orm.Model):
tts = Speak()
tablename = "scenes"
registry = models
fields = {
"id": orm.Integer(primary_key=True),
"project": orm.ForeignKey(Project),
"images": orm.JSON(default=None),
"narration": orm.String(max_length=10_000, allow_null=True, default=""),
"image_prompts": orm.JSON(default=None),
"narration_duration": orm.Float(allow_null=True, default=0),
"image_duration": orm.Float(allow_null=True, default=0),
"narration_path": orm.String(
max_length=100,
allow_null=True,
default="",
),
"narration_link": orm.String(max_length=10_000, allow_null=True, default=""),
}
async def generate_scene_transcript(self, offset):
links = [self.narration_link]
text = self.narration + " master"
transcript = await self.tts._make_transcript(links=links, text=text)
return transform_alignment_data(data=transcript, offset=offset)
async def generate_scene_data(self):
# Run narrate() and generate_images() concurrently
await asyncio.gather(self.narrate(), self.generate_images())
self.calculate_durations()
async def narrate(self):
link, path = await self._retry_narration_generation()
self.narration_path = path
self.narration_link = link
async def _retry_narration_generation(self):
retry_count = 0
while retry_count < 3:
try:
return await self.tts.say(
text=self.narration + " master"
) ### The blanks help to even stuff up.
except Exception as e:
print(f"Failed to generate narration: {e}")
retry_count += 1
await asyncio.sleep(1) # Add delay before retrying
print("Failed to generate narration after 3 attempts.")
def calculate_durations(self):
file_format = self.narration_path.split(".")[-1]
audio_file = AudioSegment.from_file(self.narration_path, format=file_format)
self.narration_duration = int(len(audio_file) / 1000)
self.image_duration = self.narration_duration / len(self.image_prompts)
async def generate_images(self):
self.images = []
async with aiohttp.ClientSession() as session:
image_generator = AsyncImageGenerator(session)
for payload in self.image_prompts:
result = await image_generator.generate_image(payload)
status = await image_generator.fetch_image_status(result["id"])
self.images.extend(status["output"])
class Transition(orm.Model):
tablename = "transitions"
registry = models
fields = {
"id": orm.Integer(primary_key=True),
"name": orm.String(max_length=100),
"file_path": orm.String(max_length=100),
}
class BackgroundMusic(orm.Model):
tablename = "background_music"
registry = models
fields = {
"id": orm.Integer(primary_key=True),
"name": orm.String(max_length=100),
"file_path": orm.String(max_length=100),
}
# class Testy(orm.Model):
# tablename = "asd"
# registry = models
# fields = {
# "id": orm.Integer(primary_key=True),
# "duration": orm.Float(allow_null=True,default=None),
# "area": orm.Float(allow_null=True,default=None),
# "radius": orm.Float(allow_null=True,default=None),
# }
# def calculate_durations(self):
# self.area = self.radius**2 * 3.14
# pass
# # Create the tables
async def create_tables():
datas = {
"narration": "Welcome to a journey through some of history's strangest moments! Get ready to explore the bizarre, the unusual, and the downright weird.",
"image_prompts": [
"Vintage book opening, revealing strange facts, mixed media collage, curious and intriguing, mysterious, eccentric, macro lens, soft lighting, conceptual photography, cross-processed film, surreal, warm tones, textured paper."
],
}
await models._create_all(database_url)
x = await Project.objects.create(name="avatar")
scene = await Scene.objects.create(project=x)
scene.narration = datas["narration"]
scene.image_prompts = datas["image_prompts"]
await scene.generate_scene_data()
await scene.objects.update(**scene.__dict__)
p = await x.get_all_scenes()
print(p)
print(scene.__dict__)
# asyncio.run(create_tables())
# # Run the function to create tables
# await create_tables()
# # Example usage:
# await Note.objects.create(text="Buy the groceries.", completed=False)
# note = await Note.objects.get(id=1)
# print(note)
|