Spaces:
Sleeping
Sleeping
Commit
·
6d2d4e1
1
Parent(s):
bbc075a
load in json
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tiktoken
|
|
|
|
| 3 |
|
| 4 |
def count_tokens(text):
|
| 5 |
"""
|
|
@@ -18,8 +19,15 @@ def count_tokens(text):
|
|
| 18 |
# Encode the input text to get the list of token IDs
|
| 19 |
tokens = encoding.encode(text)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Define the Gradio interface
|
| 25 |
iface = gr.Interface(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tiktoken
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
def count_tokens(text):
|
| 6 |
"""
|
|
|
|
| 19 |
# Encode the input text to get the list of token IDs
|
| 20 |
tokens = encoding.encode(text)
|
| 21 |
|
| 22 |
+
try:
|
| 23 |
+
parsed_json = json.loads(text)
|
| 24 |
+
text = json.dumps(parsed_json, indent=4)
|
| 25 |
+
except json.JSONDecodeError:
|
| 26 |
+
pass
|
| 27 |
+
|
| 28 |
+
text = text.replace("\\n", "\n")
|
| 29 |
+
|
| 30 |
+
return len(tokens), text
|
| 31 |
|
| 32 |
# Define the Gradio interface
|
| 33 |
iface = gr.Interface(
|