Spaces:
Sleeping
Sleeping
Download app.py from aislynajay/deeplearntest: direct link, hf CLI and curl.
- Browser
- Download file 2.87 kB
-
https://huggingface.co/spaces/aislynajay/deeplearntest/resolve/main/app.py
- Command line
-
hf download hf://spaces/aislynajay/deeplearntest/app.py
-
curl -L -o app.py https://huggingface.co/spaces/aislynajay/deeplearntest/resolve/main/app.py
2.87 kB
| import os | |
| import cv2 | |
| import numpy as np | |
| import tensorflow as tf | |
| from flask import Flask, request, jsonify | |
| from werkzeug.security import generate_password_hash, check_password_hash | |
| from werkzeug.utils import secure_filename | |
| from pymongo import MongoClient | |
| app = Flask(__name__) | |
| UPLOAD_FOLDER = "uploads" | |
| MODEL_PATH = "best_model.h5" | |
| IMG_H, IMG_W = 224, 224 | |
| os.makedirs(UPLOAD_FOLDER, exist_ok=True) | |
| # -------- LOAD MODEL -------- | |
| model = tf.keras.models.load_model(MODEL_PATH, compile=False) | |
| print("Model loaded") | |
| # -------- DATABASE -------- | |
| MONGO_URI = "mongodb+srv://aislynsoftware_db_user:xwjSjtJDPTafHYAJ@cluster0.ekaeylk.mongodb.net/database_db?retryWrites=true&w=majority" | |
| client = MongoClient(MONGO_URI) | |
| db = client["database_db"] | |
| users_collection = db["users"] | |
| # -------- IMAGE PREPROCESS -------- | |
| def preprocess_image(path): | |
| img = cv2.imread(path) | |
| img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
| img = cv2.resize(img, (IMG_W, IMG_H)) | |
| img = img.astype("float32") / 255.0 | |
| return np.expand_dims(img, axis=0) | |
| # -------- REGISTER API -------- | |
| def register(): | |
| data = request.json | |
| name = data["name"] | |
| email = data["email"] | |
| phone = data["phone"] | |
| password = data["password"] | |
| if users_collection.find_one({"email": email}): | |
| return jsonify({"error": "Email already exists"}), 400 | |
| users_collection.insert_one({ | |
| "name": name, | |
| "email": email, | |
| "phone": phone, | |
| "password": generate_password_hash(password) | |
| }) | |
| return jsonify({"message": "User registered successfully"}) | |
| # -------- LOGIN API -------- | |
| def login(): | |
| data = request.json | |
| email = data["email"] | |
| password = data["password"] | |
| user = users_collection.find_one({"email": email}) | |
| if user and check_password_hash(user["password"], password): | |
| return jsonify({ | |
| "message": "Login successful", | |
| "name": user["name"], | |
| "email": user["email"] | |
| }) | |
| return jsonify({"error": "Invalid credentials"}), 401 | |
| # -------- PREDICT API -------- | |
| def predict(): | |
| img1 = request.files["img1"] | |
| img2 = request.files["img2"] | |
| path1 = os.path.join(UPLOAD_FOLDER, secure_filename(img1.filename)) | |
| path2 = os.path.join(UPLOAD_FOLDER, secure_filename(img2.filename)) | |
| img1.save(path1) | |
| img2.save(path2) | |
| i1 = preprocess_image(path1) | |
| i2 = preprocess_image(path2) | |
| pred = model.predict([i1, i2])[0][0] | |
| similarity = round((1 - pred) * 100, 2) | |
| result = "GENUINE SIGNATURE" if pred < 0.5 else "FORGED SIGNATURE" | |
| return jsonify({ | |
| "result": result, | |
| "similarity": similarity | |
| }) | |
| # -------- RUN -------- | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) |