File size: 1,502 Bytes
2fbf461
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from flask import Flask, request, jsonify
from flask_cors import CORS
from transformers import pipeline
import requests
from PIL import Image
from decouple import config
from io import BytesIO
app = Flask(__name__)
CORS(app, resources={r"/generate_caption": {"origins": "https://www.google.com"}})
CORS(app)

# Initialize the pipeline for image captioning
model_name = config('BLIP_MODEL_NAME', default="Salesforce/blip-image-captioning-large")
pipe = pipeline("image-to-text", model=model_name)

@app.route('/generate_caption', methods=['POST'])
def generate_caption():
    try:
        print("Received a request to generate captions for images.")
        image_urls = request.json.get('image_urls')
        results = []

        for image_url in image_urls:
            # Load the image from the URL
            response = requests.get(image_url)
            image = Image.open(BytesIO(response.content))

            # Use the pipeline to generate the caption
            generated_caption = pipe(image)[0]['generated_text']

            # Create a dictionary with image URL and caption
            result_item = {
                "image_url": image_url,
                "caption": generated_caption
            }

            results.append(result_item)

        print("===================results===========")
        print(results)
        return jsonify(results)
    except Exception as e:
        print(e)
        return jsonify({'error': str(e)}), 400

if __name__ == '__main__':
    app.run()