Im new to create a machine learning model in a web extension tool, so basically im trying to create a machine learning model that helps to analyze email whether is it a phishing email or not. But when i try to run the code it show
ERROR in backend: An error occurred: 'dict' object has no attribute 'lower'
Any suggestion how to resolve this issue?
dataset = pd.read_csv("mail_data.csv")
mail_data = dataset.where((pd.notnull(dataset)), '')
mail_data.loc[mail_data['Category'] == 'spam', 'Category',] = 0
mail_data.loc[mail_data['Category'] == 'authentic', 'Category',] = 1
mail_data.dropna(subset=['Message'], inplace=True)
X = mail_data['Message'].astype(str).tolist()
Y = mail_data['Category']
feature_extraction = TfidfVectorizer(min_df=1, stop_words='english', lowercase=True)
X_features = feature_extraction.fit_transform(X)
Y = Y.astype('int')
model = LogisticRegression()
model.fit(X_features, Y)
--
def detect_phishing(input_mail):
input_data_feature = feature_extraction.transform([input_mail])
prediction = model.predict(input_data_feature)
return prediction[0]
--
@app.route('/predict', methods=['POST'])
def predict_phishing():
if request.method == 'POST':
try:
payload = request.json
email_content = payload.get('email_content', None) # Assuming 'email_content' is the key in your JSON payload
print('Received data:', email_content)
# Call detect_phishing function
prediction = detect_phishing(email_content)
return jsonify({'prediction': prediction[0]})
except Exception as e:
app.logger.error(f'An error occurred: {str(e)}')
app.logger.info(traceback.format_exc())
return jsonify({'error': 'An error occurred'}), HTTPStatus.INTERNAL_SERVER_ERROR
else:
return jsonify({'error': 'Invalid method'}), HTTPStatus.METHOD_NOT_ALLOWED
Error code
ERROR in backend: An error occurred: 'dict' object has no attribute 'lower'
INFO in backend: Traceback (most recent call last): line 80, in predict_phishing
line 60, in detect_phishing
File "C:\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\sklearn\feature_extraction\text.py", line 2162, in transform
X = super().transform(raw_documents)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\sklearn\feature_extraction\text.py", line 1434, in transform
_, X = self._count_vocab(raw_documents, fixed_vocab=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\sklearn\feature_extraction\text.py", line 1276, in _count_vocab
for feature in analyze(doc):
^^^^^^^^^^^^
File "C:\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\sklearn\feature_extraction\text.py", line 110, in _analyze
doc = preprocessor(doc)
^^^^^^^^^^^^^^^^^
File "C:\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\sklearn\feature_extraction\text.py", line 68, in _preprocess
doc = doc.lower()
^^^^^^^^^
AttributeError: 'dict' object has no attribute 'lower'
127.0.0.1 - - [11/Mar/2024 02:36:05] "POST /predict HTTP/1.1" 500 -
Most probably you are passing input_mail
of the wrong type (expected: str
, actual: dict
) in your feature_extraction.transform([input_mail])
call in detect_phishing
.
To fix that, make sure you are passing an str
:
if not isinstance(input_mail, str):
raise TypeError('Expected str for input_mail, got: %s' % type(input_mail))
input_data_feature = feature_extraction.transform([input_mail])