Spaces:
Sleeping
Sleeping
sudipanpodder
commited on
Commit
•
74c9d23
1
Parent(s):
4cf6d35
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import requests
|
4 |
+
from io import BytesIO
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
# Models used for caption generation
|
8 |
+
transformer_models = {
|
9 |
+
"Model 1": "Salesforce/blip-image-captioning-base", # https://huggingface.co/Salesforce/blip-image-captioning-base
|
10 |
+
"Model 2": "Salesforce/blip-image-captioning-large", # https://huggingface.co/Salesforce/blip-image-captioning-large
|
11 |
+
"Model 3": "ydshieh/vit-gpt2-coco-en" # https://huggingface.co/ydshieh/vit-gpt2-coco-en
|
12 |
+
}
|
13 |
+
|
14 |
+
# Function to generate captions
|
15 |
+
def generate_captions(image, model_name):
|
16 |
+
caption_generator = pipeline('image-to-text', model=model_name)
|
17 |
+
captions = caption_generator(image)
|
18 |
+
return captions
|
19 |
+
|
20 |
+
|
21 |
+
def main():
|
22 |
+
st.title("IMGWhisper: An Image Caption Generator")
|
23 |
+
|
24 |
+
image_source = st.radio("Choose image source:", ("Upload an image", "Provide an image URL"))
|
25 |
+
|
26 |
+
# Load the image
|
27 |
+
if image_source == "Upload an image":
|
28 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
29 |
+
if uploaded_file is not None:
|
30 |
+
image = Image.open(uploaded_file)
|
31 |
+
else:
|
32 |
+
image_url = st.text_input("Enter the image URL")
|
33 |
+
if image_url:
|
34 |
+
try:
|
35 |
+
response = requests.get(image_url)
|
36 |
+
image = Image.open(BytesIO(response.content))
|
37 |
+
except:
|
38 |
+
st.error("Error: Failed to load image from the provided URL")
|
39 |
+
|
40 |
+
# Display the image
|
41 |
+
if "image" in locals():
|
42 |
+
st.image(image, caption="Uploaded/Provided Image", width=300)
|
43 |
+
|
44 |
+
num_captions = st.slider("How many captions do you want to generate?", min_value=1, max_value=3, value=1)
|
45 |
+
|
46 |
+
if st.button("Generate Caption"):
|
47 |
+
captions = []
|
48 |
+
for i in range(num_captions):
|
49 |
+
model_name = transformer_models[f"Model {i+1}"]
|
50 |
+
caption = generate_captions(image, model_name)
|
51 |
+
captions.append(caption[0]['generated_text'])
|
52 |
+
|
53 |
+
# Display the captions
|
54 |
+
st.header("Generated Captions:")
|
55 |
+
for i, caption in enumerate(captions):
|
56 |
+
st.subheader(f"Caption {i+1}: {caption}")
|
57 |
+
|
58 |
+
# Run the app
|
59 |
+
if __name__ == "__main__":
|
60 |
+
main()
|