joey1101 commited on
Commit
f34973b
·
verified ·
1 Parent(s): 5b0621a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -20
app.py CHANGED
@@ -1,27 +1,38 @@
1
- # Import convention
2
- import streamlit as st
3
- from transformers import pipeline
4
- from PIL import Image
5
 
6
- # Load the image-to-text pipeline
7
- image_read = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
 
 
8
 
9
- def read(image):
10
- results = image_read(image)
11
- return results
12
 
13
- # Streamlit UI
14
- st.title("Storytelling from images for 3-10 year-old kids")
15
 
16
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
 
17
 
18
- if uploaded_file is not None:
19
- image = Image.open(uploaded_file).convert("RGB")
20
- st.image(image, caption="Uploaded Image", use_column_width=True)
 
 
21
 
22
- # Read image
23
- image_content = read(image)
 
 
24
 
25
- # Display results
26
- st.subheader("Image Content:")
27
- st.write(f"Image Content: {image_content[0]}")
 
 
 
 
 
 
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-captioning", model="facebook/blip-image-captioning-base") # 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