Spaces:
Sleeping
Sleeping
wavesoumen
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,28 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
import requests
|
4 |
-
from transformers import BlipProcessor, BlipForConditionalGeneration
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
9 |
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
inputs = processor(raw_image, text, return_tensors="pt")
|
16 |
-
out = model.generate(**inputs)
|
17 |
-
conditional_caption = processor.decode(out[0], skip_special_tokens=True)
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
def main():
|
27 |
-
st.title("Image Captioning App")
|
28 |
-
|
29 |
-
img_url = st.text_input("Enter the image URL:")
|
30 |
-
if img_url:
|
31 |
-
try:
|
32 |
-
# Display the image
|
33 |
-
image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
|
34 |
-
st.image(image, caption='Input Image', use_column_width=True)
|
35 |
-
|
36 |
-
# Generate captions
|
37 |
-
conditional_caption, unconditional_caption = generate_caption(img_url)
|
38 |
-
|
39 |
-
# Display captions
|
40 |
-
st.subheader("Conditional Image Caption")
|
41 |
-
st.write(conditional_caption)
|
42 |
-
|
43 |
-
st.subheader("Unconditional Image Caption")
|
44 |
-
st.write(unconditional_caption)
|
45 |
-
except Exception as e:
|
46 |
-
st.error(f"Error processing the image: {e}")
|
47 |
-
|
48 |
-
if __name__ == "__main__":
|
49 |
-
main()
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline
|
|
|
|
|
3 |
|
4 |
+
# Initialize the image captioning pipeline
|
5 |
+
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
|
|
6 |
|
7 |
+
# Streamlit app title
|
8 |
+
st.title("Image to Text Captioning")
|
9 |
|
10 |
+
# Input for image URL
|
11 |
+
image_url = st.text_input("Enter the URL of the image:")
|
|
|
|
|
|
|
12 |
|
13 |
+
# If an image URL is provided
|
14 |
+
if image_url:
|
15 |
+
try:
|
16 |
+
# Display the image
|
17 |
+
st.image(image_url, caption="Provided Image", use_column_width=True)
|
18 |
+
|
19 |
+
# Generate the caption
|
20 |
+
caption = captioner(image_url)
|
21 |
+
|
22 |
+
# Display the caption
|
23 |
+
st.write("**Generated Caption:**")
|
24 |
+
st.write(caption[0]['generated_text'])
|
25 |
+
except Exception as e:
|
26 |
+
st.error(f"An error occurred: {e}")
|
27 |
|
28 |
+
# To run this app, save this code to a file (e.g., `app.py`) and run `streamlit run app.py` in your terminal.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|