Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,19 @@
|
|
1 |
# import part
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
|
|
|
|
4 |
|
5 |
# function part
|
6 |
-
|
7 |
-
def generate_image_caption(image_path):
|
8 |
"""Generates a caption for the given image using a pre-trained model."""
|
9 |
img2caption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
11 |
return result[0]['generated_text']
|
12 |
|
13 |
# text2story
|
@@ -16,9 +22,6 @@ def text2story(text):
|
|
16 |
story_text = pipe(text)[0]['generated_text']
|
17 |
return story_text
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
def main():
|
23 |
# App title
|
24 |
st.title("Streamlit Demo on Hugging Face")
|
@@ -29,13 +32,11 @@ def main():
|
|
29 |
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
30 |
|
31 |
if uploaded_image is not None:
|
32 |
-
|
33 |
st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
|
34 |
|
35 |
-
|
36 |
-
#Stage 1: Image to Text
|
37 |
st.text('Processing img2text...')
|
38 |
-
image_caption = generate_image_caption(uploaded_image
|
39 |
st.write(image_caption)
|
40 |
|
41 |
if __name__ == "__main__":
|
|
|
1 |
# import part
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
|
7 |
# function part
|
8 |
+
def generate_image_caption(image_file):
|
|
|
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 |
+
# Read the image file
|
13 |
+
image = Image.open(image_file)
|
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")
|
|
|
32 |
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
33 |
|
34 |
if uploaded_image is not None:
|
|
|
35 |
st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
|
36 |
|
37 |
+
# Stage 1: Image to Text
|
|
|
38 |
st.text('Processing img2text...')
|
39 |
+
image_caption = generate_image_caption(uploaded_image)
|
40 |
st.write(image_caption)
|
41 |
|
42 |
if __name__ == "__main__":
|