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import mode
|
| 2 |
+
import urllib.request
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import unidecode
|
| 5 |
+
import string
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
urllib.request.urlretrieve(
|
| 9 |
+
'https://drive.google.com/uc?export=download&id=1TXD41vfqNWA6UNDQ73WhtJdZivCJatn-',
|
| 10 |
+
'bot_questions_tags.json'
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
answers = ['Hello! My name is Ai.ra, and I am an artificial intelligence (AI). More specifically, I am an NLP (Natural Language Processing) model trained in conversation (a chatbot!).',
|
| 14 |
+
'You can ask me things about "Artificial Intelligence," "Machine Learning," "AI Safety," or "AI Ethics."',
|
| 15 |
+
"I don't have that kind of property hahaha I am software!",
|
| 16 |
+
"AIRES (AI Robotics Ethics Society) is a society focused on educating the leaders and developers of tomorrow's Artificial Intelligence (AI) to ensure that AI is created ethically and responsibly.",
|
| 17 |
+
'Aron Hui is the president/founder of AIRES.',
|
| 18 |
+
'What "intelligence" is, remains an open question. However, not to leave you in the lurch, I will define "intelligence" as follows: "Intelligence is the ability of an agent to achieve goals in a wide range of environments."',
|
| 19 |
+
'There is no consensus in the literature on what "AI" is (a corollary of not having a robust definition of what "intelligence\'\' is). However, we can say that AI is the intelligence demonstrated by machines, as opposed to the natural intelligence possessed by animals and humans.',
|
| 20 |
+
'General Intelligence, or Universal Intelligence, can be defined as the ability to efficiently achieve goals in a wide range of domains.',
|
| 21 |
+
'GOFAI ("good-old-fashioned-ai"), or symbolic artificial intelligence, is the term used to refer to methods of developing AI systems based on high-level symbolic (interpretable) representations, logic, and search.',
|
| 22 |
+
'A multi-agent system (MAS "Multi-Agent Systems") is a computer system composed of multiple interacting intelligent agents.',
|
| 23 |
+
'Machine Learning (ML) is a field of research dedicated to understanding and building methods that "learn", i.e., methods that use information/data to improve performance on some tasks.']
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
with open('bot_questions_tags.json') as json_file:
|
| 27 |
+
dictionary = json.load(json_file)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def generate_ngrams(text, WordsToCombine):
|
| 31 |
+
"""
|
| 32 |
+
Generates n-grams of length WordsToCombine from the input text.
|
| 33 |
+
|
| 34 |
+
Args:
|
| 35 |
+
text: A string representing the input text
|
| 36 |
+
WordsToCombine: An integer representing the
|
| 37 |
+
size of the n-grams to be generated
|
| 38 |
+
|
| 39 |
+
Returns:
|
| 40 |
+
A list of n-grams generated from the input text, where each
|
| 41 |
+
n-gram is a list of WordsToCombine words
|
| 42 |
+
"""
|
| 43 |
+
words = text.split()
|
| 44 |
+
output = []
|
| 45 |
+
for i in range(len(words) - WordsToCombine+1):
|
| 46 |
+
output.append(words[i:i+WordsToCombine])
|
| 47 |
+
return output
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def make_keys(text, WordsToCombine):
|
| 51 |
+
"""
|
| 52 |
+
Given a text and a number of words to combine, returns
|
| 53 |
+
a list of keys that correspond to all possible combinations
|
| 54 |
+
of n-grams (sequences of n consecutive words) in the text.
|
| 55 |
+
|
| 56 |
+
Args:
|
| 57 |
+
- text (str): The input text.
|
| 58 |
+
- WordsToCombine (int): The number of words to combine.
|
| 59 |
+
|
| 60 |
+
Returns:
|
| 61 |
+
- sentences (list of str): A list of all the keys, which are
|
| 62 |
+
the n-grams in the text.
|
| 63 |
+
"""
|
| 64 |
+
gram = generate_ngrams(text, WordsToCombine)
|
| 65 |
+
sentences = []
|
| 66 |
+
for i in range(0, len(gram)):
|
| 67 |
+
sentence = ' '.join(gram[i])
|
| 68 |
+
sentences.append(sentence)
|
| 69 |
+
return sentences
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def chat(message, history):
|
| 73 |
+
"""
|
| 74 |
+
A function that generates a response to a user input message
|
| 75 |
+
based on a pre-built dictionary of responses.
|
| 76 |
+
|
| 77 |
+
Args:
|
| 78 |
+
message (str): A string representing the user's input message.
|
| 79 |
+
history (list): A list of tuples containing previous
|
| 80 |
+
messages and responses.
|
| 81 |
+
|
| 82 |
+
Returns:
|
| 83 |
+
tuple: A tuple containing two lists of tuples. The first list is
|
| 84 |
+
the original history with the user's input message and the bot's
|
| 85 |
+
response appended as a tuple. The second list is an updated history
|
| 86 |
+
with the same tuples.
|
| 87 |
+
"""
|
| 88 |
+
history = history or []
|
| 89 |
+
text = message.lower()
|
| 90 |
+
sentences = []
|
| 91 |
+
values = []
|
| 92 |
+
new_text = text.translate(str.maketrans('', '', string.punctuation))
|
| 93 |
+
new_text = unidecode.unidecode(new_text)
|
| 94 |
+
|
| 95 |
+
if len(new_text.split()) == 1:
|
| 96 |
+
if new_text in dictionary.keys():
|
| 97 |
+
l = [dictionary[new_text]] * 100
|
| 98 |
+
values.append(l)
|
| 99 |
+
new_text = new_text + ' ' + new_text
|
| 100 |
+
|
| 101 |
+
else:
|
| 102 |
+
if new_text in dictionary.keys():
|
| 103 |
+
l = [dictionary[new_text]] * 100
|
| 104 |
+
values.append(l)
|
| 105 |
+
|
| 106 |
+
for i in range(1, len(new_text.split()) + 1):
|
| 107 |
+
sentence = make_keys(new_text, i)
|
| 108 |
+
sentences.append(sentence)
|
| 109 |
+
|
| 110 |
+
for i in range(len(sentences)):
|
| 111 |
+
attention = sentences[i]
|
| 112 |
+
for i in range(len(attention)):
|
| 113 |
+
if attention[i] in dictionary.keys():
|
| 114 |
+
l = [dictionary[attention[i]]] * i
|
| 115 |
+
values.append(l)
|
| 116 |
+
|
| 117 |
+
if len([item for sublist in values for item in sublist]) == 0:
|
| 118 |
+
bot_input_ids = "I'm sorry, either I didn't understand the question, or it is not part of my domain of expertise... :( Try asking it in another way or using other words. Maybe then I can help you!"
|
| 119 |
+
history.append((message, bot_input_ids))
|
| 120 |
+
return history, history
|
| 121 |
+
|
| 122 |
+
else:
|
| 123 |
+
values = [item for sublist in values for item in sublist]
|
| 124 |
+
prediction = mode(values)
|
| 125 |
+
bot_input_ids = answers[int(prediction)-1]
|
| 126 |
+
history.append((message, bot_input_ids))
|
| 127 |
+
return history, history
|
| 128 |
+
|
| 129 |
+
title = "Basic Chatbot - By Teeny-Tiny Castle 🏰"
|
| 130 |
+
|
| 131 |
+
head = (
|
| 132 |
+
"<center>"
|
| 133 |
+
"<img src='https://d2vrvpw63099lz.cloudfront.net/do-i-need-a-chatbot/header-chat-box.png' width=400>"
|
| 134 |
+
"This is an example of a rules-based closed domain chatbot. It knows a couple of answers to questions related to AI."
|
| 135 |
+
"<br>"
|
| 136 |
+
"</center>"
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
ref = (
|
| 140 |
+
"<center>"
|
| 141 |
+
"To see its full version (ML style) of this bot, go to <a href='https://playground.airespucrs.org/aira)'>this link</a>."
|
| 142 |
+
"</center>")
|
| 143 |
+
|
| 144 |
+
# create a chat interface
|
| 145 |
+
#chatbot = gr.Chatbot().style(color_map=("green", "pink")) # Não aceitou style deu erro: AttributeError: 'Chatbot' object has no attribute 'style'
|
| 146 |
+
chatbot = gr.Chatbot()
|
| 147 |
+
|
| 148 |
+
demo = gr.Interface(
|
| 149 |
+
chat,
|
| 150 |
+
["text", "state"],
|
| 151 |
+
[chatbot, "state"],
|
| 152 |
+
allow_flagging="never",
|
| 153 |
+
title=title,
|
| 154 |
+
description=head,
|
| 155 |
+
article=ref
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
demo.launch()
|