aryan10022001 commited on
Commit
f91e545
·
verified ·
1 Parent(s): 76cf8db

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +0 -135
app/main.py CHANGED
@@ -1,132 +1,3 @@
1
- # from fastapi import FastAPI, UploadFile, File, HTTPException
2
- # from fastapi.responses import JSONResponse
3
- # from PIL import Image
4
- # import numpy as np
5
- # import pickle
6
- # import tensorflow as tf
7
- # from keras.preprocessing import image
8
- # from keras.layers import GlobalMaxPooling2D
9
- # from keras.applications.resnet50 import ResNet50, preprocess_input
10
- # from sklearn.neighbors import NearestNeighbors
11
- # from numpy.linalg import norm
12
- # import io
13
-
14
- # # Initialize FastAPI app
15
- # app = FastAPI()
16
-
17
- # # Load feature list and filenames
18
- # feature_list = np.array(pickle.load(open('/Users/aryansingh/e-commerce_recommendations/Embeddings.pkl', 'rb')))
19
- # filenames = pickle.load(open('/Users/aryansingh/e-commerce_recommendations/filenames.pkl', 'rb'))
20
-
21
- # # Load the ResNet50 model
22
- # model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
23
- # model.trainable = False
24
- # model = tf.keras.Sequential([
25
- # model,
26
- # GlobalMaxPooling2D()
27
- # ])
28
-
29
- # def feature_extraction(img, model):
30
- # img = img.resize((224, 224))
31
- # img_array = image.img_to_array(img)
32
- # expanded_img_array = np.expand_dims(img_array, axis=0)
33
- # preprocessed_img = preprocess_input(expanded_img_array)
34
- # result = model.predict(preprocessed_img).flatten()
35
- # normalized_result = result / norm(result)
36
- # return normalized_result
37
-
38
- # def recommend(features, feature_list):
39
- # neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean')
40
- # neighbors.fit(feature_list)
41
- # distances, indices = neighbors.kneighbors([features])
42
- # return indices
43
-
44
- # @app.post("/recommend")
45
- # async def recommend_fashion(file: UploadFile = File(...)):
46
- # try:
47
- # contents = await file.read()
48
- # img = Image.open(io.BytesIO(contents))
49
- # features = feature_extraction(img, model)
50
- # indices = recommend(features, feature_list)
51
- # recommendations = [filenames[idx] for idx in indices[0]]
52
- # return JSONResponse(content={"recommendations": recommendations})
53
- # except Exception as e:
54
- # raise HTTPException(status_code=400, detail=str(e))
55
-
56
- # # Run the app with: uvicorn script_name:app --reload
57
- # # Example: uvicorn main:app --reload if the script is named main.py
58
- # if __name__ == "__main__":
59
- # import uvicorn
60
- # uvicorn.run(app, host="0.0.0.0", port=8000)
61
-
62
- # from fastapi import FastAPI, UploadFile, File, HTTPException, Request
63
- # from fastapi.responses import HTMLResponse
64
- # from fastapi.templating import Jinja2Templates
65
- # from fastapi.staticfiles import StaticFiles
66
- # from PIL import Image
67
- # import numpy as np
68
- # import pickle
69
- # import tensorflow as tf
70
- # from keras.preprocessing import image
71
- # from keras.layers import GlobalMaxPooling2D
72
- # from keras.applications.resnet50 import ResNet50, preprocess_input
73
- # from sklearn.neighbors import NearestNeighbors
74
- # from numpy.linalg import norm
75
- # import io
76
- # import os
77
-
78
- # app = FastAPI()
79
-
80
- # # Load feature list and filenames
81
- # feature_list = np.array(pickle.load(open('Embeddings.pkl', 'rb')))
82
- # filenames = pickle.load(open('filenames.pkl', 'rb'))
83
-
84
- # # Load the ResNet50 model
85
- # model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
86
- # model.trainable = False
87
- # model = tf.keras.Sequential([
88
- # model,
89
- # GlobalMaxPooling2D()
90
- # ])
91
-
92
- # # Initialize templates
93
- # templates = Jinja2Templates(directory="templates")
94
-
95
- # # Mount the directory containing images
96
- # app.mount("/archive", StaticFiles(directory="archive (4)"), name="archive")
97
-
98
- # def feature_extraction(img, model):
99
- # img = img.resize((224, 224))
100
- # img_array = image.img_to_array(img)
101
- # expanded_img_array = np.expand_dims(img_array, axis=0)
102
- # preprocessed_img = preprocess_input(expanded_img_array)
103
- # result = model.predict(preprocessed_img).flatten()
104
- # normalized_result = result / norm(result)
105
- # return normalized_result
106
-
107
- # def recommend(features, feature_list):
108
- # neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean')
109
- # neighbors.fit(feature_list)
110
- # distances, indices = neighbors.kneighbors([features])
111
- # return indices
112
-
113
- # @app.post("/recommend", response_class=HTMLResponse)
114
- # async def recommend_fashion(request: Request, file: UploadFile = File(...)):
115
- # try:
116
- # contents = await file.read()
117
- # img = Image.open(io.BytesIO(contents))
118
- # features = feature_extraction(img, model)
119
- # indices = recommend(features, feature_list)
120
- # recommendations = ["/archive (4)/images/" + os.path.basename(filenames[idx]) for idx in indices[0]]
121
- # return templates.TemplateResponse("recommendation.html", {"request": request, "recommendations": recommendations})
122
- # except Exception as e:
123
- # raise HTTPException(status_code=400, detail=str(e))
124
-
125
- # # Run the app with: uvicorn main:app --reload
126
- # if __name__ == "__main__":
127
- # import uvicorn
128
- # uvicorn.run(app, host="0.0.0.0", port=8000)
129
-
130
  from fastapi import FastAPI, UploadFile, File, HTTPException, Request
131
  from fastapi.responses import HTMLResponse
132
  from fastapi.templating import Jinja2Templates
@@ -210,9 +81,3 @@ async def recommend_fashion(request: Request, file: UploadFile = File(...)):
210
  return templates.TemplateResponse("recommendation.html", {"request": request, "recommendations": recommendations})
211
  except Exception as e:
212
  raise HTTPException(status_code=400, detail=str(e))
213
-
214
- # Run the app with: uvicorn main:app --reload
215
- if __name__ == "__main__":
216
- import uvicorn
217
- uvicorn.run(app, host="0.0.0.0", port=8000)
218
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI, UploadFile, File, HTTPException, Request
2
  from fastapi.responses import HTMLResponse
3
  from fastapi.templating import Jinja2Templates
 
81
  return templates.TemplateResponse("recommendation.html", {"request": request, "recommendations": recommendations})
82
  except Exception as e:
83
  raise HTTPException(status_code=400, detail=str(e))