File size: 4,155 Bytes
c060b6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
108
109
110
111
112
import streamlit as st
from constants import *
from stqdm import stqdm
from prompts import *
from generator import *
from utils import *


st.set_page_config(
    layout="wide",
    page_title="AI Book Generator",
    page_icon=":book:",
)
st.title("AI Book Generator")
st.markdown("<h3>Select options </h3>", unsafe_allow_html=True)
with st.expander("Educational value *"):
    age_range = st.select_slider("Age range of the reader", options=AGE_RANGE)
    skill_development = st.selectbox("Skill development", options=SKILL_DEVELOPMENT)
    learning_obectives = st.selectbox(
        "Learning objectives", options=LEARNING_OBJECTIVES
    )
with st.expander("Emotional value *"):
    theme = st.selectbox("Theme", options=THEME)
    mood = st.selectbox("Moood of story", options=MODD_OF_STORY)
    positive_messaging = st.selectbox("Skill development", options=POSITIVE_MESSAGNG)
with st.expander("Personal *"):
    theme = st.selectbox("Gender", options=GENDER)
    fvrt_book = st.text_input("Favorite book")
with st.expander("Book Details * "):
    chapters = st.number_input(
        "How many chapters should the book have?", min_value=3, max_value=100, value=5
    )

    title = st.text_input("Title of the book")
    genre = st.selectbox("Genre", options=GENRE)
    topic = st.selectbox("Topic ", options=TOPIC)
    main_name = st.text_input("Name of main character")
    type_of_main_character = st.selectbox(
        "Type of main character", TYPE_OF_MAIN_CHARACTER
    )
    antagonist_name = st.text_input("Antagonist name")
    antagonsit_type = st.selectbox("Antagonist type", options=ANTAGONIST_TYPE)
    suuporting_character_name = st.text_input("Supporting character name (if any)")
    suporting_character_type = st.selectbox(
        "Supporting character type", options=SUPPORTING_CHARACTER_TYPE
    )
    settings = st.selectbox("Setting ", options=SETTINGS)
    resolution = st.selectbox("Resolution", options=RESOLUTION)

btn = st.button("Generate Book")
if btn:
    content = []
    for x in stqdm(range(chapters), desc="Generating book"):
        if x == 0:
            prmpt = get_initial_prompts(
                genre,
                type_of_main_character,
                main_name,
                skill_development,
                learning_obectives,
                theme,
                topic,
            )
            content.append(complete_with_gpt(prmpt, 200, "gpt2", 1500, 0.7, 1.5))
        if x == 1:
            prmpt = story_setting_prompt(
                genre,
                type_of_main_character,
                main_name,
                skill_development,
                learning_obectives,
                theme,
                mood,
                antagonist_name,
                antagonsit_type,
            )
            previous = " ".join(x for x in content)
            prmpt = previous + " " + prmpt
            content.append(complete_with_gpt(prmpt, 200, "gpt2", 1500, 0.7, 1.5))

        if x % 3 == 0:
            prmpt = supporting_character_inclusion(
                genre,
                suuporting_character_name,
                suporting_character_type,
                positive_messaging,
            )
            previous = " ".join(x for x in content)
            prmpt = previous + " " + prmpt
            content.append(complete_with_gpt(prmpt, 200, "gpt2", 1500, 0.7, 1.5))
        if x == chapters - 1:
            prmpt = ending_scene(genre, resolution, main_name, positive_messaging)
            previous = " ".join(x for x in content)
            prmpt = previous + " " + prmpt
            content.append(complete_with_gpt(prmpt, 200, "gpt2", 1500, 0.7, 1.5))
        else:
            previous = " ".join(x for x in content)
            prmpt = previous
            content.append(complete_with_gpt(prmpt, 200, "gpt2", 1500, 0.7, 1.5))

    st.write(content)
    filenamee = to_pdf(convert(create_md(text=content, title=title)))
    with open(filenamee, "rb") as pdf_file:
        PDFbyte = pdf_file.read()

    st.download_button(
        label="Download Book",
        data=PDFbyte,
        file_name=filenamee,
        mime="application/octet-stream",
    )