smtsead commited on
Commit
dbfc4d0
Β·
verified Β·
1 Parent(s): 8ac8023

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -11
app.py CHANGED
@@ -3,8 +3,15 @@ import streamlit as st
3
  from transformers import pipeline
4
  from gtts import gTTS
5
  import os
 
 
 
 
 
 
6
 
7
  # Function to convert image to text using Hugging Face's BLIP model
 
8
  def img2text(url):
9
  """
10
  Converts an image to text using the Salesforce/blip-image-captioning-base model.
@@ -15,7 +22,8 @@ def img2text(url):
15
  Returns:
16
  str: Generated text caption from the image, without words like "illustration".
17
  """
18
- image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
 
19
  text = image_to_text_model(url)[0]["generated_text"]
20
 
21
  # Remove unwanted words like "illustration"
@@ -26,6 +34,7 @@ def img2text(url):
26
  return text.strip()
27
 
28
  # Function to generate a kid-friendly story from the text caption
 
29
  def text2story(text):
30
  """
31
  Generates a kid-friendly story from the text caption using the aspis/gpt2-genre-story-generation model.
@@ -37,10 +46,10 @@ def text2story(text):
37
  str: Generated story suitable for kids aged 3-10, within 100 words.
38
  """
39
  # Add a prompt to ensure the story is happy, fun, and appropriate for kids
40
- prompt = f"Write a happy,fun and complete story for kids based on the following scenario: {text} with no sad, violent, scary elements."
41
 
42
- # Load the text generation model
43
- story_generator = pipeline("text-generation", model="aspis/gpt2-genre-story-generation")
44
 
45
  # Generate the story
46
  story = story_generator(prompt, max_length=150, num_return_sequences=1, temperature=0.7, top_k=50, top_p=0.9)[0]["generated_text"]
@@ -48,7 +57,7 @@ def text2story(text):
48
  # Remove the prompt from the generated story
49
  story = story.replace(prompt, "").strip()
50
 
51
- # Ensure the story is within 95 words
52
  story_words = story.split()
53
  if len(story_words) > 100:
54
  story = " ".join(story_words[:100])
@@ -101,21 +110,31 @@ if uploaded_file is not None:
101
 
102
  # Stage 1: Image to Text
103
  st.text('✨ Turning your picture into words...')
104
- scenario = img2text(uploaded_file.name)
105
- st.write("**What we see:**", scenario)
 
 
 
106
 
107
  # Stage 2: Text to Story
108
  st.text('πŸ“– Creating a fun story for you...')
109
- story = text2story(scenario)
110
- st.write("**Your story:**", story)
 
 
 
111
 
112
  # Stage 3: Story to Audio
113
  st.text('🎧 Turning your story into audio...')
114
- audio_file = text2audio(story)
 
 
 
115
 
116
  # Play button for the generated audio
117
  if st.button("🎡 **Play Audio**"):
118
  st.audio(audio_file, format="audio/mp3")
119
 
120
  # Clean up the generated audio file
121
- os.remove(audio_file)
 
 
3
  from transformers import pipeline
4
  from gtts import gTTS
5
  import os
6
+ from retrying import retry # For retry logic
7
+
8
+ # Retry decorator for handling transient errors
9
+ def retry_if_timeout_error(exception):
10
+ """Return True if the exception is a timeout error."""
11
+ return isinstance(exception, Exception) and "Read timed out" in str(exception)
12
 
13
  # Function to convert image to text using Hugging Face's BLIP model
14
+ @retry(stop_max_attempt_number=3, wait_fixed=2000, retry_on_exception=retry_if_timeout_error) # Retry 3 times with a 2-second delay
15
  def img2text(url):
16
  """
17
  Converts an image to text using the Salesforce/blip-image-captioning-base model.
 
22
  Returns:
23
  str: Generated text caption from the image, without words like "illustration".
24
  """
25
+ # Increase timeout to 30 seconds
26
+ image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base", device_map="auto", timeout=30)
27
  text = image_to_text_model(url)[0]["generated_text"]
28
 
29
  # Remove unwanted words like "illustration"
 
34
  return text.strip()
35
 
36
  # Function to generate a kid-friendly story from the text caption
37
+ @retry(stop_max_attempt_number=3, wait_fixed=2000, retry_on_exception=retry_if_timeout_error) # Retry 3 times with a 2-second delay
38
  def text2story(text):
39
  """
40
  Generates a kid-friendly story from the text caption using the aspis/gpt2-genre-story-generation model.
 
46
  str: Generated story suitable for kids aged 3-10, within 100 words.
47
  """
48
  # Add a prompt to ensure the story is happy, fun, and appropriate for kids
49
+ prompt = f"Base on the following scenario: {text}, write a happy,fun and complete story for kids with no sad, violent, scary elements."
50
 
51
+ # Load the text generation model with increased timeout
52
+ story_generator = pipeline("text-generation", model="aspis/gpt2-genre-story-generation", device_map="auto", timeout=30)
53
 
54
  # Generate the story
55
  story = story_generator(prompt, max_length=150, num_return_sequences=1, temperature=0.7, top_k=50, top_p=0.9)[0]["generated_text"]
 
57
  # Remove the prompt from the generated story
58
  story = story.replace(prompt, "").strip()
59
 
60
+ # Ensure the story is within 100 words
61
  story_words = story.split()
62
  if len(story_words) > 100:
63
  story = " ".join(story_words[:100])
 
110
 
111
  # Stage 1: Image to Text
112
  st.text('✨ Turning your picture into words...')
113
+ try:
114
+ scenario = img2text(uploaded_file.name)
115
+ st.write("**What we see:**", scenario)
116
+ except Exception as e:
117
+ st.error(f"Oops! Something went wrong while processing the image. Please try again. Error: {e}")
118
 
119
  # Stage 2: Text to Story
120
  st.text('πŸ“– Creating a fun story for you...')
121
+ try:
122
+ story = text2story(scenario)
123
+ st.write("**Your story:**", story)
124
+ except Exception as e:
125
+ st.error(f"Oops! Something went wrong while generating the story. Please try again. Error: {e}")
126
 
127
  # Stage 3: Story to Audio
128
  st.text('🎧 Turning your story into audio...')
129
+ try:
130
+ audio_file = text2audio(story)
131
+ except Exception as e:
132
+ st.error(f"Oops! Something went wrong while converting the story to audio. Please try again. Error: {e}")
133
 
134
  # Play button for the generated audio
135
  if st.button("🎡 **Play Audio**"):
136
  st.audio(audio_file, format="audio/mp3")
137
 
138
  # Clean up the generated audio file
139
+ if os.path.exists(audio_file):
140
+ os.remove(audio_file)