File size: 6,420 Bytes
06d0a3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import uuid
from PIL import Image
from io import BytesIO
import base64
import os
from typing import Union
import numpy as np
import json
import re
import pandas as pd
import time
from cryptography.fernet import Fernet
import shutil
import asyncio
import logging
from config import session_keys, CryptographyKey


cipher = Fernet(CryptographyKey)
session_expiry = {}

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def save_session_keys(session_id, keys):
    with open(f"session_keys/{session_id}.json", "w") as f:
        json.dump(keys, f)

def load_session_keys(session_id):
    try:
        with open(f"session_keys/{session_id}.json") as f:
            return json.load(f)
    except:
        return {}
    
def get_or_create_session():
    return str(uuid.uuid4())

def reset_session():
    return str(uuid.uuid4())

def encode_image_to_base64(image: Union[Image.Image, np.ndarray]) -> str:
    if isinstance(image, np.ndarray):
        image = Image.fromarray(image.astype("uint8"))

    buffered = BytesIO()
    image.save(buffered, format="PNG")
    img_bytes = buffered.getvalue()
    return base64.b64encode(img_bytes).decode("utf-8")

def decode_base64_image(base64_str: str) -> Image.Image:
    img_bytes = base64.b64decode(base64_str)
    return Image.open(BytesIO(img_bytes))


def save_uploaded_image(image_name: str, image: Image.Image, save_dir: str):
    os.makedirs(save_dir, exist_ok=True)
    image_path = os.path.join(save_dir, f"{image_name}.png")
    image.save(image_path)
    return image_path

class JsonFileHandle:
    @staticmethod
    def save_json_data(json_name: str, data: dict, save_dir: str) -> str:
        os.makedirs(save_dir, exist_ok=True)
        json_path = os.path.join(save_dir, f"{json_name}.json")

        with open(json_path, "w", encoding="utf-8") as f:
            json.dump(data, f, ensure_ascii=False, indent=2)

        return json_path
    
    @staticmethod
    def load_json_data(json_name: str, save_dir: str) -> dict:
        json_path = os.path.join(save_dir, f"{json_name}.json")

        if not os.path.exists(json_path):
            return {}

        try:
            with open(json_path, "r", encoding="utf-8") as f:
                return json.load(f)
        except Exception as e:
            return {}
        
def parse_face_color_string(color_string: str) -> dict:
    pattern = r"<(.*?)>#([0-9a-fA-F]{6})<\1>"
    matches = re.findall(pattern, color_string)

    color_mapping = {part: f"#{color}" for part, color in matches}
    return color_mapping


def encrypt_session_id(session_id: str) -> str:
    return cipher.encrypt(session_id.encode()).decode()

def decrypt_session_id(encrypted_id: str) -> str:
    return cipher.decrypt(encrypted_id.encode()).decode()


def load_combined_dataset(styles_path: str, images_path: str) -> pd.DataFrame:
    try:
        # Load both CSVs and skip malformed lines
        styles_df = pd.read_csv(styles_path, on_bad_lines='skip')
        images_df = pd.read_csv(images_path, on_bad_lines='skip')
    except Exception as e:
        raise RuntimeError(f"❌ Failed to read CSV files: {e}")

    try:
        # Remove '.jpg' and convert to int for joining
        images_df["id"] = images_df["filename"].str.replace(".jpg", "", regex=False).astype(int)
    except Exception as e:
        raise ValueError(f"❌ Failed to process 'filename' column in images.csv: {e}")

    try:
        # Drop rows with any null values
        styles_df.dropna(inplace=True)
        images_df.dropna(inplace=True)

        # Only keep rows where IDs exist in both tables
        combined_df = pd.merge(styles_df, images_df, on="id", how="inner")
        combined_df.dropna(inplace=True)  # Ensure final result has no nulls

    except Exception as e:
        raise RuntimeError(f"❌ Failed during merge or cleaning: {e}")

    product_to_sell = ['Apparel', 'Footwear']
    combined_df = pd.concat([
        combined_df[combined_df["masterCategory"].str.lower() == product.lower()]
        for product in product_to_sell
    ], ignore_index=True)

    return combined_df


def mark_session_active(session_id):
    session_expiry[session_id] = time.time()

async def cleanup_old_sessions(threshold_seconds=600):
    while True:
        now = time.time()
        to_delete = []
        for session_id, last_active in session_expiry.items():
            if now - last_active > threshold_seconds:
                to_delete.append(session_id)
            else:
                logger.info(f"{session_id} still active!")

        for session_id in to_delete:
            # Delete CSV file
            csv_path = f"csv_logs/{session_id}.csv"
            if os.path.exists(csv_path):
                os.remove(csv_path)
                logger.info(f"Deleted CSV: {csv_path}")
            
            # Delete session folder
            folder_path = f"tmp/{session_id}"
            if os.path.exists(folder_path):
                shutil.rmtree(folder_path)
                logger.info(f"Deleted folder: {folder_path}")

            del session_expiry[session_id]
            
            if session_id in session_keys:
                del session_keys[session_id]
                logger.info(f"Deleted API keys for session: {session_id}")

        await asyncio.sleep(5)
        
def save_encrypted_session_keys(session_id: str, data: dict):
    folder = f"tmp/{session_id}"
    os.makedirs(folder, exist_ok=True)
    filepath = os.path.join(folder, "session_settings.enc")

    try:
        encrypted = cipher.encrypt(json.dumps(data).encode())
        with open(filepath, "wb") as f:
            f.write(encrypted)
        logger.info(f"πŸ” Encrypted session settings saved to {filepath}")
    except Exception as e:
        logger.error(f"❌ Failed to save encrypted session keys for {session_id}: {e}")
        
def load_encrypted_session_keys(session_id: str) -> dict:
    filepath = os.path.join("tmp", session_id, "session_settings.enc")

    if not os.path.exists(filepath):
        logger.warning(f"⚠️ Encrypted session file not found: {filepath}")
        return {}

    try:
        with open(filepath, "rb") as f:
            encrypted_data = f.read()
        decrypted = cipher.decrypt(encrypted_data)
        return json.loads(decrypted.decode())
    except Exception as e:
        logger.error(f"❌ Failed to load or decrypt session keys for {session_id}: {e}")
        return {}