', unsafe_allow_html=True) st.title("🌄 AI-Powered Visual Storytelling") if api_key and uploaded_files: if 'analyzed_files' not in st.session_state: st.session_state.analyzed_files = {} files_to_remove = set(st.session_state.analyzed_files.keys()) - set(f.name for f in uploaded_files) for file_name in files_to_remove: del st.session_state.analyzed_files[file_name] for i in range(0, len(uploaded_files), 2): img_col1, img_col2 = st.columns(2) for j in range(2): if i + j < len(uploaded_files): with img_col1 if j == 0 else img_col2: uploaded_file = uploaded_files[i + j] image = Image.open(uploaded_file) resized_image = resize_image(image) buffered = io.BytesIO() resized_image.save(buffered, format="PNG") img_str = base64.b64encode(buffered.getvalue()).decode() st.markdown(f"""

{uploaded_file.name}

""", unsafe_allow_html=True) if uploaded_file.name not in st.session_state.analyzed_files: with st.spinner("分析圖片及生成語音中..."): try: description = analyze_image(image, api_key, model) if language == "繁體中文": with st.spinner("翻譯中..."): description = translate_to_chinese(description, api_key, model) st.session_state.analyzed_files[uploaded_file.name] = description time.sleep(1) except Exception as e: st.error(f"處理圖片時發生錯誤: {str(e)}") continue description = st.session_state.analyzed_files[uploaded_file.name] paragraphs = [p.strip() for p in description.split('\n') if p.strip()] if paragraphs: formatted_description = ''.join([f'

{p}

' for p in paragraphs]) st.markdown(f'
{formatted_description}
', unsafe_allow_html=True) tts = gTTS(text=description, lang='zh-tw' if language == "繁體中文" else 'en') audio_file = f"audio_{uuid.uuid4()}.mp3" tts.save(audio_file) st.audio(audio_file) os.remove(audio_file) else: st.warning("無法獲取圖片描述。") elif uploaded_files: st.warning("請輸入有效的 API Key 以分析圖片。") st.markdown('