Update app.py
Browse files
app.py
CHANGED
@@ -6,14 +6,6 @@ from transformers import pipeline
|
|
6 |
from gtts import gTTS
|
7 |
import tempfile
|
8 |
|
9 |
-
# —––––––– Requirements —–––––––
|
10 |
-
# streamlit>=1.20
|
11 |
-
# pillow>=9.0
|
12 |
-
# torch>=2.0.0
|
13 |
-
# transformers>=4.30
|
14 |
-
# sentencepiece>=0.1.97
|
15 |
-
# gTTS>=2.3.1
|
16 |
-
|
17 |
# —––––––– Page Setup —–––––––
|
18 |
st.set_page_config(page_title="Magic Story Generator", layout="centered")
|
19 |
st.title("📖✨ Turn Images into Children's Stories")
|
@@ -27,16 +19,15 @@ def load_pipelines():
|
|
27 |
model="Salesforce/blip-image-captioning-base",
|
28 |
device=-1 # CPU; set to 0+ for GPU
|
29 |
)
|
30 |
-
# 2) Story-generation pipeline (
|
31 |
storyteller = pipeline(
|
32 |
task="text2text-generation",
|
33 |
-
model="
|
34 |
-
tokenizer="
|
35 |
-
device=-1,
|
36 |
temperature=0.7,
|
37 |
top_p=0.9,
|
38 |
repetition_penalty=1.2,
|
39 |
-
no_repeat_ngram_size=3,
|
40 |
max_new_tokens=150
|
41 |
)
|
42 |
return captioner, storyteller
|
@@ -46,7 +37,6 @@ captioner, storyteller = load_pipelines()
|
|
46 |
# —––––––– Main App —–––––––
|
47 |
uploaded = st.file_uploader("Upload an image:", type=["jpg", "png", "jpeg"])
|
48 |
if uploaded:
|
49 |
-
# Load and display the image
|
50 |
img = Image.open(uploaded).convert("RGB")
|
51 |
st.image(img, use_column_width=True)
|
52 |
|
@@ -60,10 +50,7 @@ if uploaded:
|
|
60 |
st.success(f"**Caption:** {caption}")
|
61 |
|
62 |
# Build prompt and generate story
|
63 |
-
prompt =
|
64 |
-
f"Image description: {caption}\n"
|
65 |
-
"Write a coherent, 50-100 word children’s story that flows naturally."
|
66 |
-
)
|
67 |
with st.spinner("📝 Writing story..."):
|
68 |
start = time.time()
|
69 |
out = storyteller(prompt)
|
@@ -93,3 +80,4 @@ if uploaded:
|
|
93 |
# Footer
|
94 |
st.markdown("---\n*Made with ❤️ by your friendly story wizard*")
|
95 |
|
|
|
|
6 |
from gtts import gTTS
|
7 |
import tempfile
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
# —––––––– Page Setup —–––––––
|
10 |
st.set_page_config(page_title="Magic Story Generator", layout="centered")
|
11 |
st.title("📖✨ Turn Images into Children's Stories")
|
|
|
19 |
model="Salesforce/blip-image-captioning-base",
|
20 |
device=-1 # CPU; set to 0+ for GPU
|
21 |
)
|
22 |
+
# 2) Story-generation pipeline (T5-base Story)
|
23 |
storyteller = pipeline(
|
24 |
task="text2text-generation",
|
25 |
+
model="mrm8488/t5-base-finetuned-story-generation",
|
26 |
+
tokenizer="mrm8488/t5-base-finetuned-story-generation",
|
27 |
+
device=-1,
|
28 |
temperature=0.7,
|
29 |
top_p=0.9,
|
30 |
repetition_penalty=1.2,
|
|
|
31 |
max_new_tokens=150
|
32 |
)
|
33 |
return captioner, storyteller
|
|
|
37 |
# —––––––– Main App —–––––––
|
38 |
uploaded = st.file_uploader("Upload an image:", type=["jpg", "png", "jpeg"])
|
39 |
if uploaded:
|
|
|
40 |
img = Image.open(uploaded).convert("RGB")
|
41 |
st.image(img, use_column_width=True)
|
42 |
|
|
|
50 |
st.success(f"**Caption:** {caption}")
|
51 |
|
52 |
# Build prompt and generate story
|
53 |
+
prompt = f"generate story: {caption}"
|
|
|
|
|
|
|
54 |
with st.spinner("📝 Writing story..."):
|
55 |
start = time.time()
|
56 |
out = storyteller(prompt)
|
|
|
80 |
# Footer
|
81 |
st.markdown("---\n*Made with ❤️ by your friendly story wizard*")
|
82 |
|
83 |
+
|