berkaydun commited on
Commit
91b95fb
Β·
verified Β·
1 Parent(s): 896c13b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -40
app.py CHANGED
@@ -1,41 +1,41 @@
1
- import streamlit as st
2
- from PIL import Image
3
- import torch
4
- from transformers import BlipProcessor, BlipForConditionalGeneration
5
-
6
- # Cihaz ayarΔ±
7
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
-
9
- # Model ve processor yΓΌklemesi
10
- model_name = "Salesforce/blip-image-captioning-base"
11
- processor = BlipProcessor.from_pretrained(model_name)
12
- model = BlipForConditionalGeneration.from_pretrained(model_name)
13
-
14
- # Eğittiğin modeli yükle
15
- model.load_state_dict(torch.load("model_chunk_21.pt", map_location=device))
16
- model.to(device)
17
- model.eval()
18
-
19
-
20
- # Caption tahmini yapan fonksiyon
21
- def predict_caption(image):
22
- inputs = processor(image, return_tensors="pt").to(device)
23
- with torch.no_grad():
24
- output_ids = model.generate(**inputs, max_length=16, num_beams=4)
25
- caption = processor.decode(output_ids[0], skip_special_tokens=True)
26
- return caption
27
-
28
-
29
- # Streamlit ArayΓΌzΓΌ
30
- st.title("Image Captioning with BLIP")
31
- st.write("Upload an image and get a generated caption!")
32
-
33
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
34
-
35
- if uploaded_file is not None:
36
- image = Image.open(uploaded_file).convert("RGB")
37
- st.image(image, caption="Uploaded Image", use_column_width=True)
38
-
39
- if st.button("Generate Caption"):
40
- caption = predict_caption(image)
41
  st.markdown(f"**Caption:** {caption}")
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import torch
4
+ from transformers import BlipProcessor, BlipForConditionalGeneration
5
+
6
+ # Cihaz ayarΔ±
7
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
+
9
+ # Model ve processor yΓΌklemesi
10
+ model_name = "Salesforce/blip-image-captioning-base"
11
+ processor = BlipProcessor.from_pretrained(model_name)
12
+ model = BlipForConditionalGeneration.from_pretrained(model_name)
13
+
14
+ # Eğittiğin modeli yükle
15
+ model.load_state_dict(torch.load("model_chunk_21.pt", map_location=device,weights_only=False))
16
+ model.to(device)
17
+ model.eval()
18
+
19
+
20
+ # Caption tahmini yapan fonksiyon
21
+ def predict_caption(image):
22
+ inputs = processor(image, return_tensors="pt").to(device)
23
+ with torch.no_grad():
24
+ output_ids = model.generate(**inputs, max_length=16, num_beams=4)
25
+ caption = processor.decode(output_ids[0], skip_special_tokens=True)
26
+ return caption
27
+
28
+
29
+ # Streamlit ArayΓΌzΓΌ
30
+ st.title("Image Captioning with BLIP")
31
+ st.write("Upload an image and get a generated caption!")
32
+
33
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
34
+
35
+ if uploaded_file is not None:
36
+ image = Image.open(uploaded_file).convert("RGB")
37
+ st.image(image, caption="Uploaded Image", use_column_width=True)
38
+
39
+ if st.button("Generate Caption"):
40
+ caption = predict_caption(image)
41
  st.markdown(f"**Caption:** {caption}")