Firefly777a commited on
Commit
fe7c422
1 Parent(s): 88527b9

Added token count feature

Browse files
Files changed (1) hide show
  1. app.py +13 -3
app.py CHANGED
@@ -1,20 +1,30 @@
1
  import os
 
2
 
3
  import gradio as gr
4
  import openai
5
 
6
-
7
  openai.api_key = os.environ['OPENAI_API_KEY']
8
 
 
 
 
 
 
 
 
9
  def moderation(text):
10
  response = openai.Moderation.create(input=text)
11
  output = response["results"][0]
12
  return output
13
 
 
 
 
14
  iface = gr.Interface(
15
- fn=moderation,
16
  inputs=gr.inputs.Textbox(lines=10, label="Text"),
17
- outputs="text",
18
  title="OpenAI Moderation API",
19
  description="This is a demo of the OpenAI Moderation API. Enter text in the box below and click submit to see the output.",
20
  allow_flagging=False,
1
  import os
2
+ import tiktoken
3
 
4
  import gradio as gr
5
  import openai
6
 
 
7
  openai.api_key = os.environ['OPENAI_API_KEY']
8
 
9
+ # Tokenizer (Used solely for counting tokens here)
10
+ enc = tiktoken.encoding_for_model("text-davinci-003")
11
+
12
+ def token_count(text):
13
+ num_tokens = len(enc.encode(text))
14
+ return f"There are {num_tokens} tokens."
15
+
16
  def moderation(text):
17
  response = openai.Moderation.create(input=text)
18
  output = response["results"][0]
19
  return output
20
 
21
+ def main(text):
22
+ return moderation(text), token_count(text)
23
+
24
  iface = gr.Interface(
25
+ fn=main,
26
  inputs=gr.inputs.Textbox(lines=10, label="Text"),
27
+ outputs=["text","text"],
28
  title="OpenAI Moderation API",
29
  description="This is a demo of the OpenAI Moderation API. Enter text in the box below and click submit to see the output.",
30
  allow_flagging=False,