Soufianesejjari commited on
Commit
3680e02
1 Parent(s): c3e50d5

Add application file

Browse files
Files changed (1) hide show
  1. app/model.py +25 -50
app/model.py CHANGED
@@ -1,79 +1,54 @@
1
- from flask import Flask, request, jsonify
 
2
  from keras.models import load_model
3
  import pickle
4
  import numpy as np
5
  from keras.preprocessing.sequence import pad_sequences
6
 
7
- app = Flask(__name__)
8
 
9
  max_sequence_length = 180
10
 
11
- # Charger le modèle entraîné
12
  try:
13
  model = load_model('word_prediction_model.h5')
14
  except Exception as e:
15
- print(f"Erreur lors du chargement du modèle : {str(e)}")
16
  model = None
17
 
18
- # Charger le tokenizer
19
  try:
20
  with open('tokenizer.pickle', 'rb') as handle:
21
  tokenizer = pickle.load(handle)
22
  except Exception as e:
23
- print(f"Erreur lors du chargement du tokenizer : {str(e)}")
24
  tokenizer = None
25
 
26
- # Fonction de prédiction des mots suivants avec probabilités
27
- def predict_next_words_with_proba(input_phrase, top_n=5):
 
 
 
 
 
 
 
 
28
  if tokenizer is None or model is None:
29
- return [], []
 
 
 
30
 
31
- # Tokeniser la phrase d'entrée
32
  input_sequence = tokenizer.texts_to_sequences([input_phrase])[0]
33
-
34
- # Remplir la séquence à la longueur maximale de séquence
35
  padded_sequence = pad_sequences([input_sequence], maxlen=max_sequence_length-1, padding='pre')
36
-
37
- # Prédire les probabilités des mots suivants
38
  predicted_probs = model.predict(padded_sequence)[0]
39
-
40
- # Obtenir les indices des mots avec les probabilités les plus élevées
41
  top_indices = predicted_probs.argsort()[-top_n:][::-1]
42
-
43
- # Obtenir les mots correspondants aux indices
44
  top_words = [tokenizer.index_word[index] for index in top_indices]
45
-
46
- # Obtenir les probabilités correspondantes
47
  top_probabilities = predicted_probs[top_indices]
48
-
49
- return top_words, top_probabilities
50
- @app.route('/test', methods=['GET'])
51
- def test():
52
- data = request.get_json()
53
- input_phrase = data['input_phrase']
54
 
55
-
56
- response = {
57
- "top_words": "test",
58
- "top_probabilities": input_phrase
59
- }
60
-
61
- return jsonify(response)
62
- @app.route('/predict', methods=['POST'])
63
- def predict():
64
- try:
65
- data = request.get_json()
66
- input_phrase = data['input_phrase']
67
- top_n = data.get('top_n', 5) # Par défaut, retourne les 5 meilleurs mots
68
-
69
- top_words, top_probabilities = predict_next_words_with_proba(input_phrase, top_n)
70
-
71
- response = {
72
- "top_words": top_words,
73
- "top_probabilities": top_probabilities.tolist()
74
- }
75
-
76
- return jsonify(response)
77
- except Exception as e:
78
- return jsonify(error=str(e)), 500
79
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
  from keras.models import load_model
4
  import pickle
5
  import numpy as np
6
  from keras.preprocessing.sequence import pad_sequences
7
 
8
+ app = FastAPI()
9
 
10
  max_sequence_length = 180
11
 
12
+ # Load the trained model
13
  try:
14
  model = load_model('word_prediction_model.h5')
15
  except Exception as e:
16
+ print(f"Error loading the model: {str(e)}")
17
  model = None
18
 
19
+ # Load the tokenizer
20
  try:
21
  with open('tokenizer.pickle', 'rb') as handle:
22
  tokenizer = pickle.load(handle)
23
  except Exception as e:
24
+ print(f"Error loading the tokenizer: {str(e)}")
25
  tokenizer = None
26
 
27
+ class PredictionRequest(BaseModel):
28
+ input_phrase: str
29
+ top_n: int = 5
30
+
31
+ class PredictionResponse(BaseModel):
32
+ top_words: list
33
+ top_probabilities: list
34
+
35
+ @app.post("/predict", response_model=PredictionResponse)
36
+ def predict(request: PredictionRequest):
37
  if tokenizer is None or model is None:
38
+ raise HTTPException(status_code=500, detail="Model or tokenizer not loaded")
39
+
40
+ input_phrase = request.input_phrase
41
+ top_n = request.top_n
42
 
 
43
  input_sequence = tokenizer.texts_to_sequences([input_phrase])[0]
 
 
44
  padded_sequence = pad_sequences([input_sequence], maxlen=max_sequence_length-1, padding='pre')
 
 
45
  predicted_probs = model.predict(padded_sequence)[0]
 
 
46
  top_indices = predicted_probs.argsort()[-top_n:][::-1]
 
 
47
  top_words = [tokenizer.index_word[index] for index in top_indices]
 
 
48
  top_probabilities = predicted_probs[top_indices]
 
 
 
 
 
 
49
 
50
+ return {"top_words": top_words, "top_probabilities": top_probabilities.tolist()}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ @app.get("/")
53
+ def read_root():
54
+ return {"message": "Hello from MDS Darija Prediction Team!"}