Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,38 @@
|
|
1 |
-
# Import
|
2 |
-
import streamlit as st
|
3 |
-
from transformers import pipeline
|
4 |
-
from PIL import Image
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
return results
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
|
16 |
-
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
|
|
|
|
24 |
|
25 |
-
#
|
26 |
-
st.subheader("Image
|
27 |
-
st.write(
|
|
|
|
|
|
|
|
|
|
|
|
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
|