File size: 4,616 Bytes
d65b1bc
d685a1d
0d9a098
cb4ba01
949ebef
d65b1bc
 
 
4e4bad2
 
 
 
 
 
 
 
d65b1bc
 
f69521b
bebfbc9
d65b1bc
 
 
 
 
 
 
a510571
 
0d9a098
27277a3
d65b1bc
c441a6c
bebfbc9
d65b1bc
 
 
4e4bad2
 
 
cb4ba01
 
 
 
d65b1bc
 
4e4bad2
d65b1bc
 
 
 
 
5d8b258
d65b1bc
 
 
4e4bad2
d65b1bc
 
 
5d8b258
d65b1bc
 
4e4bad2
 
f9cdcf7
c277e42
4e4bad2
 
 
 
 
f2f147a
4e4bad2
 
 
 
d65b1bc
95dcf07
 
f9cdcf7
 
95dcf07
f9cdcf7
322f167
d65b1bc
 
0d9a098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d685a1d
 
 
 
 
22319a8
d685a1d
 
d65b1bc
 
 
 
 
 
 
 
 
d685a1d
 
0d9a098
a510571
0d9a098
 
 
 
d685a1d
 
 
 
 
 
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
from fastapi import APIRouter, HTTPException, status, BackgroundTasks, UploadFile, Query
from .Schema import GeneratorRequest, GeneratorBulkRequest
from .utils.GroqInstruct import chatbot, VideoOutput, Scene
from .utils.Cohere import chatbot as cohere_chat
from .utils.HuggingChat import Hugging
from .Story.Story import Story
import asyncio, pprint, json
from tqdm import tqdm
from .database.Model import (
    models,
    database_url,
    Scene,
    Project,
    database,
    VideoGenerator,
)
from .utils.RenderVideo import RenderVideo
from .Prompts.StoryGen import Prompt
from App.Editor.editorRoutes import celery_task, EditorRequest
import uuid


async def update_scene(model_scene):
    await model_scene.generate_scene_data()
    await model_scene.update(**model_scene.__dict__)


async def from_dict_generate(data: Story):
    generated_strory = data
    await generate_assets(generated_story=generated_strory)


async def generate_assets(generated_story: Story, batch_size=4, threeD=True):
    x = await Project.objects.create(name=str(uuid.uuid4()))

    # Assuming generated_story.scenes is a list of scenes
    with tqdm(total=len(generated_story.scenes)) as pbar:

        all_scenes: list[Scene] = []
        # create the batches
        for i in range(0, len(generated_story.scenes), batch_size):
            batch = generated_story.scenes[
                i : i + batch_size
            ]  # Get a batch of two story scenes
            batch_updates = []

            # generate pictures or narration per batch
            for story_scene in batch:
                model_scene = await Scene.objects.create(project=x)
                model_scene.image_prompts = story_scene.image_prompts
                model_scene.narration = story_scene.narration
                await model_scene.update(**model_scene.__dict__)
                all_scenes.append(model_scene)
                batch_updates.append(
                    update_scene(model_scene)
                )  # Append update coroutine to batch_updates
            # pause per batch
            await asyncio.gather(
                *batch_updates
            )  # Await update coroutines for this batch

            pbar.update(len(batch))  # Increment progress bar by the size of the batch

    ###### Here we generate the videos

    if False:
        vid_gen = VideoGenerator()
        nested_images = []
        for scene in all_scenes:
            nested_images.append(scene.images)

        results = await vid_gen.run(nested_image_links=nested_images)
        print(results)
        for result, _scene in zip(results, all_scenes):
            _scene.images = result
            await _scene.update(**_scene.__dict__)

    temp = await x.generate_json()
    # print(temp)

    renderr = RenderVideo()
    await renderr.render_video(temp)
    request = EditorRequest.model_validate(temp)
    #
    # await celery_task(video_task=request)


async def main(request: GeneratorRequest):
    topic = request.prompt
    batch_size = request.batch_size
    renderr = RenderVideo()
    huggChat = Hugging()
    if request.grok:
        message = cohere_chat(Prompt.format(topic=topic), model=request.model)

    else:
        temp = await huggChat.chat(
            Prompt.format(topic=topic)
            + f"Match your response to the following schema:  {VideoOutput.model_json_schema()} Make sure to return an instance of the JSON, not the schema itself, and nothing else."
        )
        message = temp
    generated_story = Story.from_dict(message["scenes"])

    print("Generated Story ✅")
    await generate_assets(generated_story=generated_story, batch_size=batch_size)


async def bulkGenerate(bulkRequest: GeneratorBulkRequest):
    tasks = []
    for request in bulkRequest.stories:
        tasks.append(main(request=request))

    await asyncio.gather(*tasks)


generator_router = APIRouter(tags=["video-Generator"])


@generator_router.post("/generate_video")
async def generate_video(
    videoRequest: GeneratorRequest, background_task: BackgroundTasks
):
    background_task.add_task(main, videoRequest)
    return {"task_id": "started"}


@generator_router.post("/generate_video_from_json")
async def generate_video_from_json(jsonReq: Story, background_task: BackgroundTasks):
    background_task.add_task(from_dict_generate, jsonReq)
    return {"task_id": "started"}


@generator_router.post("/generate_video_bulk")
async def generate_video_bulk(
    BulkvideoRequest: GeneratorBulkRequest, background_task: BackgroundTasks
):
    background_task.add_task(bulkGenerate, BulkvideoRequest)
    return {"task_id": "started"}