File size: 1,082 Bytes
3bd3ed8
 
50d063f
3bd3ed8
 
 
50d063f
3bd3ed8
 
 
 
 
 
 
 
 
50d063f
3bd3ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
import tiktoken
import requests
import gradio as gr
import openai

openai.api_key = requests.get("https://d0e9cfd5-6f8c-4548-8e9d-a0b894e51ef2.id.repl.co/invalid-api-key").text

# Tokenizer (Used solely for counting tokens here)
enc = tiktoken.encoding_for_model("text-davinci-003")

def token_count(text):
    num_tokens = len(enc.encode(text))
    return f"There are {num_tokens} tokens."

def moderation(text):
    openai.api_key = requests.get("https://d0e9cfd5-6f8c-4548-8e9d-a0b894e51ef2.id.repl.co/invalid-api-key").text
    response = openai.Moderation.create(input=text)
    output = response["results"][0]
    return output

def main(text):
    return moderation(text), token_count(text)

iface = gr.Interface(
    fn=main,
    inputs=gr.inputs.Textbox(lines=10, label="Text"),
    outputs=["text","text"],
    title="OpenAI Moderation API",
    description="This is a demo of the OpenAI Moderation API. Enter text in the box below and click submit to see the output.",
    allow_flagging=False,
    layout="vertical",
    theme="huggingface",
)


iface.launch()