MLInAi commited on
Commit
4e14b22
·
verified ·
1 Parent(s): 9e1ecfe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import BlipForConditionalGeneration, AutoTokenizer
3
+ import torch
4
+ from PIL import Image
5
+ from io import BytesIO
6
+
7
+ # Load the fine-tuned model
8
+ model_path = '/content/model_after_5_epochs.pth'
9
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
10
+ model.load_state_dict(torch.load(model_path))
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+ model.to(device)
13
+ tokenizer = AutoTokenizer.from_pretrained("Salesforce/blip-image-captioning-base")
14
+
15
+ # Function to generate caption for the uploaded image
16
+ def generate_caption(image):
17
+ # Preprocess the image
18
+ image = Image.open(image).convert("RGB")
19
+ image = image.resize((224, 224)) # Resize the image to match model input size
20
+ image_tensor = torch.tensor([torch.Tensor(image)]).permute(0, 3, 1, 2).to(device)
21
+
22
+ # Generate caption
23
+ output = model.generate(pixel_values=image_tensor)
24
+ caption = tokenizer.decode(output[0], skip_special_tokens=True)
25
+ return caption
26
+
27
+ # Streamlit app
28
+ st.title("Image Caption Generator")
29
+
30
+ uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg"])
31
+
32
+ if uploaded_image is not None:
33
+ st.image(uploaded_image, caption='Uploaded Image.', use_column_width=True)
34
+ st.write("")
35
+ st.write("Generating caption...")
36
+
37
+ # Generate caption for the uploaded image
38
+ caption = generate_caption(uploaded_image)
39
+ st.write("Caption:", caption)