Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
# app.py
|
2 |
import streamlit as st
|
3 |
from PIL import Image
|
4 |
from io import BytesIO
|
@@ -6,11 +5,10 @@ from huggingface_hub import InferenceApi
|
|
6 |
from gtts import gTTS
|
7 |
import tempfile
|
8 |
|
9 |
-
# —––––––– Page Config
|
10 |
st.set_page_config(page_title="Magic Story Generator", layout="centered")
|
11 |
st.title("📖✨ Turn Images into Children's Stories")
|
12 |
|
13 |
-
# —––––––– Clients (cached)
|
14 |
@st.cache_resource
|
15 |
def load_clients():
|
16 |
hf_token = st.secrets["HF_TOKEN"]
|
@@ -21,52 +19,42 @@ def load_clients():
|
|
21 |
|
22 |
caption_client, story_client = load_clients()
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
def process_image(uploaded_file):
|
26 |
try:
|
27 |
img = Image.open(uploaded_file).convert("RGB")
|
28 |
-
|
29 |
-
# Only resize if absolutely necessary
|
30 |
-
if img.size[0] > 2048 or img.size[1] > 2048:
|
31 |
img.thumbnail((2048, 2048))
|
32 |
-
|
33 |
return img
|
34 |
except Exception as e:
|
35 |
-
st.error(f"Image processing error: {
|
36 |
st.stop()
|
37 |
-
|
38 |
uploaded = st.file_uploader("Upload an image:", type=["jpg", "png", "jpeg"])
|
39 |
if uploaded:
|
40 |
img = process_image(uploaded)
|
41 |
-
st.image(img,
|
42 |
-
|
43 |
with st.spinner("Generating caption..."):
|
44 |
caption = generate_caption(img)
|
45 |
-
|
46 |
-
# Your original validation
|
47 |
if not caption:
|
48 |
st.error("😢 Couldn't understand this image. Try another one!")
|
49 |
st.stop()
|
50 |
-
|
51 |
st.success(f"**Caption:** {caption}")
|
52 |
-
|
53 |
-
|
54 |
-
# Generate Caption
|
55 |
-
with st.spinner("🔍 Discovering image secrets..."):
|
56 |
-
try:
|
57 |
-
img_bytes = BytesIO()
|
58 |
-
img.save(img_bytes, format="JPEG")
|
59 |
-
caption_response = caption_client(data=img_bytes.getvalue())
|
60 |
-
caption = caption_response[0]['generated_text'].strip() if isinstance(caption_response, list) else ""
|
61 |
-
|
62 |
-
if not caption:
|
63 |
-
st.error("😢 Couldn't understand this image. Try another one!")
|
64 |
-
st.stop()
|
65 |
-
except Exception as e:
|
66 |
-
st.error(f"🚨 Oops! Problem making caption: {str(e)}")
|
67 |
-
st.stop()
|
68 |
-
|
69 |
-
st.success(f"**Caption Magic:** {caption}")
|
70 |
|
71 |
# Story Generation Prompt
|
72 |
story_prompt = (
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
from io import BytesIO
|
|
|
5 |
from gtts import gTTS
|
6 |
import tempfile
|
7 |
|
|
|
8 |
st.set_page_config(page_title="Magic Story Generator", layout="centered")
|
9 |
st.title("📖✨ Turn Images into Children's Stories")
|
10 |
|
11 |
+
# —––––––– Clients (cached) —–––––––
|
12 |
@st.cache_resource
|
13 |
def load_clients():
|
14 |
hf_token = st.secrets["HF_TOKEN"]
|
|
|
19 |
|
20 |
caption_client, story_client = load_clients()
|
21 |
|
22 |
+
def generate_caption(img):
|
23 |
+
"""
|
24 |
+
Runs the BLIP caption model on a PIL.Image and returns the generated text.
|
25 |
+
"""
|
26 |
+
img_bytes = BytesIO()
|
27 |
+
img.save(img_bytes, format="JPEG")
|
28 |
+
try:
|
29 |
+
result = caption_client(data=img_bytes.getvalue())
|
30 |
+
if isinstance(result, list) and result:
|
31 |
+
return result[0].get("generated_text", "").strip()
|
32 |
+
return ""
|
33 |
+
except Exception as e:
|
34 |
+
st.error(f"Caption generation error: {e}")
|
35 |
+
return ""
|
36 |
+
|
37 |
def process_image(uploaded_file):
|
38 |
try:
|
39 |
img = Image.open(uploaded_file).convert("RGB")
|
40 |
+
if max(img.size) > 2048:
|
|
|
|
|
41 |
img.thumbnail((2048, 2048))
|
|
|
42 |
return img
|
43 |
except Exception as e:
|
44 |
+
st.error(f"Image processing error: {e}")
|
45 |
st.stop()
|
46 |
+
|
47 |
uploaded = st.file_uploader("Upload an image:", type=["jpg", "png", "jpeg"])
|
48 |
if uploaded:
|
49 |
img = process_image(uploaded)
|
50 |
+
st.image(img, use_container_width=True)
|
51 |
+
|
52 |
with st.spinner("Generating caption..."):
|
53 |
caption = generate_caption(img)
|
|
|
|
|
54 |
if not caption:
|
55 |
st.error("😢 Couldn't understand this image. Try another one!")
|
56 |
st.stop()
|
|
|
57 |
st.success(f"**Caption:** {caption}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
# Story Generation Prompt
|
60 |
story_prompt = (
|