Spaces:
Runtime error
Runtime error
Soufianesejjari
commited on
Commit
•
bcb9fa1
1
Parent(s):
7eb291e
Add application file
Browse files- Dockerfile +11 -0
- app/model.py +80 -0
- app/tokenizer.pickle +3 -0
- app/word_prediction_model.h5 +3 -0
- requirements.txt +3 -0
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Utilisez l'image de base Hugging Face Spaces
|
2 |
+
FROM huggingface/transformers-env:4.11.3
|
3 |
+
|
4 |
+
# Copiez les fichiers de l'application
|
5 |
+
COPY ./app /app
|
6 |
+
|
7 |
+
# Installation des dépendances Python
|
8 |
+
RUN pip install -r /app/requirements.txt
|
9 |
+
|
10 |
+
# Commande pour exécuter l'application Flask
|
11 |
+
CMD ["python", "/app/model.py"]
|
app/model.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
if __name__ == '__main__':
|
80 |
+
app.run(debug=True)
|
app/tokenizer.pickle
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:be21f35560989378b270f92402fd78d1087eca6676d2ca25382fc5227cba988f
|
3 |
+
size 1480776
|
app/word_prediction_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8f827a27bbafb55f14c623fb32bc70038696a3f1ae9d7b12024b7102e3050bf2
|
3 |
+
size 80934136
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
Flask
|
2 |
+
tensorflow
|
3 |
+
pickle
|