smtsead commited on
Commit
a63c8c4
Β·
verified Β·
1 Parent(s): 5d3ae61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -10
app.py CHANGED
@@ -4,25 +4,55 @@ from transformers import pipeline
4
  from gtts import gTTS
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 convert text to a story
14
  def text2story(text):
15
- # Initialize the text generation pipeline
16
- text_generator = pipeline("text-generation", model="distilbert/distilgpt2")
 
 
 
 
 
 
 
 
 
17
 
18
- # Generate the story directly from the input text
19
- story = text_generator(text, max_length=95, num_return_sequences=1)[0]['generated_text']
 
 
 
20
 
21
  return story
22
 
23
- # Function to convert text to audio
24
  def text2audio(story_text):
25
- # Convert the story text to audio using gTTS
 
 
 
 
 
 
 
 
 
26
  tts = gTTS(text=story_text, lang='en')
27
  audio_file = "story_audio.mp3"
28
  tts.save(audio_file)
@@ -32,7 +62,9 @@ def text2audio(story_text):
32
  # Main application
33
  st.set_page_config(page_title="Your Image to Audio Story",
34
  page_icon="🦜")
35
- st.header("Turn Your Image into a Fun Audio Story!")
 
 
36
  uploaded_file = st.file_uploader("Select an Image...", type=["jpg", "jpeg", "png"])
37
 
38
  if uploaded_file is not None:
@@ -47,7 +79,7 @@ if uploaded_file is not None:
47
  # Stage 1: Image to Text
48
  st.text('Processing image to text...')
49
  scenario = img2text(uploaded_file.name)
50
- st.write("**Scenario:**", scenario)
51
 
52
  # Stage 2: Text to Story
53
  st.text('Generating a fun story for kids...')
 
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.
11
+
12
+ Args:
13
+ url (str): Path to the image file.
14
+
15
+ Returns:
16
+ str: Generated text caption from the image.
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
  return text
21
 
22
+ # Function to generate a kid-friendly story from the text caption
23
  def text2story(text):
24
+ """
25
+ Generates a kid-friendly story from the text caption using the aspis/gpt2-genre-story-generation model.
26
+
27
+ Args:
28
+ text (str): Text caption generated from the image.
29
+
30
+ Returns:
31
+ str: Generated story suitable for kids aged 3-10.
32
+ """
33
+ # Add a prompt to ensure the story is kid-friendly and happy
34
+ prompt = f"Write a happy and fun story for kids aged 3-10 based on the following scenario: {text}."
35
 
36
+ # Load the text generation model
37
+ story_generator = pipeline("text-generation", model="aspis/gpt2-genre-story-generation")
38
+
39
+ # Generate the story
40
+ story = story_generator(prompt, max_length=95, num_return_sequences=1)[0]["generated_text"]
41
 
42
  return story
43
 
44
+ # Function to convert text to audio using gTTS
45
  def text2audio(story_text):
46
+ """
47
+ Converts the generated story text to audio using gTTS.
48
+
49
+ Args:
50
+ story_text (str): The generated story text.
51
+
52
+ Returns:
53
+ str: Path to the generated audio file.
54
+ """
55
+ # Convert text to speech
56
  tts = gTTS(text=story_text, lang='en')
57
  audio_file = "story_audio.mp3"
58
  tts.save(audio_file)
 
62
  # Main application
63
  st.set_page_config(page_title="Your Image to Audio Story",
64
  page_icon="🦜")
65
+ st.header("Turn Your Image into a Fun Audio Story! πŸŽ‰")
66
+
67
+ # Upload image
68
  uploaded_file = st.file_uploader("Select an Image...", type=["jpg", "jpeg", "png"])
69
 
70
  if uploaded_file is not None:
 
79
  # Stage 1: Image to Text
80
  st.text('Processing image to text...')
81
  scenario = img2text(uploaded_file.name)
82
+ st.write("**Caption:**", scenario)
83
 
84
  # Stage 2: Text to Story
85
  st.text('Generating a fun story for kids...')