File size: 7,789 Bytes
2f62519
 
 
 
 
 
 
 
 
5d95877
 
 
2f62519
 
 
 
 
 
62da724
4fced91
2f62519
4fced91
2f62519
 
 
 
 
 
 
 
 
d5131f4
 
 
 
 
2f62519
 
 
d5131f4
2f62519
 
 
d5131f4
 
 
2f62519
d5131f4
2f62519
 
d5131f4
2f62519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbf22dd
2f62519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List
import asyncio
import os
from concurrent.futures import ThreadPoolExecutor
from src.Generate_caption import load_model_from_path, tokenizer_load
from src.Color_extraction import extract_colors
from src.Generate_productName_description import generate_product_name, generate_description, clean_response
from huggingface_hub import hf_hub_download
import tempfile

app = FastAPI()

# Load environment variables
API_KEY = os.environ.get("APIKey")

if not API_KEY:
    print(API_KEY)
    raise ValueError("API_KEY not set. Please configure your .env file or system environment.")

# Global variables for models and ThreadPool
vgg16_model = None
fifth_version_model = None
tokenizer = None
executor = ThreadPoolExecutor(max_workers=4)

# Ensure ONNX model path is set
HF_CACHE_DIR = "/app/hf_models_cache"
os.makedirs(HF_CACHE_DIR, exist_ok=True)

os.environ["XDG_CACHE_HOME"] = "/app/onnx_cache"
os.makedirs(os.environ["XDG_CACHE_HOME"], exist_ok=True)

async def download_model_from_hf(repo_id: str, filename: str) -> str:
    try:
        # Use the defined cache directory
        model_path = hf_hub_download(
            repo_id=repo_id,
            filename=filename,
            cache_dir=HF_CACHE_DIR, # Use defined cache dir
            local_dir=HF_CACHE_DIR, # Ensure download to this dir
            force_download=False # Avoid re-downloading if already cached
        )
        print(f"Using model {filename} from {model_path}")
        return model_path
    except Exception as e:
        print(f"Error downloading/finding {filename}: {str(e)}")
        raise


async def load_models():
    global vgg16_model, fifth_version_model, tokenizer
    if not all([vgg16_model, fifth_version_model, tokenizer]):
        print("Downloading and loading models from Hugging Face Hub...")

        try:
            # Download models in parallel
            vgg16_path, model_path, tokenizer_path = await asyncio.gather(
                download_model_from_hf("abdallah-03/AI_product_helper_models", "vgg16_feature_extractor.keras"),
                download_model_from_hf("abdallah-03/AI_product_helper_models", "fifth_version_model.keras"),
                download_model_from_hf("abdallah-03/AI_product_helper_models", "tokenizer.pkl")
            )

            # Load models using the downloaded paths
            vgg16_task = asyncio.to_thread(load_model_from_path, vgg16_path)
            fifth_version_task = asyncio.to_thread(load_model_from_path, model_path)
            tokenizer_task = asyncio.to_thread(tokenizer_load, tokenizer_path)

            vgg16_model, fifth_version_model, tokenizer = await asyncio.gather(
                vgg16_task, fifth_version_task, tokenizer_task
            )
            print("Models loaded successfully!")

        except Exception as e:
            print(f"Error loading models: {str(e)}")
            raise


@app.on_event("startup")
async def startup_event():
    asyncio.create_task(load_models())


# Pydantic Models
class ImagePathsRequest(BaseModel):
    image_paths: List[str]


class GenerateProductRequest(ImagePathsRequest):
    Brand_name: str


class GenerateDescriptionRequest(BaseModel):
    product_name: str


class AIproducthelper(ImagePathsRequest):
    Brand_name: str


# Exception Handlers
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
    return JSONResponse(
        status_code=500,
        content={"success": False, "message": "Internal Server Error", "error": repr(exc)},
    )


@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
    return JSONResponse(
        status_code=exc.status_code,
        content={"success": False, "message": exc.detail},
    )


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
    return JSONResponse(
        status_code=422,
        content={"success": False, "message": "Validation Error", "errors": exc.errors()},
    )


# Endpoints
@app.get("/")
def read_root():
    return {"message": "Hello from our API, models are loading in the background!"}


@app.get("/status/")
async def check_status():
    if all([vgg16_model, fifth_version_model, tokenizer]):
        return {
            "success": True,
            "message": "Models are ready!",
            "models_loaded": {
                "vgg16": vgg16_model is not None,
                "fifth_version": fifth_version_model is not None,
                "tokenizer": tokenizer is not None
            }
        }
    return {
        "success": False,
        "message": "Models are still loading...",
        "models_loaded": {
            "vgg16": vgg16_model is not None,
            "fifth_version": fifth_version_model is not None,
            "tokenizer": tokenizer is not None
        }
    }


@app.post("/extract-colors/")
async def extract_colors_endpoint(request: ImagePathsRequest):
    if not request.image_paths:
        raise HTTPException(status_code=400, detail="Image list cannot be empty.")

    try:
        colors = await asyncio.get_event_loop().run_in_executor(executor, extract_colors, request.image_paths)
        return {"success": True, "colors": colors}
    except Exception as exc:
        raise HTTPException(status_code=500, detail=f"Error extracting colors: {repr(exc)}")


@app.post("/generate-product-name/")
async def generate_product_name_endpoint(request: GenerateProductRequest):
    if not request.image_paths:
        raise HTTPException(status_code=400, detail="Image list cannot be empty.")

    try:
        product_name = await asyncio.get_event_loop().run_in_executor(
            executor, generate_product_name, request.image_paths, request.Brand_name,
            vgg16_model, fifth_version_model, tokenizer, API_KEY
        )
        return {"success": True, "product_name": product_name}
    except Exception as exc:
        raise HTTPException(status_code=500, detail=f"Error generating product name: {repr(exc)}")


@app.post("/generate-description/")
async def generate_description_endpoint(request: GenerateDescriptionRequest):
    try:
        description = await asyncio.get_event_loop().run_in_executor(
            executor, generate_description, API_KEY, request.product_name,
            vgg16_model, fifth_version_model, tokenizer
        )
        return {"success": True, "description": description}
    except Exception as exc:
        raise HTTPException(status_code=500, detail=f"Error generating description: {repr(exc)}")


@app.post("/AI-product_help/")
async def ai_product_help_endpoint(request: AIproducthelper):
    if not request.image_paths:
        raise HTTPException(status_code=400, detail="Image list cannot be empty.")

    try:
        product_name = await asyncio.get_event_loop().run_in_executor(
            executor, generate_product_name, request.image_paths, request.Brand_name,
            vgg16_model, fifth_version_model, tokenizer, API_KEY
        )
        product_name = clean_response(product_name)

        description = await asyncio.get_event_loop().run_in_executor(
            executor, generate_description, API_KEY, product_name,
            vgg16_model, fifth_version_model, tokenizer
        )
        description = clean_response(description)

        return {"success": True, "product_name": product_name, "description": description}

    except Exception as exc:
        raise HTTPException(status_code=500, detail=f"Error in AI product helper: {repr(exc)}")