Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,35 @@
|
|
1 |
-
import
|
|
|
2 |
from PIL import Image
|
3 |
-
import
|
4 |
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
# To read file as bytes:
|
11 |
-
bytes_data = uploaded_file.getvalue()
|
12 |
-
st.write("Filename: ", uploaded_file.name)
|
13 |
-
# st.write(bytes_data) # This will display the raw bytes, typically not useful for users
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
#
|
|
|
|
|
|
|
20 |
|
21 |
-
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
product_origin = st.text_input("Product Origin", placeholder="e.g., City, Country, Region")
|
26 |
-
product_description = st.text_area("Brief Description", placeholder="Provide a brief description of your product")
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
st.write("Thank you for your submission!")
|
31 |
-
st.write("### Product Details")
|
32 |
-
st.write(f"**Type of Product:** {product_type}")
|
33 |
-
st.write(f"**Product Origin:** {product_origin}")
|
34 |
-
st.write(f"**Description:** {product_description}")
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoProcessor, AutoModel, VisionEncoderDecoderModel, ViTFeatureExtractor, AutoTokenizer
|
3 |
from PIL import Image
|
4 |
+
import streamlit as st
|
5 |
|
6 |
+
# Load the saved model state dictionary
|
7 |
+
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
8 |
+
model.load_state_dict(torch.load("model.pth", map_location=torch.device('cpu')))
|
9 |
|
10 |
+
# Load the necessary components
|
11 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
# Function to generate a caption for an image
|
15 |
+
@st.cache_resource
|
16 |
+
def generate_caption(image):
|
17 |
+
pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values
|
18 |
+
output_ids = model.generate(pixel_values, max_length=100, num_beams=5, early_stopping=True)
|
19 |
+
caption = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]
|
20 |
+
return caption
|
21 |
|
22 |
+
# Streamlit app
|
23 |
+
def main():
|
24 |
+
st.title("Image Captioning")
|
25 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
26 |
|
27 |
+
if uploaded_file is not None:
|
28 |
+
image = Image.open(uploaded_file)
|
29 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
30 |
|
31 |
+
caption = generate_caption(image)
|
32 |
+
st.write(f"Caption: {caption}")
|
|
|
|
|
33 |
|
34 |
+
if __name__ == "__main__":
|
35 |
+
main()
|
|
|
|
|
|
|
|
|
|