Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from flask import Flask, render_template, request
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Flask setup
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
UPLOAD_FOLDER = "static/uploads"
|
| 10 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
# Hugging Face BLIP model
|
| 13 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 14 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 15 |
+
|
| 16 |
+
# OpenAI client
|
| 17 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 18 |
+
|
| 19 |
+
def generate_caption(image_path):
|
| 20 |
+
"""Generate caption from image using BLIP."""
|
| 21 |
+
raw_image = Image.open(image_path).convert("RGB")
|
| 22 |
+
inputs = processor(raw_image, return_tensors="pt")
|
| 23 |
+
out = model.generate(**inputs, max_new_tokens=50)
|
| 24 |
+
return processor.decode(out[0], skip_special_tokens=True)
|
| 25 |
+
|
| 26 |
+
def process_with_openai(user_message, caption=None):
|
| 27 |
+
"""Send user + image caption to OpenAI API."""
|
| 28 |
+
context = ""
|
| 29 |
+
if caption:
|
| 30 |
+
context += f"The user uploaded an image. BLIP caption: '{caption}'.\n"
|
| 31 |
+
if user_message:
|
| 32 |
+
context += f"User message: {user_message}\n"
|
| 33 |
+
|
| 34 |
+
if not context.strip():
|
| 35 |
+
return "Please enter a message or upload an image."
|
| 36 |
+
|
| 37 |
+
response = client.chat.completions.create(
|
| 38 |
+
model="gpt-4o-mini",
|
| 39 |
+
messages=[
|
| 40 |
+
{"role": "system", "content": "You are FUTURE_ON AI assistant. Combine image info + text queries."},
|
| 41 |
+
{"role": "user", "content": context}
|
| 42 |
+
]
|
| 43 |
+
)
|
| 44 |
+
return response.choices[0].message.content.strip()
|
| 45 |
+
|
| 46 |
+
@app.route("/", methods=["GET", "POST"])
|
| 47 |
+
@app.route("/Query.html", methods=["GET", "POST"])
|
| 48 |
+
def query():
|
| 49 |
+
filename = None
|
| 50 |
+
user_message = None
|
| 51 |
+
bot_response = None
|
| 52 |
+
|
| 53 |
+
if request.method == "POST":
|
| 54 |
+
user_message = request.form.get("message-input")
|
| 55 |
+
file = request.files.get("file-input")
|
| 56 |
+
|
| 57 |
+
caption = None
|
| 58 |
+
if file and file.filename != "":
|
| 59 |
+
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
|
| 60 |
+
file.save(filepath)
|
| 61 |
+
filename = filepath
|
| 62 |
+
# BLIP generates caption
|
| 63 |
+
caption = generate_caption(filepath)
|
| 64 |
+
|
| 65 |
+
# Pass text + BLIP caption to OpenAI
|
| 66 |
+
bot_response = process_with_openai(user_message, caption)
|
| 67 |
+
|
| 68 |
+
return render_template("query.html",
|
| 69 |
+
filename=filename,
|
| 70 |
+
user_message=user_message,
|
| 71 |
+
bot_response=bot_response)
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
app.run(host="0.0.0.0", port=7860)
|