Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,103 +1,111 @@
|
|
| 1 |
-
from flask import Flask, render_template, request, jsonify, session, send_from_directory
|
| 2 |
-
import os
|
| 3 |
-
from llms import prompt_llm_image, prompt_llm, parse_bullet_points
|
| 4 |
-
app = Flask(__name__)
|
| 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 |
app.run(debug=True, port=5005)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify, session, send_from_directory
|
| 2 |
+
import os
|
| 3 |
+
from llms import prompt_llm_image, prompt_llm, parse_bullet_points
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
from flask import jsonify
|
| 7 |
+
|
| 8 |
+
@app.get("/health")
|
| 9 |
+
def health():
|
| 10 |
+
return jsonify(status="ok")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
app.secret_key = "32fe450cc58c8e1487b36b9e7f06d349f67afa85d4a074e151a75e5f39517d0c"
|
| 14 |
+
UPLOAD_FOLDER = "uploads"
|
| 15 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 16 |
+
|
| 17 |
+
@app.route("/")
|
| 18 |
+
def index():
|
| 19 |
+
return render_template("index.html")
|
| 20 |
+
|
| 21 |
+
@app.route("/upload", methods=["POST"])
|
| 22 |
+
def upload_image():
|
| 23 |
+
if "image" not in request.files:
|
| 24 |
+
return jsonify({"error": "No image uploaded"})
|
| 25 |
+
file = request.files["image"]
|
| 26 |
+
if file.filename == "":
|
| 27 |
+
return jsonify({"error": "No image selected"})
|
| 28 |
+
filename = file.filename
|
| 29 |
+
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
| 30 |
+
file.save(filepath)
|
| 31 |
+
# Analyze image with LLM
|
| 32 |
+
image_prompt = """
|
| 33 |
+
You are helpful museum guide who can explain the history of fashion items.
|
| 34 |
+
|
| 35 |
+
Analyze this fashion/clothing image and identify the 5 most interesting items.
|
| 36 |
+
|
| 37 |
+
For EACH item, provide:
|
| 38 |
+
1. A short name (8 words max)
|
| 39 |
+
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)
|
| 40 |
+
|
| 41 |
+
Format your response EXACTLY like this:
|
| 42 |
+
- Item name | x,y
|
| 43 |
+
|
| 44 |
+
Example:
|
| 45 |
+
- Elegant lace collar detail | 50,20
|
| 46 |
+
- Puffed sleeve with gathered fabric | 20,40
|
| 47 |
+
- Pearl button embellishments | 50,60
|
| 48 |
+
|
| 49 |
+
Give me exactly 5 items in this format.
|
| 50 |
+
"""
|
| 51 |
+
response = prompt_llm_image(filepath, image_prompt)
|
| 52 |
+
# Parse items with coordinates
|
| 53 |
+
items_with_coords = []
|
| 54 |
+
for line in response.strip().split('\n'):
|
| 55 |
+
line = line.strip()
|
| 56 |
+
if line.startswith('-') or line.startswith('•'):
|
| 57 |
+
line = line.lstrip('-•').strip()
|
| 58 |
+
if '|' in line:
|
| 59 |
+
parts = line.split('|')
|
| 60 |
+
item_name = parts[0].strip()
|
| 61 |
+
coords = parts[1].strip() if len(parts) > 1 else "50,50"
|
| 62 |
+
items_with_coords.append({"name": item_name, "coords": coords})
|
| 63 |
+
|
| 64 |
+
# Fallback if no coordinates found
|
| 65 |
+
if not items_with_coords:
|
| 66 |
+
bullet_list = parse_bullet_points(response)
|
| 67 |
+
items_with_coords = [{"name": item, "coords": "50,50"} for item in bullet_list]
|
| 68 |
+
session["image_path"] = filepath
|
| 69 |
+
session["items"] = items_with_coords
|
| 70 |
+
return jsonify({
|
| 71 |
+
"success": True,
|
| 72 |
+
"items": items_with_coords,
|
| 73 |
+
"image_filename": filename,
|
| 74 |
+
"raw_response": response
|
| 75 |
+
})
|
| 76 |
+
|
| 77 |
+
@app.route("/explain", methods=["POST"])
|
| 78 |
+
def explain_item():
|
| 79 |
+
data = request.get_json()
|
| 80 |
+
item_index = data.get("item_index")
|
| 81 |
+
if "items" not in session or item_index >= len(session["items"]):
|
| 82 |
+
return jsonify({"error": "Invalid item selection"})
|
| 83 |
+
item_data = session["items"][item_index]
|
| 84 |
+
item = item_data["name"] if isinstance(item_data, dict) else item_data
|
| 85 |
+
history_prompt = f"""
|
| 86 |
+
explain the item: {item}
|
| 87 |
+
* instructions:
|
| 88 |
+
- a short quick history time period of the item
|
| 89 |
+
- make your response structured as follows:
|
| 90 |
+
# History of the item
|
| 91 |
+
- Circa 1890
|
| 92 |
+
# Where is it popular
|
| 93 |
+
- United States
|
| 94 |
+
# What is it made of
|
| 95 |
+
- Gold
|
| 96 |
+
# What is it used for
|
| 97 |
+
- Jewelry
|
| 98 |
+
# Who made it
|
| 99 |
+
- Tiffany & Co.
|
| 100 |
+
# Who owned it
|
| 101 |
+
- John D. Rockefeller
|
| 102 |
+
"""
|
| 103 |
+
explanation = prompt_llm(history_prompt)
|
| 104 |
+
return jsonify({"success": True, "item": item, "explanation": explanation})
|
| 105 |
+
|
| 106 |
+
@app.route("/uploads/<filename>")
|
| 107 |
+
def uploaded_file(filename):
|
| 108 |
+
return send_from_directory(UPLOAD_FOLDER, filename)
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
app.run(debug=True, port=5005)
|