ethanrom commited on
Commit
3e0a1b1
Β·
1 Parent(s): f6043dd

Upload 4 files

Browse files
Files changed (4) hide show
  1. Birada!.ttf +0 -0
  2. app.py +55 -3
  3. generate_horror_plot.py +141 -0
  4. markup.py +7 -7
Birada!.ttf ADDED
Binary file (42.2 kB). View file
 
app.py CHANGED
@@ -1,12 +1,16 @@
1
  import streamlit as st
2
  from streamlit_option_menu import option_menu
3
  from generate_plot import generate_plot, generate_storybook, generate_book_cover
 
4
  from markup import app_intro, how_use_intro
5
 
6
 
7
  if "generated_code" not in st.session_state:
8
  st.session_state["generated_code"] = ""
9
 
 
 
 
10
  def tab1():
11
 
12
  col1, col2 = st.columns([1, 2])
@@ -76,7 +80,55 @@ def tab2():
76
  st.markdown(f"<Page {page_number}>")
77
  st.image(image, use_column_width=True)
78
 
 
79
  def tab3():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  st.header("Book Cover Page Generator")
81
  st.markdown("Auto Generate a book cover for your children's storybook.")
82
 
@@ -111,7 +163,7 @@ def tab3():
111
 
112
  def main():
113
  st.set_page_config(page_title="Children storybook generator", page_icon=":memo:", layout="wide")
114
- tabs = ["Intro", "Generate book", "Generate Cover"]
115
 
116
  with st.sidebar:
117
 
@@ -119,8 +171,8 @@ def main():
119
 
120
  tab_functions = {
121
  "Intro": tab1,
122
- "Generate book": tab2,
123
- "Generate Cover": tab3,
124
  }
125
 
126
  if current_tab in tab_functions:
 
1
  import streamlit as st
2
  from streamlit_option_menu import option_menu
3
  from generate_plot import generate_plot, generate_storybook, generate_book_cover
4
+ from generate_horror_plot import generate_horror_plot, generate_horror_storybook
5
  from markup import app_intro, how_use_intro
6
 
7
 
8
  if "generated_code" not in st.session_state:
9
  st.session_state["generated_code"] = ""
10
 
11
+ if "generated_horror_code" not in st.session_state:
12
+ st.session_state["generated_horror_code"] = ""
13
+
14
  def tab1():
15
 
16
  col1, col2 = st.columns([1, 2])
 
80
  st.markdown(f"<Page {page_number}>")
81
  st.image(image, use_column_width=True)
82
 
83
+
84
  def tab3():
85
+ st.header("πŸ•·οΈ Horror Story Generator 🏚️")
86
+ st.markdown("Create a spine-chilling horror story with custom length and illustrations!")
87
+ st.write("Follow the eerie instructions below:")
88
+
89
+ #openai_api_key = st.text_input("πŸ”‘ Enter your OpenAI API key:", type='password')
90
+
91
+ col1, col2 = st.columns(2)
92
+ with col1:
93
+
94
+ st.subheader("Step 1: Choose the Number of Pages πŸ“–")
95
+ st.write("Select the number of pages for your bone-chilling horror story (between 2 and 7).")
96
+ number_of_pages = st.number_input("Enter the Number of Pages:", min_value=2, max_value=7, value=3)
97
+
98
+ with col2:
99
+
100
+ st.subheader("Step 2: Choose the Art Style 🎨")
101
+ st.write("Select the art style that will add terror to your horror story illustrations.")
102
+ art_styles = ["Style 1", "Style 2", "Style 3"]
103
+ selected_style = st.selectbox("Select Art Style:", art_styles)
104
+
105
+ result = None
106
+
107
+ if st.button("Generate Plot"):
108
+
109
+ with st.spinner('Generating plot...'):
110
+ result = generate_horror_plot(number_of_pages, selected_style)
111
+ st.code(result)
112
+ st.session_state.generated_code = result
113
+
114
+ if st.button("Generate Horror Storybook"):
115
+ if not st.session_state.generated_horror_code:
116
+ st.warning("Please generate the plot first before creating the horror storybook.")
117
+ return
118
+
119
+ with st.spinner('Generating horror storybook...'):
120
+ horror_storybook = generate_horror_storybook(st.session_state.generated_horror_code)
121
+
122
+ if len(horror_storybook) == 0:
123
+ st.error("Error: Unable to generate horror storybook.")
124
+ return
125
+
126
+ for page_number, image in horror_storybook:
127
+ st.markdown(f"<Page {page_number}>")
128
+ st.image(image, use_column_width=True)
129
+
130
+
131
+ def tab4():
132
  st.header("Book Cover Page Generator")
133
  st.markdown("Auto Generate a book cover for your children's storybook.")
134
 
 
163
 
164
  def main():
165
  st.set_page_config(page_title="Children storybook generator", page_icon=":memo:", layout="wide")
166
+ tabs = ["Intro", "Children", "Horror"]
167
 
168
  with st.sidebar:
169
 
 
171
 
172
  tab_functions = {
173
  "Intro": tab1,
174
+ "Children": tab2,
175
+ "Horror": tab3,
176
  }
177
 
178
  if current_tab in tab_functions:
generate_horror_plot.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import io
4
+ from PIL import Image
5
+ from langchain import PromptTemplate, LLMChain
6
+ from PIL import Image, ImageDraw, ImageFont, ImageFilter
7
+ from langchain.llms import OpenAI
8
+ import openai
9
+ from g4f import Provider, Model
10
+ from langchain_g4f import G4FLLM
11
+
12
+ def set_openai_api_key(api_key):
13
+ openai.api_key = api_key
14
+ os.environ["OPENAI_API_KEY"] = openai.api_key
15
+
16
+ template = template = """Write a very short and unsettling {number_of_pages}-sentence horror story with images that will give you chills.
17
+
18
+
19
+ Your answer should be structured like this with <Text> and <Image> tags.
20
+ <Text> first sentence of the horror story </Text>
21
+ <Image> describe a matching eerie or spooky image for first sentence here without including names so that prompt can be used to generate an image using an ML model.</Image>
22
+ <Text> second sentence of the horror story </Text>
23
+ <Image> describe a matching eerie or spooky image for second sentence here without including names so that prompt can be used to generate an image using an ML model.</Image>
24
+
25
+ for all {number_of_pages} sentences.
26
+ =============
27
+ Answer:"""
28
+
29
+ prompt = PromptTemplate(template=template, input_variables=["number_of_pages"])
30
+
31
+ def query(payload):
32
+ #API_URL = "https://api-inference.huggingface.co/models/prompthero/openjourney"
33
+ API_URL = "https://api-inference.huggingface.co/models/stablediffusionapi/all-526-animated"
34
+ headers = {"Authorization": "Bearer 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"}
35
+ response = requests.post(API_URL, headers=headers, json=payload)
36
+ return response.content
37
+
38
+ def query_alt(payload):
39
+ API_URL = "https://api-inference.huggingface.co/models/stablediffusionapi/anything-v5"
40
+ headers = {"Authorization": "Bearer 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"}
41
+ response = requests.post(API_URL, headers=headers, json=payload)
42
+ return response.content
43
+
44
+ def generate_horror_plot(number_of_pages, selected_style):
45
+ llm = G4FLLM(
46
+ model=Model.gpt_35_turbo,
47
+ provider=Provider.GetGpt,
48
+ )
49
+
50
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
51
+ response = llm_chain.run(number_of_pages=number_of_pages)
52
+ pages = response.split("<Text>")
53
+ plot_result = []
54
+
55
+ additional_texts = {
56
+ "Style 1": " chilling horror illustration, dark and mysterious, haunting shadows, eerie atmosphere, spooky vector art, 8k, artist unknown",
57
+ "Style 2": " horror story illustration, monochromatic, deep shadows, creepy background, unsettling, digital art, artist unknown",
58
+ "Style 3": " horror art, dark and creepy, foggy night, ghostly presence, terrifying, trending on artstation, artist unknown"
59
+ }
60
+
61
+ for i, page in enumerate(pages[1:]):
62
+ text, img_text = page.split("<Image>", 1)
63
+ selected_additional_text = additional_texts.get(selected_style, "")
64
+
65
+ new_img_text = img_text.strip() + " " + selected_additional_text
66
+ text = text.replace("</Text>", "")
67
+
68
+ new_img_text = new_img_text.replace("</Image>", "")
69
+
70
+ plot_result.append((i + 1, "<Text>" + text.strip() + "</Text>", "<Image>" + new_img_text + "</Image>"))
71
+ return plot_result
72
+
73
+
74
+
75
+ def generate_horror_storybook(plot_result):
76
+ storybook = []
77
+ for page_number, text, image_text in plot_result:
78
+ image_bytes = query({"inputs": image_text})
79
+ image = Image.open(io.BytesIO(image_bytes))
80
+ blurred_image = image.filter(ImageFilter.GaussianBlur(8))
81
+
82
+ draw = ImageDraw.Draw(blurred_image)
83
+
84
+ font_size = 30
85
+ font = ImageFont.truetype("Birada!.ttf", font_size)
86
+
87
+ first_letter_font_size = 60
88
+ first_letter_font = ImageFont.truetype("Birada!.ttf", first_letter_font_size)
89
+
90
+ text_x, text_y = 50, 50
91
+ max_width = blurred_image.width - text_x * 2
92
+
93
+ text = text.replace("<Text>", "").replace("</Text>", "")
94
+ wrapped_text = ""
95
+ words = text.split()
96
+ for i, word in enumerate(words):
97
+ if i == 0:
98
+ first_letter_width = draw.textsize(word[0], font=first_letter_font)[0]
99
+ draw.text((text_x, text_y), word[0], fill="white", font=first_letter_font, stroke_width=2, stroke_fill="black")
100
+ text_x += first_letter_width + 10
101
+ wrapped_text += word[1:] + " "
102
+ elif draw.textsize(wrapped_text + word, font=font)[0] < max_width:
103
+ wrapped_text += word + " "
104
+ else:
105
+ draw.text((text_x, text_y), wrapped_text.strip(), fill="white", font=font, stroke_width=2, stroke_fill="black")
106
+ text_y += font.getsize(wrapped_text)[1] + 10
107
+ wrapped_text = word + " "
108
+
109
+ draw.text((text_x, text_y), wrapped_text.strip(), fill="white", font=font, stroke_width=2, stroke_fill="black")
110
+ combined_image = Image.new('RGB', (image.width * 2, image.height))
111
+ combined_image.paste(blurred_image, (0, 0))
112
+ combined_image.paste(image, (image.width, 0))
113
+
114
+ storybook.append((page_number, combined_image))
115
+
116
+ return storybook
117
+
118
+
119
+ def generate_book_cover(title, author, image_text):
120
+ book_cover = []
121
+
122
+ image_bytes = query({"inputs": image_text})
123
+ image = Image.open(io.BytesIO(image_bytes))
124
+
125
+ cover_image = Image.new('RGB', (image.width * 2, image.height), color='white')
126
+ cover_image.paste(image, (0, 0))
127
+
128
+ draw = ImageDraw.Draw(cover_image)
129
+ font_size = 50
130
+ title_font = ImageFont.truetype("DeliusSwashCaps-Regular.ttf", font_size)
131
+ author_font = ImageFont.truetype("DeliusSwashCaps-Regular.ttf", 30)
132
+
133
+ title_x, title_y = image.width + 50, 50
134
+ author_x, author_y = image.width + 50, title_y + 100
135
+
136
+ draw.text((title_x, title_y), title, fill="black", font=title_font)
137
+ draw.text((author_x, author_y), "By " + author, fill="black", font=author_font)
138
+
139
+ book_cover.append(cover_image)
140
+
141
+ return book_cover
markup.py CHANGED
@@ -1,15 +1,15 @@
1
  def app_intro():
2
  return """
3
  <div style='text-align: left;'>
4
- <h2 style='text-align: center;'>Welcome to the AI Children Storybook Generator! πŸ“˜πŸŽ¨</h2>
5
  <h3 style='text-align: center;'>Unleash Your Imagination</h3>
6
 
7
- <p>Get ready for magical adventures and delightful tales! This AI-powered app lets you create wonderful storybooks for children of all ages, complete with enchanting art and captivating plots!</p>
8
 
9
  <h4>What You Can Do:</h4>
10
  <ul>
11
- <li><b>Custom Storybooks:</b> Tell us the age group and the number of pages you want, then enter the names of your favorite characters like Robot, Puppy, or anything you like! Pick a cute art style to bring your story to life.</li>
12
- <li><b>Magical Plot Generation:</b> Need inspiration? Just hit the "Generate Plot" button, and our AI will conjure up a whimsical plot for your storybook, filled with surprises and excitement!</li>
13
  </ul>
14
  </div>
15
  """
@@ -21,9 +21,9 @@ def how_use_intro():
21
  <br>
22
  <h4>How to Use:</h4>
23
  <ul>
24
- <li><b>Create Your Story:</b> First, tell us the age group and the number of pages you want for your storybook. Then, enter the names of the adorable characters who will star in your tale!</li>
25
- <li><b>Art Style:</b> Choose a magical art style that suits your story best! Whether it's dreamy watercolors or vibrant cartoons, the choice is yours!</li>
26
- <li><b>Plot Magic:</b> Want a thrilling plot? Just press the "Generate Plot" button, and our AI will weave an enchanting story filled with wonder and joy!</li>
27
  <li><b>Cover Creation:</b> Want a beautiful cover for your masterpiece? We've got you covered! Select the "Generate Cover" option to create a captivating cover for your storybook.</li>
28
  </ul>
29
  <br>
 
1
  def app_intro():
2
  return """
3
  <div style='text-align: left;'>
4
+ <h2 style='text-align: center;'>Welcome to the AI Storybook Generator! πŸ“šπŸŽ¨</h2>
5
  <h3 style='text-align: center;'>Unleash Your Imagination</h3>
6
 
7
+ <p>Get ready for magical adventures and spine-chilling tales! This AI-powered app lets you create wonderful storybooks for children of all ages, from delightful fairytales to bone-chilling horror tales, complete with enchanting art and captivating plots!</p>
8
 
9
  <h4>What You Can Do:</h4>
10
  <ul>
11
+ <li><b>Custom Storybooks:</b> Tell us the age group and the number of pages you want for your storybook. Enter the names of your favorite characters, whether it's friendly fairies or terrifying monsters! Pick a suitable art style to bring your story to life.</li>
12
+ <li><b>Magical Plot Generation:</b> Need inspiration? Just hit the "Generate Plot" button, and our AI will conjure up a whimsical or spine-chilling plot for your storybook, filled with surprises and excitement!</li>
13
  </ul>
14
  </div>
15
  """
 
21
  <br>
22
  <h4>How to Use:</h4>
23
  <ul>
24
+ <li><b>Create Your Story:</b> First, tell us the age group and the number of pages you want for your storybook. Then, enter the names of the adorable characters for your children's story or the terrifying entities for your horror tale!</li>
25
+ <li><b>Art Style:</b> Choose a magical art style for children's stories or a haunting art style for horror stories that suits your tale best! Whether it's dreamy watercolors or dark and mysterious illustrations, the choice is yours!</li>
26
+ <li><b>Plot Magic:</b> Want a thrilling plot for your horror story or a heartwarming adventure for your children's tale? Just press the "Generate Plot" button, and our AI will weave an enchanting or chilling story filled with wonder and suspense!</li>
27
  <li><b>Cover Creation:</b> Want a beautiful cover for your masterpiece? We've got you covered! Select the "Generate Cover" option to create a captivating cover for your storybook.</li>
28
  </ul>
29
  <br>