Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
from PIL import Image, ImageDraw
|
|
|
|
| 4 |
import requests
|
| 5 |
from io import BytesIO
|
| 6 |
-
|
| 7 |
from transformers import pipeline as hf_pipeline
|
| 8 |
from diffusers import StableDiffusionPipeline
|
| 9 |
import torch
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def screenwriter(prompt: str) -> str:
|
| 14 |
instructions = f"""
|
|
@@ -25,28 +31,12 @@ def screenwriter(prompt: str) -> str:
|
|
| 25 |
- Very short description of the main character's appearance.
|
| 26 |
- IMPORTANT!!! ALWAYS use a delimiter '---' to separate the story from the character description.
|
| 27 |
|
| 28 |
-
Here's an example:
|
| 29 |
-
User input: "A unicorn named Jeff discovers a mysterious dish."
|
| 30 |
-
Your output:
|
| 31 |
-
"Jeff is wandering around the enchanted forest.
|
| 32 |
-
Jeff stumbles upon a glowing dish in the enchanted forest.
|
| 33 |
-
Intrigued, Jeff approaches it cautiously, sensing its magical aura.
|
| 34 |
-
As Jeff touches the dish, it reveals that it can either dispose of his horns or make them glow.
|
| 35 |
-
Jeff is hesitant, but decides to try the dish.
|
| 36 |
-
After taking a sip, Jeff notices that his horns started glowing.
|
| 37 |
-
The dish disappears and Jeff is alone in the enchanted forest. "
|
| 38 |
-
---
|
| 39 |
-
"Jeff is a majestic unicorn with shimmering white fur and a spiraled golden horn.
|
| 40 |
-
|
| 41 |
-
Be creative, compelling, and format the comic book clearly. Do not include your thought process or any additional commentary in the output.
|
| 42 |
-
|
| 43 |
STORY PROMPT: {prompt}
|
| 44 |
"""
|
| 45 |
-
|
| 46 |
result = story_gen(instructions, max_new_tokens=250)[0]["generated_text"]
|
| 47 |
return result
|
| 48 |
|
| 49 |
-
def remove_think_block(text:str):
|
| 50 |
return re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL).strip()
|
| 51 |
|
| 52 |
def parse_screenwriter_output(output: str):
|
|
@@ -69,21 +59,14 @@ def error_image(message):
|
|
| 69 |
d.text((10, 250), message, fill=(255, 0, 0))
|
| 70 |
return img
|
| 71 |
|
| 72 |
-
|
| 73 |
-
pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2", torch_dtype=torch.float16)
|
| 74 |
-
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 75 |
-
|
| 76 |
-
|
| 77 |
def illustrator(story: str, character: str):
|
| 78 |
-
|
| 79 |
if not story or not character:
|
| 80 |
raise ValueError('Could not parse story or character from input.')
|
| 81 |
|
| 82 |
scenes = [s.strip() for s in story.split('.') if s.strip()]
|
| 83 |
-
|
| 84 |
images = []
|
| 85 |
for idx, scene in enumerate(scenes):
|
| 86 |
-
prompt = f
|
| 87 |
try:
|
| 88 |
image = pipe(prompt).images[0]
|
| 89 |
images.append((image, scene))
|
|
@@ -100,20 +83,14 @@ def pipeline(prompt: str):
|
|
| 100 |
return f"{story}\n---\n{character}", images
|
| 101 |
|
| 102 |
|
| 103 |
-
with gr.Blocks(
|
| 104 |
-
|
| 105 |
-
gr.
|
| 106 |
-
'''
|
| 107 |
-
# Comic Generator
|
| 108 |
-
Generates a comic off of your prompt.
|
| 109 |
-
''')
|
| 110 |
-
with gr.Row():
|
| 111 |
-
story_input= gr.Textbox(label='Story Prompt', placeholder='A unicorn named Jeff discovers a mysterious dish')
|
| 112 |
generated_story = gr.Button('Generate Story')
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
generated_story.click(pipeline, inputs=story_input, outputs=[story_output, gallery])
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
-
demo.launch(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
from PIL import Image, ImageDraw
|
| 4 |
+
import re
|
| 5 |
import requests
|
| 6 |
from io import BytesIO
|
| 7 |
+
|
| 8 |
from transformers import pipeline as hf_pipeline
|
| 9 |
from diffusers import StableDiffusionPipeline
|
| 10 |
import torch
|
| 11 |
|
| 12 |
+
# Use a small text2text model (lightweight)
|
| 13 |
+
story_gen = hf_pipeline("text2text-generation", model="google/flan-t5-base")
|
| 14 |
+
|
| 15 |
+
# Use a lighter Stable Diffusion model for images
|
| 16 |
+
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
|
| 17 |
+
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 18 |
|
| 19 |
def screenwriter(prompt: str) -> str:
|
| 20 |
instructions = f"""
|
|
|
|
| 31 |
- Very short description of the main character's appearance.
|
| 32 |
- IMPORTANT!!! ALWAYS use a delimiter '---' to separate the story from the character description.
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
STORY PROMPT: {prompt}
|
| 35 |
"""
|
|
|
|
| 36 |
result = story_gen(instructions, max_new_tokens=250)[0]["generated_text"]
|
| 37 |
return result
|
| 38 |
|
| 39 |
+
def remove_think_block(text: str):
|
| 40 |
return re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL).strip()
|
| 41 |
|
| 42 |
def parse_screenwriter_output(output: str):
|
|
|
|
| 59 |
d.text((10, 250), message, fill=(255, 0, 0))
|
| 60 |
return img
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
def illustrator(story: str, character: str):
|
|
|
|
| 63 |
if not story or not character:
|
| 64 |
raise ValueError('Could not parse story or character from input.')
|
| 65 |
|
| 66 |
scenes = [s.strip() for s in story.split('.') if s.strip()]
|
|
|
|
| 67 |
images = []
|
| 68 |
for idx, scene in enumerate(scenes):
|
| 69 |
+
prompt = f"Comic book illustration. Scene: {scene}. Character: {character}. No text, just visual scene."
|
| 70 |
try:
|
| 71 |
image = pipe(prompt).images[0]
|
| 72 |
images.append((image, scene))
|
|
|
|
| 83 |
return f"{story}\n---\n{character}", images
|
| 84 |
|
| 85 |
|
| 86 |
+
with gr.Blocks(title='Comic Generator') as demo:
|
| 87 |
+
gr.Markdown("# Comic Generator\nGenerate a comic book from your idea!")
|
| 88 |
+
story_input = gr.Textbox(label='Story Prompt', placeholder='A unicorn named Jeff discovers a mysterious dish')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
generated_story = gr.Button('Generate Story')
|
| 90 |
+
story_output = gr.Textbox(label='Generated Story', lines=5)
|
| 91 |
+
gallery = gr.Gallery(label='Comic Scenes').style(grid=(2, 2))
|
| 92 |
+
|
| 93 |
generated_story.click(pipeline, inputs=story_input, outputs=[story_output, gallery])
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
| 96 |
+
demo.launch()
|