Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,58 +1,50 @@
|
|
1 |
import gradio as gr
|
2 |
-
import tensorflow as tf
|
3 |
-
from tensorflow.keras.models import load_model
|
4 |
-
import numpy as np
|
5 |
import json
|
|
|
|
|
6 |
|
7 |
-
# Charger
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
def preprocess_input(input_text):
|
16 |
-
# Exemple simple : convertissez le texte en une séquence de mots ou indices numériques
|
17 |
-
input_text = input_text.lower() # Mise en minuscules
|
18 |
-
# Ajoutez ici la tokenisation et le padding si nécessaire
|
19 |
-
# Par exemple, si vous utilisez un tokenizer Keras :
|
20 |
-
# tokenizer = ... # Chargez votre tokenizer ici
|
21 |
-
# tokens = tokenizer.texts_to_sequences([input_text]) # Transformez le texte en séquences
|
22 |
-
# Ajoutez du padding si nécessaire
|
23 |
-
# tokens = tf.keras.preprocessing.sequence.pad_sequences(tokens, padding='post')
|
24 |
-
# return tokens
|
25 |
-
# Exemple de test avec un tableau vide pour démonstration
|
26 |
-
return np.array([[1]]) # À ajuster selon votre modèle
|
27 |
|
28 |
-
# Fonction pour
|
29 |
def chatbot_response(user_input):
|
30 |
-
#
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
try:
|
41 |
-
prediction = model.predict(processed_input) # Faites la prédiction
|
42 |
-
# Post-traitement de la prédiction (ajustez selon votre modèle)
|
43 |
-
response = process_prediction(prediction)
|
44 |
-
return response
|
45 |
-
except Exception as e:
|
46 |
-
return f"Erreur dans la prédiction: {str(e)}"
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
51 |
|
52 |
# Interface Gradio
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
import json
|
3 |
+
from difflib import SequenceMatcher
|
4 |
+
from unidecode import unidecode
|
5 |
|
6 |
+
# Charger les réponses prédéfinies
|
7 |
+
def load_data():
|
8 |
+
try:
|
9 |
+
with open("responses.json", "r") as f:
|
10 |
+
responses = json.load(f)
|
11 |
+
except FileNotFoundError:
|
12 |
+
responses = {
|
13 |
+
"bonjour": "Bonjour! Comment puis-je vous aider?",
|
14 |
+
"comment ça va?": "Je suis une IA, je vais toujours bien! Et vous?",
|
15 |
+
"quel est ton nom?": "Je suis un chatbot simple créé pour discuter avec vous.",
|
16 |
+
"au revoir": "Au revoir! Passez une bonne journée!",
|
17 |
+
}
|
18 |
+
return responses
|
19 |
|
20 |
+
responses = load_data()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
# Fonction pour obtenir la réponse du chatbot
|
23 |
def chatbot_response(user_input):
|
24 |
+
user_input = unidecode(user_input.lower()) # Normaliser l'entrée
|
25 |
+
max_similarity = 0
|
26 |
+
best_match = None
|
27 |
+
for key in responses.keys():
|
28 |
+
normalized_key = unidecode(key.lower()) # Normaliser la clé
|
29 |
+
similarity = SequenceMatcher(None, user_input, normalized_key).ratio()
|
30 |
+
print(f"Comparing '{user_input}' with '{normalized_key}' -> Similarity: {similarity}")
|
31 |
+
if similarity > max_similarity:
|
32 |
+
max_similarity = similarity
|
33 |
+
best_match = key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
if max_similarity > 0.5: # Seuil ajusté
|
36 |
+
return responses[best_match]
|
37 |
+
|
38 |
+
return "Je ne suis pas sûr de comprendre. Pouvez-vous reformuler?"
|
39 |
|
40 |
# Interface Gradio
|
41 |
+
def gradio_interface(user_input):
|
42 |
+
return chatbot_response(user_input)
|
43 |
+
|
44 |
+
iface = gr.Interface(fn=gradio_interface,
|
45 |
+
inputs="text",
|
46 |
+
outputs="text",
|
47 |
+
title="Chatbot Simple",
|
48 |
+
description="Discutez avec un chatbot basé sur des réponses prédéfinies.")
|
49 |
|
50 |
+
iface.launch()
|
|