berkaydun commited on
Commit
5888e2a
Β·
verified Β·
1 Parent(s): 1706d00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -10
app.py CHANGED
@@ -7,15 +7,15 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
 
8
  model_name = "Salesforce/blip-image-captioning-base"
9
  processor = BlipProcessor.from_pretrained(model_name)
10
-
11
- # Bu şekilde yap!
12
  model = BlipForConditionalGeneration.from_pretrained(model_name)
13
- model.load_state_dict(torch.load("model_chunk_21.pt", map_location=device))
 
 
 
 
14
  model.to(device)
15
  model.eval()
16
 
17
-
18
- # Caption tahmini yapan fonksiyon
19
  def predict_caption(image):
20
  inputs = processor(image, return_tensors="pt").to(device)
21
  with torch.no_grad():
@@ -23,17 +23,14 @@ def predict_caption(image):
23
  caption = processor.decode(output_ids[0], skip_special_tokens=True)
24
  return caption
25
 
26
-
27
- # Streamlit ArayΓΌzΓΌ
28
  st.title("Image Captioning with BLIP")
29
  st.write("Upload an image and get a generated caption!")
30
 
31
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
32
-
33
  if uploaded_file is not None:
34
  image = Image.open(uploaded_file).convert("RGB")
35
  st.image(image, caption="Uploaded Image", use_column_width=True)
36
 
37
  if st.button("Generate Caption"):
38
  caption = predict_caption(image)
39
- st.markdown(f"**Caption:** {caption}")
 
7
 
8
  model_name = "Salesforce/blip-image-captioning-base"
9
  processor = BlipProcessor.from_pretrained(model_name)
 
 
10
  model = BlipForConditionalGeneration.from_pretrained(model_name)
11
+
12
+ # βœ… PyTorch 2.6 fix
13
+ state_dict = torch.load("model_chunk_21.pt", map_location=device, weights_only=False)
14
+ model.load_state_dict(state_dict)
15
+
16
  model.to(device)
17
  model.eval()
18
 
 
 
19
  def predict_caption(image):
20
  inputs = processor(image, return_tensors="pt").to(device)
21
  with torch.no_grad():
 
23
  caption = processor.decode(output_ids[0], skip_special_tokens=True)
24
  return caption
25
 
 
 
26
  st.title("Image Captioning with BLIP")
27
  st.write("Upload an image and get a generated caption!")
28
 
29
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
 
30
  if uploaded_file is not None:
31
  image = Image.open(uploaded_file).convert("RGB")
32
  st.image(image, caption="Uploaded Image", use_column_width=True)
33
 
34
  if st.button("Generate Caption"):
35
  caption = predict_caption(image)
36
+ st.markdown(f"**Caption:** {caption}")