from flask import Flask
from flask import render_template
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
import os
import numpy as np
import tensorflow as tf
import PIL
from tensorflow import keras


#backend instantiation
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = "static/upload_folder" 

#loading ai model
model = tf.keras.models.load_model('ai/fingernail_model')
class_names = ['long', 'short']
@app.route('/')
def home(name=None):
    return render_template("index.html")

@app.route("/upload", methods = ['POST'])
def upload():
    if 'file' not in request.files:
        flash('No file part')
        return redirect(request.url)
    file = request.files['file']
    
    if file.filename == '':
        flash('No selected file')
        return redirect(request.url)
    if file:
        filename = secure_filename(file.filename)
        file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        file.save(file_path)
        img_array = tf.keras.preprocessing.image.load_img(file_path, target_size = (64, 64))
        img_array = tf.expand_dims(img_array, 0)

        predictions = model.predict(img_array)
        score = tf.nn.softmax(predictions)
        statement = "I am {:.2f} percent confident that your fingernails are {}".format(100 * np.max(score), class_names[np.argmax(score)])
        os.remove(file_path)
    return statement

if __name__ == "__main__":
    app.run(debug=True)
    app.run(host='0.0.0.0')


from flask import Flask
from flask import render_template
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
import os
import numpy as np
import tensorflow as tf
import PIL
from tensorflow import keras


#backend instantiation
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = "static/upload_folder" 

#loading ai model
model = tf.keras.models.load_model('ai/fingernail_model')
class_names = ['long', 'short']
@app.route('/')
def home(name=None):
    return render_template("index.html")

@app.route("/upload", methods = ['POST'])
def upload():
    if 'file' not in request.files:
        flash('No file part')
        return redirect(request.url)
    file = request.files['file']
    
    if file.filename == '':
        flash('No selected file')
        return redirect(request.url)
    if file:
        filename = secure_filename(file.filename)
        file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        file.save(file_path)
        img_array = tf.keras.preprocessing.image.load_img(file_path, target_size = (64, 64))
        img_array = tf.expand_dims(img_array, 0)

        predictions = model.predict(img_array)
        score = tf.nn.softmax(predictions)
        statement = "I am {:.2f} percent confident that your fingernails are {}".format(100 * np.max(score), class_names[np.argmax(score)])
        os.remove(file_path)
    return statement

if __name__ == "__main__":
    app.run(debug=True)
    app.run(host='0.0.0.0')


#backend instantiation #loading ai model