malavika-2016 commited on
Commit
50e7ed6
·
verified ·
1 Parent(s): 43e29a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -18
app.py CHANGED
@@ -1,42 +1,38 @@
1
  from flask import Flask, request, jsonify
2
- from transformers import BlipImageProcessor, AutoTokenizer, BlipForConditionalGeneration
3
  from PIL import Image
4
  import torch
 
5
 
6
  app = Flask(__name__)
7
 
8
- # Load components properly
9
- image_processor = BlipImageProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
10
- tokenizer = AutoTokenizer.from_pretrained("Salesforce/blip-image-captioning-base")
11
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
12
  model.eval()
13
 
14
- # Set device
15
  device = "cuda" if torch.cuda.is_available() else "cpu"
16
  model.to(device)
17
 
18
  @app.route("/caption", methods=["POST"])
19
  def caption_image():
20
- try:
21
- if 'image' not in request.files:
22
- return jsonify({"error": "No image file provided"}), 400
23
 
 
24
  image_file = request.files['image']
25
- image = Image.open(image_file.stream).convert("RGB")
26
 
27
- # Process image and caption
28
- pixel_values = image_processor(images=image, return_tensors="pt").pixel_values.to(device)
29
- generated_ids = model.generate(pixel_values)
30
- caption = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
31
 
32
- return jsonify({"caption": caption})
 
33
 
 
 
 
34
  except Exception as e:
35
  return jsonify({"error": str(e)}), 500
36
 
37
- @app.route("/")
38
- def home():
39
- return "BLIP Captioning API is running!"
40
-
41
  if __name__ == "__main__":
42
  app.run(host="0.0.0.0", port=7860)
 
1
  from flask import Flask, request, jsonify
2
+ from transformers import BlipProcessor, BlipForConditionalGeneration
3
  from PIL import Image
4
  import torch
5
+ import io
6
 
7
  app = Flask(__name__)
8
 
9
+ # Load model and processor
10
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
 
11
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
12
  model.eval()
13
 
 
14
  device = "cuda" if torch.cuda.is_available() else "cpu"
15
  model.to(device)
16
 
17
  @app.route("/caption", methods=["POST"])
18
  def caption_image():
19
+ if 'image' not in request.files:
20
+ return jsonify({"error": "No image file provided"}), 400
 
21
 
22
+ try:
23
  image_file = request.files['image']
24
+ image = Image.open(io.BytesIO(image_file.read())).convert("RGB")
25
 
26
+ processed_image = processor(images=image, return_tensors="pt").pixel_values.to(device)
 
 
 
27
 
28
+ with torch.no_grad():
29
+ output = model.generate(processed_image)
30
 
31
+ caption = processor.decode(output[0], skip_special_tokens=True)
32
+ return jsonify({"caption": caption})
33
+
34
  except Exception as e:
35
  return jsonify({"error": str(e)}), 500
36
 
 
 
 
 
37
  if __name__ == "__main__":
38
  app.run(host="0.0.0.0", port=7860)