Update app.py
Browse files
app.py
CHANGED
|
@@ -397,121 +397,134 @@ st.set_page_config(layout="wide")
|
|
| 397 |
st.markdown("<h1 style='text-align: center;'>Clarifai Story Teller</h1>", unsafe_allow_html=True)
|
| 398 |
st.markdown("<h2 style='text-align: center;'>Branching Reading Adventure</h2>", unsafe_allow_html=True)
|
| 399 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
|
| 401 |
# Create tabs
|
| 402 |
-
tab1, tab2, tab3 = st.tabs(
|
| 403 |
|
| 404 |
# Tab 1: Introduction
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
|
|
|
| 427 |
|
| 428 |
# Tab 2: Data Visualization
|
| 429 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 430 |
|
|
|
|
|
|
|
|
|
|
| 431 |
|
| 432 |
-
# Streamlit main page
|
| 433 |
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
sentence_chunks = split_text_into_sentences_and_chunks(st.session_state.text_block , 8)
|
| 437 |
-
prompts = [' '.join(chunk) for chunk in sentence_chunks]
|
| 438 |
-
cols = st.columns(4)
|
| 439 |
-
with st.spinner('Generating Content...'):
|
| 440 |
-
for i, prompt in enumerate(prompts):
|
| 441 |
-
image_path, img_error = generate_image(prompt+st.session_state.on_topic )
|
| 442 |
-
audio, audio_error = generate_audio(prompt)
|
| 443 |
|
| 444 |
-
|
| 445 |
-
if img_error:
|
| 446 |
-
st.error(img_error)
|
| 447 |
-
else:
|
| 448 |
-
st.session_state['image_paths'].append(image_path)
|
| 449 |
-
st.image(image_path, prompt, use_column_width=True)
|
| 450 |
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
with col1:
|
| 479 |
-
|
| 480 |
-
st.header("Image Details")
|
| 481 |
-
st.divider()
|
| 482 |
-
st.subheader("Image Components")
|
| 483 |
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
|
|
|
| 492 |
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
st.
|
| 497 |
-
|
| 498 |
-
st.
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
st.header("Create a Story About This Image")
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
# Button for actions related to the selected image
|
| 512 |
-
if st.button("Create a New Story"):
|
| 513 |
-
st.session_state['text_block'] = image_text_output
|
| 514 |
-
#crewai_process(st.session_state['text_block'])
|
| 515 |
-
#switch_tab(1)
|
| 516 |
|
| 517 |
|
|
|
|
| 397 |
st.markdown("<h1 style='text-align: center;'>Clarifai Story Teller</h1>", unsafe_allow_html=True)
|
| 398 |
st.markdown("<h2 style='text-align: center;'>Branching Reading Adventure</h2>", unsafe_allow_html=True)
|
| 399 |
|
| 400 |
+
tabs = ["Create Your Story Script", "Build Your Image-Audio Book", "Interact with Your Characters"]
|
| 401 |
+
|
| 402 |
+
# Initialize the current tab in session state
|
| 403 |
+
if "current_tab" not in st.session_state:
|
| 404 |
+
st.session_state.current_tab = tabs[0]
|
| 405 |
+
|
| 406 |
+
# Function to switch tabs
|
| 407 |
+
def switch_tab(tab_name):
|
| 408 |
+
st.session_state.current_tab = tab_name
|
| 409 |
|
| 410 |
# Create tabs
|
| 411 |
+
tab1, tab2, tab3 = st.tabs(tabs)
|
| 412 |
|
| 413 |
# Tab 1: Introduction
|
| 414 |
+
if st.session_state.current_tab == tabs[0]:
|
| 415 |
+
with tab1:
|
| 416 |
+
# Set up the Streamlit interface
|
| 417 |
+
col1, col2, col3 = st.columns([1, 4, 1])
|
| 418 |
+
|
| 419 |
+
with col1:
|
| 420 |
+
st.image('crewai/resources/whale.jpg')
|
| 421 |
+
|
| 422 |
+
with col2:
|
| 423 |
+
# Input for the user
|
| 424 |
+
input_topic = st.text_area("What Exciting Adventures Awaits Us", height=100, placeholder="Start Our Story...")
|
| 425 |
+
st.session_state['on_topic'] = input_topic
|
| 426 |
+
# Button to run the process
|
| 427 |
+
if st.button("Create a Story"):
|
| 428 |
+
# Run the crewai process
|
| 429 |
+
with st.spinner('Generating Content...'):
|
| 430 |
+
result = crewai_process(input_topic)
|
| 431 |
+
# Display the result
|
| 432 |
+
st.session_state['text_block'] = result
|
| 433 |
+
st.text_area("Output", value=result , height=300)
|
| 434 |
+
|
| 435 |
+
with col3:
|
| 436 |
+
st.image('crewai/resources/clarifai.png')
|
| 437 |
|
| 438 |
# Tab 2: Data Visualization
|
| 439 |
+
elif st.session_state.current_tab == tabs[1]:
|
| 440 |
+
with tab2:
|
| 441 |
+
|
| 442 |
+
|
| 443 |
+
# Streamlit main page
|
| 444 |
+
|
| 445 |
+
if st.button("Generate Images and Audio"):
|
| 446 |
+
|
| 447 |
+
sentence_chunks = split_text_into_sentences_and_chunks(st.session_state.text_block , 8)
|
| 448 |
+
prompts = [' '.join(chunk) for chunk in sentence_chunks]
|
| 449 |
+
cols = st.columns(4)
|
| 450 |
+
with st.spinner('Generating Content...'):
|
| 451 |
+
for i, prompt in enumerate(prompts):
|
| 452 |
+
image_path, img_error = generate_image(prompt+st.session_state.on_topic )
|
| 453 |
+
audio, audio_error = generate_audio(prompt)
|
| 454 |
+
|
| 455 |
+
with cols[i % 4]:
|
| 456 |
+
if img_error:
|
| 457 |
+
st.error(img_error)
|
| 458 |
+
else:
|
| 459 |
+
st.session_state['image_paths'].append(image_path)
|
| 460 |
+
st.image(image_path, prompt, use_column_width=True)
|
| 461 |
+
|
| 462 |
+
if audio_error:
|
| 463 |
+
st.error(audio_error)
|
| 464 |
+
else:
|
| 465 |
+
st.audio(audio, format='audio/wav')
|
| 466 |
|
| 467 |
+
# Tab 3: User Input and Results
|
| 468 |
+
elif st.session_state.current_tab == tabs[2]:
|
| 469 |
+
with tab3:
|
| 470 |
|
|
|
|
| 471 |
|
| 472 |
+
if 'image_paths' in st.session_state and st.session_state['image_paths']:
|
| 473 |
+
# Create a slider for image selection
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
|
| 475 |
+
col1, col2 = st.columns([2, 3])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
|
| 477 |
+
|
| 478 |
+
with col2:
|
| 479 |
+
# Display the selected image
|
| 480 |
+
|
| 481 |
+
image_index = st.radio(
|
| 482 |
+
"Choose an image",
|
| 483 |
+
options=list(range(len(st.session_state['image_paths']))),
|
| 484 |
+
format_func=lambda x: f"Image {x + 1}"
|
| 485 |
+
)
|
| 486 |
+
|
| 487 |
+
|
| 488 |
+
st.image(st.session_state['image_paths'][image_index])
|
| 489 |
+
|
| 490 |
+
with col1:
|
| 491 |
+
|
| 492 |
+
st.header("Image Details")
|
| 493 |
+
st.divider()
|
| 494 |
+
st.subheader("Image Components")
|
| 495 |
+
|
| 496 |
+
image_conepts = get_image_concepts(st.session_state['image_paths'][image_index])
|
| 497 |
+
|
| 498 |
+
unique_names = set()
|
| 499 |
+
for region in image_conepts:
|
| 500 |
+
for concept in region.data.concepts:
|
| 501 |
+
name = concept.name
|
| 502 |
+
# Add unique names to the set
|
| 503 |
+
unique_names.add(name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
|
| 505 |
+
# Display unique names
|
| 506 |
+
|
| 507 |
+
if unique_names:
|
| 508 |
+
st.write(', '.join(unique_names))
|
| 509 |
+
else:
|
| 510 |
+
st.write("No unique items detected.")
|
| 511 |
+
|
| 512 |
+
st.divider()
|
| 513 |
+
st.subheader("Description of Our Image")
|
| 514 |
|
| 515 |
+
image_text_output = analyze_image(st.session_state['image_paths'][image_index])
|
| 516 |
+
|
| 517 |
+
st.write(image_text_output)
|
| 518 |
+
st.divider()
|
| 519 |
+
|
| 520 |
+
st.header("Create a Story About This Image")
|
| 521 |
+
|
| 522 |
+
|
| 523 |
+
# Button for actions related to the selected image
|
| 524 |
+
if st.button("Create a New Story"):
|
| 525 |
+
st.session_state['text_block'] = image_text_output
|
| 526 |
+
crewai_process(st.session_state['text_block'])
|
| 527 |
+
switch_tab(tabs[0])
|
| 528 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
|
| 530 |
|