smtsead commited on
Commit
aa762d0
Β·
verified Β·
1 Parent(s): 4e17b21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -10,19 +10,25 @@ 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
- return text
 
 
14
  except Exception as e:
15
- st.error(f"Error in image to text conversion: {e}")
16
  return None
17
 
18
  # text2story
19
  def text2story(text):
20
  try:
21
  story_generator = pipeline("text-generation", model="gpt2")
22
- story = story_generator(text, max_length=100, num_return_sequences=1)[0]["generated_text"]
23
- return story
 
 
 
 
24
  except Exception as e:
25
- st.error(f"Error in story generation: {e}")
26
  return None
27
 
28
  # text2audio
@@ -33,39 +39,40 @@ def text2audio(story_text):
33
  tts.save(audio_file)
34
  return audio_file
35
  except Exception as e:
36
- st.error(f"Error in text to audio conversion: {e}")
37
  return None
38
 
39
  # main part
40
- st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
41
- st.header("Turn Your Image to Audio Story")
42
- uploaded_file = st.file_uploader("Select an Image...", type=["jpg", "jpeg", "png"])
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="Uploaded Image", use_column_width=True)
50
 
51
  # Stage 1: Image to Text
52
- st.text('Processing img2text...')
53
  scenario = img2text(uploaded_file.name)
54
  if scenario:
55
- st.write("Image Caption:", scenario)
56
 
57
  # Stage 2: Text to Story
58
- st.text('Generating a story...')
59
  story = text2story(scenario)
60
  if story:
61
- st.write("Generated Story:", story)
 
62
 
63
  # Stage 3: Story to Audio data
64
- st.text('Generating audio data...')
65
  audio_file = text2audio(story)
66
  if audio_file:
67
  # Play button
68
- if st.button("Play Audio"):
69
  st.audio(audio_file, format="audio/mp3")
70
  # Clean up the audio file after playing
71
  os.remove(audio_file)
 
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 happy
14
+ fun_caption = f"🌟 Wow! This picture shows {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 fun and happy prompt to guide the story generation
25
+ prompt = f"One sunny day, {text}. "
26
+ story = story_generator(prompt, max_length=150, num_return_sequences=1)[0]["generated_text"]
27
+ # Make the story more fun by adding a happy ending
28
+ happy_story = story + " And everyone had a big smile on their faces at the end of the day! πŸ˜„πŸŒˆ"
29
+ return happy_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
 
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="Fun Story Maker", page_icon="😊")
47
+ st.header("πŸ˜„ Fun Story Maker: Turn Your Picture into a Happy 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 fun 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 fun story... πŸ“–")
65
  story = text2story(scenario)
66
  if story:
67
+ st.write("πŸ“– Here’s your fun story:")
68
+ st.write(story)
69
 
70
  # Stage 3: Story to Audio data
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)