TLH01 commited on
Commit
e0d6a8d
·
verified ·
1 Parent(s): 8101211

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -30
app.py CHANGED
@@ -1,42 +1,86 @@
 
 
1
  import streamlit as st
2
  from PIL import Image
3
- import tempfile
 
4
  import pyttsx3
5
- from transformers import pipeline
6
-
7
- st.set_page_config(page_title="Image Storytelling App", layout="centered")
8
 
9
- # Title
10
- st.title("🧒 Children's Image Storytelling App")
11
 
12
- # Stage 1: Image Upload & Caption
13
- st.header("Step 1: Upload an Image")
14
- uploaded_file = st.file_uploader("Upload an illustration", type=["jpg", "jpeg", "png"])
 
 
15
 
16
- if uploaded_file:
17
- image = Image.open(uploaded_file)
18
- st.image(image, caption="Uploaded Image", use_column_width=True)
 
 
19
 
20
- # Generate image description (simulated for this example)
21
- st.subheader("Image Description")
22
- description = "Children playing in the park" # You can use BLIP or similar models here
23
- st.success(f"Caption: {description}")
24
 
25
- # Stage 2: Description to Story
26
- st.header("Step 2: Generate a Short Story")
27
- story_generator = pipeline("text-generation", model="tiiuae/falcon-7b-instruct", max_new_tokens=150)
 
 
28
 
 
 
29
  prompt = (
30
- f"Write a creative short story (50-100 words) for children (ages 3–10) "
31
- f"based on this image description: '{description}'. Make it imaginative, safe, and fun."
32
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- if st.button("Generate Story"):
35
- with st.spinner("Generating story..."):
36
- try:
37
- result = story_generator(prompt)
38
- story = result[0]['generated_text']
39
- st.subheader("Generated Story")
40
- st.write(story)
41
- except Exception as e:
42
- st.error(f"Story generation
 
1
+ # app.py
2
+
3
  import streamlit as st
4
  from PIL import Image
5
+ from transformers import BlipProcessor, BlipForConditionalGeneration, AutoTokenizer, AutoModelForCausalLM
6
+ import torch
7
  import pyttsx3
8
+ import io
 
 
9
 
10
+ # ----------- Stage 1: Image to Description -----------
 
11
 
12
+ @st.cache_resource
13
+ def load_caption_model():
14
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
15
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
16
+ return processor, model
17
 
18
+ def generate_caption(image):
19
+ processor, model = load_caption_model()
20
+ inputs = processor(images=image, return_tensors="pt")
21
+ out = model.generate(**inputs)
22
+ return processor.decode(out[0], skip_special_tokens=True)
23
 
24
+ # ----------- Stage 2: Description to Story -----------
 
 
 
25
 
26
+ @st.cache_resource
27
+ def load_story_model():
28
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-1_5")
29
+ model = AutoModelForCausalLM.from_pretrained("microsoft/phi-1_5")
30
+ return tokenizer, model
31
 
32
+ def generate_story(description):
33
+ tokenizer, model = load_story_model()
34
  prompt = (
35
+ f"Write a short and fun story (50-100 words) for children based on the following: {description}\n\n"
36
+ "Story:"
37
  )
38
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
39
+ output = model.generate(**inputs, max_new_tokens=120, do_sample=True, top_k=50, top_p=0.95)
40
+ story = tokenizer.decode(output[0], skip_special_tokens=True)
41
+ return story.split("Story:")[-1].strip()
42
+
43
+ # ----------- Stage 3: Story to Speech -----------
44
+
45
+ def generate_speech(story):
46
+ engine = pyttsx3.init()
47
+ engine.setProperty('rate', 150)
48
+ engine.setProperty('volume', 0.9)
49
+
50
+ with io.BytesIO() as audio:
51
+ engine.save_to_file(story, 'temp.mp3')
52
+ engine.runAndWait()
53
+ with open('temp.mp3', 'rb') as f:
54
+ audio_bytes = f.read()
55
+ return audio_bytes
56
+
57
+ # ----------- Streamlit Interface -----------
58
+
59
+ st.set_page_config(page_title="Children's Story Generator", layout="centered")
60
+
61
+ st.title("📖 Children's Storytelling from Images")
62
+ st.markdown("Upload an illustration and we'll turn it into a fun story with voice narration!")
63
+
64
+ uploaded_image = st.file_uploader("Upload a drawing or illustration", type=["jpg", "jpeg", "png"])
65
+
66
+ if uploaded_image:
67
+ image = Image.open(uploaded_image)
68
+ st.image(image, caption="Uploaded Image", use_column_width=True)
69
+
70
+ # Stage 1
71
+ with st.spinner("Generating description..."):
72
+ description = generate_caption(image)
73
+ st.success("✅ Description Generated!")
74
+ st.markdown(f"**Image Caption:** _{description}_")
75
+
76
+ # Stage 2
77
+ with st.spinner("Generating children's story..."):
78
+ story = generate_story(description)
79
+ st.success("✅ Story Generated!")
80
+ st.markdown("**Generated Story:**")
81
+ st.write(story)
82
 
83
+ # Stage 3
84
+ with st.spinner("Generating voice..."):
85
+ audio_data = generate_speech(story)
86
+ st.audio(audio_data, format='audio/mp3')