Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,21 @@ import os
|
|
8 |
import io
|
9 |
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# function part
|
12 |
# img2text
|
13 |
def img2text(url):
|
@@ -22,6 +37,7 @@ def translate_to_chinese(text):
|
|
22 |
translation = translator(text)[0]["translation_text"]
|
23 |
return translation
|
24 |
|
|
|
25 |
# text2story
|
26 |
def text2story(text):
|
27 |
# Initialize the text generation pipeline
|
@@ -261,59 +277,92 @@ with st.container():
|
|
261 |
uploaded_file = st.file_uploader("", key="upload")
|
262 |
|
263 |
if uploaded_file is not None:
|
|
|
264 |
bytes_data = uploaded_file.getvalue()
|
265 |
-
|
|
|
266 |
file.write(bytes_data)
|
267 |
|
268 |
# Display image
|
269 |
st.image(uploaded_file, use_column_width=True)
|
270 |
|
271 |
-
#
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
#
|
290 |
-
st.
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
295 |
|
296 |
# Stage 3: Story to Audio data
|
297 |
with st.container():
|
298 |
st.markdown("<h3><span class='stage-icon'>🔊</span> 故事準備朗讀中</h3>", unsafe_allow_html=True)
|
299 |
|
300 |
-
# Create audio for Chinese story
|
301 |
-
audio_data = text2audio(story_zh, lang='zh')
|
302 |
-
|
303 |
# Play button with Cantonese text
|
304 |
if st.button("🔊 播放故事"):
|
305 |
-
if
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
310 |
else:
|
311 |
st.error("哎呀!再試多次啦!")
|
312 |
|
313 |
-
# Cleanup: Remove the temporary file
|
314 |
-
if os.path.exists(
|
315 |
-
os.remove(
|
316 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
317 |
# Welcome message in Cantonese
|
318 |
st.markdown("""
|
319 |
<div class="welcome-message">
|
|
|
8 |
import io
|
9 |
|
10 |
|
11 |
+
# Initialize session state for storing data
|
12 |
+
if 'scenario' not in st.session_state:
|
13 |
+
st.session_state.scenario = None
|
14 |
+
if 'scenario_zh' not in st.session_state:
|
15 |
+
st.session_state.scenario_zh = None
|
16 |
+
if 'story' not in st.session_state:
|
17 |
+
st.session_state.story = None
|
18 |
+
if 'story_zh' not in st.session_state:
|
19 |
+
st.session_state.story_zh = None
|
20 |
+
if 'audio_generated' not in st.session_state:
|
21 |
+
st.session_state.audio_generated = False
|
22 |
+
if 'audio_data' not in st.session_state:
|
23 |
+
st.session_state.audio_data = None
|
24 |
+
|
25 |
+
|
26 |
# function part
|
27 |
# img2text
|
28 |
def img2text(url):
|
|
|
37 |
translation = translator(text)[0]["translation_text"]
|
38 |
return translation
|
39 |
|
40 |
+
|
41 |
# text2story
|
42 |
def text2story(text):
|
43 |
# Initialize the text generation pipeline
|
|
|
277 |
uploaded_file = st.file_uploader("", key="upload")
|
278 |
|
279 |
if uploaded_file is not None:
|
280 |
+
# Save uploaded file
|
281 |
bytes_data = uploaded_file.getvalue()
|
282 |
+
temp_file_path = uploaded_file.name
|
283 |
+
with open(temp_file_path, "wb") as file:
|
284 |
file.write(bytes_data)
|
285 |
|
286 |
# Display image
|
287 |
st.image(uploaded_file, use_column_width=True)
|
288 |
|
289 |
+
# Reset session state if a new file is uploaded (detect by checking if there's no scenario yet)
|
290 |
+
if st.session_state.scenario is None:
|
291 |
+
# Stage 1: Image to Text
|
292 |
+
with st.container():
|
293 |
+
st.markdown("<h3><span class='stage-icon'>🔍</span> 圖片解讀中</h3>", unsafe_allow_html=True)
|
294 |
+
|
295 |
+
# Generate caption if not already done
|
296 |
+
st.session_state.scenario = img2text(temp_file_path)
|
297 |
+
|
298 |
+
# Display English caption
|
299 |
+
st.text("英文描述: " + st.session_state.scenario)
|
300 |
+
|
301 |
+
# Translate the caption to Chinese
|
302 |
+
st.session_state.scenario_zh = translate_to_chinese(st.session_state.scenario)
|
303 |
+
|
304 |
+
# Display Chinese caption
|
305 |
+
st.text("中文描述: " + st.session_state.scenario_zh)
|
306 |
+
|
307 |
+
# Stage 2: Text to Story
|
308 |
+
with st.container():
|
309 |
+
st.markdown("<h3><span class='stage-icon'>📝</span> 故事創作中</h3>", unsafe_allow_html=True)
|
310 |
+
|
311 |
+
# Generate story if not already done
|
312 |
+
st.session_state.story = text2story(st.session_state.scenario)
|
313 |
+
|
314 |
+
# Display English story
|
315 |
+
st.text("英文故事: " + st.session_state.story)
|
316 |
+
|
317 |
+
# Translate the story to Chinese
|
318 |
+
st.session_state.story_zh = translate_to_chinese(st.session_state.story)
|
319 |
+
|
320 |
+
# Display Chinese story
|
321 |
+
st.text("中文故事: " + st.session_state.story_zh)
|
322 |
+
else:
|
323 |
+
# Display saved results from session state
|
324 |
+
with st.container():
|
325 |
+
st.markdown("<h3><span class='stage-icon'>🔍</span> 圖片解讀中</h3>", unsafe_allow_html=True)
|
326 |
+
st.text("英文描述: " + st.session_state.scenario)
|
327 |
+
st.text("中文描述: " + st.session_state.scenario_zh)
|
328 |
+
|
329 |
+
with st.container():
|
330 |
+
st.markdown("<h3><span class='stage-icon'>📝</span> 故事創作中</h3>", unsafe_allow_html=True)
|
331 |
+
st.text("英文故事: " + st.session_state.story)
|
332 |
+
st.text("中文故事: " + st.session_state.story_zh)
|
333 |
|
334 |
# Stage 3: Story to Audio data
|
335 |
with st.container():
|
336 |
st.markdown("<h3><span class='stage-icon'>🔊</span> 故事準備朗讀中</h3>", unsafe_allow_html=True)
|
337 |
|
|
|
|
|
|
|
338 |
# Play button with Cantonese text
|
339 |
if st.button("🔊 播放故事"):
|
340 |
+
# Only generate audio if not already done
|
341 |
+
if not st.session_state.audio_generated:
|
342 |
+
st.session_state.audio_data = text2audio(st.session_state.story_zh, lang='zh')
|
343 |
+
st.session_state.audio_generated = True
|
344 |
+
|
345 |
+
# Play the audio
|
346 |
+
if st.session_state.audio_data:
|
347 |
+
st.audio(st.session_state.audio_data['audio'],
|
348 |
+
format="audio/wav",
|
349 |
+
start_time=0,
|
350 |
+
sample_rate=st.session_state.audio_data['sampling_rate'])
|
351 |
else:
|
352 |
st.error("哎呀!再試多次啦!")
|
353 |
|
354 |
+
# Cleanup: Remove the temporary file when the user is done
|
355 |
+
if os.path.exists(temp_file_path):
|
356 |
+
os.remove(temp_file_path)
|
357 |
else:
|
358 |
+
# Clear session state when no file is uploaded
|
359 |
+
st.session_state.scenario = None
|
360 |
+
st.session_state.scenario_zh = None
|
361 |
+
st.session_state.story = None
|
362 |
+
st.session_state.story_zh = None
|
363 |
+
st.session_state.audio_generated = False
|
364 |
+
st.session_state.audio_data = None
|
365 |
+
|
366 |
# Welcome message in Cantonese
|
367 |
st.markdown("""
|
368 |
<div class="welcome-message">
|