daniihc16 commited on
Commit
f4008e9
·
verified ·
1 Parent(s): 7e3e7b4

Rename app.py to proyect.toml

Browse files
Files changed (2) hide show
  1. app.py +0 -92
  2. proyect.toml +3 -0
app.py DELETED
@@ -1,92 +0,0 @@
1
- import gradio as gr
2
- from huggingface_hub import hf_hub_download
3
- import time, traceback, os
4
- import torch
5
- import numpy as np
6
- from fastai.vision.all import *
7
-
8
- os.environ["OMP_NUM_THREADS"] = "1"
9
- torch.set_num_threads(1)
10
-
11
- def log(msg):
12
- print(f"[DEBUG {time.strftime('%H:%M:%S')}] {msg}", flush=True)
13
-
14
-
15
- # --- CARGA DEL MODELO ---
16
- repo_id = "daniihc16/chest-xray-classifier"
17
- filename = "model.pkl"
18
-
19
- try:
20
- log("Descargando...")
21
- model_path = hf_hub_download(repo_id=repo_id, filename=filename)
22
-
23
- log("Cargando estructura...")
24
- learn = load_learner(model_path)
25
- model = learn.model.eval() # Extraemos el modelo puro de PyTorch
26
- vocab = learn.dls.vocab # Guardamos las etiquetas
27
-
28
- log(f"Modelo extraído. Vocabulario: {vocab}")
29
-
30
- except Exception as e:
31
- log(f"ERROR FATAL: {e}")
32
- raise e
33
-
34
- # --- PREDICCIÓN MANUAL (SIN DATALOADERS) ---
35
- def predict_pure_pytorch(img):
36
- start = time.time()
37
- log("1. Petición recibida")
38
-
39
- try:
40
- # 1. PREPROCESAMIENTO MANUAL
41
-
42
- if img.size != (224, 224):
43
- img = img.resize((224, 224))
44
-
45
- x = torch.tensor(np.array(img)).float()
46
-
47
- x = x.permute(2, 0, 1)
48
-
49
- x /= 255.0
50
- mean = torch.tensor([0.485, 0.456, 0.406]).view(3, 1, 1)
51
- std = torch.tensor([0.229, 0.224, 0.225]).view(3, 1, 1)
52
- x = (x - mean) / std
53
-
54
- x = x.unsqueeze(0)
55
-
56
- log(f" Tensor preparado: {x.shape}")
57
-
58
- # 2. INFERENCIA
59
- log("2. Ejecutando forward pass...")
60
- with torch.no_grad():
61
- out = model(x) # Inferencia directa, sin wrappers
62
- probs = torch.softmax(out, dim=1) # Convertir logits a probabilidades
63
-
64
- probs_np = probs[0].numpy()
65
- log(f"3. Probabilidades crudas: {probs_np}")
66
-
67
- # 3. RESULTADO
68
- result = {vocab[i]: float(probs_np[i]) for i in range(len(vocab))}
69
- log(f" Tiempo: {time.time()-start:.3f}s")
70
-
71
- return result
72
-
73
- except Exception as e:
74
- log("ERROR EN PREDICCIÓN MANUAL")
75
- traceback.print_exc()
76
- return {f"Error: {str(e)}": 0.0}
77
-
78
- # --- INTERFAZ ---
79
- interface = gr.Interface(
80
- fn=predict_pure_pytorch,
81
- inputs=gr.Image(type="pil"),
82
- outputs=gr.Label(),
83
- title="Clasificador Radiografías",
84
- description="",
85
- examples=[
86
- "person1_bacteria_1.jpeg",
87
- "IM-0115-0001.jpeg"
88
- ],
89
- )
90
-
91
- if __name__ == "__main__":
92
- interface.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
proyect.toml ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [build-system]
2
+ requires = ["setuptools>=40.8.0", "wheel", "python=3.12.12", "fastai=2.8.1", "fastcore=1.8.18"]
3
+ build-backend = "setuptools.build_meta:__legacy__"