Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
def check_key_gemini_availability(key):
|
| 5 |
+
"""
|
| 6 |
+
Checks the availability of a Gemini API key.
|
| 7 |
+
|
| 8 |
+
Args:
|
| 9 |
+
key: The API key to check.
|
| 10 |
+
|
| 11 |
+
Returns:
|
| 12 |
+
A tuple containing a boolean indicating whether the key is valid and an error message (or None if the key is valid).
|
| 13 |
+
"""
|
| 14 |
+
try:
|
| 15 |
+
url_getListModel = f"https://generativelanguage.googleapis.com/v1beta/models?key={key}"
|
| 16 |
+
rq = requests.get(url_getListModel)
|
| 17 |
+
rq.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
|
| 18 |
+
result = rq.json()
|
| 19 |
+
if 'models' in result:
|
| 20 |
+
return True, None
|
| 21 |
+
else:
|
| 22 |
+
return False, "Invalid key: 'models' field not found in response."
|
| 23 |
+
except requests.exceptions.HTTPError as e:
|
| 24 |
+
return False, f"Invalid key: HTTP error - {e.response.status_code}"
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return False, f"Error: {e}"
|
| 27 |
+
|
| 28 |
+
def check_keys(keys_text):
|
| 29 |
+
"""
|
| 30 |
+
Checks a list of Gemini API keys and categorizes them into valid and invalid.
|
| 31 |
+
|
| 32 |
+
Args:
|
| 33 |
+
keys_text: A string containing API keys separated by lines.
|
| 34 |
+
|
| 35 |
+
Returns:
|
| 36 |
+
A tuple containing two strings: a list of valid keys and a list of invalid keys with error messages.
|
| 37 |
+
"""
|
| 38 |
+
key_list = keys_text.strip().splitlines()
|
| 39 |
+
key_list = list(set(key_list)) # Remove duplicates
|
| 40 |
+
|
| 41 |
+
valid_keys = []
|
| 42 |
+
invalid_keys = []
|
| 43 |
+
|
| 44 |
+
for key in key_list:
|
| 45 |
+
is_valid, error_msg = check_key_gemini_availability(key)
|
| 46 |
+
if is_valid:
|
| 47 |
+
valid_keys.append(key)
|
| 48 |
+
else:
|
| 49 |
+
invalid_keys.append(f"{key} - {error_msg}")
|
| 50 |
+
|
| 51 |
+
return "\n".join(valid_keys), "\n".join(invalid_keys)
|
| 52 |
+
|
| 53 |
+
def clear_inputs():
|
| 54 |
+
"""Clears the input text area."""
|
| 55 |
+
return ""
|
| 56 |
+
|
| 57 |
+
with gr.Blocks() as demo:
|
| 58 |
+
gr.Markdown('''
|
| 59 |
+
# Gemini API Key Status Checker - batch mode
|
| 60 |
+
''')
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column():
|
| 64 |
+
keys = gr.TextArea(label="API Keys (by lines)")
|
| 65 |
+
with gr.Row():
|
| 66 |
+
clear_button = gr.Button("Clear")
|
| 67 |
+
submit_button = gr.Button("Submit", variant="primary")
|
| 68 |
+
with gr.Column():
|
| 69 |
+
infos = gr.TextArea(label="Alive Keys")
|
| 70 |
+
errs = gr.TextArea(label="Invalid Keys-status code")
|
| 71 |
+
|
| 72 |
+
clear_button.click(fn=clear_inputs, outputs=[keys])
|
| 73 |
+
submit_button.click(fn=check_keys, inputs=[keys], outputs=[infos, errs], api_name="check_keys")
|
| 74 |
+
|
| 75 |
+
demo.launch()
|