Delete app.py
Browse files
app.py
DELETED
|
@@ -1,116 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import pipeline
|
| 4 |
-
import time
|
| 5 |
-
from functools import wraps
|
| 6 |
-
import sys
|
| 7 |
-
import multimolecule # Importar para registrar los modelos de multimolecule
|
| 8 |
-
import spaces # Aseg煤rate de que este m贸dulo est茅 correctamente instalado
|
| 9 |
-
|
| 10 |
-
# Decorador para medir el tiempo de ejecuci贸n
|
| 11 |
-
def medir_tiempo(func):
|
| 12 |
-
@wraps(func)
|
| 13 |
-
def wrapper(*args, **kwargs):
|
| 14 |
-
inicio = time.time()
|
| 15 |
-
resultado = func(*args, **kwargs)
|
| 16 |
-
fin = time.time()
|
| 17 |
-
tiempo_transcurrido = fin - inicio
|
| 18 |
-
print(f"Tiempo de ejecuci贸n de '{func.__name__}': {tiempo_transcurrido:.2f} segundos")
|
| 19 |
-
return resultado
|
| 20 |
-
return wrapper
|
| 21 |
-
|
| 22 |
-
# Decorador para asignar GPU (seg煤n la funcionalidad proporcionada por spaces)
|
| 23 |
-
@spaces.GPU
|
| 24 |
-
@medir_tiempo
|
| 25 |
-
def predecir_fill_mask(secuencias):
|
| 26 |
-
"""
|
| 27 |
-
Funci贸n que realiza una predicci贸n de Fill-Mask para las secuencias de ARN proporcionadas.
|
| 28 |
-
"""
|
| 29 |
-
try:
|
| 30 |
-
if not secuencias.strip():
|
| 31 |
-
return "Por favor, ingresa una o m谩s secuencias de ARN v谩lidas con <mask> para predecir."
|
| 32 |
-
|
| 33 |
-
# Separar las secuencias por l铆neas y eliminar espacios vac铆os
|
| 34 |
-
secuencias_lista = [seq.strip().upper() for seq in secuencias.strip().split('\n') if seq.strip()]
|
| 35 |
-
resultados = []
|
| 36 |
-
|
| 37 |
-
for seq in secuencias_lista:
|
| 38 |
-
# Asegurarse de que la secuencia contenga al menos un <mask>
|
| 39 |
-
if "<MASK>" not in seq and "<mask>" not in seq:
|
| 40 |
-
resultados.append(f"Secuencia sin token <mask>: {seq}. Agrega <mask> donde desees predecir.")
|
| 41 |
-
continue
|
| 42 |
-
|
| 43 |
-
# Reemplazar <mask> con el token de m谩scara del modelo
|
| 44 |
-
seq = seq.replace("<mask>", mask_token).replace("<MASK>", mask_token)
|
| 45 |
-
|
| 46 |
-
# Realizar la predicci贸n de Fill-Mask
|
| 47 |
-
try:
|
| 48 |
-
predictions = fill_mask(seq)
|
| 49 |
-
except Exception as e:
|
| 50 |
-
resultados.append(f"Error al predecir para la secuencia: {seq}\n{e}")
|
| 51 |
-
continue
|
| 52 |
-
|
| 53 |
-
# Formatear las predicciones
|
| 54 |
-
pred_str = ""
|
| 55 |
-
for pred in predictions:
|
| 56 |
-
pred_str += f"Predicci贸n: {pred['sequence']}, Score: {pred['score']:.4f}\n"
|
| 57 |
-
|
| 58 |
-
resultados.append(f"Secuencia: {seq}\n{pred_str}")
|
| 59 |
-
|
| 60 |
-
return "\n\n".join(resultados)
|
| 61 |
-
|
| 62 |
-
except Exception as e:
|
| 63 |
-
print(f"Error durante la predicci贸n: {e}")
|
| 64 |
-
return f"Error al realizar la predicci贸n: {e}"
|
| 65 |
-
|
| 66 |
-
# Configurar el dispositivo
|
| 67 |
-
device = 0 if torch.cuda.is_available() else -1
|
| 68 |
-
if device == -1:
|
| 69 |
-
print("Advertencia: CUDA no est谩 disponible. Se usar谩 la CPU, lo que puede ser lento.")
|
| 70 |
-
|
| 71 |
-
# Cargar el pipeline de Fill-Mask
|
| 72 |
-
try:
|
| 73 |
-
print("Cargando el pipeline de Fill-Mask...")
|
| 74 |
-
fill_mask = pipeline('fill-mask', model='multimolecule/mrnafm', device=device)
|
| 75 |
-
except Exception as e:
|
| 76 |
-
print(f"Error al cargar el pipeline de Fill-Mask: {e}")
|
| 77 |
-
sys.exit(1)
|
| 78 |
-
|
| 79 |
-
# Obtener el token de m谩scara del modelo
|
| 80 |
-
mask_token = fill_mask.tokenizer.mask_token
|
| 81 |
-
print(f"Mask token utilizado por el modelo: {mask_token}")
|
| 82 |
-
|
| 83 |
-
# Definir la interfaz de Gradio
|
| 84 |
-
titulo = "OmniGenome: Predicci贸n de Fill-Mask para Secuencias de ARN"
|
| 85 |
-
descripcion = (
|
| 86 |
-
"Ingresa una o m谩s secuencias de ARN (una por l铆nea) con un token <mask> donde deseas realizar la predicci贸n. "
|
| 87 |
-
"El modelo utilizado es mRNA-FM de MultiMolecule, un modelo pre-entrenado de lenguaje para secuencias de ARN."
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
iface = gr.Interface(
|
| 91 |
-
fn=predecir_fill_mask,
|
| 92 |
-
inputs=gr.Textbox(
|
| 93 |
-
lines=10,
|
| 94 |
-
placeholder="Escribe tus secuencias de ARN aqu铆, una por l铆nea, incluyendo <mask> donde desees predecir...",
|
| 95 |
-
label="Secuencias de ARN con <mask>"
|
| 96 |
-
),
|
| 97 |
-
outputs=gr.Textbox(label="Predicciones de Fill-Mask"),
|
| 98 |
-
title=titulo,
|
| 99 |
-
description=descripcion,
|
| 100 |
-
examples=[
|
| 101 |
-
[
|
| 102 |
-
"AUGGCUACUUU<mask>G",
|
| 103 |
-
"GCGCGAU<mask>CGACGUAGCUAGC"
|
| 104 |
-
],
|
| 105 |
-
[
|
| 106 |
-
"AUAUGCGGUAUCGU<mask>GUA",
|
| 107 |
-
"GGAUACGUGAU<mask>GCUAGCAGU"
|
| 108 |
-
]
|
| 109 |
-
],
|
| 110 |
-
cache_examples=False,
|
| 111 |
-
allow_flagging="never"
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
# Ejecutar la interfaz
|
| 115 |
-
if __name__ == "__main__":
|
| 116 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|