|
|
import os |
|
|
from flask import Flask, render_template, request |
|
|
from transformers import BlipProcessor, BlipForConditionalGeneration |
|
|
from PIL import Image |
|
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
UPLOAD_FOLDER = "static/uploads" |
|
|
os.makedirs(UPLOAD_FOLDER, exist_ok=True) |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
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) |
|
|
|