abhinav0231 commited on
Commit
cc2ec1c
Β·
verified Β·
1 Parent(s): db06c0a

Update app.py

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