sshenai commited on
Commit
eb15ec0
·
verified ·
1 Parent(s): 1f9f0c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -33
app.py CHANGED
@@ -6,43 +6,35 @@ import tempfile
6
  import os
7
 
8
 
9
- # Function to generate image caption
10
- def generate_image_caption(image_path):
11
  """Generates a caption for the given image using a pre-trained model."""
12
- img2caption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
13
- result = img2caption(image_path)
14
- return result[0]['generated_text']
15
 
16
- # Function to generate story from text
17
- def text2story(text):
18
- """Generates a story from input text"""
19
- pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
20
- story_text = pipe(text, max_length=200)[0]['generated_text']
21
- return story_text
22
 
23
- # Function to convert text to speech
24
- def text_to_speech(text):
25
- """Converts text to speech audio"""
26
- try:
27
- # Initialize text-to-audio pipeline
28
- tts_pipe = pipeline("text-to-audio", model="facebook/mms-tts-eng")
29
 
30
- # Generate audio (returns dict with 'audio' array and 'sampling_rate')
31
- audio_output = tts_pipe(text[:1000]) # Limit text length
32
-
33
- # Return the audio array and sampling rate
34
- return audio_output['audio'], audio_output['sampling_rate']
35
- except Exception as e:
36
- st.error(f"Speech generation failed: {str(e)}")
37
- return None, None
38
 
39
  # Main application
40
  def main():
41
- st.title("Image to Story with Speech")
42
- st.write("Upload an image to generate a caption, story, and audio narration")
43
-
44
- uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
45
-
46
  if uploaded_image is not None:
47
  try:
48
  # Process image
@@ -57,19 +49,19 @@ def main():
57
 
58
  # Generate caption
59
  with st.spinner("Generating caption..."):
60
- caption = generate_image_caption(image_path)
61
  st.subheader("Generated Caption")
62
  st.write(caption)
63
 
64
  # Generate story
65
  with st.spinner("Generating story..."):
66
- story = text2story(caption)
67
  st.subheader("Generated Story")
68
  st.write(story)
69
 
70
  # Generate speech
71
  with st.spinner("Generating audio..."):
72
- audio_array, sample_rate = text_to_speech(story)
73
  if audio_array is not None:
74
  st.subheader("Audio Narration")
75
  st.audio(audio_array, sample_rate=sample_rate)
 
6
  import os
7
 
8
 
9
+ # Generate caption from image
10
+ def image_to_caption(image_path):
11
  """Generates a caption for the given image using a pre-trained model."""
12
+ imgtocaption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
13
+ caption = imgtocaption(image_path)
14
+ return caption[0]['generated_text']
15
 
16
+ # Generate story from caption
17
+ def caption_to_story(text):
18
+ captiontostory = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
19
+ story = captiontostory(text, max_length=150, min_length=50)
20
+ return story[0]['generated_text']
 
21
 
22
+ # Convert story to speech
23
+ def story_to_audio(text):
24
+ tts_pipe = pipeline("text-to-audio", model="facebook/mms-tts-eng")
25
+ audio_output = tts_pipe(text[:1000])
26
+ return audio_output['audio'], audio_output['sampling_rate']
 
27
 
28
+ # Child-Friendly Interface Design
29
+ st.set_page_config(page_title="Magic Story House", page_icon="🧚")
30
+ st.title("🧚 Magic Image Story Generator")
31
+ st.markdown("Upload an image and generate your exclusive fairy tale!")
32
+
33
+ # File Upload
34
+ uploaded_image = st.file_uploader("Choose a picture", type=["jpg", "jpeg", "png"], key="image_uploader")
 
35
 
36
  # Main application
37
  def main():
 
 
 
 
 
38
  if uploaded_image is not None:
39
  try:
40
  # Process image
 
49
 
50
  # Generate caption
51
  with st.spinner("Generating caption..."):
52
+ caption = image_to_caption(image_path)
53
  st.subheader("Generated Caption")
54
  st.write(caption)
55
 
56
  # Generate story
57
  with st.spinner("Generating story..."):
58
+ story = caption_to_story(caption)
59
  st.subheader("Generated Story")
60
  st.write(story)
61
 
62
  # Generate speech
63
  with st.spinner("Generating audio..."):
64
+ audio_array, sample_rate = story_to_audio(story)
65
  if audio_array is not None:
66
  st.subheader("Audio Narration")
67
  st.audio(audio_array, sample_rate=sample_rate)