2024-11-09 18:33:19.146490: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-11-09 18:33:25.163255: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
Traceback (most recent call last):
  File "c:\Users\adars\OneDrive\Desktop\Minor_5th_sem\MultiModal_Mental_Health_Care\app.py", line 4, in <module>
    import tensorflow as tf
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\tensorflow\__init__.py", line 467, in <module>
    importlib.import_module("keras.src.optimizers")
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\importlib\__init__.py", line 126, in import_module  
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\__init__.py", line 4, in <module>
    from keras.api import DTypePolicy
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\api\__init__.py", line 8, in <module>
    from keras.api import activations
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\api\activations\__init__.py", line 7, in <module>
    from keras.src.activations import deserialize
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\__init__.py", line 1, in <module>
    from keras.src import activations
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\activations\__init__.py", line 22, in <module>
    from keras.src.saving import object_registration
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\saving\__init__.py", line 7, in <module>
    from keras.src.saving.saving_api import load_model
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\saving\saving_api.py", line 7, in <module>
    from keras.src.legacy.saving import legacy_h5_format
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\legacy\saving\legacy_h5_format.py", line 13, in <module>
    from keras.src.legacy.saving import saving_utils
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\legacy\saving\saving_utils.py", line 10, in <module>
    from keras.src import models
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\models\__init__.py", line 1, in <module>
    from keras.src.models.functional import Functional
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\models\functional.py", line 16, in <module>
    from keras.src.models.model import Model
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\models\model.py", line 12, in <module>
    from keras.src.trainers import trainer as base_trainer
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\trainers\trainer.py", line 14, in <module>
    from keras.src.trainers.data_adapters import data_adapter_utils
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\trainers\data_adapters\__init__.py", line 4, in <module>
    from keras.src.trainers.data_adapters import array_data_adapter
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\trainers\data_adapters\array_data_adapter.py", line 7, in <module>
    from keras.src.trainers.data_adapters import array_slicing
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\trainers\data_adapters\array_slicing.py", line 11, in <module>
    import pandas
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pandas\__init__.py", line 62, in <module>
    from pandas.core.api import (
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pandas\core\api.py", line 1, in <module>
    from pandas._libs import (
  File "C:\Users\adars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pandas\_libs\__init__.py", line 18, in <module>
    from pandas._libs.interval import Interval
  File "interval.pyx", line 1, in init pandas._libs.interval
ValueError: numpy.dtype size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject ,, from flask import Flask, request,render_template, redirect,session
from flask_sqlalchemy import SQLAlchemy
import bcrypt
import tensorflow as tf
from tensorflow import keras
import os
import pickle
import librosa
import numpy as np
from keras_models import load_model
from keras_preprocessing.sequence import pad_sequences
import h5py as h5

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test1.db'
db = SQLAlchemy(app)
app.secret_key = 'secret_key'

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    email = db.Column(db.String(100), unique=True)
    emailwell=db.Column(db.String(100), unique=True)
    password = db.Column(db.String(100))

    def __init__(self,email,password,name,emailwell):
        self.name = name
        self.email = email
        self.emailwell=emailwell
        self.password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
    
    def check_password(self,password):
        return bcrypt.checkpw(password.encode('utf-8'),self.password.encode('utf-8'))

with app.app_context():
    db.create_all()


model = load_model('mental_health_model.h5')

with open('tokenizer.pkl', 'rb') as f:
    tokenizer = pickle.load(f)

with open('scaler.pkl', 'rb') as f:
    scaler = pickle.load(f)

def extract_mfcc(filename, n_mfcc=40):
    y, sr = librosa.load(filename, duration=3, offset=0.5)
    mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=n_mfcc)
    return np.mean(mfcc.T, axis=0)

def preprocess_input(text, audio_filename):
    sequences = tokenizer.texts_to_sequences([text])
    text_processed = pad_sequences(sequences, maxlen=100)[0]

    audio_features = extract_mfcc(audio_filename)
    audio_features_scaled = scaler.transform([audio_features])

    return np.array([text_processed]), audio_features_scaled


@app.route('/')
def index():
    return render_template('index.html')

@app.route('/register',methods=['GET','POST'])
def register():
    if request.method == 'POST':
        # handle request
        name = request.form['name']
        email = request.form['email']
        emailwell=request.form['emailwell']
        password = request.form['password']

        new_user = User(name=name,email=email,password=password,emailwell=emailwell)
        db.session.add(new_user)
        db.session.commit()
        return redirect('/login')

    return render_template('register.html')

    
@app.route('/login',methods=['GET','POST'])
def login():
    if request.method == 'POST':
        email = request.form['email']
        password = request.form['password']

        user = User.query.filter_by(email=email).first()
        
        if user and user.check_password(password):
            session['email'] = user.email
            return redirect('/home')
        else:
            return render_template('login.html',error='Invalid user')

    return render_template('login.html')


@app.route('/home')
def dashboard():
    if session['email']:
        user = User.query.filter_by(email=session['email']).first()
        return render_template('home.html',user=user)
    
    return redirect('/login')

@app.route('/health.html',methods=["GET","POST"])
def first():
    if request.method == 'POST':
        feeling_text = request.form['feelingText']
        audio_file = request.files['audioFile']
        audio_path = os.path.join('uploads', audio_file.filename)
        audio_file.save(audio_path)

        text_processed, audio_features_scaled = preprocess_input(feeling_text, audio_path)
        prediction = model.predict([text_processed, audio_features_scaled])
        os.remove(audio_path)  # Delete the uploaded audio file

        return render_template('result.html', prediction=prediction[0][0])
    
    return render_template('health.html')

@app.route('/result.html',methods=["GET","POST"])
def result():
    return render_template('result.html')

@app.route('/logout')
def logout():
    session.pop('email',None)
    return redirect('/login')

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