Spaces:
Sleeping
Sleeping
Prasanna Kumar
commited on
Commit
•
007d05b
1
Parent(s):
0d3569b
Added validation on token ids input part
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ import anthropic
|
|
12 |
model_path = "models/"
|
13 |
|
14 |
# Available models
|
15 |
-
MODELS = ["Meta-Llama-3.1-8B", "gemma-2b", "gpt-3.5-turbo","gpt-4","gpt-4o"
|
16 |
openai_models = ["gpt-3.5-turbo","gpt-4","gpt-4o"]
|
17 |
# Color palette visible on both light and dark themes
|
18 |
COLOR_PALETTE = [
|
@@ -38,6 +38,20 @@ def create_vertical_histogram(data, title):
|
|
38 |
)
|
39 |
return fig
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
def process_text(text: str, model_name: str, api_key: str = None):
|
42 |
if model_name in ["Meta-Llama-3.1-8B", "gemma-2b"]:
|
43 |
tokenizer = AutoTokenizer.from_pretrained(model_path + model_name)
|
@@ -102,6 +116,9 @@ def create_html_tokens(tokens):
|
|
102 |
return html_output
|
103 |
|
104 |
def process_input(input_type, input_value, model_name, api_key):
|
|
|
|
|
|
|
105 |
if input_type == "Text":
|
106 |
text, tokens, token_ids = process_text(text=input_value, model_name=model_name, api_key=api_key)
|
107 |
elif input_type == "Token IDs":
|
@@ -150,7 +167,7 @@ with gr.Blocks() as iface:
|
|
150 |
input_type = gr.Radio(["Text", "Token IDs"], label="Input Type", value="Text")
|
151 |
model_name = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[0])
|
152 |
|
153 |
-
api_key = gr.Textbox(label="API Key Claude models)", type="password")
|
154 |
input_text = gr.Textbox(lines=5, label="Input")
|
155 |
|
156 |
with gr.Row():
|
@@ -181,7 +198,7 @@ with gr.Blocks() as iface:
|
|
181 |
|
182 |
submit_button.click(
|
183 |
process_input,
|
184 |
-
inputs=[input_type, input_text, model_name
|
185 |
outputs=[analysis_output, text_output, tokens_output, token_ids_output, words_plot, special_chars_plot, numbers_plot]
|
186 |
)
|
187 |
|
|
|
12 |
model_path = "models/"
|
13 |
|
14 |
# Available models
|
15 |
+
MODELS = ["Meta-Llama-3.1-8B", "gemma-2b", "gpt-3.5-turbo","gpt-4","gpt-4o"]
|
16 |
openai_models = ["gpt-3.5-turbo","gpt-4","gpt-4o"]
|
17 |
# Color palette visible on both light and dark themes
|
18 |
COLOR_PALETTE = [
|
|
|
38 |
)
|
39 |
return fig
|
40 |
|
41 |
+
def validate_input(input_type, input_value):
|
42 |
+
if input_type == "Text":
|
43 |
+
if not isinstance(input_value, str):
|
44 |
+
return False, "Input must be a string for Text input type."
|
45 |
+
elif input_type == "Token IDs":
|
46 |
+
try:
|
47 |
+
token_ids = ast.literal_eval(input_value)
|
48 |
+
if not isinstance(token_ids, list) or not all(isinstance(id, int) for id in token_ids):
|
49 |
+
return False, "Token IDs must be a list of integers."
|
50 |
+
except (ValueError, SyntaxError):
|
51 |
+
return False, "Invalid Token IDs format. Please provide a valid list of integers."
|
52 |
+
return True, ""
|
53 |
+
|
54 |
+
|
55 |
def process_text(text: str, model_name: str, api_key: str = None):
|
56 |
if model_name in ["Meta-Llama-3.1-8B", "gemma-2b"]:
|
57 |
tokenizer = AutoTokenizer.from_pretrained(model_path + model_name)
|
|
|
116 |
return html_output
|
117 |
|
118 |
def process_input(input_type, input_value, model_name, api_key):
|
119 |
+
is_valid, error_message = validate_input(input_type, input_value)
|
120 |
+
if not is_valid:
|
121 |
+
raise gr.Error(error_message)
|
122 |
if input_type == "Text":
|
123 |
text, tokens, token_ids = process_text(text=input_value, model_name=model_name, api_key=api_key)
|
124 |
elif input_type == "Token IDs":
|
|
|
167 |
input_type = gr.Radio(["Text", "Token IDs"], label="Input Type", value="Text")
|
168 |
model_name = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[0])
|
169 |
|
170 |
+
# api_key = gr.Textbox(label="API Key Claude models)", type="password")
|
171 |
input_text = gr.Textbox(lines=5, label="Input")
|
172 |
|
173 |
with gr.Row():
|
|
|
198 |
|
199 |
submit_button.click(
|
200 |
process_input,
|
201 |
+
inputs=[input_type, input_text, model_name],
|
202 |
outputs=[analysis_output, text_output, tokens_output, token_ids_output, words_plot, special_chars_plot, numbers_plot]
|
203 |
)
|
204 |
|