smtsead commited on
Commit
8f9ddb2
·
verified ·
1 Parent(s): 9819f3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -39
app.py CHANGED
@@ -1,65 +1,78 @@
1
  # import part
2
  import streamlit as st
3
  from transformers import pipeline
4
- import torch
 
5
 
6
  # function part
7
  # img2text
8
  def img2text(url):
9
- image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
10
- text = image_to_text_model(url)[0]["generated_text"]
11
- # Make the caption simple and fun for kids
12
- fun_caption = f"Look what we found! 🎨 {text}"
13
- return fun_caption
 
 
 
 
14
 
15
  # text2story
16
  def text2story(text):
17
- # Use DistilGPT-2 for text generation
18
- story_generator = pipeline("text-generation", model="distilgpt2")
19
- # Generate a story with a maximum of 90 words
20
- story = story_generator(text, max_length=90, num_return_sequences=1)[0]["generated_text"]
21
- # Ensure the story does not exceed 90 words
22
- story = " ".join(story.split()[:90]) # Truncate to 90 words
23
- # Make the story simple and fun for kids
24
- fun_story = f"Once upon a time... 🌟 {story}"
25
- return fun_story
 
 
26
 
27
  # text2audio
28
  def text2audio(story_text):
29
- # Use FastSpeech 2 for text-to-speech
30
- tts_pipeline = pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech")
31
- audio_data = tts_pipeline(story_text)
32
- return audio_data
 
 
 
 
33
 
34
  # main part
35
- st.set_page_config(page_title="Story Maker", page_icon="🦜")
36
- st.header("Story Maker: Turn Your Picture into a Story!")
37
- uploaded_file = st.file_uploader("Select an Image...")
38
 
39
  if uploaded_file is not None:
40
  bytes_data = uploaded_file.getvalue()
41
  with open(uploaded_file.name, "wb") as file:
42
  file.write(bytes_data)
43
 
44
- st.image(uploaded_file, caption="Your Picture", use_container_width=True)
45
 
46
  # Stage 1: Image to Text
47
- st.text('Discovering what’s in your picture...')
48
  scenario = img2text(uploaded_file.name)
49
- st.write(f"Here’s what we found: {scenario}")
 
50
 
51
- # Stage 2: Text to Story
52
- st.text('🎭 Creating a fun story for you...')
53
- story = text2story(scenario)
54
- st.write(story)
 
 
55
 
56
- # Stage 3: Story to Audio data
57
- st.text('🔊 Turning your story into audio...')
58
- audio_data = text2audio(story)
59
-
60
- # Play button
61
- if st.button("Play Audio"):
62
- st.audio(audio_data['audio'],
63
- format="audio/wav",
64
- start_time=0,
65
- sample_rate=audio_data['sampling_rate'])
 
1
  # import part
2
  import streamlit as st
3
  from transformers import pipeline
4
+ from gtts import gTTS
5
+ import os
6
 
7
  # function part
8
  # img2text
9
  def img2text(url):
10
+ try:
11
+ image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
12
+ text = image_to_text_model(url)[0]["generated_text"]
13
+ # Make the caption more fun and creative
14
+ fun_caption = f"✨ Wow! Look at this! It’s a picture of {text.lower()}. Let’s turn it into a fun story! ✨"
15
+ return fun_caption
16
+ except Exception as e:
17
+ st.error(f"Oops! Something went wrong while looking at your picture. Please try again! 🪄")
18
+ return None
19
 
20
  # text2story
21
  def text2story(text):
22
+ try:
23
+ story_generator = pipeline("text-generation", model="gpt2")
24
+ # Add a playful prompt to guide the story generation
25
+ prompt = f"Once upon a time, there was {text}. "
26
+ story = story_generator(prompt, max_length=95, num_return_sequences=1)[0]["generated_text"]
27
+ # Make the story more fun by adding a happy ending
28
+ fun_story = story + " And they all lived happily ever after! 🌈✨"
29
+ return fun_story
30
+ except Exception as e:
31
+ st.error(f"Oops! Something went wrong while creating your story. Please try again! 🪄")
32
+ return None
33
 
34
  # text2audio
35
  def text2audio(story_text):
36
+ try:
37
+ tts = gTTS(text=story_text, lang='en')
38
+ audio_file = "story_audio.mp3"
39
+ tts.save(audio_file)
40
+ return audio_file
41
+ except Exception as e:
42
+ st.error(f"Oops! Something went wrong while turning your story into audio. Please try again! 🪄")
43
+ return None
44
 
45
  # main part
46
+ st.set_page_config(page_title="Story Maker", page_icon="🦄")
47
+ st.header("🌟 Story Maker: Turn Your Picture into a Fun Story! 🌟")
48
+ uploaded_file = st.file_uploader("📷 Choose a picture to create a fun story...", type=["jpg", "jpeg", "png"])
49
 
50
  if uploaded_file is not None:
51
  bytes_data = uploaded_file.getvalue()
52
  with open(uploaded_file.name, "wb") as file:
53
  file.write(bytes_data)
54
 
55
+ st.image(uploaded_file, caption="Your picture!", use_container_width=True)
56
 
57
  # Stage 1: Image to Text
58
+ st.write("Let’s see what’s in your picture... ✨")
59
  scenario = img2text(uploaded_file.name)
60
+ if scenario:
61
+ st.write(scenario)
62
 
63
+ # Stage 2: Text to Story
64
+ st.write("🧙‍♂️ Turning your picture into a story... 🧙‍♀️")
65
+ story = text2story(scenario)
66
+ if story:
67
+ st.write("📖 Here’s your story:")
68
+ st.write(story)
69
 
70
+ # Stage 3: Story to Audio
71
+ st.write("🎤 Getting ready to tell your story... 🎤")
72
+ audio_file = text2audio(story)
73
+ if audio_file:
74
+ # Play button
75
+ if st.button("🎧 Listen to Your Story!"):
76
+ st.audio(audio_file, format="audio/mp3")
77
+ # Clean up the audio file after playing
78
+ os.remove(audio_file)