File size: 2,653 Bytes
1f7ec3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py
from flask import Flask, request, jsonify
from flask_cors import CORS
from config.database import db
import numpy as np
import tempfile
import os
from voice_embedder import get_embedding, cosine_match

app = Flask(__name__)
CORS(app)

users_collection = db["users"]

# ---------------- Upload PIN audio ----------------
@app.route("/upload_pin", methods=["POST"])
def upload_pin():
    if "pin_audio" not in request.files:
        return jsonify({"error": "No audio file found"}), 400

    audio_file = request.files["pin_audio"]
    temp_path = tempfile.mktemp(suffix=".wav")
    audio_file.save(temp_path)

    try:
        emb = get_embedding(temp_path).tolist()
    except Exception as e:
        return jsonify({"error": str(e)}), 500
    finally:
        os.remove(temp_path)

    return jsonify({"message": "PIN processed successfully", "embedding": emb})

# ---------------- Signup ----------------
@app.route("/signup", methods=["POST"])
def signup():
    data = request.json
    required = ("name", "email", "password", "embedding")
    if not all(k in data for k in required):
        return jsonify({"error": "Missing fields"}), 400

    if users_collection.find_one({"email": data["email"]}):
        return jsonify({"error": "Email already exists"}), 400

    new_user = {
        "name": data["name"],
        "email": data["email"],
        "password": data["password"],
        "embedding": data["embedding"]
    }
    users_collection.insert_one(new_user)
    return jsonify({"message": "Signup successful!"})

# ---------------- Login ----------------
@app.route("/login", methods=["POST"])
def login():
    if "pin_audio" not in request.files:
        return jsonify({"error": "Audio file missing"}), 400

    email = request.form.get("email")
    user = users_collection.find_one({"email": email})
    if not user:
        return jsonify({"error": "User not found"}), 404

    audio_file = request.files["pin_audio"]
    temp_path = tempfile.mktemp(suffix=".wav")
    audio_file.save(temp_path)

    try:
        new_emb = get_embedding(temp_path)
        saved_emb = np.array(user["embedding"], dtype=np.float32)
        score, status = cosine_match(saved_emb, new_emb)
    except Exception as e:
        return jsonify({"error": str(e)}), 500
    finally:
        os.remove(temp_path)

    return jsonify({"score": score, "status": status})

# ---------------- Home ----------------
@app.route("/")
def home():
    return jsonify({"message": "VoiceAuth Backend Running"})

if __name__ == "__main__":
    app.run(port=5000, debug=True)