File size: 1,465 Bytes
c53c419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba6748c
c53c419
 
 
 
 
 
 
 
 
 
 
 
 
ba6748c
c53c419
 
 
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
import os
from flask import Flask, render_template, request
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image

# Flask setup
app = Flask(__name__)
UPLOAD_FOLDER = "static/uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)

# Hugging Face BLIP model
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")

def generate_caption(image_path):
    """Generate caption from image using BLIP."""
    raw_image = Image.open(image_path).convert("RGB")
    inputs = processor(raw_image, return_tensors="pt")
    out = model.generate(**inputs, max_new_tokens=50)
    return processor.decode(out[0], skip_special_tokens=True)

@app.route("/", methods=["GET", "POST"])
@app.route("/Query.html", methods=["GET", "POST"])
def query():
    filename = None
    caption = None

    if request.method == "POST":
        file = request.files.get("file-input")

        if file and file.filename != "":
            filepath = os.path.join(UPLOAD_FOLDER, file.filename)
            file.save(filepath)
            filename = filepath
            # BLIP generates caption
            caption = generate_caption(filepath)

    return render_template("query.html",
                           filename=filename,
                           bot_response=caption)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)