import streamlit as st from PIL import Image from transformers import pipeline from gtts import gTTS import torch st.set_page_config(page_title="Image to Audio Story", page_icon="🦜") def extract_image_caption(image_data): """ 利用预训练模型从图像中提取描述性文字。 """ img_obj = Image.open(image_data) caption_pipeline = pipeline( "image-to-text", model="Salesforce/blip-image-captioning-base", ) caption_results = caption_pipeline(img_obj) caption_text = caption_results[0]['generated_text'] return caption_text def compose_story_from_caption(caption_detail): """ 根据图像描述创作一篇充满创意的儿童故事。 """ story_pipeline = pipeline( "text-generation", model="Qwen/Qwen2-1.5B", ) prompt_text = ( "You are a creative children's story writer. Based on the following image details, " "please write an imaginative story for children aged 3-10. Do not simply rephrase the image details; " "instead, expand creatively by adding fun characters, adventures, and unexpected twists. " "The story must be at least 100 words long.\n\n" f"Image Details: {caption_detail}\n\nStory:" ) story_results = story_pipeline(prompt_text, max_length=300, num_return_sequences=1) story_text = story_results[0]['generated_text'] return story_text 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("