from flask import Flask, render_template, request, jsonify, session, send_from_directory import os from llms import prompt_llm_image, prompt_llm, parse_bullet_points app = Flask(__name__) from flask import jsonify, Response @app.route("/health", methods=["GET"]) def health(): return jsonify(status="ok") @app.route("/ping", methods=["GET"]) def ping(): return Response("pong", mimetype="text/plain") app.secret_key = "32fe450cc58c8e1487b36b9e7f06d349f67afa85d4a074e151a75e5f39517d0c" UPLOAD_FOLDER = "uploads" os.makedirs(UPLOAD_FOLDER, exist_ok=True) @app.route("/") def index(): return render_template("index.html") @app.route("/upload", methods=["POST"]) def upload_image(): if "image" not in request.files: return jsonify({"error": "No image uploaded"}) file = request.files["image"] if file.filename == "": return jsonify({"error": "No image selected"}) filename = file.filename filepath = os.path.join(UPLOAD_FOLDER, filename) file.save(filepath) # Analyze image with LLM image_prompt = """ You are helpful museum guide who can explain the history of fashion items. Analyze this fashion/clothing image and identify the 5 most interesting items. For EACH item, provide: 1. A short name (8 words max) 2. The approximate location as a percentage from top-left corner (x, y coordinates where 0,0 is top-left and 100,100 is bottom-right) Format your response EXACTLY like this: - Item name | x,y Example: - Elegant lace collar detail | 50,20 - Puffed sleeve with gathered fabric | 20,40 - Pearl button embellishments | 50,60 Give me exactly 5 items in this format. """ response = prompt_llm_image(filepath, image_prompt) # Parse items with coordinates items_with_coords = [] for line in response.strip().split('\n'): line = line.strip() if line.startswith('-') or line.startswith('•'): line = line.lstrip('-•').strip() if '|' in line: parts = line.split('|') item_name = parts[0].strip() coords = parts[1].strip() if len(parts) > 1 else "50,50" items_with_coords.append({"name": item_name, "coords": coords}) # Fallback if no coordinates found if not items_with_coords: bullet_list = parse_bullet_points(response) items_with_coords = [{"name": item, "coords": "50,50"} for item in bullet_list] session["image_path"] = filepath session["items"] = items_with_coords return jsonify({ "success": True, "items": items_with_coords, "image_filename": filename, "raw_response": response }) @app.route("/explain", methods=["POST"]) def explain_item(): data = request.get_json() item_index = data.get("item_index") if "items" not in session or item_index >= len(session["items"]): return jsonify({"error": "Invalid item selection"}) item_data = session["items"][item_index] item = item_data["name"] if isinstance(item_data, dict) else item_data history_prompt = f""" explain the item: {item} * instructions: - a short quick history time period of the item - make your response structured as follows: # History of the item - Circa 1890 # Where is it popular - United States # What is it made of - Gold # What is it used for - Jewelry # Who made it - Tiffany & Co. # Who owned it - John D. Rockefeller """ explanation = prompt_llm(history_prompt) return jsonify({"success": True, "item": item, "explanation": explanation}) @app.route("/uploads/") def uploaded_file(filename): return send_from_directory(UPLOAD_FOLDER, filename) if __name__ == "__main__": import os app.run(host="0.0.0.0", port=int(os.getenv("PORT", 7860)))