Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,67 @@
|
|
1 |
-
#
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
4 |
-
|
5 |
import os
|
6 |
-
import re # For removing unwanted words
|
7 |
|
8 |
-
#
|
9 |
-
# img2text
|
10 |
def img2text(url):
|
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 |
-
|
14 |
-
fun_caption = f"Wow! {text.capitalize()}! π"
|
15 |
-
return fun_caption
|
16 |
|
17 |
-
#
|
18 |
def text2story(text):
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
prompt = f"Write a fun and realistic story for kids based on this: {text}. The story should be under 95 words and suitable for children aged 3-10. Avoid using the word 'illustration'."
|
23 |
-
story = story_generator(prompt, max_length=95, num_return_sequences=1)[0]["generated_text"]
|
24 |
-
# Remove the prompt from the generated story
|
25 |
-
story = story.replace(prompt, "").strip()
|
26 |
-
# Remove the word "illustration" (case-insensitive) using regex
|
27 |
-
story = re.sub(r"\billustration\b", "", story, flags=re.IGNORECASE)
|
28 |
-
return story[:95] # Limit to 95 words
|
29 |
|
30 |
-
#
|
31 |
def text2audio(story_text):
|
32 |
-
|
33 |
-
|
34 |
-
audio_file
|
35 |
-
|
36 |
return audio_file
|
37 |
|
38 |
-
#
|
39 |
-
st.set_page_config(page_title="Story
|
40 |
-
st.header("
|
|
|
41 |
|
42 |
-
uploaded_file = st.file_uploader("
|
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="Your
|
50 |
|
51 |
# Stage 1: Image to Text
|
52 |
-
st.text('
|
53 |
scenario = img2text(uploaded_file.name)
|
54 |
-
st.write(
|
55 |
|
56 |
# Stage 2: Text to Story
|
57 |
-
st.text('Creating a
|
58 |
story = text2story(scenario)
|
59 |
-
st.write(
|
60 |
|
61 |
-
# Stage 3: Story to Audio
|
62 |
-
st.text('Turning your story into audio...
|
63 |
audio_file = text2audio(story)
|
64 |
|
65 |
-
# Play button
|
66 |
-
if st.button("
|
67 |
-
st.audio(audio_file, format="audio/
|
68 |
|
69 |
-
# Clean up the audio file
|
70 |
-
os.remove(audio_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
4 |
+
import pyttsx3
|
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 generate a story from text using GPT-2
|
14 |
def text2story(text):
|
15 |
+
text_generator = pipeline("text-generation", model="gpt2")
|
16 |
+
story = text_generator(text, max_length=95, num_return_sequences=1)[0]["generated_text"]
|
17 |
+
return story
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Function to convert text to audio using pyttsx3
|
20 |
def text2audio(story_text):
|
21 |
+
engine = pyttsx3.init()
|
22 |
+
audio_file = "story_audio.wav"
|
23 |
+
engine.save_to_file(story_text, audio_file)
|
24 |
+
engine.runAndWait()
|
25 |
return audio_file
|
26 |
|
27 |
+
# Main application
|
28 |
+
st.set_page_config(page_title="Image to Story", page_icon="π")
|
29 |
+
st.header("π Image to Story")
|
30 |
+
st.markdown("### Turn your image into a fun story!")
|
31 |
|
32 |
+
uploaded_file = st.file_uploader("Select an Image...", type=["jpg", "jpeg", "png"])
|
33 |
|
34 |
if uploaded_file is not None:
|
35 |
+
# Save the uploaded file
|
36 |
bytes_data = uploaded_file.getvalue()
|
37 |
with open(uploaded_file.name, "wb") as file:
|
38 |
file.write(bytes_data)
|
39 |
|
40 |
+
st.image(uploaded_file, caption="Your Uploaded Image", use_column_width=True)
|
41 |
|
42 |
# Stage 1: Image to Text
|
43 |
+
st.text('πΌοΈ Processing image...')
|
44 |
scenario = img2text(uploaded_file.name)
|
45 |
+
st.write("**What I see:**", scenario)
|
46 |
|
47 |
# Stage 2: Text to Story
|
48 |
+
st.text('π Creating a story...')
|
49 |
story = text2story(scenario)
|
50 |
+
st.write("**Your Story:**", story)
|
51 |
|
52 |
+
# Stage 3: Story to Audio
|
53 |
+
st.text('ποΈ Turning your story into audio...')
|
54 |
audio_file = text2audio(story)
|
55 |
|
56 |
+
# Play button for audio
|
57 |
+
if st.button("π§ Listen to the Story"):
|
58 |
+
st.audio(audio_file, format="audio/wav")
|
59 |
|
60 |
+
# Clean up the generated audio file
|
61 |
+
os.remove(audio_file)
|
62 |
+
|
63 |
+
# Add some fun prompts for kids
|
64 |
+
st.markdown("### π¨ Tips for a Great Story!")
|
65 |
+
st.write("1. Upload a picture of your favorite animal, place, or toy!")
|
66 |
+
st.write("2. Imagine what's happening in the picture and let the story begin!")
|
67 |
+
st.write("3. Listen to your story and share it with your friends!")
|