Spaces:
Running
Running
File size: 11,081 Bytes
2797a7a |
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
import shutil
import statistics
import tempfile
import uuid
from dataclasses import dataclass, field
from pathlib import Path
from typing import List
import streamlit as st
from llama_index.core.schema import Document
from streamlit.runtime.uploaded_file_manager import UploadedFile
from aistorybooks.phidataa.classic_stories import PhiStoryBookGenerator
@dataclass
class AppInputs:
"""
Data class to hold the input values for the Streamlit app.
"""
uploaded_file: UploadedFile | None = None
language: str = "German"
level: str = "B1 Intermediate"
summary_size: str = "Long (150 sentences/1200 words)"
writing_style: str = "Philosophical"
chunk_size: int = 10
padding: int = 1
skip_first_n_pages: int = 0
language_options: List[str] = field(
default_factory=lambda: ["German", "English", "Spanish", "French"]
)
level_options: List[str] = field(
default_factory=lambda: [
"A1 Beginner",
"A2 Elementary",
"B1 Intermediate",
"B2 Upper Intermediate",
"C1 Advanced",
"C2 Proficiency",
]
)
summary_size_options: List[str] = field(
default_factory=lambda: [
"Short (50 sentences/400 words)",
"Medium (100 sentences/800 words)",
"Long (150 sentences/1200 words)",
]
)
writing_style_options: List[str] = field(
default_factory=lambda: [
"Philosophical",
"Narrative",
"Descriptive",
"Humorous",
"Formal",
]
)
def st_sidebar(inputs: AppInputs):
"""
Creates the sidebar for the Streamlit app and populates the input values.
Args:
inputs (AppInputs): An instance of the AppInputs data class.
"""
st.header("Input Options:")
with st.form(key='inputs_form', border=False):
inputs.uploaded_file = st.file_uploader(
"Upload your novel (PDF)",
type=["pdf"],
accept_multiple_files=False,
help="Upload the PDF file of the novel you want to convert.",
)
inputs.language = st.selectbox(
"Select Target Story Language",
inputs.language_options,
index=inputs.language_options.index(inputs.language),
help="Choose the language you want the storybook to be in.",
)
inputs.level = st.selectbox(
"Select Language Level",
inputs.level_options,
index=inputs.level_options.index(inputs.level),
help="Select the target language proficiency level for the storybook.",
)
inputs.summary_size = st.selectbox(
"Desired Summary Length (Per Chunk)",
inputs.summary_size_options,
index=inputs.summary_size_options.index(inputs.summary_size),
help="Specify the desired length of the summary for each chunk of the novel.",
)
inputs.writing_style = st.selectbox(
"Desired Writing Style",
inputs.writing_style_options,
index=inputs.writing_style_options.index(inputs.writing_style),
help="Choose the writing style for the generated storybook.",
)
inputs.chunk_size = st.number_input(
"Chunk Size",
min_value=1,
value=inputs.chunk_size,
help="Number of pages to process per iteration. Larger chunks may take longer to process.",
)
inputs.padding = st.number_input(
"Padding",
min_value=0,
value=inputs.padding,
help="Number of pages to overlap between chunks. Helps maintain context.",
)
inputs.skip_first_n_pages = st.number_input(
"Skip First N Pages",
min_value=0,
value=inputs.skip_first_n_pages,
help="Number of pages to skip at the beginning of the novel (e.g., table of contents).",
)
submit_button = st.form_submit_button(label='Submit')
return submit_button
def st_process_file(inputs: AppInputs) -> List[List[Document]]:
uploaded_file_name = inputs.uploaded_file.name
st.info(f"Uploaded File: **{inputs.uploaded_file.name}**. Preparing your storybook... (Working in the background)."
f" \nPlease note: Processing is powered by the free tier of Gemini, which may experience rate limiting.",
icon=":material/info:")
try:
temp_folder = Path(tempfile.mkdtemp(prefix="story_gen_temp_"))
progress_value = 0
progress = st.progress(value=progress_value, text=f"Processing file...")
pdf_file = temp_folder.joinpath(uploaded_file_name)
md_file_name = f"{pdf_file.stem}.md"
# pdf_file_final = pdf_file.parent.joinpath(f"{pdf_file.stem}_story.pdf")
pdf_file.write_bytes(inputs.uploaded_file.getvalue())
generator = PhiStoryBookGenerator(
language=inputs.language,
level=inputs.level,
summary_size=inputs.summary_size,
writing_style=inputs.writing_style,
)
st.session_state[md_file_name] = ""
button_container = st.empty()
info_container = st.empty()
it = generator.run(pdf_file=pdf_file,
chunk_size=inputs.chunk_size,
padding=inputs.padding,
skip_first_n_pages=inputs.skip_first_n_pages
)
for response in it:
if response.event == "RunFailed":
st.error(f"{response.content}", icon=":material/error:")
progress.progress(value=progress_value, text=f"Error: {response.content}")
else:
progress_value = response.metrics['progress_percent']
progress.progress(value=progress_value, text=response.metrics['progress_info'])
st.session_state[md_file_name] += f"\n\n{response.content}"
button_container.empty()
button_container.download_button(label='Download Storybook as Markdown',
data=st.session_state.get(md_file_name),
file_name=md_file_name,
mime='text/markdown',
on_click="ignore",
key=str(uuid.uuid4()),
type="primary",
icon=":material/download:",
)
info_container.empty()
metrics = generator.model.metrics
avg_response_time = statistics.mean(metrics.get('response_times', [])) if metrics else 0
input_tokens = metrics.get('input_tokens', 0) if metrics else 0
output_tokens = metrics.get('output_tokens', 0) if metrics else 0
total_tokens = metrics.get('total_tokens', 0) if metrics else 0
info_container.info(f"Model: {generator.model.name} "
f" \n Avg response time: {avg_response_time} "
f" \n Input tokens: {input_tokens} "
f" \n Output tokens: {output_tokens} "
f" \n Total tokens: {total_tokens}",
icon=":material/info:")
finally:
if temp_folder and temp_folder.exists():
shutil.rmtree(temp_folder)
st.info(f"Temporary folder and its contents cleared", icon=":material/info:")
def st_main_page(inputs: AppInputs):
"""
Creates the main page for the Streamlit app and displays the input values.
Args:
inputs (AppInputs): An instance of the AppInputs data class.
"""
st.title("Novel to Storybook Generator")
st.write("---")
options_text = f"""
**Selected Options:**
**Language:** {inputs.language} |
**Language Level:** {inputs.level} |
**Summary Length:** {inputs.summary_size} |
**Writing Style:** {inputs.writing_style} |
**Chunk Size:** {inputs.chunk_size} |
**Padding:** {inputs.padding} |
**Skip First N Pages:** {inputs.skip_first_n_pages}
"""
st.markdown(options_text)
if inputs.uploaded_file:
st_process_file(inputs=inputs)
def st_set_css_and_footer():
st.markdown(
"""
<style>
.stAppHeader {
background-color: rgba(255, 255, 255, 0.0); /* Transparent background */
visibility: visible; /* Ensure the header is visible */
}
.block-container {
padding-top: 0.5rem;
padding-bottom: 0rem;
padding-left: 5rem;
padding-right: 5rem;
}
.footer {
position: fixed;
left: 0;
bottom: 2px;
width: 100%;
text-align: right;
padding-right: 40px;
}
.footer a {
margin: 0 5px; /* Reduced margin for smaller icons */
text-decoration: none;
}
.footer img {
height: 18px; /* Adjusted height for smaller icons */
width: 18px; /* Adjusted width for smaller icons */
vertical-align: middle;
opacity: 0.7; /* Added opacity for a softer look */
transition: opacity 0.3s ease; /* Added transition for hover effect */
}
.footer img:hover {
opacity: 1.0; /* Increased opacity on hover */
}
</style>
""",
unsafe_allow_html=True,
)
footer_html = """
<div class='footer'>
<p>
Need to leverage AI for your business, improve your data and analytics setup or strategy?
Feel free to contact.
<a href="https://github.com/ismailsimsek" target="_blank">
<img src="https://cdn-icons-png.flaticon.com/512/25/25231.png" alt="GitHub">
</a>
<a href="https://www.linkedin.com/in/ismailsimsek/" target="_blank">
<img src="https://cdn-icons-png.flaticon.com/512/174/174857.png" alt="LinkedIn">
</a>
<a href="https://medium.com/@ismail-simsek" target="_blank">
<img src="https://cdn-icons-png.flaticon.com/512/1384/1384015.png" alt="Medium">
</a>
Copyright 2025 Ismail Simsek</p>
</div>
"""
st.markdown(footer_html, unsafe_allow_html=True)
def main():
st.set_page_config(layout="wide")
st_set_css_and_footer()
inputs = AppInputs()
with st.sidebar:
st_sidebar(inputs)
st_main_page(inputs)
if __name__ == "__main__":
main()
|