Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,55 @@
|
|
1 |
-
#
|
2 |
-
|
3 |
-
from
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
10 |
# Load the text generation model
|
11 |
-
story_model = pipeline("text-generation", model="gpt2")
|
12 |
-
#
|
13 |
-
|
14 |
-
return
|
15 |
-
|
16 |
-
# Function to generate story from caption
|
17 |
-
def generate_story(caption, story_model):
|
18 |
-
# Generate a story based on the caption
|
19 |
-
story = story_model(caption, max_length=100, num_return_sequences=1)[0]['generated_text'] # Generate the story
|
20 |
-
return story # Return the generated story
|
21 |
|
22 |
# Function to convert text to audio
|
23 |
-
def
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
#
|
37 |
-
|
38 |
-
st.
|
39 |
-
|
40 |
-
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
st.
|
57 |
-
st.write(story) # Display the generated story
|
58 |
-
|
59 |
-
# Convert story to audio and play it
|
60 |
-
audio = text_to_audio(story, tts_model) # Convert story to audio
|
61 |
-
st.audio(audio, format='audio/wav') # Play the audio
|
62 |
-
|
63 |
-
# Run the app
|
64 |
-
if __name__ == "__main__":
|
65 |
-
main() # Call the main function to run the app
|
|
|
1 |
+
import streamlit as st # Streamlit for building the web application
|
2 |
+
from transformers import pipeline # Hugging Face Transformers pipeline for models
|
3 |
+
from PIL import Image # PIL for handling image files
|
4 |
+
|
5 |
+
# Function to convert image to text
|
6 |
+
def img2text(image):
|
7 |
+
# Load the image-to-text model
|
8 |
+
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
9 |
+
# Generate a caption for the image
|
10 |
+
text = image_to_text_model(image)[0]["generated_text"]
|
11 |
+
return text # Return the generated caption
|
12 |
+
|
13 |
+
# Function to generate a story based on the caption
|
14 |
+
def text2story(text):
|
15 |
# Load the text generation model
|
16 |
+
story_model = pipeline("text-generation", model="gpt2")
|
17 |
+
# Generate a story based on the input text
|
18 |
+
story_text = story_model(f"Once upon a time, {text}.", max_length=100, num_return_sequences=1)
|
19 |
+
return story_text[0]["generated_text"] # Return the generated story
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# Function to convert text to audio
|
22 |
+
def text2audio(story_text):
|
23 |
+
# Load the text-to-speech model
|
24 |
+
text_to_audio_model = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
25 |
+
# Generate audio data from the story text
|
26 |
+
audio_data = text_to_audio_model(story_text)
|
27 |
+
return audio_data # Return the audio data
|
28 |
+
|
29 |
+
# Main part of the application
|
30 |
+
st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜") # Set the title and icon of the app
|
31 |
+
st.header("Storytelling From Your Image") # Header for the application
|
32 |
+
uploaded_file = st.file_uploader("Select an Image...", type=["jpg", "jpeg", "png"]) # File uploader for images
|
33 |
+
|
34 |
+
if uploaded_file is not None:
|
35 |
+
# Open and read the uploaded image
|
36 |
+
image = Image.open(uploaded_file) # Use PIL to open the uploaded image
|
37 |
+
st.image(image, caption="Uploaded Image", use_container_width=True) # Display the uploaded image
|
38 |
+
|
39 |
+
# Stage 1: Image to Text
|
40 |
+
st.text('Processing image to text...') # Inform the user about the processing stage
|
41 |
+
scenario = img2text(image) # Get the caption for the uploaded image
|
42 |
+
st.write("Caption:", scenario) # Display the generated caption
|
43 |
+
|
44 |
+
# Stage 2: Text to Story
|
45 |
+
st.text('Generating a story...') # Inform the user about the story generation stage
|
46 |
+
story = text2story(scenario) # Generate a story based on the caption
|
47 |
+
st.write("Story:", story) # Display the generated story
|
48 |
+
|
49 |
+
# Stage 3: Story to Audio data
|
50 |
+
st.text('Generating audio data...') # Inform the user about the audio generation stage
|
51 |
+
audio_data = text2audio(story) # Convert the generated story into audio
|
52 |
+
|
53 |
+
# Play button for the audio
|
54 |
+
if st.button("Play Audio"): # Create a button to play the audio
|
55 |
+
st.audio(audio_data['audio'], format="audio/wav", start_time=0, sample_rate=audio_data['sampling_rate']) # Play the audio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|