import streamlit as st from PIL import Image from transformers import pipeline from gtts import gTTS st.set_page_config(page_title="Image to Audio Story", page_icon="🦜") caption_pipeline = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") story_pipeline = pipeline("text-generation", model="Qwen/Qwen2.5-0.5B-Instruct") def extract_image_caption(image_data): img_obj = Image.open(image_data) caption_results = caption_pipeline(img_obj) return caption_results[0]['generated_text'] def compose_story_from_caption(caption_detail): prompt_text = ( "You are a talented and imaginative storyteller for children aged 3 to 10. " "Using the details derived from the image below, craft a captivating tale that includes three main characters, " "an adventurous journey, and delightful surprises. " "Your story should be vivid, original, and between 90 and 100 words in length.\n\n" f"Image Details: {caption_detail}\n\nStory:" ) # 生成故事,设置最大长度为150,以确保有足够空间生成90-100词的内容 story_results = story_pipeline(prompt_text, num_return_sequences=1, max_length=150) story_text = story_results[0]['generated_text'] # 提取故事文本 if "Story:" in story_text: return story_text.split("Story:", 1)[1].strip() return story_text.strip() def convert_text_to_audio(text_content, audio_path="output.mp3"): """ 将文本转换为音频文件。 """ tts_engine = gTTS(text=text_content, lang="en") tts_engine.save(audio_path) return audio_path def run_app(): st.markdown("