svarshas/audio-sentiment-classification-project
0
1# frontend/app.py2from flask import Flask, render_template, request3import torch4import librosa5from cnn_model import CNNEmotionClassifier, extract_features_from_waveform6 7# Flask app8app = Flask(__name__)9 10# Device11device = torch.device("cuda" if torch.cuda.is_available() else "cpu")12 13# Load CNN model14model = CNNEmotionClassifier()15model_path = "best_cnn_model.pt"16model.load_state_dict(torch.load(model_path, map_location=device))17model.to(device)18model.eval()19 20# Labels21LABELS = ["neutral", "calm", "happy", "sad", "angry", "fearful", "disgust", "surprised"]22 23# Flask routes24@app.route("/", methods=["GET", "POST"])25def index():26 result = None27 if request.method == "POST":28 if "file" in request.files and request.files["file"].filename != "":29 file = request.files["file"]30 31 import tempfile32 with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:33 file.save(tmp.name)34 y, sr = librosa.load(tmp.name, sr=None)35 x = extract_features_from_waveform(y,sr)36 37 with torch.no_grad():38 logits = model(x)39 pred_idx = torch.argmax(logits, dim=1).item()40 result = LABELS[pred_idx]41 42 return render_template("index.html", result=result)43 44# Run app45if __name__ == "__main__":46 app.run(debug=True)47 