File size: 5,985 Bytes
dd81387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
from fastapi.responses import PlainTextResponse
from fastapi_router_controller import Controller
from fastapi import APIRouter, File, UploadFile, HTTPException
from utils.BagOfWords import bag_of_words_model
from utils.cnn import extract_features_from_image, get_similar_products_cnn
from utils.logger import Logger
from utils.tfidf import tfidf_model
import shutil
import os

logger = Logger.get_logger(__name__)

router = APIRouter(prefix='/v1')
controller = Controller(router, openapi_tag={
    'name': 'Recommendation System',
})


@controller.use()
@controller.resource()
class RecommendationController():
    def __init__(self):
        pass
    @controller.route.get(
        '/recommend/bog',
        tags=['recommend-apparel'],
        summary='Recommends the apparel')
    async def recommend(self, input: str):
        try:
            if not input:
                logger.error('Input is required.')
                raise HTTPException(
                    status_code=500, detail='Input is required.')

            results = bag_of_words_model(input, 5)
            return {"results": results}
        except asyncio.CancelledError:
            logger.error(
                'Canceling network request due to disconnect in client.')
        except Exception as error:
            logger.error('Error {}'.format(error))

    @controller.route.get(
        '/recommend/tfidf',
        tags=['recommend-apparel'],
        summary='Recommends the apparel')
    async def recommend(self, input: str):
        try:
            if not input:
                logger.error('Input is required.')
                raise HTTPException(
                    status_code=500, detail='Input is required.')

            results = tfidf_model(input, 5)
            return {"results": results}
        except asyncio.CancelledError:
            logger.error(
                'Canceling network request due to disconnect in client.')
        except Exception as error:
            logger.error('Error {}'.format(error))

    @controller.route.post(
        '/recommend/cnn',
        tags=['recommend-apparel'],
        summary='Recommends the apparel')
    async def recommend(self, file: UploadFile = File(...)):
        try:
            UPLOAD_FOLDER = 'uploads/'
            # Save the uploaded file
            file_path = os.path.join(UPLOAD_FOLDER, file.filename)
            with open(file_path, "wb") as buffer:
                shutil.copyfileobj(file.file, buffer)

            # Process the uploaded image
            with open(file_path, "rb") as img_file:
                image_bytes = img_file.read()

            # Log image size for debugging
            if len(image_bytes) == 0:
                raise HTTPException(status_code=400, detail="Uploaded image is empty.")
            else:
                print(f"Image size: {len(image_bytes)} bytes")

            # Extract features from the image
            image_features = extract_features_from_image(image_bytes)

            results = get_similar_products_cnn(image_features, 5)
            return {"results": results}
        except asyncio.CancelledError:
            logger.error(
                'Canceling network request due to disconnect in client.')
        except Exception as error:
            logger.error('Error {}'.format(error))
    
    @controller.route.get(
        '/deleteUpload')
    async def recommend(self, password: str):
        try:
            if not password:
                logger.error('Password is required.')
                raise HTTPException(
                    status_code=500, detail='Password is required.')

            if password != "1328":
                return {"results": "Unauthorized: Incorrect password"}

            UPLOAD_FOLDER = 'uploads/'
            # Check if the uploads folder exists
            if not os.path.exists(UPLOAD_FOLDER):
                raise HTTPException(status_code=404, detail="Uploads folder does not exist")
            # List all files in the uploads folder
            files = os.listdir(UPLOAD_FOLDER)
            if not files:
                return {"results": "No files to delete"}
             # Delete all files in the uploads folder
            for file in files:
                file_path = os.path.join(UPLOAD_FOLDER, file)
                try:
                    # Check if it is a file before trying to delete it
                    if os.path.isfile(file_path):
                        os.remove(file_path)
                        print(f"Deleted: {file_path}")
                    else:
                        print(f"Skipping directory: {file_path}")
                except Exception as e:
                    print(f"Error deleting file {file_path}: {str(e)}")

            return {"results": "All files have been deleted successfully."}
        except asyncio.CancelledError:
            logger.error(
                'Canceling network request due to disconnect in client.')
        except Exception as error:
            logger.error('Error {}'.format(error))

    @controller.route.get(
        '/readLogs')
    async def readLogs(self):
        try:
            # Check if the log file exists
            LOG_FILE_PATH = 'logs.log'
            if not os.path.exists(LOG_FILE_PATH):
                raise HTTPException(status_code=404, detail="Log file not found")
    
            # Read the log file content
            try:
                with open(LOG_FILE_PATH, 'r') as log_file:
                    logs_content = log_file.read()
            except Exception as e:
                raise HTTPException(status_code=500, detail=f"Error reading log file: {str(e)}")

            # Return the log content as plain text
            return PlainTextResponse(content=logs_content)
        except asyncio.CancelledError:
            logger.error(
                'Canceling network request due to disconnect in client.')
        except Exception as error:
            logger.error('Error {}'.format(error))