joey1101 commited on
Commit
815a364
·
verified ·
1 Parent(s): 7d50472

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -29
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 transformer pipeline
4
- from PIL import Image # Python Imaging Library for image handling
5
 
6
- # Set the title of the Streamlit app
7
- st.set_page_config(page_title="Storytelling Friend",
8
- page_icon="haha")
9
- # Title of the application
 
 
 
 
 
10
 
11
- # Create a file uploader for the image
12
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) # User uploads an image
 
 
 
13
 
14
- # Load the image captioning model
15
- caption_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large") # Load pre-trained model
 
 
16
 
17
- # Load the text generation model
18
- story_model = pipeline("text-generation", model="gpt2") # Load a text generation model
 
 
 
 
 
19
 
20
- # Function to generate a story from the caption
21
- def generate_story(caption): # Define a function to generate a story
22
- story_input = f"Once upon a time, {caption}" # Create a story prompt
23
- story = story_model(story_input, max_length=150, num_return_sequences=1)[0]['generated_text'] # Generate the story
24
- return story # Return the generated story
25
 
26
- # Process the uploaded image and generate story
27
- if uploaded_file is not None: # Check if a file is uploaded
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
- caption = caption_model(image)[0]['caption'] # Generate caption from the image
32
- st.subheader("Image Caption:") # Subtitle for the caption
33
- st.write(caption) # Display the caption
34
 
35
- # Generate story based on the caption
36
- story = generate_story(caption) # Call the story generation function
37
- st.subheader("Generated Story:") # Subtitle for the generated story
38
- st.write(story) # Display the generated story
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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