Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +32 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import TFAutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
|
| 5 |
+
# Cargar el modelo y tokenizer desde Hugging Face
|
| 6 |
+
model_name = "albabernal03/mi-modelo-sentimiento"
|
| 7 |
+
model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Funci贸n para predecir el sentimiento
|
| 11 |
+
def predict_sentiment(text):
|
| 12 |
+
inputs = tokenizer(text, return_tensors="tf", truncation=True, padding=True)
|
| 13 |
+
outputs = model(**inputs)
|
| 14 |
+
logits = outputs.logits
|
| 15 |
+
probabilities = tf.nn.softmax(logits, axis=-1).numpy()
|
| 16 |
+
labels = ["Negative", "Neutral", "Positive"]
|
| 17 |
+
predicted_label = labels[probabilities.argmax()]
|
| 18 |
+
confidence = probabilities.max()
|
| 19 |
+
return f"{predicted_label} ({confidence:.2f})"
|
| 20 |
+
|
| 21 |
+
# Crear la interfaz con Gradio
|
| 22 |
+
demo = gr.Interface(
|
| 23 |
+
fn=predict_sentiment,
|
| 24 |
+
inputs=gr.Textbox(lines=2, placeholder="Escribe un texto..."),
|
| 25 |
+
outputs="text",
|
| 26 |
+
title="Sentiment Analysis Model",
|
| 27 |
+
description="Este modelo predice el sentimiento de un texto como Negativo, Neutral o Positivo."
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Ejecutar la demo
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
tensorflow
|
| 3 |
+
gradio
|