Open aaditya-029 opened 1 month ago
from flask import Flask, request, jsonify import numpy as np import pandas as pd import pennylane as qml from sklearn.model_selection import train_test_split from sklearn.decomposition import PCA
app = Flask(name)
global dev_params
@app.route('/train', methods=['POST']) def train():
file = request.files['file']
epochs = int(request.form['epochs'])
# Load dataset
data = pd.read_csv(file)
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Reduce dimensionality if necessary (based on your backend code)
num_qubits = min(X_train.shape[1], 4)
if X_train.shape[1] > num_qubits:
pca = PCA(n_components=num_qubits)
X_train = pca.fit_transform(X_train)
X_test = pca.transform(X_test)
# Train the quantum model
train_quantum_model(X_train, y_train, epochs)
# Evaluate the model
accuracy, report = evaluate_model(X_test, y_test)
# Return the result as JSON
return jsonify({"accuracy": accuracy, "report": report})
if name == 'main': app.run(debug=True)
<!DOCTYPE html>
Quantum Anomaly Detection