Skip to content

Commit

Permalink
added hf api token validation
Browse files Browse the repository at this point in the history
  • Loading branch information
manufy committed May 20, 2024
1 parent bc298a6 commit dc46752
Showing 1 changed file with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import gradio as gr
from PIL import Image, ImageDraw, ImageFont
import requests

# imprime un texto en el centro de una imagen en negro por defecto

def print_text_on_image_centered(image, text, color="black"):
# Crea un objeto Draw para la imagen
draw = ImageDraw.Draw(image)
# Define el texto y la fuente
text = "HuggingFace API Token invalid. Please enter a valid token."


# Define el tamaño inicial de la fuente
font_size = 30
font = ImageFont.load_default().font_variant(size=font_size)
Expand Down Expand Up @@ -37,18 +38,49 @@ def print_text_on_image_centered(image, text, color="black"):
def create_background_image(width, height, color="white"):
return Image.new("RGB", (width, height), color)

def segment_gradio_image(api_token, image):



# Valida el token de la API de Hugging Face contra su API whoami-v2

def hf_validate_api_token(api_token):

# Define la URL de la API
url = "https://huggingface.co/api/whoami-v2"

# Define los encabezados de la solicitud
headers = {
"Authorization": f"Bearer {api_token}"
}

# Realiza la solicitud a la API
response = requests.get(url, headers=headers)
print(response.content)

# Si la respuesta tiene un código de estado 200, el token es válido
if response.status_code == 200:
return True, response.json()['fullname']
else:
return False, response.json()['error']

def segment_gradio_image(api_token, model, image):
print("api_token: " + api_token)
is_token_valid, result = hf_validate_api_token(api_token)
print("is_token_valid: " + str(is_token_valid))
print("result: " + str(result))
if is_token_valid == False:
text_image = print_text_on_image_centered(create_background_image(500, 500, "white"), 'HuggingFace API Token invalid. Please enter a valid token.', 'red')
else:
text_image = print_text_on_image_centered(create_background_image(500, 500, "white"), 'PROCESANDO', 'blue')

#response = segment_image(image)
# Crea una imagen en blanco de 500x500 píxeles


text_image = print_text_on_image_centered(create_background_image(500, 500, "white"), 'HuggingFace API Token invalid. Please enter a valid token.', 'red')



return text_image


# Create the Gradio interface
interface = gr.Interface(
fn=segment_gradio_image,
Expand All @@ -57,13 +89,23 @@ def segment_gradio_image(api_token, image):
label="API Token",
placeholder="Enter your Hugging Face API token here"

),
gr.Textbox(
label="AI Segmentation model",
placeholder="Enter your Segmentation model here",
value="facebook/mask2former-swin-tiny-coco-panoptic"

),
"image"
],
outputs="image",
live=True,
title="Segment Image",
description="Upload an image and let the model segment it."
description="Upload an image and let the model segment it.",
allow_flagging=False,
examples=[
["", "https://api-inference.huggingface.co/models/facebook/mask2former-swin-tiny-coco-panoptic"]
]
)

# Launch the interface
Expand Down

0 comments on commit dc46752

Please sign in to comment.