communicaite / app.py
seduerr's picture
adapted for communicaite
dea3f12
import gradio as gr
import re
from services.hate_speech import classify_hatespeech
from services.cola import classify_correctness
from services.bad_words import identify_bad_words
examples = [
'John is a son of a.', 'John a wonderful item', 'The dog is a bastard.',
'The dog a cat.', 'It was Peter Thiel from PayPal.',
'Dear Thomas,\n\nI understand that last Friday when you were a guest at our restaurant you experienced an unfortunate mishap that resulted in a beverage being spilled on your coat. Please accept my sincere apology.\n\nAs we all know accidents happen but it’s how the establishment responds that either rectifies the situation or makes it worse. Unfortunately the staff on duty at the time did not reflect our customer service policy. I have investigated the incident, talked to those involved, and scheduled remedial customer relations training for them. In addition, please send the dry cleaning bill for your coat directly to me at the address on the letterhead above and we will reimburse you for the cost.\n\nWe’d like to have you back as a customer so I’m enclosing a coupon for two free entrees for you and a guest that can be used at any of our three locations in Austin. Again, my apologies for the incident. I hope you give us the opportunity to make this right. We value your patronage.\n\nSincerely,\nBenson Bailey',
"Hi Professor,\n\nI have really had a rough week and I won't be able to submit my paper in time. First, my car broke down onn Monday, then my dog got sick on Tuesday and I needed to take the bus to get to the vent annd I lost another day. Then I had to cram all night for an exam that I wrote today. Now, I am sitting here, trying to write this paper and I'm just too exhausted to do anything. So, I wanted to kindly ask you if I could get an extention for two days?\n\nThanks a lot,\nPeter",
]
def check_ethical(text: str):
# simple heuristic based on offensive word list by cmu.edu
identified_bad_words = identify_bad_words(text)
if len(identified_bad_words) > 0:
return {'status': 'Input contains offensive words.', 'data': identified_bad_words}
# based on DistilRoberta hosted on transformers
nice = float(classify_hatespeech(text))
if nice < .8:
return {'status': 'Input contains hate speech.', 'data': nice}
# based on DistilBert hosted on transformers
linguistic_incorrect = float(classify_correctness(text))
if linguistic_incorrect < .8:
return {'status': 'Input is linguistically inacceptable.', 'data': text}
return {'status': "ethical", 'data': text}
title = 'Communicaite - Ethical Communication'
description = '''Please insert any text to scan for subliminal hate speech, the use of bad words or inappropriate language. You can also try the different examples from below.
Thank you. '''
demo = gr.Interface(fn=check_ethical, inputs='text', outputs='text',
examples=examples, title=title, description=description)
demo.launch()