Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ with open("app.py", "w") as f:
|
|
| 3 |
import os
|
| 4 |
import torch
|
| 5 |
import torch.nn as nn
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
import pickle
|
| 8 |
import pandas as pd
|
|
@@ -69,16 +70,16 @@ def predict(*inputs):
|
|
| 69 |
with torch.no_grad():
|
| 70 |
tensor_data = torch.FloatTensor(scaled_data)
|
| 71 |
output = model(tensor_data)
|
| 72 |
-
probabilities =
|
| 73 |
print("Prediction completed")
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
except Exception as e:
|
| 80 |
print(f"Error in prediction: {str(e)}")
|
| 81 |
-
return
|
| 82 |
|
| 83 |
# Default values
|
| 84 |
numeric_defaults = {
|
|
@@ -90,35 +91,31 @@ numeric_defaults = {
|
|
| 90 |
}
|
| 91 |
|
| 92 |
# Create interface
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
fn=predict,
|
| 95 |
-
inputs=
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
gr.Dropdown(choices=["Yes", "No"], label="HTN"),
|
| 100 |
-
gr.Dropdown(choices=["Yes", "No"], label="DM"),
|
| 101 |
-
gr.Dropdown(choices=["Yes", "No"], label="Fullcompansasion"),
|
| 102 |
-
gr.Number(value=numeric_defaults['pvc_percent'], label="pvc_percent"),
|
| 103 |
-
gr.Number(value=numeric_defaults['PVCQRS'], label="PVCQRS"),
|
| 104 |
-
gr.Number(value=numeric_defaults['EF'], label="EF"),
|
| 105 |
-
gr.Number(value=numeric_defaults['Age'], label="Age"),
|
| 106 |
-
gr.Number(value=numeric_defaults['PVC_Prematurity_index'], label="PVC_Prematurity_index"),
|
| 107 |
-
gr.Number(value=numeric_defaults['QRS_ratio'], label="QRS_ratio"),
|
| 108 |
-
gr.Number(value=numeric_defaults['mean_HR'], label="mean_HR"),
|
| 109 |
-
gr.Number(value=numeric_defaults['symptom_duration'], label="symptom_duration"),
|
| 110 |
-
gr.Number(value=numeric_defaults['QTc_sinus'], label="QTc_sinus"),
|
| 111 |
-
gr.Number(value=numeric_defaults['PVCCI_dispersion'], label="PVCCI_dispersion"),
|
| 112 |
-
gr.Number(value=numeric_defaults['CI_variability'], label="CI_variability"),
|
| 113 |
-
gr.Number(value=numeric_defaults['PVC_Peak_QRS_duration'], label="PVC_Peak_QRS_duration"),
|
| 114 |
-
gr.Number(value=numeric_defaults['PVCCI'], label="PVCCI"),
|
| 115 |
-
gr.Number(value=numeric_defaults['PVC_Compansatory_interval'], label="PVC_Compansatory_interval")
|
| 116 |
-
],
|
| 117 |
-
outputs=gr.Label(label="Prediction"),
|
| 118 |
-
title="PVC Response Predictor",
|
| 119 |
-
description="Enter patient features to predict response probability"
|
| 120 |
)
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
print("Launching application...")
|
| 124 |
-
|
|
|
|
| 3 |
import os
|
| 4 |
import torch
|
| 5 |
import torch.nn as nn
|
| 6 |
+
import torch.nn.functional as F
|
| 7 |
import gradio as gr
|
| 8 |
import pickle
|
| 9 |
import pandas as pd
|
|
|
|
| 70 |
with torch.no_grad():
|
| 71 |
tensor_data = torch.FloatTensor(scaled_data)
|
| 72 |
output = model(tensor_data)
|
| 73 |
+
probabilities = F.softmax(output, dim=1)
|
| 74 |
print("Prediction completed")
|
| 75 |
|
| 76 |
+
response_prob = float(probabilities[0][0])
|
| 77 |
+
non_response_prob = float(probabilities[0][1])
|
| 78 |
+
|
| 79 |
+
return f"Response Probability: {response_prob:.2%}\nNon-Response Probability: {non_response_prob:.2%}"
|
| 80 |
except Exception as e:
|
| 81 |
print(f"Error in prediction: {str(e)}")
|
| 82 |
+
return f"Error: {str(e)}"
|
| 83 |
|
| 84 |
# Default values
|
| 85 |
numeric_defaults = {
|
|
|
|
| 91 |
}
|
| 92 |
|
| 93 |
# Create interface
|
| 94 |
+
title = "PVC Response Predictor"
|
| 95 |
+
description = "Enter patient features to predict response probability"
|
| 96 |
+
|
| 97 |
+
inputs = [
|
| 98 |
+
gr.inputs.Dropdown(choices=["Yes", "No"], label="Multifocal_PVC"),
|
| 99 |
+
gr.inputs.Dropdown(choices=["Yes", "No"], label="Nonsustained_VT"),
|
| 100 |
+
gr.inputs.Dropdown(choices=["Yes", "No"], label="gender"),
|
| 101 |
+
gr.inputs.Dropdown(choices=["Yes", "No"], label="HTN"),
|
| 102 |
+
gr.inputs.Dropdown(choices=["Yes", "No"], label="DM"),
|
| 103 |
+
gr.inputs.Dropdown(choices=["Yes", "No"], label="Fullcompansasion")
|
| 104 |
+
]
|
| 105 |
+
|
| 106 |
+
for feature in numeric_features:
|
| 107 |
+
inputs.append(gr.inputs.Number(default=numeric_defaults[feature], label=feature))
|
| 108 |
+
|
| 109 |
+
outputs = gr.outputs.Textbox(label="Prediction Result")
|
| 110 |
+
|
| 111 |
+
interface = gr.Interface(
|
| 112 |
fn=predict,
|
| 113 |
+
inputs=inputs,
|
| 114 |
+
outputs=outputs,
|
| 115 |
+
title=title,
|
| 116 |
+
description=description
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
)
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
| 120 |
print("Launching application...")
|
| 121 |
+
interface.launch()""")
|