Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,88 @@
|
|
| 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 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
import streamlit as st
|
| 32 |
+
import torch
|
| 33 |
+
import requests
|
| 34 |
+
from PIL import Image
|
| 35 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 36 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 37 |
+
import nltk
|
| 38 |
+
|
| 39 |
+
nltk.download('punkt')
|
| 40 |
+
|
| 41 |
+
@st.cache_resource
|
| 42 |
+
def load_models():
|
| 43 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 44 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 45 |
+
tokenizer = AutoTokenizer.from_pretrained("fabiochiu/t5-base-tag-generation")
|
| 46 |
+
model2 = AutoModelForSeq2SeqLM.from_pretrained("fabiochiu/t5-base-tag-generation")
|
| 47 |
+
return processor, model, tokenizer, model2
|
| 48 |
+
|
| 49 |
+
processor, model, tokenizer, model2 = load_models()
|
| 50 |
+
|
| 51 |
+
def get_image_caption_and_tags(img_url):
|
| 52 |
+
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
|
| 53 |
+
|
| 54 |
+
# conditional image captioning
|
| 55 |
+
alltexts = "a photography of"
|
| 56 |
+
inputs = processor(raw_image, alltexts, return_tensors="pt")
|
| 57 |
+
out = model.generate(**inputs)
|
| 58 |
+
conditional_caption = processor.decode(out[0], skip_special_tokens=True)
|
| 59 |
+
|
| 60 |
+
# unconditional image captioning
|
| 61 |
+
inputs = processor(raw_image, return_tensors="pt")
|
| 62 |
+
out = model.generate(**inputs)
|
| 63 |
+
unconditional_caption = processor.decode(out[0], skip_special_tokens=True)
|
| 64 |
+
|
| 65 |
+
inputs = tokenizer([alltexts], max_length=512, truncation=True, return_tensors="pt")
|
| 66 |
+
output = model2.generate(**inputs, num_beams=8, do_sample=True, min_length=10, max_length=64)
|
| 67 |
+
decoded_output = tokenizer.batch_decode(output, skip_special_tokens=True)[0]
|
| 68 |
+
tags = list(set(decoded_output.strip().split(", ")))
|
| 69 |
+
|
| 70 |
+
return raw_image, conditional_caption, unconditional_caption, tags
|
| 71 |
+
|
| 72 |
+
st.title('Image Captioning and Tag Generation')
|
| 73 |
+
|
| 74 |
+
img_url = st.text_input("Enter Image URL:")
|
| 75 |
|
| 76 |
+
if st.button("Generate Captions and Tags"):
|
| 77 |
+
with st.spinner('Processing...'):
|
| 78 |
+
try:
|
| 79 |
+
image, cond_caption, uncond_caption, tags = get_image_caption_and_tags(img_url)
|
| 80 |
+
st.image(image, caption='Input Image', use_column_width=True)
|
| 81 |
+
st.subheader("Conditional Caption:")
|
| 82 |
+
st.write(cond_caption)
|
| 83 |
+
st.subheader("Unconditional Caption:")
|
| 84 |
+
st.write(uncond_caption)
|
| 85 |
+
st.subheader("Generated Tags:")
|
| 86 |
+
st.write(", ".join(tags))
|
| 87 |
+
except Exception as e:
|
| 88 |
+
st.error(f"An error occurred: {e}")
|