Finish chatbot
This commit is contained in:
parent
7bb36b5bac
commit
e24d1894a3
@ -7,6 +7,7 @@ db_path = './hotels_data.json'
|
||||
|
||||
|
||||
class DialoguePolicy(Policy):
|
||||
info_dict = None
|
||||
def __init__(self):
|
||||
Policy.__init__(self)
|
||||
self.db = self.load_database(db_path)
|
||||
|
@ -32,6 +32,7 @@ def default_state():
|
||||
|
||||
|
||||
class DialogueStateTracker(DST):
|
||||
info_dict = None
|
||||
def __init__(self):
|
||||
DST.__init__(self)
|
||||
self.state = default_state()
|
||||
|
29
Main.py
29
Main.py
@ -1,31 +1,22 @@
|
||||
import requests
|
||||
|
||||
from NaturalLanguageAnalyzer import NaturalLanguageAnalyzer
|
||||
from DialoguePolicy import DialoguePolicy
|
||||
from DialogueStateTracker import DialogueStateTracker
|
||||
from NaturalLanguageGeneration import NaturalLanguageGeneration
|
||||
from convlab.dialog_agent import PipelineAgent
|
||||
from MachineLearningNLG import MachineLearningNLG # Importujemy nowy komponent NLG
|
||||
|
||||
|
||||
def translate_text(text, target_language='pl'):
|
||||
url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl={}&dt=t&q={}'.format(
|
||||
target_language, text)
|
||||
response = requests.get(url)
|
||||
if response.status_code == 200:
|
||||
translated_text = response.json()[0]
|
||||
translated_text_joined = ''.join([sentence[0] for sentence in translated_text])
|
||||
return translated_text_joined
|
||||
else:
|
||||
return None
|
||||
import warnings
|
||||
warnings.filterwarnings("ignore")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
text = "chciałbym zarezerwować drogi hotel z parkingiem 1 stycznia w Warszawie w centrum"
|
||||
nlu = NaturalLanguageAnalyzer()
|
||||
dst = DialogueStateTracker()
|
||||
policy = DialoguePolicy()
|
||||
nlg = MachineLearningNLG()
|
||||
nlg = NaturalLanguageGeneration()
|
||||
|
||||
agent = PipelineAgent(nlu=nlu, dst=dst, policy=policy, nlg=nlg, name='sys')
|
||||
response = agent.response(text)
|
||||
print(translate_text(response))
|
||||
|
||||
print("Witam, jestem systemem do rezerwowania pokoi hotelowych. W czym mogę Ci pomóc?")
|
||||
while True:
|
||||
text = input()
|
||||
response = agent.response(text)
|
||||
print(response)
|
||||
|
@ -14,6 +14,7 @@ def translate_text(text, target_language='en'):
|
||||
|
||||
|
||||
class NaturalLanguageAnalyzer:
|
||||
info_dict = None
|
||||
def predict(self, text, context=None):
|
||||
# Inicjalizacja modelu NLU
|
||||
model_name = "ConvLab/t5-small-nlu-multiwoz21"
|
||||
|
@ -1,12 +1,28 @@
|
||||
import re
|
||||
import requests
|
||||
|
||||
from convlab.nlg.template.multiwoz import TemplateNLG
|
||||
|
||||
|
||||
def translate_text(text, target_language='pl'):
|
||||
url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl={}&dt=t&q={}'.format(
|
||||
target_language, text)
|
||||
response = requests.get(url)
|
||||
if response.status_code == 200:
|
||||
translated_text = response.json()[0]
|
||||
translated_text_joined = ''.join([sentence[0] for sentence in translated_text])
|
||||
return translated_text_joined
|
||||
else:
|
||||
return None
|
||||
|
||||
class NaturalLanguageGeneration:
|
||||
info_dict = None
|
||||
def generate(self, system_act):
|
||||
if len(system_act) == 0:
|
||||
return "Nie rozumiem."
|
||||
tnlg = TemplateNLG(is_user=False)
|
||||
response_en = tnlg.generate(system_act)
|
||||
return translate_text(response_en)
|
||||
|
||||
def nlg(self, system_act):
|
||||
response = None
|
||||
pattern = r'inform\(name=([^\)]+)\)'
|
||||
matching = re.search(pattern, system_act)
|
||||
if matching:
|
||||
name = matching.group(1)
|
||||
response = f"Witaj, nazywam się {name}"
|
||||
return response
|
||||
|
||||
def init_session(self):
|
||||
pass # Dodanie pustej metody init_session
|
||||
|
Loading…
Reference in New Issue
Block a user