Spaces:
Runtime error
Runtime error
import streamlit as st | |
from streamlit_option_menu import option_menu | |
from generate_plot import generate_plot, generate_storybook, generate_book_cover, set_openai_api_key | |
from generate_horror_plot import generate_horror_plot, generate_horror_storybook, set_openai_api_key | |
from markup import app_intro, how_use_intro | |
from g4f import Provider | |
if "generated_code" not in st.session_state: | |
st.session_state["generated_code"] = "" | |
if "generated_horror_code" not in st.session_state: | |
st.session_state["generated_horror_code"] = "" | |
def tab1(): | |
col1, col2 = st.columns([1, 2]) | |
with col1: | |
st.image("image.jpg", use_column_width=True) | |
with col2: | |
st.markdown(app_intro(), unsafe_allow_html=True) | |
st.markdown(how_use_intro(),unsafe_allow_html=True) | |
github_link = '[<img src="https://badgen.net/badge/icon/github?icon=github&label">](https://github.com/ethanrom)' | |
huggingface_link = '[<img src="https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue">](https://huggingface.co/ethanrom)' | |
st.write(github_link + ' ' + huggingface_link, unsafe_allow_html=True) | |
def tab2(): | |
st.header("π Auto Children Story Generator π¨") | |
st.markdown("Create an enchanting children's book with custom characters and illustrations!") | |
st.write("Follow the magical instructions below:") | |
col1, col2 = st.columns(2) | |
with col1: | |
st.subheader("Step 1: Enter the Age π§") | |
st.write("Enter the targeted age group (between 1 and 10) for your delightful readers.") | |
age = st.number_input("Enter the targeted age:", min_value=1, max_value=10, value=4) | |
st.subheader("Step 2: Choose the Number of Pages π") | |
st.write("Select the number of pages for your captivating children's book (between 2 and 7).") | |
number_of_pages = st.number_input("Enter the Number of Pages:", min_value=2, max_value=7, value=3) | |
with col2: | |
st.subheader("Step 3: Name Your Characters π") | |
st.write("Enter the names of the endearing characters you wish to bring to life in the story.") | |
character_names = st.text_area("Enter Character names here:", value="Lilly") | |
st.subheader("Step 4: Choose the Art Style π¨") | |
st.write("Select the art style that will add magic to your children's book illustrations.") | |
art_styles = ["Style 1", "Style 2", "Style 3"] | |
selected_style = st.selectbox("Select Art Style:", art_styles) | |
result = None | |
st.subheader("Step 5: Choose API π") | |
st.write("Select between G4F free providers or OpenAI. Please note that some G4F providers may not work all time. If it gives any errors for G4F try other providers or use OpenAI api key") | |
provider = st.selectbox("Select API:", ["G4F", "OpenAI"]) | |
if provider == "OpenAI": | |
openai_api_key = st.text_input("π Enter your OpenAI API key:", type='password') | |
if st.button("Generate Plot") and openai_api_key: | |
set_openai_api_key(openai_api_key) | |
with st.spinner('Generating plot...'): | |
result = generate_plot(number_of_pages, character_names, age, selected_style, provider="OpenAI") | |
st.code(result) | |
st.session_state.generated_code = result | |
elif not openai_api_key: | |
st.warning("Please enter your OpenAI API key.") | |
elif provider == "G4F": | |
selected_provider = st.selectbox("Select G4F Provider:", [ | |
"Ails", "You", "Aichat", "Bard", | |
"Forefront", "DeepAi", "GetGpt" | |
]) | |
if st.button("Generate Plot"): | |
with st.spinner('Generating plot...'): | |
result = generate_plot(number_of_pages, character_names, age, selected_style, provider="G4F", selected_provider=selected_provider) | |
st.code(result) | |
st.session_state.generated_code = result | |
if st.button("Generate Storybook"): | |
if not st.session_state.generated_code: | |
st.warning("Please generate the plot first before creating the storybook.") | |
return | |
with st.spinner('Generating storybook...'): | |
storybook = generate_storybook(st.session_state.generated_code) | |
if len(storybook) == 0: | |
st.error("Error: Unable to generate storybook.") | |
return | |
for page_number, image in storybook: | |
st.markdown(f"<Page {page_number}>") | |
st.image(image, use_column_width=True) | |
def tab3(): | |
st.header("π·οΈ Horror Story Generator ποΈ") | |
st.markdown("Create a spine-chilling horror story with custom length and illustrations!") | |
st.write("Follow the eerie instructions below:") | |
#openai_api_key = st.text_input("π Enter your OpenAI API key:", type='password') | |
col1, col2 = st.columns(2) | |
with col1: | |
st.subheader("Step 1: Choose the Number of Pages π") | |
st.write("Select the number of pages for your bone-chilling horror story (between 2 and 7).") | |
number_of_pages = st.number_input("Enter the Number of Pages:", min_value=2, max_value=7, value=2) | |
with col2: | |
st.subheader("Step 2: Choose the Art Style π¨") | |
st.write("Select the art style that will add terror to your horror story illustrations.") | |
art_styles = ["Style 1", "Style 2", "Style 3"] | |
selected_style = st.selectbox("Select Art Style:", art_styles) | |
result = None | |
st.subheader("Step 3: Choose API π") | |
st.write("Select between G4F free providers or OpenAI. Please note that some G4F providers may not work all time") | |
provider = st.selectbox("Select API:", ["G4F", "OpenAI"]) | |
if provider == "OpenAI": | |
openai_api_key = st.text_input("π Enter your OpenAI API key:", type='password') | |
if st.button("Generate Plot") and openai_api_key: | |
set_openai_api_key(openai_api_key) | |
with st.spinner('Generating plot...'): | |
result = generate_horror_plot(number_of_pages, selected_style, provider="OpenAI") | |
st.code(result) | |
st.session_state.generated_code = result | |
elif not openai_api_key: | |
st.warning("Please enter your OpenAI API key.") | |
elif provider == "G4F": | |
selected_provider = st.selectbox("Select G4F Provider:", [ | |
"Ails", "You", "Aichat", "Bard", | |
"Forefront", "DeepAi", "GetGpt" | |
]) | |
if st.button("Generate Plot"): | |
with st.spinner('Generating plot...'): | |
result = generate_horror_plot(number_of_pages, selected_style, provider="G4F", selected_provider=selected_provider) | |
st.code(result) | |
st.session_state.generated_horror_code = result | |
if st.button("Generate Horror Storybook"): | |
if not st.session_state.generated_horror_code: | |
st.warning("Please generate the plot first before creating the horror storybook.") | |
return | |
with st.spinner('Generating horror storybook...'): | |
horror_storybook = generate_horror_storybook(st.session_state.generated_horror_code) | |
if len(horror_storybook) == 0: | |
st.error("Error: Unable to generate horror storybook.") | |
return | |
for page_number, image in horror_storybook: | |
st.markdown(f"<Page {page_number}>") | |
st.image(image, use_column_width=True) | |
def tab4(): | |
st.header("Book Cover Page Generator") | |
st.markdown("Auto Generate a book cover for your children's storybook.") | |
#openai_api_key = st.text_input("Enter your OpenAI API key:", type='password') | |
st.subheader("Step 1: Enter Book Title") | |
title = st.text_input("Enter Book Title:", value="Adventures of Lilly") | |
st.subheader("Step 2: Enter Author Name") | |
author = st.text_input("Enter Author Name:", value="Lilly's mom") | |
st.subheader("Step 3: Enter Image Description") | |
st.write("Enter a description of the image you want to use for the book cover.") | |
image_text = st.text_area("Enter Image Description:", value="storybook art of a little girl's adventure") | |
result = None | |
if st.button("Generate Book Cover"): | |
with st.spinner('Generating book cover...'): | |
result = generate_book_cover(title, author, image_text) | |
if len(result) == 0: | |
st.warning("Error: Unable to generate book cover.") | |
return | |
for page_number, image in enumerate(result, start=1): | |
st.markdown(f"<Cover Page>") | |
st.image(image, use_column_width=True) | |
def main(): | |
st.set_page_config(page_title="Children storybook generator", page_icon=":memo:", layout="wide") | |
tabs = ["Intro", "Children", "Horror"] | |
with st.sidebar: | |
current_tab = option_menu("Select a Tab", tabs, menu_icon="cast") | |
tab_functions = { | |
"Intro": tab1, | |
"Children": tab2, | |
"Horror": tab3, | |
} | |
if current_tab in tab_functions: | |
tab_functions[current_tab]() | |
if __name__ == "__main__": | |
main() |