Skip to content

Instantly share code, notes, and snippets.

View robgon-art's full-sized avatar
😀

Robert A. Gonsalves robgon-art

😀
View GitHub Profile
@robgon-art
robgon-art / ai8ball.py
Last active July 31, 2021 15:44
Yes/No question answering with RoBERTa
import math
import torch
def predict(question, passage):
sequence = tokenizer.encode_plus(question, passage, return_tensors="pt",
max_length=512, truncation=True)['input_ids'].to(device)
logits = model(sequence)[0]
probabilities = torch.softmax(logits, dim=1).detach().cpu().tolist()[0]
vector = logits.detach().cpu().tolist()[0]
n = (vector[0]/2.75 + 1)/2
y = (vector[1]/2.75 + 1)/2
@robgon-art
robgon-art / aiaball.txt
Last active October 27, 2020 22:32
Answers to questions
Passage: The Magic 8-Ball is a plastic sphere, made to look like an eight-ball, that is used for
fortune-telling or seeking advice. It was invented in 1950 by Albert C. Carter and Abe Bookman
and is currently manufactured by Mattel. The user asks a yes–no question to the ball and then
turns it over to reveal an answer in a window on the ball
Question: Is the Magic 8-Ball a sphere?, Yes: 0.99, No: 0.01 Conf.: 0.98
Question: Is the Magic 8-Ball a cube?, Yes: 0.11, No: 0.89 Conf.: 0.42
Question: Was the Magic 8-Ball invented in 1940?, Yes: 0.04, No: 0.96 Conf.: 0.64
Question: Was the Magic 8-Ball invented in 1950?, Yes: 0.98, No: 0.02 Conf.: 0.77
Question: Was the Magic 8-Ball invented by Carter and Bookman?, Yes: 0.97, No: 0.03 Conf.: 0.71
@robgon-art
robgon-art / ai8ball_train.py
Last active October 25, 2020 15:00
Train the ai8ball
from tqdm import tqdm
batch_size = 8
epochs = 3
grad_acc_steps = 4
train_loss_values = []
dev_acc_values = []
for _ in tqdm(range(epochs), desc="Epoch"):
# Training
epoch_train_loss = 0
model.train()
@robgon-art
robgon-art / get_keywords.py
Created October 25, 2020 21:56
Get keywords from text
import pytextrank
import spacy
nlp = spacy.load("en_core_web_sm")
tr = pytextrank.TextRank()
nlp.add_pipe(tr.PipelineComponent, name="textrank", last=True)
question = "Can a computer beat a grandmaster chess player?"
query = ""
keywords = nlp(question.lower())
@robgon-art
robgon-art / get_passages.py
Created October 25, 2020 22:56
Get text passages
import wikipedia
print("Checking the Wikipedia.")
results = wikipedia.search(query, results = 3)
wiki_passage = ""
for r in results[1:]:
try:
s = wikipedia.summary(r)
except:
continue
wiki_passage += s.strip() + " "
@robgon-art
robgon-art / process_results.py
Created October 25, 2020 23:14
Process the AI 8-Ball results
min_dist = float("inf")
pick = 0
if (conf < 0.5):
map_conf = 1 + conf * 19 / 2
for i, a in enumerate(answers[:5]):
c = a[1][1]
distance = abs(map_conf-c)
if distance < min_dist:
min_dist = distance
@robgon-art
robgon-art / best_result.py
Created October 25, 2020 23:17
Choose the best result
conf = 0
yes = 0.5
passage = ""
source = "no source"
if (wiki_conf > nyt_conf and wiki_conf > boolq_conf):
yes = wiki_yes
conf = wiki_conf
passage = wiki_passage
source = "Wikipedia"
@robgon-art
robgon-art / words_to_syllables.py
Last active November 25, 2020 23:21
Break words into syllables
from gensim.models import Word2Vec
syllModel = Word2Vec.load(syll_model_path)
wordModel = Word2Vec.load(word_model_path)
import pyphen
dic = pyphen.Pyphen(lang='en_US')
poem = '''A plow, they say, to plow the snow.
They cannot mean to plant it, no–
Unless in bitterness to mock
@robgon-art
robgon-art / transpose.py
Last active November 23, 2020 00:37
Transpose notes using Music21
import music21
def transpose_notes(notes, new_key):
midi_stream = music21.stream.Stream(notes)
key = midi_stream.analyze('key')
interval = music21.interval.Interval(key.tonic, new_key.tonic)
new_stream = midi_stream.transpose(interval)
return new_stream.notes
# quantize the start times and note durations
@robgon-art
robgon-art / generate_melodies.py
Created November 23, 2020 00:43
Generate melodies using the lyric conditioned LSTM-GAN
length_song = len(lyrics)
cond = []
for i in range(20):
if i < length_song:
syll2Vec = syllModel.wv[lyrics[i][0]]
word2Vec = wordModel.wv[lyrics[i][1]]
cond.append(np.concatenate((syll2Vec, word2Vec)))
else:
cond.append(np.concatenate((syll2Vec, word2Vec)))