forked from hana11112/AI_PROJ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
132 lines (99 loc) · 4.21 KB
/
Copy pathanalysis.py
File metadata and controls
132 lines (99 loc) · 4.21 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
import json
import io
import numpy as np
import tensorflow as tf
from PIL import Image
import google.generativeai as genai
from fastapi import APIRouter , File, UploadFile, Form
from fastapi.responses import JSONResponse
router = APIRouter()
genai.configure(api_key="")
gemini_model = genai.GenerativeModel('gemini-2.5-flash')
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
MODEL_PATH = 'phoenix_ai_model.h5'
CLASS_NAMES = ['gender_female', 'gender_male', 'kids', 'safe', 'unsafe']
try:
mobilenet_model = tf.keras.models.load_model(MODEL_PATH, compile=False)
print("AI System Online with FastAPI")
except Exception as e:
print(f"Error loading model: {e}")
mobilenet_model = None
async def get_dynamic_analysis_from_gemini(image_pil, detected_label, title="", description="", category=""):
prompt = f"""
The product is classified as '{detected_label}'.
User Context:
Title: {title}
Description: {description}
Category: {category}
Tasks:
1. Verify if the image matches the provided title, description, and category.
2. Estimate suitable 'min_age' and 'max_age'.
3. Confirm matching (True/False).
Return ONLY JSON:
{{"min_age": int, "max_age": int, "matching_confirmed": bool}}
"""
try:
response = gemini_model.generate_content([prompt, image_pil])
raw_text = response.text.strip()
start = raw_text.find("{")
end = raw_text.rfind("}") + 1
if start != -1 and end != -1:
clean_json = raw_text[start:end]
return json.loads(clean_json)
else:
raise ValueError("Invalid JSON from Gemini")
except Exception:
return {"min_age": 12, "max_age": 60, "matching_confirmed": True}
@router.post("/analyze-product")
async def analyze_endpoint(
image: UploadFile = File(...),
title: str = Form(""),
description: str = Form(""),
category: str = Form("")
):
if mobilenet_model is None:
return JSONResponse(content={"error": "AI Model not loaded"}, status_code=500)
try:
image_bytes = await image.read()
pil_img = Image.open(io.BytesIO(image_bytes)).convert('RGB')
img_resized = pil_img.resize((224, 224))
img_array = np.array(img_resized) / 255.0
img_array = np.expand_dims(img_array, axis=0).astype(np.float32)
predictions = mobilenet_model.predict(img_array, verbose=0)
confidence = float(np.max(predictions[0]))
label = CLASS_NAMES[np.argmax(predictions[0])]
if confidence < 0.6:
return {
"status": "rejected_low_confidence",
"data": None,
"error_msg": "Model confidence too low."
}
status = "rejected"
final_data = {"target_gender": "unknown", "min_age": 0, "max_age": 0}
if label != 'unsafe':
dynamic_data = await get_dynamic_analysis_from_gemini(
pil_img, label, title, description, category
)
if dynamic_data.get("matching_confirmed") is True:
status = "approved"
target_gender = "unisex"
if label == 'gender_male': target_gender = "male"
elif label == 'gender_female': target_gender = "female"
elif label == 'kids': target_gender = "kids"
final_data = {
"target_gender": target_gender,
"min_age": int(dynamic_data.get("min_age", 12)),
"max_age": int(dynamic_data.get("max_age", 60))
}
else:
status = "rejected_content_mismatch"
else:
status = "rejected_unsafe"
return {
"status": status,
"data": final_data if status == "approved" else None,
"error_msg": "" if status == "approved" else f"Analysis failed: {status}"
}
except Exception as e:
return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)