Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,40 @@
|
|
1 |
# Import necessary libraries
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
4 |
-
import
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
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(
|
|
|
|
|
|
|
17 |
return story
|
18 |
|
19 |
-
# Function to convert text to audio using
|
20 |
def text2audio(story_text):
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
engine.runAndWait()
|
25 |
return audio_file
|
26 |
|
27 |
# Main application
|
@@ -37,7 +50,7 @@ if uploaded_file is not None:
|
|
37 |
with open(uploaded_file.name, "wb") as file:
|
38 |
file.write(bytes_data)
|
39 |
|
40 |
-
st.image(uploaded_file, caption="Your Uploaded Image",
|
41 |
|
42 |
# Stage 1: Image to Text
|
43 |
st.text('🖼️ Processing image...')
|
@@ -55,7 +68,7 @@ if uploaded_file is not None:
|
|
55 |
|
56 |
# Play button for audio
|
57 |
if st.button("🎧 Listen to the Story"):
|
58 |
-
st.audio(audio_file, format="audio/
|
59 |
|
60 |
# Clean up the generated audio file
|
61 |
os.remove(audio_file)
|
|
|
1 |
# Import necessary libraries
|
2 |
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
|
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 |
+
|
12 |
+
# Remove unwanted words like "illustration"
|
13 |
+
unwanted_words = ["illustration", "painting", "drawing", "artwork"]
|
14 |
+
for word in unwanted_words:
|
15 |
+
text = text.replace(word, "")
|
16 |
+
return text.strip()
|
17 |
|
18 |
# Function to generate a story from text using GPT-2
|
19 |
def text2story(text):
|
20 |
+
# Add a strong prompt to guide the model
|
21 |
+
prompt = f"Write a short, happy, and fun story for kids aged 3-10 based on the following description: {text}. " \
|
22 |
+
"The story should be cheerful, imaginative, and suitable for young children. " \
|
23 |
+
"Avoid any scary or sad elements. Keep the story under 95 words and make sure it has a clear beginning, middle, and end."
|
24 |
+
|
25 |
+
# Use the GPT-2 model to generate the story
|
26 |
text_generator = pipeline("text-generation", model="gpt2")
|
27 |
+
story = text_generator(prompt, max_length=95, num_return_sequences=1)[0]["generated_text"]
|
28 |
+
|
29 |
+
# Clean up the output to remove the prompt (if necessary)
|
30 |
+
story = story.replace(prompt, "").strip()
|
31 |
return story
|
32 |
|
33 |
+
# Function to convert text to audio using gTTS
|
34 |
def text2audio(story_text):
|
35 |
+
audio_file = "story_audio.mp3"
|
36 |
+
tts = gTTS(story_text, lang="en")
|
37 |
+
tts.save(audio_file)
|
|
|
38 |
return audio_file
|
39 |
|
40 |
# Main application
|
|
|
50 |
with open(uploaded_file.name, "wb") as file:
|
51 |
file.write(bytes_data)
|
52 |
|
53 |
+
st.image(uploaded_file, caption="Your Uploaded Image", use_container_width=True)
|
54 |
|
55 |
# Stage 1: Image to Text
|
56 |
st.text('🖼️ Processing image...')
|
|
|
68 |
|
69 |
# Play button for audio
|
70 |
if st.button("🎧 Listen to the Story"):
|
71 |
+
st.audio(audio_file, format="audio/mp3")
|
72 |
|
73 |
# Clean up the generated audio file
|
74 |
os.remove(audio_file)
|