Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,89 +3,63 @@ import json
|
|
3 |
import gradio as gr
|
4 |
from difflib import SequenceMatcher
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
def
|
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 |
-
chatbot_output = gr.Textbox(label="Réponse du chatbot", interactive=False)
|
67 |
-
|
68 |
-
user_answer = gr.Textbox(label="Entrez la bonne réponse", placeholder="Votre réponse ici", visible=False)
|
69 |
-
|
70 |
-
def respond_and_save(user_input):
|
71 |
-
response, show_user_answer = chatbot_conversation(user_input)
|
72 |
-
|
73 |
-
if show_user_answer:
|
74 |
-
user_answer.visible = True # Afficher la zone de texte pour la nouvelle réponse
|
75 |
-
else:
|
76 |
-
user_answer.visible = False # Cacher la zone de texte si aucune réponse n'est nécessaire
|
77 |
-
|
78 |
-
return response, user_answer
|
79 |
-
|
80 |
-
def save_answer(user_input, user_answer):
|
81 |
-
return save_new_answer(user_input, user_answer)
|
82 |
-
|
83 |
-
user_input.submit(respond_and_save, [user_input], [chatbot_output, user_answer])
|
84 |
-
user_answer.submit(save_answer, [user_input, user_answer], [chatbot_output])
|
85 |
-
|
86 |
-
return demo
|
87 |
-
|
88 |
-
# Lancer l'interface
|
89 |
-
if __name__ == "__main__":
|
90 |
-
demo = interface()
|
91 |
-
demo.launch()
|
|
|
3 |
import gradio as gr
|
4 |
from difflib import SequenceMatcher
|
5 |
|
6 |
+
# Désactiver l'utilisation du GPU pour TensorFlow (si nécessaire)
|
7 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Désactive le GPU
|
8 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Réduit les logs de TensorFlow
|
9 |
+
|
10 |
+
class HuggingFaceChatbot:
|
11 |
+
def __init__(self, responses_file="responses.json"):
|
12 |
+
"""Initialisation du chatbot, chargement des réponses."""
|
13 |
+
self.responses = self.load_responses(responses_file)
|
14 |
+
|
15 |
+
def load_responses(self, file_path):
|
16 |
+
"""Charge les réponses depuis un fichier JSON."""
|
17 |
+
try:
|
18 |
+
with open(file_path, "r") as f:
|
19 |
+
return json.load(f)
|
20 |
+
except FileNotFoundError:
|
21 |
+
print("Aucun fichier de réponses trouvé. Utilisation des réponses par défaut.")
|
22 |
+
return {
|
23 |
+
"bonjour": "Bonjour! Comment puis-je vous aider?",
|
24 |
+
"comment ça va?": "Je suis une IA, je vais toujours bien! Et vous?",
|
25 |
+
}
|
26 |
+
|
27 |
+
def find_best_response(self, user_input):
|
28 |
+
"""Trouve la meilleure correspondance basée sur la similarité."""
|
29 |
+
max_similarity = 0
|
30 |
+
best_match = None
|
31 |
+
for key in self.responses.keys():
|
32 |
+
similarity = SequenceMatcher(None, user_input.lower(), key).ratio()
|
33 |
+
if similarity > max_similarity:
|
34 |
+
max_similarity = similarity
|
35 |
+
best_match = key
|
36 |
+
|
37 |
+
# Retourne la réponse si une correspondance suffisante est trouvée
|
38 |
+
if max_similarity > 0.4: # Seuil ajustable
|
39 |
+
return self.responses[best_match]
|
40 |
+
return "Je ne suis pas sûr de comprendre. Pouvez-vous reformuler?"
|
41 |
+
|
42 |
+
def __call__(self, user_input):
|
43 |
+
"""Permet d'utiliser l'instance comme une fonction pour répondre."""
|
44 |
+
return self.find_best_response(user_input)
|
45 |
+
|
46 |
+
# Charger le chatbot
|
47 |
+
chatbot = HuggingFaceChatbot()
|
48 |
+
|
49 |
+
# Fonction pour l'interface Gradio
|
50 |
+
def chatbot_interface(user_input):
|
51 |
+
"""Fonction pour interagir avec Gradio."""
|
52 |
+
return chatbot(user_input)
|
53 |
+
|
54 |
+
# Créer l'interface Gradio
|
55 |
+
iface = gr.Interface(
|
56 |
+
fn=chatbot_interface,
|
57 |
+
inputs=gr.Textbox(label="Entrez votre message", placeholder="Tapez ici...", lines=2),
|
58 |
+
outputs="text",
|
59 |
+
title="Chatbot Interactif",
|
60 |
+
description="Un chatbot simple qui répond à des questions courantes.",
|
61 |
+
allow_flagging="never" # Option pour désactiver le flagging si tu ne veux pas que les utilisateurs signalent des réponses.
|
62 |
+
)
|
63 |
+
|
64 |
+
# Démarrer l'interface Gradio
|
65 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|