Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import google.generativeai as genai
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Configure Google Generative AI
|
| 8 |
+
GOOGLE_API_KEY = "AIzaSyBn9Ehq5oIqkEov_fmAMT258X6imfNXfvg" # Replace with your real API key
|
| 9 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 10 |
+
model = genai.GenerativeModel("models/gemini-2.0-flash")
|
| 11 |
+
|
| 12 |
+
# Load Stable Diffusion model
|
| 13 |
+
model_id = "prompthero/openjourney-v4"
|
| 14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
print(f"✅ Using device: {device}")
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
|
| 19 |
+
pipe.to(device)
|
| 20 |
+
print("✅ Stable Diffusion Model Loaded Successfully!")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"❌ Error loading Stable Diffusion model: {e}")
|
| 23 |
+
exit()
|
| 24 |
+
|
| 25 |
+
# Art styles (expand if needed)
|
| 26 |
+
art_styles = {
|
| 27 |
+
"Cinematic": "cinematic, highly detailed, vivid colors, realistic style",
|
| 28 |
+
"Anime": "anime style, vibrant colors, expressive characters",
|
| 29 |
+
"Watercolor": "watercolor painting, soft colors, artistic style",
|
| 30 |
+
"Cyberpunk": "cyberpunk, neon lights, futuristic, dystopian",
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def get_summary_or_concept(prompt, num_panels):
|
| 35 |
+
content_prompt = (
|
| 36 |
+
f"Summarize the key concepts or moments of '{prompt}' into {num_panels} brief points, "
|
| 37 |
+
"with 1 concise sentence for each panel, equally distributing the information."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
response = model.generate_content(content_prompt)
|
| 42 |
+
if response and response.text:
|
| 43 |
+
text_response = response.text.strip()
|
| 44 |
+
panels = [p.strip() for p in text_response.split("\n") if p.strip()]
|
| 45 |
+
return panels[:num_panels] if len(panels) > 1 else [text_response] * num_panels
|
| 46 |
+
else:
|
| 47 |
+
return []
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"❌ Error generating content: {e}")
|
| 50 |
+
return []
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def generate_comic_images(story_prompts, style):
|
| 54 |
+
images = []
|
| 55 |
+
for i, panel in enumerate(story_prompts):
|
| 56 |
+
print(f"🎨 Generating image for Panel {i+1}/{len(story_prompts)}: {panel.strip()[:50]}...")
|
| 57 |
+
|
| 58 |
+
image_prompt = (
|
| 59 |
+
f"Highly detailed, {art_styles[style]} scene depicting: {panel.strip()}, "
|
| 60 |
+
"no text, no words"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
image = pipe(
|
| 65 |
+
image_prompt,
|
| 66 |
+
num_inference_steps=20,
|
| 67 |
+
guidance_scale=7.0,
|
| 68 |
+
negative_prompt="text, words, letters, nsfw, explicit, unsafe"
|
| 69 |
+
).images[0]
|
| 70 |
+
images.append(image)
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"❌ Error generating image: {e}")
|
| 73 |
+
return None
|
| 74 |
+
return images
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def generate_comic(topic, num_panels, style):
|
| 78 |
+
"""Generates story and images"""
|
| 79 |
+
story = get_summary_or_concept(topic, num_panels)
|
| 80 |
+
if not story:
|
| 81 |
+
return "⚠️ Error generating story.", None
|
| 82 |
+
|
| 83 |
+
images = generate_comic_images(story, style)
|
| 84 |
+
return story, images
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def comic_interface(topic, num_panels, style):
|
| 88 |
+
"""Generate and display comic"""
|
| 89 |
+
story, images = generate_comic(topic, num_panels, style)
|
| 90 |
+
if images:
|
| 91 |
+
return story, images
|
| 92 |
+
else:
|
| 93 |
+
return "⚠️ Error generating comic.", None
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# Gradio UI
|
| 97 |
+
interface = gr.Interface(
|
| 98 |
+
fn=comic_interface,
|
| 99 |
+
inputs=[
|
| 100 |
+
gr.Textbox(label="Enter a topic for the comic strip", value="Government of India"),
|
| 101 |
+
gr.Slider(minimum=3, maximum=10, step=1, label="Number of Comic Panels", value=6),
|
| 102 |
+
gr.Radio(list(art_styles.keys()), label="Choose an Art Style", value="Cinematic"),
|
| 103 |
+
],
|
| 104 |
+
outputs=[
|
| 105 |
+
gr.Textbox(label="Generated Story Prompts"),
|
| 106 |
+
gr.Gallery(label="Generated Comic Strip"),
|
| 107 |
+
],
|
| 108 |
+
title="🤖 ComicWala AI Generator",
|
| 109 |
+
description="Generate AI-based comic strips using Gemini and Stable Diffusion.",
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
interface.launch()
|