Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,38 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
-
from transformers import
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
-
|
| 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 |
-
|
| 21 |
-
|
| 22 |
-
return jsonify({"error": "No image file provided"}), 400
|
| 23 |
|
|
|
|
| 24 |
image_file = request.files['image']
|
| 25 |
-
image = Image.open(image_file.
|
| 26 |
|
| 27 |
-
|
| 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 |
-
|
|
|
|
| 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)
|