smtsead commited on
Commit
9323a68
Β·
verified Β·
1 Parent(s): f3b0de6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -62
app.py CHANGED
@@ -1,86 +1,63 @@
1
  # import part
2
  import streamlit as st
3
  from transformers import pipeline
4
- import os
5
 
6
  # function part
7
  # img2text
8
  def img2text(url):
9
- try:
10
- image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
11
- text = image_to_text_model(url)[0]["generated_text"]
12
- # Remove unwanted words like "illustration"
13
- text = text.replace("illustration", "").strip()
14
- # Make the caption more fun and happy
15
- fun_caption = f"🌟 Wow! This picture shows {text.lower()}. Let’s turn it into a fun story! 🌟"
16
- return fun_caption
17
- except Exception as e:
18
- st.error(f"Oops! Something went wrong while looking at your picture. Please try again! 😊")
19
- return None
20
 
21
  # text2story
22
  def text2story(text):
23
- try:
24
- # Use a better model for text generation (e.g., GPT-Neo)
25
- story_generator = pipeline("text-generation", model="EleutherAI/gpt-neo-125M")
26
- # Add a fun and happy prompt to guide the story generation
27
- prompt = f"One sunny day, {text}. "
28
- story = story_generator(prompt, max_length=100, num_return_sequences=1)[0]["generated_text"]
29
- # Remove any unwanted text (e.g., usernames, special characters)
30
- story = " ".join([word for word in story.split() if not word.startswith("@") and not word.startswith("http")])
31
- # Make the story more fun by adding a happy ending
32
- happy_story = story + " And everyone had a big smile on their faces at the end of the day! πŸ˜„πŸŒˆ"
33
- return happy_story
34
- except Exception as e:
35
- st.error(f"Oops! Something went wrong while creating your story. Please try again! 😊")
36
- return None
37
 
38
  # text2audio
39
  def text2audio(story_text):
40
- try:
41
- # Use a reliable TTS model (e.g., ESPnet's VITS model)
42
- tts_pipeline = pipeline("text-to-speech", model="espnet/kan-bayashi_ljspeech_vits")
43
- audio_output = tts_pipeline(story_text)
44
- audio_file = "story_audio.wav"
45
- # Save the audio file
46
- with open(audio_file, "wb") as f:
47
- f.write(audio_output["audio"])
48
- return audio_file
49
- except Exception as e:
50
- st.error(f"Oops! Something went wrong while turning your story into audio. Please try again! 😊")
51
- return None
52
 
53
  # main part
54
- st.set_page_config(page_title="Story Maker", page_icon="😊")
55
- st.header("πŸ˜„ Story Maker: Turn Your Picture into a Happy Story! πŸ˜„")
56
- uploaded_file = st.file_uploader("πŸ“· Choose a picture to create a fun story...", type=["jpg", "jpeg", "png"])
57
 
58
  if uploaded_file is not None:
59
  bytes_data = uploaded_file.getvalue()
60
  with open(uploaded_file.name, "wb") as file:
61
  file.write(bytes_data)
62
 
63
- st.image(uploaded_file, caption="Your fun picture!", use_container_width=True)
64
 
65
  # Stage 1: Image to Text
66
- st.write("🌟 Let’s see what’s in your picture... 🌟")
67
  scenario = img2text(uploaded_file.name)
68
- if scenario:
69
- st.write(scenario)
70
-
71
- # Stage 2: Text to Story
72
- st.write("πŸ“– Turning your picture into a fun story... πŸ“–")
73
- story = text2story(scenario)
74
- if story:
75
- st.write("πŸ“– Here’s your fun story:")
76
- st.write(story)
77
-
78
- # Stage 3: Story to Audio data
79
- st.write("🎀 Getting ready to tell your story... 🎀")
80
- audio_file = text2audio(story)
81
- if audio_file:
82
- # Play button
83
- if st.button("🎧 Listen to Your Story!"):
84
- st.audio(audio_file, format="audio/wav")
85
- # Clean up the audio file after playing
86
- os.remove(audio_file)
 
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
+ story_generator = pipeline("text-generation", model="distilgpt2")
18
+ # Generate a story with a maximum of 90 words
19
+ story = story_generator(text, max_length=90, num_return_sequences=1)[0]["generated_text"]
20
+ # Ensure the story does not exceed 90 words
21
+ story = " ".join(story.split()[:90]) # Truncate to 90 words
22
+ # Make the story simple and fun for kids
23
+ fun_story = f"Once upon a time... 🌟 {story}"
24
+ return fun_story
 
 
 
 
 
 
25
 
26
  # text2audio
27
  def text2audio(story_text):
28
+ tts_pipeline = pipeline("text-to-speech", model="espnet/kan-bayashi_ljspeech_vits")
29
+ audio_data = tts_pipeline(story_text)
30
+ return audio_data
 
 
 
 
 
 
 
 
 
31
 
32
  # main part
33
+ st.set_page_config(page_title="Story Maker", page_icon="🦜")
34
+ st.header("Story Maker: Turn Your Picture into a Story!")
35
+ uploaded_file = st.file_uploader("Select an Image...")
36
 
37
  if uploaded_file is not None:
38
  bytes_data = uploaded_file.getvalue()
39
  with open(uploaded_file.name, "wb") as file:
40
  file.write(bytes_data)
41
 
42
+ st.image(uploaded_file, caption="Your Picture", use_container_width=True)
43
 
44
  # Stage 1: Image to Text
45
+ st.text('✨ Discovering what’s in your picture...')
46
  scenario = img2text(uploaded_file.name)
47
+ st.write(f"Here’s what we found: {scenario}")
48
+
49
+ # Stage 2: Text to Story
50
+ st.text('🎭 Creating a fun story for you...')
51
+ story = text2story(scenario)
52
+ st.write(story)
53
+
54
+ # Stage 3: Story to Audio data
55
+ st.text('πŸ”Š Turning your story into audio...')
56
+ audio_data = text2audio(story)
57
+
58
+ # Play button
59
+ if st.button("Play Audio"):
60
+ st.audio(audio_data['audio'],
61
+ format="audio/wav",
62
+ start_time=0,
63
+ sample_rate=audio_data['sampling_rate'])