PrabalPaul007 commited on
Commit
86d8974
·
verified ·
1 Parent(s): 20e3e38

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Libraries
2
+
3
+ import streamlit as st
4
+ from transformers import BlipForConditionalGeneration, AutoTokenizer
5
+ import torch
6
+ from PIL import Image
7
+ import torchvision.transforms as transforms
8
+
9
+ # Load the fine-tuned model and tokenizer
10
+ model = BlipForConditionalGeneration.from_pretrained("PrabalPaul007/Prabal_AI_ML_stable")
11
+ tokenizer = AutoTokenizer.from_pretrained("PrabalPaul007/Prabal_AI_ML_stable")
12
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
+
14
+ # Function to generate caption for the uploaded image
15
+ def generate_caption(image):
16
+ # Preprocess the image
17
+ image = Image.open(image).convert("RGB")
18
+ image = image.resize((224, 224)) # Resize the image to match model input size
19
+
20
+ # Convert the image to a tensor
21
+ transform = transforms.Compose([
22
+ transforms.ToTensor(),
23
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
24
+ ])
25
+ image_tensor = transform(image).unsqueeze(0).to(device)
26
+
27
+ # Generate caption
28
+ output = model.generate(pixel_values=image_tensor)
29
+ caption = tokenizer.decode(output[0], skip_special_tokens=True)
30
+ return caption
31
+
32
+
33
+
34
+ # Streamlit app
35
+ st.title("Cartoon Caption Generator")
36
+
37
+ uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg"])
38
+
39
+ if uploaded_image is not None:
40
+ st.image(uploaded_image, caption='Uploaded Image.', use_column_width=True)
41
+ st.write("")
42
+ st.write("Generating caption...")
43
+
44
+ # Generate caption for the uploaded image with the fixed prompt
45
+ caption = generate_caption(uploaded_image)
46
+ st.write("Caption:", caption)