Spaces:
Sleeping
Sleeping
Commit
·
7cac8a2
1
Parent(s):
50389ad
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,6 @@ RateLimitPerModel = {
|
|
| 12 |
"gpt-4-32k": 1000
|
| 13 |
}
|
| 14 |
|
| 15 |
-
|
| 16 |
def get_available_models(api_key):
|
| 17 |
headers = {"Authorization": f"Bearer {api_key}"}
|
| 18 |
url = f"{BaseApi}/engines"
|
|
@@ -44,7 +43,7 @@ def check_key_status(api_key, model):
|
|
| 44 |
|
| 45 |
error_type = response_data.get("error", {}).get("type", "")
|
| 46 |
if error_type == "insufficient_quota" and model in {"gpt-4", "gpt-4-32k"}:
|
| 47 |
-
return f"Error: The key for {model} is out of quota but has
|
| 48 |
elif error_type in ["insufficient_quota", "billing_not_active", "access_terminated"]:
|
| 49 |
return f"Error: The key for {model} is either out of quota, inactive, or access is terminated."
|
| 50 |
|
|
@@ -52,7 +51,7 @@ def check_key_status(api_key, model):
|
|
| 52 |
if (response.status_code == 400 and error_type == "invalid_request_error") or ratelimited:
|
| 53 |
ratelimit = response.headers.get("x-ratelimit-limit-requests", "0")
|
| 54 |
org = response.headers.get("openai-organization", "user-xyz")
|
| 55 |
-
is_trial_key = "Trial Key" if int(ratelimit) < RateLimitPerModel.get(model, 0) else "Paid
|
| 56 |
return f"Key for {model} is working. Ratelimit: {ratelimit}, Organization: {org}, Key Type: {is_trial_key}"
|
| 57 |
|
| 58 |
except HTTPError as http_err:
|
|
@@ -60,7 +59,7 @@ def check_key_status(api_key, model):
|
|
| 60 |
except Exception as e:
|
| 61 |
return f"Error occurred: {e}"
|
| 62 |
|
| 63 |
-
def
|
| 64 |
available_models = get_available_models(api_key)
|
| 65 |
if not available_models:
|
| 66 |
return "Error occurred: Unable to retrieve available models. Please check your API key."
|
|
@@ -68,14 +67,18 @@ def check_model(api_key):
|
|
| 68 |
model_info = "\n".join([check_key_status(api_key, model) for model in available_models])
|
| 69 |
return model_info
|
| 70 |
|
| 71 |
-
# Define Gradio interface
|
|
|
|
|
|
|
|
|
|
| 72 |
iface = gr.Interface(
|
| 73 |
-
fn=
|
| 74 |
inputs=gr.inputs.Textbox(placeholder="Enter your OpenAI API key", type="text"),
|
| 75 |
outputs=gr.outputs.Textbox(),
|
| 76 |
live=True,
|
| 77 |
title="OKC",
|
| 78 |
-
description="Enter your OpenAI API key to
|
|
|
|
| 79 |
)
|
| 80 |
|
| 81 |
iface.launch()
|
|
|
|
| 12 |
"gpt-4-32k": 1000
|
| 13 |
}
|
| 14 |
|
|
|
|
| 15 |
def get_available_models(api_key):
|
| 16 |
headers = {"Authorization": f"Bearer {api_key}"}
|
| 17 |
url = f"{BaseApi}/engines"
|
|
|
|
| 43 |
|
| 44 |
error_type = response_data.get("error", {}).get("type", "")
|
| 45 |
if error_type == "insufficient_quota" and model in {"gpt-4", "gpt-4-32k"}:
|
| 46 |
+
return f"Error: The key for {model} is out of quota, but has gpt4"
|
| 47 |
elif error_type in ["insufficient_quota", "billing_not_active", "access_terminated"]:
|
| 48 |
return f"Error: The key for {model} is either out of quota, inactive, or access is terminated."
|
| 49 |
|
|
|
|
| 51 |
if (response.status_code == 400 and error_type == "invalid_request_error") or ratelimited:
|
| 52 |
ratelimit = response.headers.get("x-ratelimit-limit-requests", "0")
|
| 53 |
org = response.headers.get("openai-organization", "user-xyz")
|
| 54 |
+
is_trial_key = "Trial Key" if int(ratelimit) < RateLimitPerModel.get(model, 0) else "Paid Key"
|
| 55 |
return f"Key for {model} is working. Ratelimit: {ratelimit}, Organization: {org}, Key Type: {is_trial_key}"
|
| 56 |
|
| 57 |
except HTTPError as http_err:
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
return f"Error occurred: {e}"
|
| 61 |
|
| 62 |
+
def check_models(api_key):
|
| 63 |
available_models = get_available_models(api_key)
|
| 64 |
if not available_models:
|
| 65 |
return "Error occurred: Unable to retrieve available models. Please check your API key."
|
|
|
|
| 67 |
model_info = "\n".join([check_key_status(api_key, model) for model in available_models])
|
| 68 |
return model_info
|
| 69 |
|
| 70 |
+
# Define Gradio interface with a button to trigger model checking
|
| 71 |
+
def trigger_model_check(api_key):
|
| 72 |
+
return check_models(api_key)
|
| 73 |
+
|
| 74 |
iface = gr.Interface(
|
| 75 |
+
fn=trigger_model_check,
|
| 76 |
inputs=gr.inputs.Textbox(placeholder="Enter your OpenAI API key", type="text"),
|
| 77 |
outputs=gr.outputs.Textbox(),
|
| 78 |
live=True,
|
| 79 |
title="OKC",
|
| 80 |
+
description="Enter your OpenAI API key and click 'Check Models' to see the available models and key status.",
|
| 81 |
+
allow_flagging=False, # Disable flagging to prevent unnecessary reporting
|
| 82 |
)
|
| 83 |
|
| 84 |
iface.launch()
|