1
File size: 2,699 Bytes
0dcd353
e508bdf
 
8367fb2
fd1d947
e508bdf
 
6949ffc
 
 
 
8367fb2
8087810
 
fd1d947
8367fb2
6949ffc
e508bdf
6949ffc
 
fd1d947
6949ffc
 
b3abd21
c876f7b
6949ffc
 
 
 
 
 
6adb177
fd1d947
 
6949ffc
e508bdf
6adb177
fd1d947
e508bdf
 
e5b9c42
b3abd21
8087810
e508bdf
8087810
6949ffc
e508bdf
 
c876f7b
6949ffc
 
 
 
 
 
8087810
6949ffc
 
 
 
 
 
 
8087810
6949ffc
8087810
 
6949ffc
 
c876f7b
1573779
e508bdf
6adb177
b3abd21
c876f7b
e508bdf
 
6adb177
c876f7b
 
 
e508bdf
c876f7b
2aae3c9
e508bdf
e616e4e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import time
import streamlit as st
from PIL import Image
from transformers import pipeline
from gtts import gTTS
import tempfile
from llama_cpp import Llama

# First install required package:
# pip install llama-cpp-python

# —––––––– Page Setup —–––––––
st.set_page_config(page_title="Magic Story Generator", layout="centered")
st.title("📖✨ Turn Images into Children's Stories")

# —––––––– Load Models (cached) —–––––––
@st.cache_resource(show_spinner=False)
def load_models():
    # 1) Image captioning model
    captioner = pipeline(
        "image-to-text",
        model="Salesforce/blip-image-captioning-base"
    )
    
    # 2) GGUF Story Model
    storyteller = Llama(
        model_path="DavidAU/L3-Grand-Story-Darkness-MOE-4X8-24.9B-e32-GGUF",
        n_ctx=2048,
        n_threads=4,
        n_gpu_layers=0  # Set based on your GPU capacity
    )
    return captioner, storyteller

captioner, storyteller = load_models()

# —––––––– Main App —–––––––
uploaded = st.file_uploader("Upload an image:", type=["jpg", "png", "jpeg"])
if uploaded:
    img = Image.open(uploaded).convert("RGB")
    st.image(img, use_column_width=True)

    # Generate caption
    with st.spinner("🔍 Generating caption..."):
        cap = captioner(img)
        caption = cap[0]['generated_text']
    st.success(f"**Caption:** {caption}")

    # Generate story
    prompt = f"""Below is an image description. Write a children's story based on it.
    
    Image Description: {caption}
    Story:"""
    
    with st.spinner("📝 Crafting magical story..."):
        start = time.time()
        output = storyteller(
            prompt=prompt,
            max_tokens=500,
            temperature=0.7,
            top_p=0.9,
            repeat_penalty=1.1
        )
        gen_time = time.time() - start
        story = output['choices'][0]['text'].strip()
        st.text(f"⏱ Generated in {gen_time:.1f}s")

    # Post-process story
    story = story.split("###")[0].strip()  # Remove any trailing artifacts
    
    # Display story
    st.subheader("📚 Your Magical Story")
    st.write(story)

    # Audio conversion
    with st.spinner("🔊 Converting to audio..."):
        try:
            tts = gTTS(text=story, lang="en", slow=False)
            with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
                tts.save(tmp.name)
                st.audio(tmp.name, format="audio/mp3")
        except Exception as e:
            st.warning(f"⚠️ Audio conversion failed: {str(e)}")

# Footer
st.markdown("---\n*Made with ❤️ by your friendly story wizard*")