-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
90 lines (74 loc) · 2.74 KB
/
app.py
File metadata and controls
90 lines (74 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from flask import Flask, request, jsonify
import joblib
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
import numpy as np
import traceback
# Download necessary NLTK data files
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')
app = Flask(__name__)
# Load models and vectorizer
try:
model_impactf = joblib.load('model_impactf.pkl')
model_probf = joblib.load('model_probf.pkl')
vectorizer = joblib.load('vectorizer.pkl')
except Exception as e:
print(f"Error loading models or vectorizer: {str(e)}")
raise
# Define stop words and lemmatizer
stop_words = set(stopwords.words('french'))
lemmatizer = WordNetLemmatizer()
def preprocess_text(text):
try:
# Convert the text to string (in case it's not)
text = str(text)
# Tokenize the text
tokens = word_tokenize(text)
# Convert to lower case
tokens = [word.lower() for word in tokens]
# Remove punctuation and stop words
tokens = [word for word in tokens if word.isalnum() and word not in stop_words]
# Lemmatize the words
tokens = [lemmatizer.lemmatize(word) for word in tokens]
# Join the tokens back into a string
return ' '.join(tokens)
except Exception as e:
print(f"Error in text preprocessing: {str(e)}")
raise
@app.route('/predict', methods=['POST'])
def predict():
try:
data = request.get_json(force=True)
print("Received data:", data) # Debugging
if 'Risques' not in data:
return jsonify({'error': 'Key "Risques" is missing in the input data'}), 400
risques = data['Risques']
# Preprocess the text
preprocessed_text = preprocess_text(risques)
print("Preprocessed text:", preprocessed_text) # Debugging
# Vectorize the input text
input_features = vectorizer.transform([preprocessed_text])
# Predict impact and probability
predicted_impact = model_impactf.predict(input_features)[0]
predicted_probability = model_probf.predict(input_features)[0]
# Convert predictions to standard Python integers
predicted_impact = int(predicted_impact.item())
predicted_probability = int(predicted_probability.item())
response = {
'Predicted Impact': predicted_impact,
'Predicted Probability': predicted_probability
}
print("Response:", response) # Debugging
return jsonify(response)
except Exception as e:
print("Error occurred:", traceback.format_exc())
response = {
'error': str(e)
}
return jsonify(response), 500
if __name__ == '__main__':
app.run(debug=True)