Spaces:
Runtime error
Runtime error
Download app.py from Soporte12/biobert-classifier: direct link, hf CLI and curl.
- Browser
- Download file 1.33 kB
-
https://huggingface.co/spaces/Soporte12/biobert-classifier/resolve/main/app.py
- Command line
-
hf download hf://spaces/Soporte12/biobert-classifier/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Soporte12/biobert-classifier/resolve/main/app.py
1.33 kB
| # app.py | |
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| # Cargar el tokenizador y el modelo entrenado (asegΓΊrate de tener los pesos guardados) | |
| MODEL_NAME = "dmis-lab/biobert-v1.1" | |
| model = AutoModelForSequenceClassification.from_pretrained( | |
| MODEL_NAME, | |
| num_labels=4, | |
| problem_type="multi_label_classification" | |
| ) | |
| # Cargar los pesos del modelo guardados localmente | |
| model.load_state_dict(torch.load('biobert_multilabel_model_2.pt', map_location=torch.device('cpu'))) | |
| model.eval() | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| app = FastAPI() | |
| class Item(BaseModel): | |
| title: str | |
| abstract: str | |
| async def predict_group(item: Item): | |
| text_to_predict = item.title + " [SEP] " + item.abstract | |
| inputs = tokenizer(text_to_predict, return_tensors="pt", truncation=True, padding=True) | |
| with torch.no_grad(): | |
| logits = model(**inputs).logits | |
| probabilities = torch.sigmoid(logits) | |
| predictions = (probabilities > 0.5).int().tolist()[0] | |
| unique_labels = ['cardiovascular', 'hepatorenal', 'neurological', 'oncological'] | |
| predicted_labels = [unique_labels[i] for i, pred in enumerate(predictions) if pred == 1] | |
| return {"predictions": predicted_labels} |