Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from huggingface_hub import InferenceApi
|
|
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 |
|
@@ -19,6 +20,7 @@ def load_clients():
|
|
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.
|
@@ -34,6 +36,7 @@ def generate_caption(img):
|
|
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")
|
@@ -44,19 +47,21 @@ def process_image(uploaded_file):
|
|
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 |
-
|
|
|
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
|
60 |
story_prompt = (
|
61 |
f"Image description: {caption}\n\n"
|
62 |
"Write a 50-100 word children's story that:\n"
|
@@ -72,22 +77,19 @@ if uploaded:
|
|
72 |
try:
|
73 |
story_response = story_client(
|
74 |
story_prompt,
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
81 |
)
|
82 |
-
|
83 |
-
# Process response
|
84 |
-
full_text = story_response[0]['generated_text']
|
85 |
story = full_text.split("Story:")[-1].strip()
|
86 |
-
|
87 |
-
# Ensure clean ending
|
88 |
if "." in story:
|
89 |
story = story.rsplit(".", 1)[0] + "."
|
90 |
-
|
91 |
except Exception as e:
|
92 |
st.error(f"🚨 Story magic failed: {str(e)}")
|
93 |
st.stop()
|
@@ -106,4 +108,6 @@ if uploaded:
|
|
106 |
except Exception as e:
|
107 |
st.warning("⚠️ Couldn't make audio version: " + str(e))
|
108 |
|
|
|
109 |
st.markdown("---\n*Made with ❤️ by your friendly story wizard*")
|
|
|
|
5 |
from gtts import gTTS
|
6 |
import tempfile
|
7 |
|
8 |
+
# —––––––– Page Config —–––––––
|
9 |
st.set_page_config(page_title="Magic Story Generator", layout="centered")
|
10 |
st.title("📖✨ Turn Images into Children's Stories")
|
11 |
|
|
|
20 |
|
21 |
caption_client, story_client = load_clients()
|
22 |
|
23 |
+
# —––––––– Helper: Generate Caption —–––––––
|
24 |
def generate_caption(img):
|
25 |
"""
|
26 |
Runs the BLIP caption model on a PIL.Image and returns the generated text.
|
|
|
36 |
st.error(f"Caption generation error: {e}")
|
37 |
return ""
|
38 |
|
39 |
+
# —––––––– Helper: Process Image —–––––––
|
40 |
def process_image(uploaded_file):
|
41 |
try:
|
42 |
img = Image.open(uploaded_file).convert("RGB")
|
|
|
47 |
st.error(f"Image processing error: {e}")
|
48 |
st.stop()
|
49 |
|
50 |
+
# —––––––– Main App Flow —–––––––
|
51 |
uploaded = st.file_uploader("Upload an image:", type=["jpg", "png", "jpeg"])
|
52 |
if uploaded:
|
53 |
img = process_image(uploaded)
|
54 |
st.image(img, use_container_width=True)
|
55 |
|
56 |
+
# Generate Caption
|
57 |
+
with st.spinner("🔍 Discovering image secrets..."):
|
58 |
caption = generate_caption(img)
|
59 |
if not caption:
|
60 |
st.error("😢 Couldn't understand this image. Try another one!")
|
61 |
st.stop()
|
62 |
st.success(f"**Caption:** {caption}")
|
63 |
|
64 |
+
# Prepare Story Prompt
|
65 |
story_prompt = (
|
66 |
f"Image description: {caption}\n\n"
|
67 |
"Write a 50-100 word children's story that:\n"
|
|
|
77 |
try:
|
78 |
story_response = story_client(
|
79 |
story_prompt,
|
80 |
+
parameters={
|
81 |
+
"max_new_tokens": 200,
|
82 |
+
"temperature": 0.8,
|
83 |
+
"top_p": 0.95,
|
84 |
+
"repetition_penalty": 1.15,
|
85 |
+
"do_sample": True,
|
86 |
+
"no_repeat_ngram_size": 2
|
87 |
+
}
|
88 |
)
|
89 |
+
full_text = story_response[0].get("generated_text", "")
|
|
|
|
|
90 |
story = full_text.split("Story:")[-1].strip()
|
|
|
|
|
91 |
if "." in story:
|
92 |
story = story.rsplit(".", 1)[0] + "."
|
|
|
93 |
except Exception as e:
|
94 |
st.error(f"🚨 Story magic failed: {str(e)}")
|
95 |
st.stop()
|
|
|
108 |
except Exception as e:
|
109 |
st.warning("⚠️ Couldn't make audio version: " + str(e))
|
110 |
|
111 |
+
# Footer
|
112 |
st.markdown("---\n*Made with ❤️ by your friendly story wizard*")
|
113 |
+
|