Spaces:
Sleeping
Sleeping
from statistics import mode | |
import urllib.request | |
import gradio as gr | |
import unidecode | |
import string | |
import json | |
urllib.request.urlretrieve( | |
'https://drive.google.com/uc?export=download&id=1TXD41vfqNWA6UNDQ73WhtJdZivCJatn-', | |
'bot_questions_tags.json' | |
) | |
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!).', | |
'You can ask me things about "Artificial Intelligence," "Machine Learning," "AI Safety," or "AI Ethics."', | |
"I don't have that kind of property hahaha I am software!", | |
"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.", | |
'Aron Hui is the president/founder of AIRES.', | |
'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."', | |
'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.', | |
'General Intelligence, or Universal Intelligence, can be defined as the ability to efficiently achieve goals in a wide range of domains.', | |
'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.', | |
'A multi-agent system (MAS "Multi-Agent Systems") is a computer system composed of multiple interacting intelligent agents.', | |
'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.'] | |
with open('bot_questions_tags.json') as json_file: | |
dictionary = json.load(json_file) | |
def generate_ngrams(text, WordsToCombine): | |
""" | |
Generates n-grams of length WordsToCombine from the input text. | |
Args: | |
text: A string representing the input text | |
WordsToCombine: An integer representing the | |
size of the n-grams to be generated | |
Returns: | |
A list of n-grams generated from the input text, where each | |
n-gram is a list of WordsToCombine words | |
""" | |
words = text.split() | |
output = [] | |
for i in range(len(words) - WordsToCombine+1): | |
output.append(words[i:i+WordsToCombine]) | |
return output | |
def make_keys(text, WordsToCombine): | |
""" | |
Given a text and a number of words to combine, returns | |
a list of keys that correspond to all possible combinations | |
of n-grams (sequences of n consecutive words) in the text. | |
Args: | |
- text (str): The input text. | |
- WordsToCombine (int): The number of words to combine. | |
Returns: | |
- sentences (list of str): A list of all the keys, which are | |
the n-grams in the text. | |
""" | |
gram = generate_ngrams(text, WordsToCombine) | |
sentences = [] | |
for i in range(0, len(gram)): | |
sentence = ' '.join(gram[i]) | |
sentences.append(sentence) | |
return sentences | |
def chat(message, history): | |
""" | |
A function that generates a response to a user input message | |
based on a pre-built dictionary of responses. | |
Args: | |
message (str): A string representing the user's input message. | |
history (list): A list of tuples containing previous | |
messages and responses. | |
Returns: | |
tuple: A tuple containing two lists of tuples. The first list is | |
the original history with the user's input message and the bot's | |
response appended as a tuple. The second list is an updated history | |
with the same tuples. | |
""" | |
history = history or [] | |
text = message.lower() | |
sentences = [] | |
values = [] | |
new_text = text.translate(str.maketrans('', '', string.punctuation)) | |
new_text = unidecode.unidecode(new_text) | |
if len(new_text.split()) == 1: | |
if new_text in dictionary.keys(): | |
l = [dictionary[new_text]] * 100 | |
values.append(l) | |
new_text = new_text + ' ' + new_text | |
else: | |
if new_text in dictionary.keys(): | |
l = [dictionary[new_text]] * 100 | |
values.append(l) | |
for i in range(1, len(new_text.split()) + 1): | |
sentence = make_keys(new_text, i) | |
sentences.append(sentence) | |
for i in range(len(sentences)): | |
attention = sentences[i] | |
for i in range(len(attention)): | |
if attention[i] in dictionary.keys(): | |
l = [dictionary[attention[i]]] * i | |
values.append(l) | |
if len([item for sublist in values for item in sublist]) == 0: | |
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!" | |
history.append((message, bot_input_ids)) | |
return history, history | |
else: | |
values = [item for sublist in values for item in sublist] | |
prediction = mode(values) | |
bot_input_ids = answers[int(prediction)-1] | |
history.append((message, bot_input_ids)) | |
return history, history | |
title = "Basic Chatbot - By Teeny-Tiny Castle 🏰" | |
head = ( | |
"<center>" | |
"<img src='https://d2vrvpw63099lz.cloudfront.net/do-i-need-a-chatbot/header-chat-box.png' width=400>" | |
"This is an example of a rules-based closed domain chatbot. It knows a couple of answers to questions related to AI." | |
"<br>" | |
"</center>" | |
) | |
ref = ( | |
"<center>" | |
"To see its full version (ML style) of this bot, go to <a href='https://playground.airespucrs.org/aira'>this link</a>." | |
"</center>") | |
# create a chat interface | |
chatbot = gr.Chatbot() | |
demo = gr.Interface( | |
chat, | |
["text", "state"], | |
[chatbot, "state"], | |
allow_flagging="never", | |
title=title, | |
description=head, | |
article=ref | |
) | |
demo.launch() |