Spaces:
Sleeping
Sleeping
File size: 1,768 Bytes
3bc0305 3dc2123 94adeb8 3dc2123 3bc0305 3dc2123 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import gradio as gr
from transformers import pipeline
# ID de tu modelo de an谩lisis de sentimientos que ya subiste
MODEL_ID = "Light-Dav/sentiment-analysis-full-project"
# Cargar el pipeline del modelo
try:
classifier = pipeline("sentiment-analysis", model=MODEL_ID)
except Exception as e:
print(f"Error al cargar el modelo {MODEL_ID}: {e}")
classifier = None # Para evitar errores si classifier no se inicializa
def analyze_sentiment(text):
if not text:
return {"Positive": 0.0, "Negative": 0.0, "Neutral": 0.0}
if classifier is None:
return {"Error": "Modelo no cargado. Contactar al administrador."}
# Realizar la inferencia
# pipeline devuelve una lista de diccionarios, tomamos el primero
results = classifier(text)[0]
# Formatear el resultado para Gradio (diccionario de etiqueta a score)
formatted_results = {}
for item in results:
formatted_results[item['label']] = item['score']
return formatted_results
# Crear la interfaz de Gradio
iface = gr.Interface(
fn=analyze_sentiment,
inputs=gr.Textbox(lines=3, placeholder="Escribe tu texto aqu铆..."),
outputs=gr.Label(num_top_classes=3), # Mostrar las 3 clases principales (Positivo, Negativo, Neutro)
title="馃 An谩lisis de Sentimientos en Espa帽ol",
description="Introduce un texto en espa帽ol y el modelo predecir谩 su sentimiento (Positivo, Negativo, Neutro).",
examples=[
["Me encant贸 este libro, es fascinante y lo recomiendo totalmente."],
["El servicio fue terrible, muy lento y poco amable."],
["La reuni贸n se program贸 para el jueves a las 10 AM."]
]
)
# Iniciar la aplicaci贸n Gradio (esto se hace autom谩ticamente en Hugging Face Spaces)
iface.launch(share=False) |