File size: 3,940 Bytes
ae58b77
 
 
 
5ab8ccc
ae58b77
5ab8ccc
ae58b77
 
 
5ab8ccc
 
 
19dd17f
 
ae58b77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a1b386
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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/<filename>")
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)))