superBleh shaocongma commited on
Commit
98c28c1
0 Parent(s):

Duplicate from shaocongma/openai_api_key_status

Browse files

Co-authored-by: Shaocong Ma <shaocongma@users.noreply.huggingface.co>

Files changed (6) hide show
  1. .gitattributes +34 -0
  2. LICENSE +9 -0
  3. README.md +52 -0
  4. api_usage.py +68 -0
  5. app.py +46 -0
  6. requirements.txt +0 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
LICENSE ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 CCCBora
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
README.md ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Openai Api Key Status
3
+ colorFrom: gray
4
+ colorTo: green
5
+ sdk: gradio
6
+ sdk_version: 3.26.0
7
+ app_file: app.py
8
+ pinned: false
9
+ license: mit
10
+ python_version: 3.10.10
11
+ duplicated_from: shaocongma/openai_api_key_status
12
+ ---
13
+
14
+ # OpenAI API Key Status Checker
15
+
16
+ This web app allows you to input your OpenAI API key and get information about your account, GPT-4 availability, API usage, and other related information.
17
+
18
+ ## Usage - Huggingface Spaces
19
+ 1. Go to [OpenAI API Key Status Checker](https://huggingface.co/spaces/shaocongma/openai_api_key_status).
20
+ 2. Enter your OpenAI API key in the provided textbox.
21
+ 3. Click the 'Submit' button to display the information associated with your API key.
22
+
23
+ ## Usage - API
24
+ 1. Install `gradio_client`.
25
+ ```angular2html
26
+ pip install gradio_client
27
+ ```
28
+ 2. Connect the client and call the API.
29
+ ```python
30
+ from gradio_client import Client
31
+
32
+ client = Client("https://shaocongma-openai-api-key-status.hf.space/")
33
+ json_file_path = client.predict("sk-......",
34
+ api_name="/get_key_info")
35
+ ```
36
+ 3. Read the output JSON file.
37
+ ```python
38
+ with open(json_file_path, "r") as f:
39
+ result = f.read()
40
+ print(result)
41
+ ```
42
+ 4. Sample output:
43
+ ```python
44
+ # result - valid key
45
+ {"account_name": "Peter Parker", "key_availability": true, "gpt4_availability": true, "has_payment_method": true, "used": 10.33174, "limit": 120.0}
46
+ # result - invalide key
47
+ {"account_name": "", "key_availability": false, "gpt4_availability": "", "has_payment_method": "", "used": "", "limit": ""}
48
+ ```
49
+
50
+ ## License
51
+
52
+ This project is released under the MIT License. Please see the LICENSE file for more information.
api_usage.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import os
3
+ from datetime import datetime
4
+ from dateutil.relativedelta import relativedelta
5
+ import openai
6
+
7
+
8
+ def get_headers(key):
9
+ headers = {'Authorization': f'Bearer {key}'}
10
+ return headers
11
+
12
+ def get_subscription(key):
13
+ queryUrl = 'https://api.openai.com/dashboard/billing/subscription'
14
+ headers = get_headers(key)
15
+ r = requests.get(queryUrl, headers=headers)
16
+ results = r.json()
17
+ if check_key_availability():
18
+ has_payment_method = results["has_payment_method"]
19
+ # hard_limit = results["hard_limit"]
20
+ hard_limit_usd = results["hard_limit_usd"]
21
+ plan = results["plan"]["title"]
22
+ account_name = results["account_name"]
23
+ return {"account_name": account_name,
24
+ "has_payment_method": has_payment_method,
25
+ "hard_limit_usd": hard_limit_usd,
26
+ "plan": plan}
27
+ else:
28
+ return {"account_name": "",
29
+ "has_payment_method": False,
30
+ "hard_limit_usd": "",
31
+ "plan": ""}
32
+
33
+ def get_usage(key):
34
+ if check_key_availability():
35
+ start_date = datetime.now().strftime('%Y-%m-01')
36
+ end_date = (datetime.now() + relativedelta(months=1)).strftime('%Y-%m-01')
37
+ queryUrl = f'https://api.openai.com/dashboard/billing/usage?start_date={start_date}&end_date={end_date}'
38
+ headers = get_headers(key)
39
+ r = requests.get(queryUrl, headers=headers)
40
+ return r.json()['total_usage']/100.0
41
+ else:
42
+ return ""
43
+
44
+ def check_gpt4_availability():
45
+ if check_key_availability():
46
+ available_models = [model["root"] for model in openai.Model.list()["data"]]
47
+ if 'gpt-4' in available_models:
48
+ return True
49
+ else:
50
+ return False
51
+ else:
52
+ return False
53
+
54
+ def check_key_availability():
55
+ try:
56
+ openai.Model.list()
57
+ return True
58
+ except:
59
+ return False
60
+
61
+ if __name__ == "__main__":
62
+ key = os.getenv("OPENAI_API_KEY")
63
+ results = get_usage(key)
64
+ print(results)
65
+
66
+ results = get_subscription(key)
67
+ for k, v in results.items():
68
+ print(f"{k}: {v}")
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ from api_usage import check_gpt4_availability, get_subscription, get_usage, check_key_availability
4
+
5
+ def get_key_info(key):
6
+ # Return a dictionary containing key information
7
+ openai.api_key = key
8
+ key_avai = check_key_availability()
9
+ info_dict = {"account_name": "",
10
+ "key_availability": key_avai,
11
+ "gpt4_availability": "",
12
+ "has_payment_method": "",
13
+ "used": "",
14
+ "limit": ""}
15
+ if key_avai:
16
+ info = get_subscription(key)
17
+ used = get_usage(key)
18
+ gpt4_avai = check_gpt4_availability()
19
+ info_dict["account_name"] = info["account_name"]
20
+ info_dict["gpt4_availability"] = gpt4_avai
21
+ info_dict["has_payment_method"] = info["has_payment_method"]
22
+ info_dict["used"] = used
23
+ info_dict["limit"] = info["hard_limit_usd"]
24
+ return info_dict
25
+
26
+
27
+ def clear_inputs(text):
28
+ return ""
29
+
30
+ with gr.Blocks() as demo:
31
+ gr.Markdown('''
32
+ # OpenAI API Key Status Checker
33
+ ''')
34
+
35
+ with gr.Row():
36
+ with gr.Column():
37
+ key = gr.Textbox(lines=1, max_lines=1, label="OpenAI API Key")
38
+ with gr.Row():
39
+ clear_button = gr.Button("Clear")
40
+ submit_button = gr.Button("Submit", variant="primary")
41
+ with gr.Column():
42
+ info = gr.JSON(label="OpenAI API Key Information")
43
+
44
+ clear_button.click(fn=clear_inputs, inputs=[key], outputs=[key])
45
+ submit_button.click(fn=get_key_info, inputs=[key], outputs=[info], api_name="get_key_info")
46
+ demo.launch()
requirements.txt ADDED
Binary file (2.28 kB). View file