Spaces:
Sleeping
Sleeping
Daniel Nichols
commited on
Commit
•
a36b415
1
Parent(s):
17b14ea
refactor error handling code
Browse files- src/perfguru.py +15 -20
src/perfguru.py
CHANGED
@@ -27,16 +27,24 @@ def token_limit_getter(model: str) -> int:
|
|
27 |
return token_limits[model]
|
28 |
return int(5e6)
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
def chat_with_llms(prompt, code_files, profile_file, profile_type):
|
32 |
model1 = select_random_model()
|
33 |
model2 = select_random_model()
|
34 |
formatter1 = select_random_formatter()
|
35 |
formatter2 = select_random_formatter()
|
36 |
-
encoder1 = None
|
37 |
-
encoder2 = None
|
38 |
-
num_tokens_1 = 0
|
39 |
-
num_tokens_2 = 0
|
40 |
|
41 |
print(f"Selected models: {model1.name} and {model2.name}")
|
42 |
|
@@ -45,22 +53,9 @@ def chat_with_llms(prompt, code_files, profile_file, profile_type):
|
|
45 |
|
46 |
if formatted1 is None or formatted2 is None:
|
47 |
error_helper("Failed to format prompt. Please try again.")
|
48 |
-
|
49 |
-
if model1.name[:3] == "gpt":
|
50 |
-
encoder1 = tiktoken.encoding_for_model(model1.name)
|
51 |
-
if model2.name[:3] == "gpt":
|
52 |
-
encoder2 = tiktoken.encoding_for_model(model2.name)
|
53 |
-
|
54 |
-
if encoder1:
|
55 |
-
num_tokens_1 = len(encoder1.encode(formatted1))
|
56 |
-
if encoder2:
|
57 |
-
num_tokens_2 = len(encoder2.encode(formatted2))
|
58 |
-
|
59 |
-
token_limit_1 = token_limit_getter(model1.name)
|
60 |
-
token_limit_2 = token_limit_getter(model2.name)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
|
65 |
response1 = model1.get_response(formatted1)
|
66 |
response2 = model2.get_response(formatted2)
|
@@ -190,4 +185,4 @@ with gr.Blocks(css=".not-voted p { color: black; } .voted p { color: green; } .r
|
|
190 |
|
191 |
# Launch the Gradio interface
|
192 |
if __name__ == '__main__':
|
193 |
-
interface.launch(share=
|
|
|
27 |
return token_limits[model]
|
28 |
return int(5e6)
|
29 |
|
30 |
+
def check_length(text, model):
|
31 |
+
if model.name.startswith("gpt"):
|
32 |
+
encoder = lambda s: len(tiktoken.encoding_for_model(model.name).encode(text))
|
33 |
+
else:
|
34 |
+
encoder = lambda s: len(s)/4 # 4 char per token heuristic
|
35 |
+
|
36 |
+
token_length = encoder(text)
|
37 |
+
token_limit = token_limit_getter(model.name)
|
38 |
+
|
39 |
+
if token_length >= token_limit:
|
40 |
+
error_helper(f"Prompt is too long. Please try reducing the size of the prompt or code uploaded.")
|
41 |
+
|
42 |
|
43 |
def chat_with_llms(prompt, code_files, profile_file, profile_type):
|
44 |
model1 = select_random_model()
|
45 |
model2 = select_random_model()
|
46 |
formatter1 = select_random_formatter()
|
47 |
formatter2 = select_random_formatter()
|
|
|
|
|
|
|
|
|
48 |
|
49 |
print(f"Selected models: {model1.name} and {model2.name}")
|
50 |
|
|
|
53 |
|
54 |
if formatted1 is None or formatted2 is None:
|
55 |
error_helper("Failed to format prompt. Please try again.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
check_length(formatted1, model1)
|
58 |
+
check_length(formatted2, model2)
|
59 |
|
60 |
response1 = model1.get_response(formatted1)
|
61 |
response2 = model2.get_response(formatted2)
|
|
|
185 |
|
186 |
# Launch the Gradio interface
|
187 |
if __name__ == '__main__':
|
188 |
+
interface.launch(share=False)
|