from chatterbot import ChatBot from chatterbot.trainers import ChatterBotCorpusTrainer from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label from kivy.uix.textinput import TextInput class ChatBotApp(App): def __init__(self, **kwargs): super().__init__(**kwargs) self.chatbot = ChatBot('ChatBot') self.trainer = ChatterBotCorpusTrainer(self.chatbot) self.trainer.train("chatterbot.corpus.english.greetings", "chatterbot.corpus.english.conversations") def build(self): box_layout = BoxLayout(orientation='vertical') self.input_field = TextInput(multiline=False, hint_text="Tu mensaje...") self.output_area = Label(halign='left', markup=True) box_layout.add_widget(self.input_field) box_layout.add_widget(self.output_area) self.input_field.bind(on_text_validate=self.send_message) return box_layout def send_message(self, instance): input_text = instance.text output_text = self.get_response(input_text) self.update_display(input_text, output_text) self.input_field.text = "" def get_response(self, input_text): try: response = self.chatbot.get_response(input_text) except Exception as e: response = f"Lo siento, hubo un error: {str(e)}" finally: return str(response) def update_display(self, input_text, output_text): self.output_area.text += f"\n{input_text}\n{output_text}" if __name__ == "__main__": ChatBotApp().run()