Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,158 +1,169 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 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 |
st.markdown("Built with β€οΈ by Abhinav")
|
|
|
|
| 1 |
+
try:
|
| 2 |
+
import spacy
|
| 3 |
+
try:
|
| 4 |
+
spacy.load("en_core_web_sm")
|
| 5 |
+
except OSError:
|
| 6 |
+
print("Installing spaCy English model...")
|
| 7 |
+
subprocess.check_call([sys.executable, "-m", "spacy", "download", "en_core_web_sm"])
|
| 8 |
+
print("β
spaCy model installed successfully")
|
| 9 |
+
except Exception as e:
|
| 10 |
+
print(f"β οΈ Warning: spaCy model installation failed: {e}")
|
| 11 |
+
|
| 12 |
+
import streamlit as st
|
| 13 |
+
import os
|
| 14 |
+
import time
|
| 15 |
+
import glob
|
| 16 |
+
|
| 17 |
+
from llm import generate_story
|
| 18 |
+
from chunker import process_story_for_multimedia, save_multimedia_data
|
| 19 |
+
from image_generation import generate_all_images_from_file
|
| 20 |
+
from tts import generate_all_audio_from_file
|
| 21 |
+
from movie import Video
|
| 22 |
+
|
| 23 |
+
# --- Page Configuration ---
|
| 24 |
+
st.set_page_config(
|
| 25 |
+
page_title="Smart Cultural Storyteller",
|
| 26 |
+
page_icon="π¬",
|
| 27 |
+
layout="wide",
|
| 28 |
+
initial_sidebar_state="collapsed"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# --- Helper Functions ---
|
| 32 |
+
def cleanup_files():
|
| 33 |
+
"""Removes old generated files to keep the space clean for a new run."""
|
| 34 |
+
print("--- Cleaning up old files... ---")
|
| 35 |
+
# Directories for generated assets
|
| 36 |
+
for directory in ["generated_images", "generated_audio", "temp_uploads"]:
|
| 37 |
+
if os.path.exists(directory):
|
| 38 |
+
for f in glob.glob(os.path.join(directory, '*.*')):
|
| 39 |
+
try:
|
| 40 |
+
os.remove(f)
|
| 41 |
+
except OSError as e:
|
| 42 |
+
st.error(f"Error removing file {f}: {e}")
|
| 43 |
+
|
| 44 |
+
# JSON files, temp files, and final video from the root directory
|
| 45 |
+
for f in glob.glob("multimedia_*.json") + glob.glob("temp_*.*") + glob.glob("*.mp4"):
|
| 46 |
+
if os.path.exists(f):
|
| 47 |
+
os.remove(f)
|
| 48 |
+
|
| 49 |
+
# --- UI Layout ---
|
| 50 |
+
st.title("SparrowTale - π¬ Smart Cultural Storyteller")
|
| 51 |
+
st.markdown("Craft beautiful, culturally rich stories with the power of AI. Provide a prompt, upload a document or audio, and watch your story come to life!")
|
| 52 |
+
|
| 53 |
+
# Use session state to store the video path and prevent re-runs
|
| 54 |
+
if 'video_path' not in st.session_state:
|
| 55 |
+
st.session_state.video_path = None
|
| 56 |
+
|
| 57 |
+
# Center the main content for a cleaner look
|
| 58 |
+
col1, col2, col3 = st.columns([1, 3, 1])
|
| 59 |
+
|
| 60 |
+
with col2:
|
| 61 |
+
with st.container(border=True):
|
| 62 |
+
# Input widgets
|
| 63 |
+
user_prompt = st.text_area("π **Enter your story idea...**", height=150, placeholder="A wise old turtle who teaches a village about patience...")
|
| 64 |
+
|
| 65 |
+
col_style, col_gender = st.columns(2)
|
| 66 |
+
with col_style:
|
| 67 |
+
story_style = st.selectbox(
|
| 68 |
+
"π¨ **Choose a Story Style**",
|
| 69 |
+
("Mythical & Folklore", "Historical & Realistic", "Futuristic & Sci-Fi", "Ancient Indian Knowledge")
|
| 70 |
+
)
|
| 71 |
+
with col_gender:
|
| 72 |
+
gender = st.selectbox(
|
| 73 |
+
"π£οΈ **Choose a Narrator Voice**",
|
| 74 |
+
("Female", "Male")
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# File and Audio Uploaders
|
| 78 |
+
doc_file = st.file_uploader("π **Upload a document for context (optional)**", type=['txt', 'pdf'])
|
| 79 |
+
audio_file = st.file_uploader("π€ **Upload an audio prompt (optional)**", type=['wav', 'mp3'])
|
| 80 |
+
|
| 81 |
+
# Generate Button
|
| 82 |
+
generate_button = st.button("β¨ **Generate Story Video**", use_container_width=True, type="primary")
|
| 83 |
+
|
| 84 |
+
# --- Generation Logic ---
|
| 85 |
+
if generate_button:
|
| 86 |
+
# Validate inputs
|
| 87 |
+
if not user_prompt and not doc_file and not audio_file:
|
| 88 |
+
st.error("Please provide a story idea, a document, or an audio prompt to begin.")
|
| 89 |
+
else:
|
| 90 |
+
# 1. Cleanup previous run's files
|
| 91 |
+
cleanup_files()
|
| 92 |
+
st.session_state.video_path = None # Reset video path
|
| 93 |
+
|
| 94 |
+
# 2. Handle file uploads
|
| 95 |
+
doc_path, audio_path = None, None
|
| 96 |
+
temp_dir = "temp_uploads"
|
| 97 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 98 |
+
if doc_file:
|
| 99 |
+
doc_path = os.path.join(temp_dir, doc_file.name)
|
| 100 |
+
with open(doc_path, "wb") as f:
|
| 101 |
+
f.write(doc_file.getbuffer())
|
| 102 |
+
if audio_file:
|
| 103 |
+
audio_path = os.path.join(temp_dir, audio_file.name)
|
| 104 |
+
with open(audio_path, "wb") as f:
|
| 105 |
+
f.write(audio_file.getbuffer())
|
| 106 |
+
st.info("Audio file uploaded. The text prompt will be ignored.")
|
| 107 |
+
user_prompt = "Transcribe and use the story from the audio file."
|
| 108 |
+
|
| 109 |
+
# 3. Main generation pipeline
|
| 110 |
+
with st.spinner("This might take a few minutes... The AI is dreaming up your story... π"):
|
| 111 |
+
progress_bar = st.progress(0, text="Initializing...")
|
| 112 |
+
try:
|
| 113 |
+
# A. Generate the story text
|
| 114 |
+
progress_bar.progress(5, text="Step 1/5: Generating the story script...")
|
| 115 |
+
story_text = generate_story(user_prompt, story_style, audio_path, doc_path)
|
| 116 |
+
|
| 117 |
+
# B. Chunk the story
|
| 118 |
+
progress_bar.progress(20, text="Step 2/5: Designing the storyboard...")
|
| 119 |
+
multimedia_data = process_story_for_multimedia(story_text)
|
| 120 |
+
save_multimedia_data(multimedia_data, "multimedia_data.json")
|
| 121 |
+
|
| 122 |
+
# C. Generate images
|
| 123 |
+
progress_bar.progress(40, text="Step 3/5: Painting the scenes...")
|
| 124 |
+
generate_all_images_from_file("multimedia_data.json", output_json_path="multimedia_data_with_images.json")
|
| 125 |
+
|
| 126 |
+
# D. Generate audio narration
|
| 127 |
+
progress_bar.progress(60, text="Step 4/5: Recording the narration...")
|
| 128 |
+
generate_all_audio_from_file(
|
| 129 |
+
"multimedia_data_with_images.json",
|
| 130 |
+
target_language="English", # This could be made a dropdown too
|
| 131 |
+
gender=gender.lower(),
|
| 132 |
+
output_json_path="multimedia_data_final.json"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# E. Stitch everything into a video using your optimized class
|
| 136 |
+
progress_bar.progress(80, text="Step 5/5: Directing the final movie...")
|
| 137 |
+
video_creator = Video()
|
| 138 |
+
final_video_path = "story_video.mp4"
|
| 139 |
+
success = video_creator.create_video_from_json(
|
| 140 |
+
"multimedia_data_final.json",
|
| 141 |
+
output_filename=final_video_path
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
if success:
|
| 145 |
+
st.session_state.video_path = final_video_path
|
| 146 |
+
progress_bar.progress(100, text="Completed!")
|
| 147 |
+
st.success("Your story video has been created!")
|
| 148 |
+
else:
|
| 149 |
+
st.error("Video creation failed. Please check the logs.")
|
| 150 |
+
|
| 151 |
+
except Exception as e:
|
| 152 |
+
st.error(f"An unexpected error occurred: {e}")
|
| 153 |
+
|
| 154 |
+
# Display the video if it has been generated
|
| 155 |
+
if st.session_state.video_path and os.path.exists(st.session_state.video_path):
|
| 156 |
+
st.video(st.session_state.video_path)
|
| 157 |
+
|
| 158 |
+
with open(st.session_state.video_path, "rb") as file:
|
| 159 |
+
st.download_button(
|
| 160 |
+
label="π₯ **Download Video**",
|
| 161 |
+
data=file,
|
| 162 |
+
file_name="my_story_video.mp4",
|
| 163 |
+
mime="video/mp4",
|
| 164 |
+
use_container_width=True
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
# --- Footer ---
|
| 168 |
+
st.markdown("---")
|
| 169 |
st.markdown("Built with β€οΈ by Abhinav")
|