Reshama commited on
Commit
ae58b77
·
verified ·
1 Parent(s): a0d00b3

Update app.py

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