Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,64 @@
|
|
| 1 |
# Import necessary libraries
|
| 2 |
-
import streamlit as st # Streamlit for web application
|
| 3 |
-
from transformers import pipeline # Hugging Face
|
| 4 |
-
from PIL import Image #
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
def
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
return story # Return the generated story
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
image = Image.open(uploaded_file) # Open the uploaded image
|
| 29 |
-
st.image(image, caption="Uploaded Image", use_column_width=True) # Display the uploaded image
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
st.write(caption) # Display the caption
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Import necessary libraries
|
| 2 |
+
import streamlit as st # Streamlit for creating the web application
|
| 3 |
+
from transformers import pipeline # Pipeline for using Hugging Face models
|
| 4 |
+
from PIL import Image # PIL for image processing
|
| 5 |
|
| 6 |
+
# Function to load models
|
| 7 |
+
def load_models():
|
| 8 |
+
# Load the image to text model
|
| 9 |
+
caption_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large") # Load pre-trained image to text model
|
| 10 |
+
# Load the text generation model
|
| 11 |
+
story_model = pipeline("text-generation", model="gpt2") # Load pre-trained text generation model
|
| 12 |
+
# Load the text-to-speech model
|
| 13 |
+
tts_model = pipeline("text-to-speech", model="facebook/tts_transformer-es-css10") # Load a TTS model
|
| 14 |
+
return caption_model, story_model, tts_model # Return all three models
|
| 15 |
|
| 16 |
+
# Function to generate story from caption
|
| 17 |
+
def generate_story(caption):
|
| 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 text_to_audio(text, tts_model):
|
| 24 |
+
audio = tts_model(text) # Generate audio from text using the TTS model
|
| 25 |
+
return audio # Return the audio object
|
| 26 |
|
| 27 |
+
# Function to process the uploaded image and generate a story
|
| 28 |
+
def process_image(image, caption_model):
|
| 29 |
+
# Generate a caption from the uploaded image
|
| 30 |
+
caption = caption_model(image)[0]['caption'] # Get the caption from the model
|
| 31 |
+
# Generate a story from the caption
|
| 32 |
+
story = generate_story(caption) # Call the story generation function
|
| 33 |
+
return caption, story # Return both caption and story
|
| 34 |
|
| 35 |
+
# Main part
|
| 36 |
+
def main():
|
| 37 |
+
st.set_page_config(page_title="Storytelling Friend", page_icon="🦦") # Title of the application
|
| 38 |
+
st.write("Upload an image to generate a story!") # Instructions for the user
|
|
|
|
| 39 |
|
| 40 |
+
# Upload image section
|
| 41 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) # File uploader for images
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Load models once
|
| 44 |
+
caption_model, story_model, tts_model = load_models() # Load models
|
|
|
|
| 45 |
|
| 46 |
+
if uploaded_file is not None:
|
| 47 |
+
# Open and read the uploaded image
|
| 48 |
+
image = Image.open(uploaded_file) # Open the uploaded image file
|
| 49 |
+
st.image(image, caption="Uploaded Image", use_column_width=True) # Display the uploaded image
|
| 50 |
+
|
| 51 |
+
# Process the image and generate story
|
| 52 |
+
caption, story = process_image(image, caption_model) # Get caption and story
|
| 53 |
+
st.subheader("Generated Caption:") # Subheader for caption
|
| 54 |
+
st.write(caption) # Display the caption
|
| 55 |
+
st.subheader("Generated Story:") # Subheader for story
|
| 56 |
+
st.write(story) # Display the generated story
|
| 57 |
+
|
| 58 |
+
# Convert story to audio and play it
|
| 59 |
+
audio = text_to_audio(story, tts_model) # Convert story to audio
|
| 60 |
+
st.audio(audio, format='audio/wav') # Play the audio
|
| 61 |
+
|
| 62 |
+
# Run the app
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
main() # Call the main function to run the app
|