Skip to content

Instantly share code, notes, and snippets.

@StevenJL
Last active July 31, 2022 03:57
Show Gist options
  • Save StevenJL/6143ae3a520b538be17beb877c7f535b to your computer and use it in GitHub Desktop.
Save StevenJL/6143ae3a520b538be17beb877c7f535b to your computer and use it in GitHub Desktop.
Zero-shot MNLI Chatbot Demo
#
# ```
# which python3
# pip3 install torch
# pip3 install transformers
# ```
import numpy as np
from transformers import pipeline
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
def classify(str_to_classify, labels):
result = classifier(str_to_classify, labels)
max_index = np.argmax(result['scores'])
return result['labels'][max_index]
def collect_data(prompt, labels, label_responses):
print("================================")
user_input = input(prompt)
best_label = classify(user_input, labels)
print(label_responses[best_label])
print("\n")
def years_experience_responses():
output = {0: 'No experience is fine', 1: "Thanks, I'll mark you down for one year"}
for n in range(2,26):
output[n] = 'Thanks, I\'ll mark you down for ' + str(n) + ' years!'
return output
while(True):
collect_data("Are you legally authorized to work?\n", ["yes", "no"], { "yes": "Great to hear you are authorized to work!", "no": "Sorry, you must be authorized to work in order to continue."})
collect_data("Do you prefer to work in the mornings, afternoons, or evenings?\n", ["morning", "afternoon", "evening"], { "morning": "Thanks, I'll find you only morning gigs then", "afternoon": "Thanks, I'll find you only afternoon gigs then.", "evening":"Gotcha, I'll find you only evening gigs, then"})
collect_data("How many years of experience do you have in the restaurant industry?\n", [x for x in range(26)], years_experience_responses())
#
#
# SAMPLE DIALOG:
# Are you legally authorized to work?
# most definitely
# Great to hear you are authorized to work!
#
#
# ================================
# Do you prefer to work in the mornings, afternoons, or evenings?
# I like working early
# Thanks, I'll find you only morning gigs then
#
#
# ================================
# How many years of experience do you have in the restaurant industry?
# None, unfortunately
# No experience is fine
#
#
# ================================
# Are you legally authorized to work?
# you bet
# Great to hear you are authorized to work!
#
#
# ================================
# Do you prefer to work in the mornings, afternoons, or evenings?
# 1pm-3pm
# Thanks, I'll find you only afternoon gigs then.
#
#
# ================================
# How many years of experience do you have in the restaurant industry?
# 5
# Thanks, I'll mark you down for 5 years!
#
#
# ================================
# Are you legally authorized to work?
# nawww
# Sorry, you must be authorized to work in order to continue.
#
#
# ================================
# Do you prefer to work in the mornings, afternoons, or evenings?
# Im a night owl, tbh
# Gotcha, I'll find you only evening gigs, then
#
#
# ================================
# How many years of experience do you have in the restaurant industry?
# a couple
# Thanks, I'll mark you down for 2 years!
#
#
# ================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment