Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
df = pd.read_csv("Cancer_Data.csv")
|
| 7 |
+
df = df.dropna(axis='columns')
|
| 8 |
+
df = df.drop(['id'],axis=1)
|
| 9 |
+
|
| 10 |
+
headers = ["diagnosis","radius_mean","texture_mean","perimeter_mean","area_mean","smoothness_mean","compactness_mean","concavity_mean","concave points_mean","symmetry_mean","fractal_dimension_mean","radius_se","texture_se","perimeter_se","area_se","smoothness_se","compactness_se","concavity_se","concave points_se","symmetry_se","fractal_dimension_se","radius_worst","texture_worst","perimeter_worst","area_worst","smoothness_worst","compactness_worst","concavity_worst","concave points_worst","symmetry_worst","fractal_dimension_worst"]
|
| 11 |
+
inputs = [gr.Dataframe(headers = headers, row_count = (2, "dynamic"), col_count=(31,"dynamic"), label="Input Data", interactive=1)]
|
| 12 |
+
outputs = [gr.Dataframe(row_count = (2, "dynamic"), col_count=(1, "fixed"), label="Predictions", headers=["Results"])]
|
| 13 |
+
|
| 14 |
+
def classify_cell(df_input):
|
| 15 |
+
# Dropping diagnosis
|
| 16 |
+
cells = df_input.drop(['diagnosis'],axis=1).values
|
| 17 |
+
|
| 18 |
+
# Classes
|
| 19 |
+
cancer_classes = ['Benign','Malignant']
|
| 20 |
+
# Loading model
|
| 21 |
+
cell_model = torch.jit.load('cancer_classifier.ptl')
|
| 22 |
+
# List I will pass into a dataframe as the return object
|
| 23 |
+
cell_results = []
|
| 24 |
+
|
| 25 |
+
# Converting to tensor and casting it as float32
|
| 26 |
+
cell = torch.tensor(cells)
|
| 27 |
+
cell = cell.to(torch.float)
|
| 28 |
+
|
| 29 |
+
# Running through model and applying softmax to output
|
| 30 |
+
cell_pred = cell_model(cell)
|
| 31 |
+
cell_pred = F.softmax(cell_pred,dim=1)
|
| 32 |
+
# Looping through model output to format string for each cell
|
| 33 |
+
for i in range(len(cell_pred)):
|
| 34 |
+
cell_prob = round(cell_pred[i][cell_pred[i].argmax()].item()*100,2)
|
| 35 |
+
output_string = f"{cancer_classes[cell_pred[i].argmax()]} Cancer Cell : {cell_prob}% confident"
|
| 36 |
+
# Appending formatted string for a cell to cell results list
|
| 37 |
+
cell_results.append(output_string)
|
| 38 |
+
return pd.DataFrame(cell_results,columns=["Results"])
|
| 39 |
+
|
| 40 |
+
demo = gr.Interface(classify_cell,
|
| 41 |
+
inputs = inputs,
|
| 42 |
+
examples=[df.iloc[17:22]],
|
| 43 |
+
outputs = outputs,
|
| 44 |
+
title = "Classify Cancer Cell",
|
| 45 |
+
description="Classifies Cancer Cell into Malignant or Benign based on its features. Click on the example to classify 5 cells! </br> First Column shows what cell should be classified as"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
demo.launch(inline=False)
|