Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,130 +1,73 @@
|
|
| 1 |
-
# Imports - just the essentials
|
| 2 |
import streamlit as st
|
| 3 |
from transformers import pipeline
|
| 4 |
from PIL import Image
|
| 5 |
-
import
|
| 6 |
import tempfile
|
| 7 |
from gtts import gTTS
|
| 8 |
|
| 9 |
-
#
|
| 10 |
@st.cache_resource
|
| 11 |
def load_models():
|
| 12 |
-
|
| 13 |
-
models = {
|
| 14 |
"image_captioner": pipeline("image-to-text", model="sooh-j/blip-image-captioning-base"),
|
| 15 |
"story_generator": pipeline("text-generation", model="Qwen/Qwen2.5-1.5B-Instruct")
|
| 16 |
}
|
| 17 |
-
return models
|
| 18 |
|
| 19 |
-
# Convert
|
| 20 |
-
def get_image_bytes(pil_img):
|
| 21 |
-
"""Convert PIL image to bytes for hashing"""
|
| 22 |
-
import io
|
| 23 |
-
buf = io.BytesIO()
|
| 24 |
-
pil_img.save(buf, format='JPEG')
|
| 25 |
-
return buf.getvalue()
|
| 26 |
-
|
| 27 |
-
# Simple image-to-text function using cached model
|
| 28 |
@st.cache_data
|
| 29 |
-
def img2text(image_bytes,
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
pil_img = Image.open(io.BytesIO(image_bytes))
|
| 33 |
-
result = _models["image_captioner"](pil_img)
|
| 34 |
-
return result[0]["generated_text"]
|
| 35 |
|
|
|
|
| 36 |
@st.cache_data
|
| 37 |
-
def text2story(caption,
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
Returns:
|
| 45 |
-
A generated story that expands on the image caption
|
| 46 |
-
"""
|
| 47 |
-
story_generator = _models["story_generator"]
|
| 48 |
-
|
| 49 |
-
# Format prompt to ensure the story expands on the image caption
|
| 50 |
-
prompt = f"""<|system|>
|
| 51 |
-
You are a creative short story writer. Write a brief, engaging story that expands on the given image caption.
|
| 52 |
-
The story should be under 100 words and have a natural beginning, middle, and end.
|
| 53 |
-
<|user|>
|
| 54 |
-
Image caption: "{caption}"
|
| 55 |
-
Create a short story that expands on this image caption and brings it to life.
|
| 56 |
-
<|assistant|>"""
|
| 57 |
-
|
| 58 |
-
# Generate story with parameters tuned for brevity and coherence
|
| 59 |
-
response = story_generator(
|
| 60 |
-
prompt,
|
| 61 |
-
max_new_tokens=120, # Allow enough tokens for a complete story
|
| 62 |
-
do_sample=True,
|
| 63 |
-
temperature=0.7, # Balanced creativity
|
| 64 |
-
top_p=0.9, # Focus on more likely tokens
|
| 65 |
-
repetition_penalty=1.2, # Avoid repetitive patterns
|
| 66 |
-
eos_token_id=story_generator.tokenizer.eos_token_id
|
| 67 |
)
|
| 68 |
-
|
| 69 |
-
# Extract just the generated story text
|
| 70 |
-
raw_story = response[0]['generated_text']
|
| 71 |
-
|
| 72 |
-
# Parse out just the assistant's response from the conversation format
|
| 73 |
-
story_text = raw_story.split("<|assistant|>")[-1].strip()
|
| 74 |
-
|
| 75 |
-
return story_text
|
| 76 |
|
| 77 |
-
#
|
| 78 |
@st.cache_data
|
| 79 |
-
def text2audio(
|
| 80 |
-
"""Convert text to audio"""
|
| 81 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
|
| 82 |
-
|
| 83 |
-
temp_file.
|
| 84 |
-
|
| 85 |
-
tts = gTTS(text=story_text, lang='en')
|
| 86 |
-
tts.save(temp_filename)
|
| 87 |
-
|
| 88 |
-
with open(temp_filename, 'rb') as audio_file:
|
| 89 |
audio_bytes = audio_file.read()
|
| 90 |
-
|
| 91 |
-
os.unlink(temp_filename)
|
| 92 |
return audio_bytes
|
| 93 |
|
| 94 |
-
# Load models
|
| 95 |
models = load_models()
|
| 96 |
-
st.write("✅ Models loaded and cached!")
|
| 97 |
|
| 98 |
-
# Streamlit
|
| 99 |
-
st.title("Image to Audio Story")
|
| 100 |
|
| 101 |
-
|
| 102 |
-
uploaded_file = st.file_uploader("Upload an image")
|
| 103 |
|
| 104 |
-
if uploaded_file
|
| 105 |
-
#
|
| 106 |
image = Image.open(uploaded_file)
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
| 115 |
caption = img2text(image_bytes, models)
|
| 116 |
-
st.write(f"**Caption:** {caption}")
|
| 117 |
-
|
| 118 |
-
# Generate story that expands on the caption
|
| 119 |
story = text2story(caption, models)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
st.session_state.audio = text2audio(story)
|
| 127 |
-
|
| 128 |
# Play audio button
|
| 129 |
-
if st.button("Play
|
| 130 |
-
st.audio(
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
import tempfile
|
| 6 |
from gtts import gTTS
|
| 7 |
|
| 8 |
+
# Cache models for faster execution
|
| 9 |
@st.cache_resource
|
| 10 |
def load_models():
|
| 11 |
+
return {
|
|
|
|
| 12 |
"image_captioner": pipeline("image-to-text", model="sooh-j/blip-image-captioning-base"),
|
| 13 |
"story_generator": pipeline("text-generation", model="Qwen/Qwen2.5-1.5B-Instruct")
|
| 14 |
}
|
|
|
|
| 15 |
|
| 16 |
+
# Convert image to text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
@st.cache_data
|
| 18 |
+
def img2text(image_bytes, models):
|
| 19 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 20 |
+
return models["image_captioner"](image)[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Generate a short story based on the caption
|
| 23 |
@st.cache_data
|
| 24 |
+
def text2story(caption, models):
|
| 25 |
+
response = models["story_generator"](
|
| 26 |
+
f"Expand this caption into a short, engaging story under 100 words:\n\n{caption}",
|
| 27 |
+
max_new_tokens=100,
|
| 28 |
+
temperature=0.7,
|
| 29 |
+
top_p=0.9,
|
| 30 |
+
repetition_penalty=1.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
+
return response[0]['generated_text'].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Convert text to audio
|
| 35 |
@st.cache_data
|
| 36 |
+
def text2audio(text):
|
|
|
|
| 37 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
|
| 38 |
+
gTTS(text=text, lang='en').save(temp_file.name)
|
| 39 |
+
with open(temp_file.name, 'rb') as audio_file:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
audio_bytes = audio_file.read()
|
|
|
|
|
|
|
| 41 |
return audio_bytes
|
| 42 |
|
| 43 |
+
# Load models once
|
| 44 |
models = load_models()
|
|
|
|
| 45 |
|
| 46 |
+
# Streamlit UI
|
| 47 |
+
st.title("📷 Image to Audio Story")
|
| 48 |
|
| 49 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
|
|
|
| 50 |
|
| 51 |
+
if uploaded_file:
|
| 52 |
+
# Resize and display image
|
| 53 |
image = Image.open(uploaded_file)
|
| 54 |
+
image.thumbnail((400, 400)) # Resize to max 400x400 pixels
|
| 55 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 56 |
+
|
| 57 |
+
# Convert image to bytes
|
| 58 |
+
image_bytes = io.BytesIO()
|
| 59 |
+
image.save(image_bytes, format="JPEG")
|
| 60 |
+
image_bytes = image_bytes.getvalue()
|
| 61 |
+
|
| 62 |
+
with st.spinner("Generating story..."):
|
| 63 |
caption = img2text(image_bytes, models)
|
|
|
|
|
|
|
|
|
|
| 64 |
story = text2story(caption, models)
|
| 65 |
+
audio = text2audio(story)
|
| 66 |
+
|
| 67 |
+
# Display results
|
| 68 |
+
st.write(f"**Caption:** {caption}")
|
| 69 |
+
st.write(f"**Story:** {story}")
|
| 70 |
+
|
|
|
|
|
|
|
| 71 |
# Play audio button
|
| 72 |
+
if st.button("🔊 Play Audio"):
|
| 73 |
+
st.audio(audio, format="audio/mp3")
|