@app.route('/create_model', methods=['POST'])
def create_model():
try:
user_id = request.form.get('user_id')
project_id = request.form.get('project_id')
target_column = request.form.get('target_column')
# Fetch the dataset from the database
dataset = Upload.query.filter_by(user_id=user_id, project_id=project_id).first()
if not dataset:
return {"error": "Dataset not found"}, 404
# Check if a model already exists
model_details = ModelDetails.query.filter_by(user_id=user_id, project_id=project_id).first()
if not model_details:
# Create a new record if no existing model found
model_details = ModelDetails(user_id=user_id, project_id=project_id, model_name="LinearRegression",
status="training",created_at = datetime.utcnow())
db.session.add(model_details)
else:
# Update the existing record
model_details.status = "training"
model_details.created_at = datetime.utcnow()
db.session.commit()
# Fetch the dataset file from S3
bucket_name = 'yungsen-test-bucket'
file_key = 'datasets/' + dataset.filename
file_obj = s3.get_object(Bucket=bucket_name, Key=file_key)
try:
mse,r2,model = train_model(file_obj,target_column)
except Exception as e:
model_details.status = "training Failed"
db.session.commit()
return {"error": f"An error occurred while training model: {str(e)}"}, 500
# Save the model to S3
model_filename = f"{user_id}_{project_id}_model.joblib"
s3_model_path = 'models/' + model_filename # S3 path
joblib.dump(model, model_filename)
s3.upload_file(model_filename, bucket_name, 'models/' + model_filename)
# Update the model details record with the metrics and status "training completed"
model_details.accuracy = r2
model_details.mse = mse
model_details.precision = None # Update with actual value if classification problem
model_details.f1_score = None # Update with actual value if classification problem
model_details.status = "training completed"
model_details.s3_model_path = s3_model_path
model_details.created_at = datetime.utcnow()
db.session.commit()
return {"message": "Model training completed", "mse": mse, "r2_score": r2}, 200
except Exception as e:
return {"error": f"An error occurred: {str(e)}"}, 500
Above program create model endpoint shows error. The endpoint program given below
import requests
# URL of the endpoint
url = 'http://127.0.0.1:5000/create_model' # Replace this with the actual URL of your endpoint
# Sample form data
form_data = {
'user_id': 'AnandX1',
'project_id': 'Sample1',
'target_column': 'Price'
}
try:
# Sending a POST request to the endpoint with form data
response = requests.post(url, data=form_data)
# Check the response status code
if response.status_code == 200:
# Request was successful, print the response message and data
data = response.json()
print("Success:", data['message'])
print("Mean Squared Error (MSE):", data['mse'])
print("R2 Score:", data['r2_score'])
elif response.status_code == 500:
# Server error occurred, print the error message
print("Server Error:", response.json()['error'])
else:
# Unexpected response status code, print the response content
print("Unexpected Response:", response.status_code, response.content)
except Exception as e:
# An exception occurred during the request, print the error message
print("Error:", str(e))
After check this endpoint threw error:
Server Error: An error occurred: An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.
The endpoint should be run. What is the problem in this endpoint?