figh8back commited on
Commit
c620527
·
verified ·
1 Parent(s): e5e32da

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -126
app.py DELETED
@@ -1,126 +0,0 @@
1
- import os
2
- from typing import List
3
- import numpy as np
4
- import pandas as pd
5
- from PIL import Image
6
- import tensorflow as tf
7
- from tensorflow.keras.models import load_model
8
- from tensorflow.keras.preprocessing import image
9
- from flask import Flask, request
10
- import gradio as gr
11
- from backend.models.skin_tone.skin_tone_knn import identify_skin_tone
12
- from backend.models.recommender.rec import recs_essentials, makeup_recommendation
13
- import base64
14
- from io import BytesIO
15
-
16
- # Initialize Flask app
17
- app = Flask(__name__)
18
-
19
- # Model variables and loading
20
- class_names1 = ['Dry_skin', 'Normal_skin', 'Oil_skin']
21
- class_names2 = ['Low', 'Moderate', 'Severe']
22
- skin_tone_dataset = 'models/skin_tone/skin_tone_dataset.csv'
23
-
24
-
25
- def get_model():
26
- global model1, model2
27
- model1 = load_model('./models/skin_model')
28
- print('Model 1 loaded')
29
- model2 = load_model('./models/acne_model')
30
- print("Model 2 loaded!")
31
-
32
- get_model()
33
-
34
- # Load and preprocess the image
35
- def load_image(img_path):
36
- img = image.load_img(img_path, target_size=(224, 224))
37
- img_tensor = image.img_to_array(img)
38
- img_tensor = np.expand_dims(img_tensor, axis=0) # Add a batch dimension
39
- img_tensor /= 255. # Normalize image
40
- return img_tensor
41
-
42
- # Prediction for skin type
43
- def prediction_skin(img_path):
44
- new_image = load_image(img_path)
45
- pred1 = model1.predict(new_image)
46
- if len(pred1[0]) > 1:
47
- pred_class1 = class_names1[tf.argmax(pred1[0])]
48
- else:
49
- pred_class1 = class_names1[int(tf.round(pred1[0]))]
50
- return pred_class1
51
-
52
- # Prediction for acne severity
53
- def prediction_acne(img_path):
54
- new_image = load_image(img_path)
55
- pred2 = model2.predict(new_image)
56
- if len(pred2[0]) > 1:
57
- pred_class2 = class_names2[tf.argmax(pred2[0])]
58
- else:
59
- pred_class2 = class_names2[int(tf.round(pred2[0]))]
60
- return pred_class2
61
-
62
- # Gradio function for handling image input
63
- def process_image(image_data):
64
- image_data = bytes(image_data, encoding="ascii")
65
- im = Image.open(BytesIO(base64.b64decode(image_data + '==')))
66
- file_path = './static/image.png'
67
- im.save(file_path) # Save the image locally
68
- skin_type = prediction_skin(file_path).split('_')[0] # Get skin type prediction
69
- acne_type = prediction_acne(file_path) # Get acne severity prediction
70
- tone = identify_skin_tone(file_path, dataset=skin_tone_dataset) # Get skin tone prediction
71
- return {'type': skin_type, 'tone': str(tone), 'acne': acne_type}
72
-
73
- # Gradio Interface
74
- iface = gr.Interface(fn=process_image,
75
- inputs=gr.Image(type="base64", label="Upload Face Image"),
76
- outputs="json",
77
- live=True)
78
-
79
- # Flask routes for API endpoints
80
- @app.route('/')
81
- def home():
82
- return "API is running. Use the Gradio interface for testing."
83
-
84
- # Recommendation API endpoint
85
- @app.route('/recommend', methods=['PUT'])
86
- def recommend():
87
- args = request.get_json() # Getting JSON data from the frontend
88
- features = args['features']
89
- tone = args['tone']
90
- skin_type = args['type'].lower()
91
- skin_tone = 'light to medium'
92
- if tone <= 2:
93
- skin_tone = 'fair to light'
94
- elif tone >= 4:
95
- skin_tone = 'medium to dark'
96
-
97
- fv = [int(value) for key, value in features.items()] # Converting feature values to integers
98
-
99
- general = recs_essentials(fv, None) # Recommendation essentials
100
- makeup = makeup_recommendation(skin_tone, skin_type) # Makeup recommendations
101
-
102
- return {'general': general, 'makeup': makeup}
103
-
104
- # Skin Metrics API endpoint
105
- @app.route('/upload', methods=['PUT'])
106
- def upload():
107
- args = request.get_json() # Get the uploaded image data
108
- file_data = args['file']
109
- starter = file_data.find(',')
110
- image_data = file_data[starter+1:] # Extract the base64-encoded image
111
-
112
- image_data = bytes(image_data, encoding="ascii")
113
- im = Image.open(BytesIO(base64.b64decode(image_data + '==')))
114
- file_path = './static/image.png'
115
- im.save(file_path) # Save image locally
116
-
117
- skin_type = prediction_skin(file_path).split('_')[0]
118
- acne_type = prediction_acne(file_path)
119
- tone = identify_skin_tone(file_path, dataset=skin_tone_dataset)
120
-
121
- return {'type': skin_type, 'tone': str(tone), 'acne': acne_type}, 200 # Return predictions
122
-
123
- if __name__ == "__main__":
124
- # Launch Gradio interface and Flask app simultaneously
125
- iface.launch(server_name="0.0.0.0", server_port=8080) # Launch Gradio UI
126
- app.run(debug=False, use_reloader=False) # Run Flask app