Spaces:
Sleeping
Sleeping
File size: 2,523 Bytes
bb30d0b 70b06f2 bb30d0b 15c4b99 a903bc9 28772d2 |
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 |
import os
import json
import gradio as gr
from difflib import SequenceMatcher
# Désactiver l'utilisation du GPU pour TensorFlow (si nécessaire)
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Désactive le GPU
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Réduit les logs de TensorFlow
class HuggingFaceChatbot:
def __init__(self, responses_file="responses.json"):
"""Initialisation du chatbot, chargement des réponses."""
self.responses = self.load_responses(responses_file)
def load_responses(self, file_path):
"""Charge les réponses depuis un fichier JSON."""
try:
with open(file_path, "r") as f:
return json.load(f)
except FileNotFoundError:
print("Aucun fichier de réponses trouvé. Utilisation des réponses par défaut.")
return {
"bonjour": "Bonjour! Comment puis-je vous aider?",
"comment ça va?": "Je suis une IA, je vais toujours bien! Et vous?",
}
def find_best_response(self, user_input):
"""Trouve la meilleure correspondance basée sur la similarité."""
max_similarity = 0
best_match = None
for key in self.responses.keys():
similarity = SequenceMatcher(None, user_input.lower(), key).ratio()
if similarity > max_similarity:
max_similarity = similarity
best_match = key
# Retourne la réponse si une correspondance suffisante est trouvée
if max_similarity > 0.4: # Seuil ajustable
return self.responses[best_match]
return "Je ne suis pas sûr de comprendre. Pouvez-vous reformuler?"
def __call__(self, user_input):
"""Permet d'utiliser l'instance comme une fonction pour répondre."""
return self.find_best_response(user_input)
# Charger le chatbot
chatbot = HuggingFaceChatbot()
# Fonction pour l'interface Gradio
def chatbot_interface(user_input):
"""Fonction pour interagir avec Gradio."""
return chatbot(user_input)
# Créer l'interface Gradio
iface = gr.Interface(
fn=chatbot_interface,
inputs=gr.Textbox(label="Entrez votre message", placeholder="Tapez ici...", lines=2),
outputs="text",
title="Chatbot Interactif",
description="Un chatbot simple qui répond à des questions courantes.",
allow_flagging="never" # Option pour désactiver le flagging si tu ne veux pas que les utilisateurs signalent des réponses.
)
# Démarrer l'interface Gradio
iface.launch() |