|
|
|
|
|
import streamlit as st |
|
|
from transformers import pipeline |
|
|
from gtts import gTTS |
|
|
import tempfile |
|
|
import os |
|
|
|
|
|
|
|
|
|
|
|
def img2text(img_path): |
|
|
|
|
|
captioner = pipeline( |
|
|
"image-to-text", |
|
|
model="nlpconnect/vit-gpt2-image-captioning" |
|
|
) |
|
|
result = captioner(img_path) |
|
|
return result[0]["generated_text"] |
|
|
|
|
|
|
|
|
def text2story(scenario): |
|
|
|
|
|
generator = pipeline( |
|
|
"text-generation", |
|
|
model="gpt2", |
|
|
max_length=200, |
|
|
num_return_sequences=1 |
|
|
) |
|
|
prompt = f"Create a children's story based on: {scenario}" |
|
|
story = generator(prompt)[0]["generated_text"] |
|
|
return story |
|
|
|
|
|
|
|
|
def text2audio(story_text): |
|
|
|
|
|
tts = gTTS(text=story_text, lang="en") |
|
|
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") |
|
|
tts.save(audio_file.name) |
|
|
return audio_file.name |
|
|
|
|
|
def main(): |
|
|
st.set_page_config( |
|
|
page_title="Image to Story", |
|
|
page_icon="๐" |
|
|
) |
|
|
st.header("Upload Your Image") |
|
|
|
|
|
uploaded_file = st.file_uploader( |
|
|
"Choose Image", |
|
|
type=["jpg", "png", "jpeg"] |
|
|
) |
|
|
|
|
|
if uploaded_file: |
|
|
temp_img = os.path.join(tempfile.gettempdir(), uploaded_file.name) |
|
|
with open(temp_img, "wb") as f: |
|
|
f.write(uploaded_file.getvalue()) |
|
|
|
|
|
st.image(uploaded_file) |
|
|
|
|
|
|
|
|
with st.status("๐ผ๏ธ Processing image..."): |
|
|
scenario = img2text(temp_img) |
|
|
st.write("Image Caption:", scenario) |
|
|
|
|
|
|
|
|
with st.status("๐ Generating story..."): |
|
|
story = text2story(scenario) |
|
|
st.subheader("Story") |
|
|
st.write(story) |
|
|
|
|
|
|
|
|
with st.status("๐ Converting audio..."): |
|
|
audio_path = text2audio(story) |
|
|
|
|
|
if st.button("โถ๏ธ Play Audio Story"): |
|
|
st.audio(audio_path, format="audio/mp3") |
|
|
|
|
|
|
|
|
os.unlink(temp_img) |
|
|
os.unlink(audio_path) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |