smtsead commited on
Commit
9dd5dc1
Β·
verified Β·
1 Parent(s): 83838a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -40
app.py CHANGED
@@ -1,70 +1,67 @@
1
- # import part
2
  import streamlit as st
3
  from transformers import pipeline
4
- from gtts import gTTS # Using gTTS for text-to-speech
5
  import os
6
- import re # For removing unwanted words
7
 
8
- # function part
9
- # img2text
10
  def img2text(url):
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 descriptive
14
- fun_caption = f"Wow! {text.capitalize()}! 🌟"
15
- return fun_caption
16
 
17
- # text2story
18
  def text2story(text):
19
- # Use facebook/bart-large-cnn for text generation
20
- story_generator = pipeline("text-generation", model="facebook/bart-large-cnn")
21
- # Add a more explicit prompt to guide the story generation
22
- prompt = f"Write a fun and realistic story for kids based on this: {text}. The story should be under 95 words and suitable for children aged 3-10. Avoid using the word 'illustration'."
23
- story = story_generator(prompt, max_length=95, num_return_sequences=1)[0]["generated_text"]
24
- # Remove the prompt from the generated story
25
- story = story.replace(prompt, "").strip()
26
- # Remove the word "illustration" (case-insensitive) using regex
27
- story = re.sub(r"\billustration\b", "", story, flags=re.IGNORECASE)
28
- return story[:95] # Limit to 95 words
29
 
30
- # text2audio
31
  def text2audio(story_text):
32
- # Use gTTS for text-to-speech conversion
33
- tts = gTTS(text=story_text, lang="en")
34
- audio_file = "story_audio.mp3"
35
- tts.save(audio_file)
36
  return audio_file
37
 
38
- # main part
39
- st.set_page_config(page_title="Story Explorer", page_icon="🦜")
40
- st.header("Story Explorer: Turn Your Picture into a Fun Story! πŸŽ¨πŸ“–")
 
41
 
42
- uploaded_file = st.file_uploader("Choose a picture...", type=["jpg", "png", "jpeg"])
43
 
44
  if uploaded_file is not None:
 
45
  bytes_data = uploaded_file.getvalue()
46
  with open(uploaded_file.name, "wb") as file:
47
  file.write(bytes_data)
48
 
49
- st.image(uploaded_file, caption="Your Picture", use_container_width=True)
50
 
51
  # Stage 1: Image to Text
52
- st.text('Let’s explore your picture! 🧐✨')
53
  scenario = img2text(uploaded_file.name)
54
- st.write(f"**Here’s what I see in your picture:** {scenario}")
55
 
56
  # Stage 2: Text to Story
57
- st.text('Creating a fun story for you! πŸ“–')
58
  story = text2story(scenario)
59
- st.write(f"**Here’s your story:** {story}")
60
 
61
- # Stage 3: Story to Audio data
62
- st.text('Turning your story into audio... 🎧')
63
  audio_file = text2audio(story)
64
 
65
- # Play button
66
- if st.button("Play Audio"):
67
- st.audio(audio_file, format="audio/mp3")
68
 
69
- # Clean up the audio file after use
70
- os.remove(audio_file)
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
  import streamlit as st
3
  from transformers import pipeline
4
+ import pyttsx3
5
  import os
 
6
 
7
+ # Function to convert image to text
 
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
+ return text
 
 
12
 
13
+ # Function to generate a story from text using GPT-2
14
  def text2story(text):
15
+ text_generator = pipeline("text-generation", model="gpt2")
16
+ story = text_generator(text, max_length=95, num_return_sequences=1)[0]["generated_text"]
17
+ return story
 
 
 
 
 
 
 
18
 
19
+ # Function to convert text to audio using pyttsx3
20
  def text2audio(story_text):
21
+ engine = pyttsx3.init()
22
+ audio_file = "story_audio.wav"
23
+ engine.save_to_file(story_text, audio_file)
24
+ engine.runAndWait()
25
  return audio_file
26
 
27
+ # Main application
28
+ st.set_page_config(page_title="Image to Story", page_icon="πŸ“–")
29
+ st.header("πŸ“– Image to Story")
30
+ st.markdown("### Turn your image into a fun story!")
31
 
32
+ uploaded_file = st.file_uploader("Select an Image...", type=["jpg", "jpeg", "png"])
33
 
34
  if uploaded_file is not None:
35
+ # Save the uploaded file
36
  bytes_data = uploaded_file.getvalue()
37
  with open(uploaded_file.name, "wb") as file:
38
  file.write(bytes_data)
39
 
40
+ st.image(uploaded_file, caption="Your Uploaded Image", use_column_width=True)
41
 
42
  # Stage 1: Image to Text
43
+ st.text('πŸ–ΌοΈ Processing image...')
44
  scenario = img2text(uploaded_file.name)
45
+ st.write("**What I see:**", scenario)
46
 
47
  # Stage 2: Text to Story
48
+ st.text('πŸ“ Creating a story...')
49
  story = text2story(scenario)
50
+ st.write("**Your Story:**", story)
51
 
52
+ # Stage 3: Story to Audio
53
+ st.text('πŸŽ™οΈ Turning your story into audio...')
54
  audio_file = text2audio(story)
55
 
56
+ # Play button for audio
57
+ if st.button("🎧 Listen to the Story"):
58
+ st.audio(audio_file, format="audio/wav")
59
 
60
+ # Clean up the generated audio file
61
+ os.remove(audio_file)
62
+
63
+ # Add some fun prompts for kids
64
+ st.markdown("### 🎨 Tips for a Great Story!")
65
+ st.write("1. Upload a picture of your favorite animal, place, or toy!")
66
+ st.write("2. Imagine what's happening in the picture and let the story begin!")
67
+ st.write("3. Listen to your story and share it with your friends!")