-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
143 lines (124 loc) · 5.35 KB
/
main.py
File metadata and controls
143 lines (124 loc) · 5.35 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import sys
import logging
from chatbot import MultilingualChatbot
class ChatbotInterface:
def __init__(self):
self.chatbot = MultilingualChatbot()
self.running = False
def initialize(self):
print("Initializing multilingual chatbot system...")
if not self.chatbot.initialize():
print("Failed to initialize chatbot. Please check the logs.")
return False
print("Chatbot system initialized successfully.")
self.display_welcome_message()
return True
def display_welcome_message(self):
print("\n" + "="*60)
print("Supported Languages:")
for code, name in self.chatbot.get_supported_languages().items():
print(f" - {name} ({code})")
print(f"\nCurrent Language: {self.chatbot.get_supported_languages()[self.chatbot.get_current_language()]}")
print("\nCommands:")
print(" /lang [code] - Change language (e.g., /lang tr)")
print(" /stats - Show statistics")
print(" /help - Show this help")
print(" /quit - Exit chatbot")
print("="*60)
def process_command(self, user_input):
if not user_input.startswith('/'):
return False
command_parts = user_input[1:].split()
command = command_parts[0].lower()
if command == 'quit' or command == 'exit':
self.running = False
print("Thank you for using the multilingual chatbot. Goodbye!")
return True
elif command == 'help':
self.display_welcome_message()
return True
elif command == 'lang':
if len(command_parts) < 2:
print("Usage: /lang [language_code]")
print("Available codes:", list(self.chatbot.get_supported_languages().keys()))
else:
lang_code = command_parts[1].lower()
if self.chatbot.set_language(lang_code):
lang_name = self.chatbot.get_supported_languages()[lang_code]
print(f"Language changed to: {lang_name}")
else:
print(f"Unsupported language: {lang_code}")
return True
elif command == 'stats':
stats = self.chatbot.get_statistics()
print("\nChatbot Statistics:")
print(f" Supported Languages: {stats['supported_languages']}")
print(f" Available Models: {stats['available_models']}")
print(f" Current Language: {stats['current_language']}")
print(f" Loaded Languages: {', '.join(stats['available_languages'])}")
return True
else:
print(f"Unknown command: {command}")
print("Type /help for available commands.")
return True
def run_interactive_mode(self):
self.running = True
while self.running:
try:
user_input = input("\nYou: ").strip()
if not user_input:
continue
if self.process_command(user_input):
continue
response = self.chatbot.get_response(user_input)
print(f"Bot: {response}")
except KeyboardInterrupt:
print("\n\nInterrupted by user. Goodbye!")
break
except Exception as e:
logging.error(f"Error in interactive mode: {str(e)}")
print("An error occurred. Please try again.")
def run_single_query(self, query, language=None):
if language:
self.chatbot.set_language(language)
response = self.chatbot.get_response(query)
return response
def run_batch_mode(self, queries):
results = []
for query in queries:
response = self.chatbot.get_response(query)
results.append({
'query': query,
'response': response,
'language': self.chatbot.get_current_language()
})
return results
def main():
interface = ChatbotInterface()
if len(sys.argv) > 1:
if sys.argv[1] == '--help':
print("Multilingual Chatbot")
print("Usage:")
print(" python main.py - Interactive mode")
print(" python main.py --help - Show this help")
print(" python main.py --query 'text' - Single query mode")
print(" python main.py --lang [code] --query 'text' - Query with specific language")
return
elif sys.argv[1] == '--query' and len(sys.argv) > 2:
if not interface.initialize():
return
query = sys.argv[2]
language = None
if '--lang' in sys.argv:
lang_index = sys.argv.index('--lang')
if lang_index + 1 < len(sys.argv):
language = sys.argv[lang_index + 1]
response = interface.run_single_query(query, language)
print(f"Query: {query}")
print(f"Response: {response}")
return
if not interface.initialize():
return
interface.run_interactive_mode()
if __name__ == "__main__":
main()