Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,19 @@
|
|
1 |
-
#
|
|
|
|
|
|
|
2 |
import streamlit as st
|
3 |
-
from PIL import Image
|
4 |
-
import time
|
5 |
from transformers import pipeline
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
def generate_image_caption(
|
9 |
"""Generates a caption for the given image using a pre-trained model."""
|
10 |
img2caption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
11 |
-
|
|
|
|
|
12 |
return result[0]['generated_text']
|
13 |
|
14 |
# text2story
|
@@ -17,21 +22,23 @@ def text2story(text):
|
|
17 |
story_text = pipe(text)[0]['generated_text']
|
18 |
return story_text
|
19 |
|
20 |
-
|
21 |
-
# App title
|
22 |
-
st.title("
|
23 |
|
24 |
-
# Write some text
|
25 |
-
st.write("
|
26 |
|
27 |
-
|
28 |
-
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
with st.spinner("Loading image..."): # make sure this line ends with a colon!
|
33 |
-
time.sleep(1) # Simulate a delay
|
34 |
-
image = Image.open(uploaded_image)
|
35 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This is NOT a complete program
|
2 |
+
|
3 |
+
|
4 |
+
# import part
|
5 |
import streamlit as st
|
|
|
|
|
6 |
from transformers import pipeline
|
7 |
+
from PIL import Image
|
8 |
+
import io
|
9 |
|
10 |
+
# function part
|
11 |
+
def generate_image_caption(image):
|
12 |
"""Generates a caption for the given image using a pre-trained model."""
|
13 |
img2caption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
14 |
+
|
15 |
+
# Generate caption
|
16 |
+
result = img2caption(image)
|
17 |
return result[0]['generated_text']
|
18 |
|
19 |
# text2story
|
|
|
22 |
story_text = pipe(text)[0]['generated_text']
|
23 |
return story_text
|
24 |
|
25 |
+
def main():
|
26 |
+
# App title
|
27 |
+
st.title("Streamlit Demo on Hugging Face")
|
28 |
|
29 |
+
# Write some text
|
30 |
+
st.write("Welcome to a demo app showcasing basic Streamlit components!")
|
31 |
|
32 |
+
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
|
|
33 |
|
34 |
+
if uploaded_image is not None:
|
35 |
+
image = Image.open(uploaded_image).convert("RGB") # Create an image object
|
|
|
|
|
|
|
36 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
37 |
+
|
38 |
+
# Stage 1: Image to Text
|
39 |
+
st.text('Processing img2text...')
|
40 |
+
image_caption = generate_image_caption(image)
|
41 |
+
st.write(image_caption)
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
main()
|