Spaces:
No application file
No application file
Commit
·
3736fe7
1
Parent(s):
5774bbe
app4.py
CHANGED
|
@@ -1,39 +1,45 @@
|
|
| 1 |
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
-
# Cargar variables
|
| 7 |
load_dotenv()
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
if TOKEN is None:
|
| 11 |
raise ValueError("❌ No se encontró la variable HF_TOKEN en el .env")
|
| 12 |
|
| 13 |
-
# Inicializar cliente de inferencia
|
| 14 |
-
client = InferenceClient(provider="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
def generar_audio(texto):
|
| 17 |
-
if not texto.strip():
|
| 18 |
-
return None
|
| 19 |
# Llamada a la API
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
# Interfaz Gradio
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
-
gr.Markdown("#
|
| 31 |
-
gr.Markdown("
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
boton = gr.Button("
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
boton.click(fn=
|
| 38 |
|
| 39 |
demo.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import gradio as gr
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
+
# Cargar variables del .env
|
| 7 |
load_dotenv()
|
| 8 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
|
| 10 |
+
if HF_TOKEN is None:
|
|
|
|
| 11 |
raise ValueError("❌ No se encontró la variable HF_TOKEN en el .env")
|
| 12 |
|
| 13 |
+
# Inicializar cliente de inferencia HF
|
| 14 |
+
client = InferenceClient(provider="hf-inference", api_key=HF_TOKEN)
|
| 15 |
+
|
| 16 |
+
def clasificar_imagen(imagen):
|
| 17 |
+
if imagen is None:
|
| 18 |
+
return "Sube una imagen para clasificar."
|
| 19 |
+
|
| 20 |
+
# Guardar temporalmente la imagen para enviarla al cliente
|
| 21 |
+
temp_path = "temp_image.jpg"
|
| 22 |
+
imagen.save(temp_path)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
# Llamada a la API
|
| 25 |
+
output = client.image_classification(temp_path, model="google/vit-base-patch16-224")
|
| 26 |
+
|
| 27 |
+
# Retornar la clasificación más probable
|
| 28 |
+
if isinstance(output, list) and len(output) > 0:
|
| 29 |
+
resultado = "\n".join([f"{item['label']}: {item['score']:.2f}" for item in output])
|
| 30 |
+
else:
|
| 31 |
+
resultado = "No se pudo clasificar la imagen."
|
| 32 |
+
return resultado
|
| 33 |
|
| 34 |
# Interfaz Gradio
|
| 35 |
with gr.Blocks() as demo:
|
| 36 |
+
gr.Markdown("# 🖼️ Clasificador de Imágenes con ViT")
|
| 37 |
+
gr.Markdown("Sube una imagen y el modelo ViT-base de Google la clasificará usando HF Inference API gratuita.")
|
| 38 |
+
|
| 39 |
+
img_input = gr.Image(type="pil", label="Sube tu imagen aquí")
|
| 40 |
+
boton = gr.Button("Clasificar Imagen")
|
| 41 |
+
salida = gr.Textbox(label="Resultados")
|
| 42 |
+
|
| 43 |
+
boton.click(fn=clasificar_imagen, inputs=img_input, outputs=salida)
|
| 44 |
|
| 45 |
demo.launch()
|