Spaces:
Sleeping
Sleeping
import streamlit as st | |
from PIL import Image | |
from transformers import pipeline | |
from gtts import gTTS | |
import os | |
os.system("pip install transformers==4.36.2") | |
# ๅ ่ผ Hugging Face ็ๆจกๅ | |
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") | |
story_generator = pipeline("text-generation", model="facebook/opt-1.3b") | |
# ๅ็ โ ๆๅญ๏ผ็ๆๆ่ฟฐ๏ผ | |
def img2text(image_path): | |
text = image_to_text_model(image_path)[0]["generated_text"] | |
return text | |
# ๆๅญ โ ๆ ไบ๏ผ็ๆๅฎๆดๆ ไบ๏ผ | |
def text2story(text): | |
prompt = f"Write a fun and magical children's story based on this idea: {text}.\n\nOnce upon a time..." | |
story = story_generator(prompt, max_length=250, do_sample=True, temperature=0.8, top_p=0.9, repetition_penalty=1.2)[0]['generated_text'] | |
return story | |
# ๆ ไบ โ ่ช้ณ๏ผTTS๏ผ | |
def text2audio_gtts(story_text, filename="story.mp3"): | |
# ้ฟๅ ๆไปถๅฒ็ช | |
if os.path.exists(filename): | |
os.remove(filename) | |
# ้ๅถ TTS ๆๆฌ้ทๅบฆ | |
max_chars = 500 # gTTS ๅฏ่ฝไธๆฏๆ้้ทๆๆฌ | |
story_text = story_text[:max_chars] | |
# ็ๆ่ฏญ้ณ | |
tts = gTTS(text=story_text, lang="en") | |
tts.save(filename) | |
return filename | |
# Streamlit Web UI | |
st.set_page_config(page_title="AI Storyteller", page_icon="๐") | |
st.header("๐ AI Storyteller: Turn Your Image into a Story with Audio") | |
uploaded_file = st.file_uploader("Upload an Image...", type=["jpg", "png"]) | |
if uploaded_file: | |
# ไฟๅญๅ็ๅฐๆฌๅฐ | |
image_path = "uploaded_image.jpg" | |
with open(image_path, "wb") as f: | |
f.write(uploaded_file.getbuffer()) | |
# ่ฎๅไธฆ้กฏ็คบๅ็ | |
image = Image.open(image_path) | |
st.image(image, caption="Uploaded Image", use_container_width=True) | |
# ็ๆๅ็ๆ่ฟฐ | |
st.text("๐ Generating image caption...") | |
caption = img2text(image_path) # ้่ฃๆนๆๆไปถ่ทฏๅพ | |
st.write("**Image Description:**", caption) | |
# ็ๆๆ ไบ | |
st.text("๐ Generating story...") | |
story = text2story(caption) | |
st.write("**Generated Story:**") | |
st.write(story) | |
# ็ๆ่ช้ณ | |
st.text("๐ Generating audio...") | |
audio_file = text2audio_gtts(story) | |
# ๆญๆพ้ณ้ ป | |
st.audio(audio_file, format="audio/mp3") | |
# ไธ่ผๆ้ฎ | |
with open(audio_file, "rb") as file: | |
st.download_button("๐ฅ Download Audio", file, file_name="story.mp3") | |