aman77singh / Aman-Singh

0 stars 1 forks source link

new site #6

Open aaditya-029 opened 1 month ago

aaditya-029 commented 1 month ago

<!DOCTYPE html>

Quantum Anomaly Detection

Quantum Anomaly Detection

aaditya-029 commented 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)

Load the global device parameters

global dev_params

Define the quantum device and circuit as shown in your original code

Define the loss and training functions as before

@app.route('/train', methods=['POST']) def train():

Get the uploaded CSV file and the number of epochs from the request

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)