File size: 2,100 Bytes
59be1d1 14b3fec 7fe7d39 14b3fec 76217ba 55ad485 909d0ca 4599b7c 76217ba 909d0ca 14b3fec 0d40aa7 c88a35f 14b3fec 909d0ca 14b3fec 909d0ca 14b3fec 71df4c0 14b3fec 909d0ca 14b3fec 71df4c0 909d0ca 14b3fec 909d0ca 14b3fec 909d0ca 14b3fec 909d0ca 14b3fec 909d0ca 76217ba c88a35f 909d0ca 14b3fec 909d0ca 14b3fec 59be1d1 14b3fec 909d0ca |
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 |
import gradio as gr
import json
from transformers import pipeline
from PIL import Image
import os
import spaces
# Text Expansion Model (ZeroGPU-compatible)
text_generator = pipeline("text2text-generation", model="t5-large")
# Image Generation Model (ZeroGPU-compatible)
image_generator = pipeline("text-to-image", model="stabilityai/sdxl-turbo")
@spaces.GPU
def generate_cartoon(script_text):
# Step 1: Generate scene breakdown
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", [])):
# Generate background image
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)
# Collect image and dialogue
dialogue = scene.get("dialogue", "")
results.append((image_path, f"Scene {idx+1} Dialogue:\n{dialogue}"))
return results
# Gradio Interface
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() |