Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,86 @@
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
-
import
|
|
|
|
| 4 |
import pyttsx3
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
st.set_page_config(page_title="Image Storytelling App", layout="centered")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
st.title("🧒 Children's Image Storytelling App")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
st.subheader("Image Description")
|
| 22 |
-
description = "Children playing in the park" # You can use BLIP or similar models here
|
| 23 |
-
st.success(f"Caption: {description}")
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
| 29 |
prompt = (
|
| 30 |
-
f"Write a
|
| 31 |
-
|
| 32 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
story = result[0]['generated_text']
|
| 39 |
-
st.subheader("Generated Story")
|
| 40 |
-
st.write(story)
|
| 41 |
-
except Exception as e:
|
| 42 |
-
st.error(f"Story generation
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import streamlit as st
|
| 4 |
from PIL import Image
|
| 5 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration, AutoTokenizer, AutoModelForCausalLM
|
| 6 |
+
import torch
|
| 7 |
import pyttsx3
|
| 8 |
+
import io
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# ----------- Stage 1: Image to Description -----------
|
|
|
|
| 11 |
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_caption_model():
|
| 14 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 15 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 16 |
+
return processor, model
|
| 17 |
|
| 18 |
+
def generate_caption(image):
|
| 19 |
+
processor, model = load_caption_model()
|
| 20 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 21 |
+
out = model.generate(**inputs)
|
| 22 |
+
return processor.decode(out[0], skip_special_tokens=True)
|
| 23 |
|
| 24 |
+
# ----------- Stage 2: Description to Story -----------
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
@st.cache_resource
|
| 27 |
+
def load_story_model():
|
| 28 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-1_5")
|
| 29 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/phi-1_5")
|
| 30 |
+
return tokenizer, model
|
| 31 |
|
| 32 |
+
def generate_story(description):
|
| 33 |
+
tokenizer, model = load_story_model()
|
| 34 |
prompt = (
|
| 35 |
+
f"Write a short and fun story (50-100 words) for children based on the following: {description}\n\n"
|
| 36 |
+
"Story:"
|
| 37 |
)
|
| 38 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
| 39 |
+
output = model.generate(**inputs, max_new_tokens=120, do_sample=True, top_k=50, top_p=0.95)
|
| 40 |
+
story = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 41 |
+
return story.split("Story:")[-1].strip()
|
| 42 |
+
|
| 43 |
+
# ----------- Stage 3: Story to Speech -----------
|
| 44 |
+
|
| 45 |
+
def generate_speech(story):
|
| 46 |
+
engine = pyttsx3.init()
|
| 47 |
+
engine.setProperty('rate', 150)
|
| 48 |
+
engine.setProperty('volume', 0.9)
|
| 49 |
+
|
| 50 |
+
with io.BytesIO() as audio:
|
| 51 |
+
engine.save_to_file(story, 'temp.mp3')
|
| 52 |
+
engine.runAndWait()
|
| 53 |
+
with open('temp.mp3', 'rb') as f:
|
| 54 |
+
audio_bytes = f.read()
|
| 55 |
+
return audio_bytes
|
| 56 |
+
|
| 57 |
+
# ----------- Streamlit Interface -----------
|
| 58 |
+
|
| 59 |
+
st.set_page_config(page_title="Children's Story Generator", layout="centered")
|
| 60 |
+
|
| 61 |
+
st.title("📖 Children's Storytelling from Images")
|
| 62 |
+
st.markdown("Upload an illustration and we'll turn it into a fun story with voice narration!")
|
| 63 |
+
|
| 64 |
+
uploaded_image = st.file_uploader("Upload a drawing or illustration", type=["jpg", "jpeg", "png"])
|
| 65 |
+
|
| 66 |
+
if uploaded_image:
|
| 67 |
+
image = Image.open(uploaded_image)
|
| 68 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 69 |
+
|
| 70 |
+
# Stage 1
|
| 71 |
+
with st.spinner("Generating description..."):
|
| 72 |
+
description = generate_caption(image)
|
| 73 |
+
st.success("✅ Description Generated!")
|
| 74 |
+
st.markdown(f"**Image Caption:** _{description}_")
|
| 75 |
+
|
| 76 |
+
# Stage 2
|
| 77 |
+
with st.spinner("Generating children's story..."):
|
| 78 |
+
story = generate_story(description)
|
| 79 |
+
st.success("✅ Story Generated!")
|
| 80 |
+
st.markdown("**Generated Story:**")
|
| 81 |
+
st.write(story)
|
| 82 |
|
| 83 |
+
# Stage 3
|
| 84 |
+
with st.spinner("Generating voice..."):
|
| 85 |
+
audio_data = generate_speech(story)
|
| 86 |
+
st.audio(audio_data, format='audio/mp3')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|