|
import gradio as gr |
|
import json |
|
from transformers import pipeline |
|
from PIL import Image |
|
import os |
|
import spaces |
|
|
|
|
|
text_generator = pipeline("text2text-generation", model="t5-large") |
|
|
|
|
|
image_generator = pipeline("text-to-image", model="stabilityai/sdxl-turbo") |
|
|
|
@spaces.GPU |
|
def generate_cartoon(script_text): |
|
|
|
prompt = f""" |
|
Convert this cartoon story into a structured scene list in JSON format. For each scene include: |
|
1. "scene_description" |
|
2. "dialogue" |
|
3. "characters" |
|
4. "background_description" |
|
|
|
Story: {script_text} |
|
Return as: {{ "scenes": [{{ ... }}] }} |
|
""" |
|
|
|
try: |
|
result = text_generator(prompt, max_new_tokens=1024)[0]['generated_text'] |
|
scene_data = json.loads(result) |
|
except Exception as e: |
|
return f"Failed to generate structured scenes. Error: {e}" |
|
|
|
os.makedirs("generated_images", exist_ok=True) |
|
|
|
results = [] |
|
for idx, scene in enumerate(scene_data.get("scenes", [])): |
|
|
|
bg_prompt = scene.get("background_description", "cartoon background, colorful") |
|
image = image_generator(bg_prompt, guidance_scale=7.5, num_inference_steps=25).images[0] |
|
image_path = f"generated_images/scene_{idx+1}.png" |
|
image.save(image_path) |
|
|
|
|
|
dialogue = scene.get("dialogue", "") |
|
results.append((image_path, f"Scene {idx+1} Dialogue:\n{dialogue}")) |
|
|
|
return results |
|
|
|
|
|
|
|
demo = gr.Interface( |
|
fn=generate_cartoon, |
|
inputs=gr.Textbox(label="Enter Cartoon Script", lines=10, placeholder="Once upon a time..."), |
|
outputs=gr.Gallery(label="Generated Scenes with Dialogues", columns=1, type="auto"), |
|
title="Cartoon Film Generator (ZeroGPU)", |
|
description="Enter a cartoon story. This tool will break it into scenes, generate background images, and show dialogues. Audio and video creation should be done in Colab." |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |