Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
-
|
| 4 |
-
from gtts import gTTS
|
| 5 |
-
import os
|
| 6 |
-
import tempfile
|
| 7 |
|
| 8 |
-
#
|
| 9 |
@st.cache_resource
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
if
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import io
|
| 3 |
+
import wave
|
| 4 |
import streamlit as st
|
| 5 |
+
from transformers import pipeline
|
| 6 |
from PIL import Image
|
| 7 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# βββ 1) MODEL LOADING (cached) ββββββββββββββββ
|
| 10 |
@st.cache_resource
|
| 11 |
+
def get_image_captioner(model_name="Salesforce/blip-image-captioning-base"):
|
| 12 |
+
return pipeline("image-to-text", model=model_name, device="cpu")
|
| 13 |
+
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def get_story_pipe(model_name="google/flan-t5-base"):
|
| 16 |
+
return pipeline("text2text-generation", model=model_name, device="cpu")
|
| 17 |
+
|
| 18 |
+
@st.cache_resource
|
| 19 |
+
def get_tts_pipe(model_name="facebook/mms-tts-eng"):
|
| 20 |
+
return pipeline("text-to-speech", model=model_name, device="cpu")
|
| 21 |
+
|
| 22 |
+
# βββ 2) TRANSFORM FUNCTIONS ββββββββββββββββ
|
| 23 |
+
def part1_image_to_text(pil_img, captioner):
|
| 24 |
+
results = captioner(pil_img)
|
| 25 |
+
return results[0].get("generated_text", "") if results else ""
|
| 26 |
+
|
| 27 |
+
def part2_text_to_story(
|
| 28 |
+
caption: str,
|
| 29 |
+
story_pipe,
|
| 30 |
+
target_words: int = 100,
|
| 31 |
+
max_length: int = 100,
|
| 32 |
+
min_length: int = 80,
|
| 33 |
+
do_sample: bool = True,
|
| 34 |
+
top_k: int = 100,
|
| 35 |
+
top_p: float= 0.9,
|
| 36 |
+
temperature: float= 0.7,
|
| 37 |
+
repetition_penalty: float = 1.1,
|
| 38 |
+
no_repeat_ngram_size: int = 4
|
| 39 |
+
) -> str:
|
| 40 |
+
prompt = (
|
| 41 |
+
f"Write a vivid, imaginative short story of about {target_words} words "
|
| 42 |
+
f"describing this scene: {caption}"
|
| 43 |
+
)
|
| 44 |
+
out = story_pipe(
|
| 45 |
+
prompt,
|
| 46 |
+
max_length=max_length,
|
| 47 |
+
min_length=min_length,
|
| 48 |
+
do_sample=do_sample,
|
| 49 |
+
top_k=top_k,
|
| 50 |
+
top_p=top_p,
|
| 51 |
+
temperature=temperature,
|
| 52 |
+
repetition_penalty=repetition_penalty,
|
| 53 |
+
no_repeat_ngram_size=no_repeat_ngram_size,
|
| 54 |
+
early_stopping=False
|
| 55 |
+
)
|
| 56 |
+
raw = out[0].get("generated_text", "").strip()
|
| 57 |
+
if not raw:
|
| 58 |
+
return ""
|
| 59 |
+
# strip echo of prompt
|
| 60 |
+
if raw.lower().startswith(prompt.lower()):
|
| 61 |
+
story = raw[len(prompt):].strip()
|
| 62 |
+
else:
|
| 63 |
+
story = raw
|
| 64 |
+
# cut at last full stop
|
| 65 |
+
idx = story.rfind(".")
|
| 66 |
+
if idx != -1:
|
| 67 |
+
story = story[:idx+1]
|
| 68 |
+
return story
|
| 69 |
+
|
| 70 |
+
def part3_text_to_speech_bytes(text: str, tts_pipe) -> bytes:
|
| 71 |
+
out = tts_pipe(text)
|
| 72 |
+
if isinstance(out, list):
|
| 73 |
+
out = out[0]
|
| 74 |
+
audio_array = out["audio"] # np.ndarray (channels, samples)
|
| 75 |
+
rate = out["sampling_rate"] # int
|
| 76 |
+
data = audio_array.T if audio_array.ndim == 2 else audio_array
|
| 77 |
+
pcm = (data * 32767).astype(np.int16)
|
| 78 |
+
|
| 79 |
+
buffer = io.BytesIO()
|
| 80 |
+
wf = wave.open(buffer, "wb")
|
| 81 |
+
channels = 1 if data.ndim == 1 else data.shape[1]
|
| 82 |
+
wf.setnchannels(channels)
|
| 83 |
+
wf.setsampwidth(2)
|
| 84 |
+
wf.setframerate(rate)
|
| 85 |
+
wf.writeframes(pcm.tobytes())
|
| 86 |
+
wf.close()
|
| 87 |
+
buffer.seek(0)
|
| 88 |
+
return buffer.read()
|
| 89 |
+
|
| 90 |
+
# βββ 3) STREAMLIT UI ββββββββββββββββββββββββββββ
|
| 91 |
+
st.set_page_config(
|
| 92 |
+
page_title="ImageβStoryβSpeech",
|
| 93 |
+
page_icon="πΌοΈπ€",
|
| 94 |
+
layout="centered"
|
| 95 |
+
)
|
| 96 |
+
st.title("πΌοΈ β‘οΈ π β‘οΈ ποΈ Image β Story β Speech")
|
| 97 |
+
|
| 98 |
+
uploaded = st.file_uploader("1οΈβ£ Upload an image", type=["jpg","jpeg","png"])
|
| 99 |
+
if not uploaded:
|
| 100 |
+
st.info("Please upload an image to begin.")
|
| 101 |
+
st.stop()
|
| 102 |
+
|
| 103 |
+
# Show image
|
| 104 |
+
with st.spinner("Rendering image..."):
|
| 105 |
+
pil_img = Image.open(uploaded)
|
| 106 |
+
st.image(pil_img, use_container_width=True)
|
| 107 |
+
|
| 108 |
+
# Generate caption
|
| 109 |
+
captioner = get_image_captioner()
|
| 110 |
+
with st.spinner("Generating caption..."):
|
| 111 |
+
caption = part1_image_to_text(pil_img, captioner)
|
| 112 |
+
st.markdown(f"**Caption:** {caption}")
|
| 113 |
+
|
| 114 |
+
# Generate story & play audio
|
| 115 |
+
if st.button("π Generate Story & Play Audio"):
|
| 116 |
+
# Story
|
| 117 |
+
story_pipe = get_story_pipe()
|
| 118 |
+
with st.spinner("Generating story..."):
|
| 119 |
+
story = part2_text_to_story(caption, story_pipe)
|
| 120 |
+
st.markdown("**Story:**")
|
| 121 |
+
st.write(story)
|
| 122 |
+
|
| 123 |
+
# TTS
|
| 124 |
+
tts_pipe = get_tts_pipe()
|
| 125 |
+
with st.spinner("Synthesizing speech..."):
|
| 126 |
+
audio_bytes = part3_text_to_speech_bytes(story, tts_pipe)
|
| 127 |
+
st.audio(audio_bytes, format="audio/wav")
|
| 128 |
+
st.success("All done!")
|