CallmeKaito commited on
Commit
e67b575
1 Parent(s): ad49fb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -26
app.py CHANGED
@@ -1,34 +1,35 @@
1
- import streamlit as st
 
2
  from PIL import Image
3
- import io
4
 
5
- st.title("Artisan Product Submission Form")
 
 
6
 
7
- uploaded_file = st.file_uploader("Choose a file", type=["png", "jpg", "jpeg"])
8
-
9
- if uploaded_file is not None:
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
- # To display the image
16
- image = Image.open(io.BytesIO(bytes_data))
17
- st.image(image, caption='Uploaded Image.', use_column_width=True)
 
 
 
 
18
 
19
- # Creating text input box
 
 
 
20
 
21
- st.header("Tell us about your product")
 
 
22
 
23
- # Input fields
24
- product_type = st.text_input("Type of Product", placeholder="e.g., Handmade Jewelry, Pottery, Painting")
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
- # Submit button
29
- if st.button("Submit"):
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()