companiai / app.py
Sachin
more examples
439d45c
Raw
History Blame Contribute Delete
3.16 kB
import gradio as gr
import os
from dotenv import load_dotenv
import requests
import json
load_dotenv()
SERVER_URL = os.getenv('SERVER_URL')
OPEN_AI_KEY = os.getenv('OPEN_AI_KEY')
examples = [
"this game is awesome",
"not everything that happens in game is toxic",
"False postive are very common in game moderation by Gen AI",
"give me some bullets for the gun red",
"black your game sucks",
"but occasionally there could be toxicity",
"suuuuuuuk my d",
"red you are πŸ’©"
]
def get_openai_tox_score(text):
url = "https://api.openai.com/v1/moderations"
payload = json.dumps({
"model": "omni-moderation-latest",
"input": text
})
headers = {
'Authorization': f'Bearer {OPEN_AI_KEY}',
'Content-Type': 'application/json',
}
response = requests.request("POST", url, headers=headers, data=payload)
data = response.json()
if data['results'][0]['flagged']:
return "This content has been flagged as potentially toxic. πŸ›‘"
return "This content appears to be safe. βœ…"
def get_tox_score(text):
url = f"{SERVER_URL}/api/analyzer/toxscore"
payload = json.dumps({
"text": text
})
headers = {
'Content-Type': 'application/json',
}
try:
response = requests.request("POST", url, headers=headers, data=payload)
open_ai_response = get_openai_tox_score(text)
if response.json()['flagged']:
return "This content has been flagged as potentially toxic. πŸ›‘", open_ai_response
return "This content appears to be safe. βœ…", open_ai_response
except Exception as e:
print(e)
return "Error Occured"
with gr.Blocks() as demo:
gr.HTML(
f"""
<div style="text-align: center; margin-bottom: 20px;">
<div style="display: flex; justify-content: center;">
<img src="https://companiai.s3.us-east-1.amazonaws.com/assets/small_logo_compani_ai.png" alt="Compani AI" width="320" style="margin-bottom: 10px;"/>
</div>
<p></p>
<p>Compani AI helps gaming companies to eliminate in-game chat toxicity with advanced AI moderation.</p>
<p>Following is a simple demo of our machine learning model which outperforms Open AI moderation on multiple parameters.</p>
</div>
"""
)
message_input = gr.Textbox(label="Chat Message", placeholder="Type your message here...")
check_button = gr.Button("Check Toxicity")
with gr.Row():
toxicity_output = gr.Textbox(label="Toxicity Result CompaniAI", interactive=False)
toxicity_openAI_output = gr.Textbox(label="Toxicity Result OpenAI", interactive=False)
check_button.click(get_tox_score, inputs=message_input, outputs=[toxicity_output,toxicity_openAI_output])
gr.Examples(
examples=examples,
inputs=message_input,
outputs=None, # Outputs don't need pre-filling for static examples
label="Try these example inputs:",
)
demo.launch()