Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,7 +58,7 @@ def obtener_predicciones(texto, nombre_modelo, top_k=10):
|
|
| 58 |
if current_model_name != nombre_modelo:
|
| 59 |
cargar_modelo(nombre_modelo)
|
| 60 |
|
| 61 |
-
entradas = current_tokenizer(texto, return_tensors="pt").to(device)
|
| 62 |
|
| 63 |
with torch.no_grad():
|
| 64 |
salidas = current_model(**entradas)
|
|
@@ -66,20 +66,19 @@ def obtener_predicciones(texto, nombre_modelo, top_k=10):
|
|
| 66 |
probabilidades = torch.nn.functional.softmax(logits, dim=-1)
|
| 67 |
|
| 68 |
top_k_prob, top_k_indices = torch.topk(probabilidades, k=top_k)
|
| 69 |
-
top_k_tokens = [current_tokenizer.decode([idx.item()]) for idx in top_k_indices]
|
| 70 |
|
| 71 |
return top_k_tokens, top_k_prob.cpu().tolist()
|
| 72 |
|
| 73 |
def generar_barplot(tokens, probabilidades):
|
| 74 |
"""Convierte los datos en un DataFrame para Gradio BarPlot."""
|
| 75 |
df = pd.DataFrame({"Palabra": tokens, "Probabilidad": probabilidades})
|
| 76 |
-
|
| 77 |
-
return df # ✅ Now returning a Pandas DataFrame instead of a list
|
| 78 |
|
| 79 |
def predecir_siguiente_palabra(nombre_modelo, texto, top_k, token_custom=""):
|
| 80 |
"""Obtiene predicciones y actualiza la UI."""
|
| 81 |
if token_custom:
|
| 82 |
-
texto
|
| 83 |
|
| 84 |
tokens, probabilidades = obtener_predicciones(texto, nombre_modelo, int(top_k))
|
| 85 |
|
|
@@ -89,10 +88,10 @@ def predecir_siguiente_palabra(nombre_modelo, texto, top_k, token_custom=""):
|
|
| 89 |
return gr.update(choices=[f"'{t}'" for t in tokens]), barplot_data
|
| 90 |
|
| 91 |
def agregar_token_seleccionado(texto, token_seleccionado):
|
| 92 |
-
"""Agrega el token seleccionado al texto de entrada."""
|
| 93 |
if token_seleccionado:
|
| 94 |
-
token_limpio = token_seleccionado.strip("'")
|
| 95 |
-
texto
|
| 96 |
return texto
|
| 97 |
|
| 98 |
# Crear la interfaz en español
|
|
@@ -137,9 +136,10 @@ with gr.Blocks() as demo:
|
|
| 137 |
with gr.Row():
|
| 138 |
barplot_resultados = gr.BarPlot(
|
| 139 |
value=pd.DataFrame(columns=["Palabra", "Probabilidad"]), # ✅ Empty DataFrame to initialize
|
| 140 |
-
x="
|
| 141 |
-
y="
|
| 142 |
-
title="📊 Predicciones del modelo"
|
|
|
|
| 143 |
)
|
| 144 |
|
| 145 |
# Acciones de botones
|
|
|
|
| 58 |
if current_model_name != nombre_modelo:
|
| 59 |
cargar_modelo(nombre_modelo)
|
| 60 |
|
| 61 |
+
entradas = current_tokenizer(texto.strip(), return_tensors="pt").to(device)
|
| 62 |
|
| 63 |
with torch.no_grad():
|
| 64 |
salidas = current_model(**entradas)
|
|
|
|
| 66 |
probabilidades = torch.nn.functional.softmax(logits, dim=-1)
|
| 67 |
|
| 68 |
top_k_prob, top_k_indices = torch.topk(probabilidades, k=top_k)
|
| 69 |
+
top_k_tokens = [current_tokenizer.decode([idx.item()]).strip() for idx in top_k_indices] # ✅ Strip spaces
|
| 70 |
|
| 71 |
return top_k_tokens, top_k_prob.cpu().tolist()
|
| 72 |
|
| 73 |
def generar_barplot(tokens, probabilidades):
|
| 74 |
"""Convierte los datos en un DataFrame para Gradio BarPlot."""
|
| 75 |
df = pd.DataFrame({"Palabra": tokens, "Probabilidad": probabilidades})
|
| 76 |
+
return df
|
|
|
|
| 77 |
|
| 78 |
def predecir_siguiente_palabra(nombre_modelo, texto, top_k, token_custom=""):
|
| 79 |
"""Obtiene predicciones y actualiza la UI."""
|
| 80 |
if token_custom:
|
| 81 |
+
texto = texto.rstrip() + " " + token_custom.strip() # ✅ Prevents extra whitespaces
|
| 82 |
|
| 83 |
tokens, probabilidades = obtener_predicciones(texto, nombre_modelo, int(top_k))
|
| 84 |
|
|
|
|
| 88 |
return gr.update(choices=[f"'{t}'" for t in tokens]), barplot_data
|
| 89 |
|
| 90 |
def agregar_token_seleccionado(texto, token_seleccionado):
|
| 91 |
+
"""Agrega el token seleccionado al texto de entrada sin espacios extra."""
|
| 92 |
if token_seleccionado:
|
| 93 |
+
token_limpio = token_seleccionado.strip("'").strip() # ✅ Removes unwanted spaces
|
| 94 |
+
texto = texto.rstrip() + " " + token_limpio # ✅ Ensures no double spaces
|
| 95 |
return texto
|
| 96 |
|
| 97 |
# Crear la interfaz en español
|
|
|
|
| 136 |
with gr.Row():
|
| 137 |
barplot_resultados = gr.BarPlot(
|
| 138 |
value=pd.DataFrame(columns=["Palabra", "Probabilidad"]), # ✅ Empty DataFrame to initialize
|
| 139 |
+
x="Probabilidad", # ✅ Swap axes to make it horizontal
|
| 140 |
+
y="Palabra",
|
| 141 |
+
title="📊 Predicciones del modelo",
|
| 142 |
+
orientation="h" # ✅ Makes the barplot horizontal
|
| 143 |
)
|
| 144 |
|
| 145 |
# Acciones de botones
|