File size: 3,474 Bytes
43fae26 |
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 |
# Required Imports
import streamlit as st
import google.generativeai as genai
import torch
from diffusers import StableDiffusionPipeline
from PIL import Image
import io
# -----------------------------
# π CONFIGURE API KEYS
# -----------------------------
# Google Generative AI API Key
GOOGLE_API_KEY = "AIzaSyBn9Ehq5oIqkEov_fmAMT258X6imfNXfvg"
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel("models/gemini-2.0-flash")
# -----------------------------
# π¨ LOAD STABLE DIFFUSION MODEL
# -----------------------------
model_id = "prompthero/openjourney-v4"
device = "cuda" if torch.cuda.is_available() else "cpu"
try:
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
pipe.to(device)
print("β
Stable Diffusion Model Loaded Successfully!")
except Exception as e:
print(f"β Error loading Stable Diffusion model: {e}")
exit()
# -----------------------------
# π§ GENERATE STORY PANELS
# -----------------------------
def get_summary_or_concept(prompt, num_panels=6):
content_prompt = (
f"Summarize the key concepts or moments of '{prompt}' into {num_panels} brief points, "
"with 1 concise sentence for each panel, equally distributing the information."
)
try:
response = model.generate_content(content_prompt)
if response and response.text:
text_response = response.text.strip()
panels = [p.strip() for p in text_response.split("\n") if p.strip()]
if len(panels) >= num_panels:
return panels[:num_panels]
else:
return panels + [panels[-1]] * (num_panels - len(panels))
except Exception as e:
print(f"β Error generating content: {e}")
return ["Error: Unable to generate story."]
# -----------------------------
# π¨ GENERATE COMIC IMAGES
# -----------------------------
def generate_comic_images(story_prompts, num_steps=20):
images = []
for i, panel in enumerate(story_prompts):
st.write(f"πΌοΈ Generating image for Panel {i+1}/{len(story_prompts)}: {panel.strip()[:50]}...")
image_prompt = (
f"Highly detailed, cinematic scene depicting: {panel.strip()}, "
"vivid colors, realistic style, no text, no words"
)
try:
image = pipe(
image_prompt,
num_inference_steps=num_steps,
guidance_scale=7.0,
negative_prompt="text, words, letters, nsfw, explicit, unsafe"
).images[0]
images.append(image)
except Exception as e:
st.error(f"β Error generating image: {e}")
return None
return images
# -----------------------------
# πΌοΈ CREATE COMIC STRIP
# -----------------------------
def create_comic_strip(images, panel_width=300, spacing=10):
widths, heights = zip(*(i.size for i in images))
target_height = 300
resized_images = [
img.resize((int(w * target_height / h), target_height)) for img, (w, h) in zip(images, zip(widths, heights))
]
total_width = sum(img.size[0] for img in resized_images) + (spacing * (len(images) - 1))
comic_strip = Image.new("RGB", (total_width, target_height), "white")
x_offset = 0
for img in resized_images:
comic_strip.paste(img, (x_offset, 0))
x_offset += img.size[0] + spacing
return comic_strip
# -----------------------------
#
|