Spaces:
Build error
Build error
Create Main.py
Browse files
Main.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from chatterbot import ChatBot
|
2 |
+
from chatterbot.trainers import ChatterBotCorpusTrainer
|
3 |
+
from kivy.app import App
|
4 |
+
from kivy.uix.boxlayout import BoxLayout
|
5 |
+
from kivy.uix.label import Label
|
6 |
+
from kivy.uix.textinput import TextInput
|
7 |
+
|
8 |
+
class ChatBotApp(App):
|
9 |
+
def __init__(self, **kwargs):
|
10 |
+
super().__init__(**kwargs)
|
11 |
+
self.chatbot = ChatBot('ChatBot')
|
12 |
+
self.trainer = ChatterBotCorpusTrainer(self.chatbot)
|
13 |
+
self.trainer.train("chatterbot.corpus.english.greetings",
|
14 |
+
"chatterbot.corpus.english.conversations")
|
15 |
+
|
16 |
+
def build(self):
|
17 |
+
box_layout = BoxLayout(orientation='vertical')
|
18 |
+
self.input_field = TextInput(multiline=False, hint_text="Tu mensaje...")
|
19 |
+
self.output_area = Label(halign='left', markup=True)
|
20 |
+
box_layout.add_widget(self.input_field)
|
21 |
+
box_layout.add_widget(self.output_area)
|
22 |
+
self.input_field.bind(on_text_validate=self.send_message)
|
23 |
+
return box_layout
|
24 |
+
|
25 |
+
def send_message(self, instance):
|
26 |
+
input_text = instance.text
|
27 |
+
output_text = self.get_response(input_text)
|
28 |
+
self.update_display(input_text, output_text)
|
29 |
+
self.input_field.text = ""
|
30 |
+
|
31 |
+
def get_response(self, input_text):
|
32 |
+
try:
|
33 |
+
response = self.chatbot.get_response(input_text)
|
34 |
+
except Exception as e:
|
35 |
+
response = f"Lo siento, hubo un error: {str(e)}"
|
36 |
+
finally:
|
37 |
+
return str(response)
|
38 |
+
|
39 |
+
def update_display(self, input_text, output_text):
|
40 |
+
self.output_area.text += f"\n<b>{input_text}</b>\n{output_text}"
|
41 |
+
|
42 |
+
if __name__ == "__main__":
|
43 |
+
ChatBotApp().run()
|