|
|
import gradio as gr |
|
|
from groq import Groq |
|
|
import openai |
|
|
import requests |
|
|
import json |
|
|
from google import genai |
|
|
from google.genai import types |
|
|
from googleapiclient.discovery import build |
|
|
|
|
|
|
|
|
def verificar_api_groq(api_key): |
|
|
try: |
|
|
client = Groq(api_key=api_key) |
|
|
completion = client.chat.completions.create( |
|
|
model="openai/gpt-oss-20b", |
|
|
messages=[{"role": "user", "content": "hola"}], |
|
|
temperature=1, |
|
|
max_completion_tokens=50, |
|
|
top_p=1, |
|
|
reasoning_effort="medium", |
|
|
stream=False, |
|
|
stop=None |
|
|
) |
|
|
|
|
|
|
|
|
if completion.choices and completion.choices[0].message.content: |
|
|
return True |
|
|
return False |
|
|
except Exception as e: |
|
|
print("Error en Groq:", e) |
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def verificar_api_gemini(api_key): |
|
|
try: |
|
|
client = genai.Client(api_key=api_key) |
|
|
model = "gemini-2.5-pro" |
|
|
|
|
|
contents = [ |
|
|
types.Content( |
|
|
role="user", |
|
|
parts=[types.Part.from_text(text="Hola")] |
|
|
) |
|
|
] |
|
|
|
|
|
generate_content_config = types.GenerateContentConfig( |
|
|
thinking_config=types.ThinkingConfig(thinking_budget=-1) |
|
|
) |
|
|
|
|
|
|
|
|
response = client.models.generate_content( |
|
|
model=model, |
|
|
contents=contents, |
|
|
config=generate_content_config |
|
|
) |
|
|
|
|
|
|
|
|
return bool(response.candidates and response.candidates[0].content.parts[0].text.strip()) |
|
|
except Exception as e: |
|
|
print("Error en Gemini:", e) |
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def verificar_api_openai(api_key): |
|
|
openai.api_key = api_key |
|
|
try: |
|
|
response = openai.ChatCompletion.create( |
|
|
model="gpt-3.5-turbo", |
|
|
messages=[{"role": "user", "content": "Hola"}], |
|
|
max_tokens=50, |
|
|
temperature=0.7 |
|
|
) |
|
|
return True |
|
|
except Exception: |
|
|
return False |
|
|
|
|
|
|
|
|
def verificar_api_openrouter(api_key): |
|
|
url = "https://openrouter.ai/api/v1/chat/completions" |
|
|
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"} |
|
|
data = { |
|
|
"model": "google/gemma-2-9b-it:free", |
|
|
"messages": [{"role": "user", "content": "What is the meaning of life?"}] |
|
|
} |
|
|
|
|
|
try: |
|
|
r = requests.post(url, headers=headers, data=json.dumps(data)) |
|
|
return r.status_code == 200 |
|
|
except Exception: |
|
|
return False |
|
|
|
|
|
|
|
|
def verificar_api_youtube(api_key): |
|
|
try: |
|
|
youtube = build("youtube", "v3", developerKey=api_key) |
|
|
resp = youtube.search().list(q="test", part="snippet", maxResults=1).execute() |
|
|
return bool(resp.get("items", [])) |
|
|
except Exception: |
|
|
return False |
|
|
|
|
|
|
|
|
def verificar_api_pexels(api_key): |
|
|
try: |
|
|
url = "https://api.pexels.com/v1/search" |
|
|
headers = {"Authorization": api_key} |
|
|
params = {"query": "nature", "per_page": 1} |
|
|
r = requests.get(url, headers=headers, params=params) |
|
|
if r.status_code == 200: |
|
|
d = r.json() |
|
|
return "photos" in d and len(d["photos"]) > 0 |
|
|
return False |
|
|
except Exception: |
|
|
return False |
|
|
|
|
|
|
|
|
def verificar_api_google_places(api_key): |
|
|
try: |
|
|
url = "https://maps.googleapis.com/maps/api/place/textsearch/json" |
|
|
params = {"query": "restaurants", "location": "0,0", "radius": 1000, "key": api_key} |
|
|
r = requests.get(url, params=params) |
|
|
if r.status_code == 200: |
|
|
d = r.json() |
|
|
return "results" in d and len(d["results"]) > 0 |
|
|
return False |
|
|
except Exception: |
|
|
return False |
|
|
|
|
|
|
|
|
def verificar_api_unsplash(api_key): |
|
|
try: |
|
|
url = "https://api.unsplash.com/search/photos" |
|
|
headers = {"Authorization": f"Client-ID {api_key}"} |
|
|
params = {"query": "nature", "per_page": 1} |
|
|
r = requests.get(url, headers=headers, params=params) |
|
|
if r.status_code == 200: |
|
|
d = r.json() |
|
|
return "results" in d and len(d["results"]) > 0 |
|
|
return False |
|
|
except Exception: |
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def verificar_apis(api_keys_text, api_type): |
|
|
api_keys = api_keys_text.strip().split("\n") |
|
|
apis_validas = [] |
|
|
apis_invalidas = [] |
|
|
|
|
|
for api_key in api_keys: |
|
|
api_key = api_key.strip() |
|
|
if not api_key: |
|
|
continue |
|
|
|
|
|
if api_type == "Groq": |
|
|
es_valida = verificar_api_groq(api_key) |
|
|
elif api_type == "OpenAI": |
|
|
es_valida = verificar_api_openai(api_key) |
|
|
elif api_type == "Google Gemini": |
|
|
es_valida = verificar_api_gemini(api_key) |
|
|
elif api_type == "OpenRouter": |
|
|
es_valida = verificar_api_openrouter(api_key) |
|
|
elif api_type == "YouTube": |
|
|
es_valida = verificar_api_youtube(api_key) |
|
|
elif api_type == "Pexels": |
|
|
es_valida = verificar_api_pexels(api_key) |
|
|
elif api_type == "Google Places": |
|
|
es_valida = verificar_api_google_places(api_key) |
|
|
elif api_type == "Unsplash": |
|
|
es_valida = verificar_api_unsplash(api_key) |
|
|
else: |
|
|
continue |
|
|
|
|
|
if es_valida: |
|
|
apis_validas.append(api_key) |
|
|
else: |
|
|
apis_invalidas.append(api_key) |
|
|
|
|
|
resultado_final = ( |
|
|
"✅ APIs válidas:\n" + "\n".join(apis_validas) + |
|
|
"\n\n⛔ APIs inválidas:\n" + "\n".join(apis_invalidas) |
|
|
) |
|
|
return resultado_final |
|
|
|
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=verificar_apis, |
|
|
inputs=[ |
|
|
gr.Textbox(label="API Keys (una por línea)", lines=10, placeholder="Introduce cada API key..."), |
|
|
gr.Radio(["Groq", "OpenAI", "Google Gemini", "OpenRouter", "YouTube", "Pexels", "Google Places", "Unsplash"], label="Tipo de API") |
|
|
], |
|
|
outputs="text", |
|
|
title="Verificación de APIs (Groq, OpenAI, Gemini, etc.)", |
|
|
description="Comprueba si tus API keys siguen activas y funcionando." |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
iface.launch() |
|
|
|