Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,11 +36,28 @@ def adr_predict(x):
|
|
| 36 |
|
| 37 |
scores = torch.softmax(output.logits, dim=-1)[0].detach().cpu().numpy()
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
shap_values = explainer([text_input])
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
try:
|
| 46 |
res = ner_pipe(text_input)
|
|
@@ -54,7 +71,6 @@ def adr_predict(x):
|
|
| 54 |
'Biological_structure':'#d9d9d9'
|
| 55 |
}
|
| 56 |
|
| 57 |
-
# FIX: Added inline "color: black;" to force all un-highlighted text to be black
|
| 58 |
htext = "<div style='line-height: 2.0; font-size: 1.1em; color: black;'>"
|
| 59 |
prev_end = 0
|
| 60 |
res = sorted(res, key=lambda x: x['start'])
|
|
@@ -64,7 +80,6 @@ def adr_predict(x):
|
|
| 64 |
color = entity_colors.get(entity['entity_group'], '#f3f3f3')
|
| 65 |
|
| 66 |
htext += f"{text_input[prev_end:start]}"
|
| 67 |
-
# Highlighted text is also explicitly set to black
|
| 68 |
htext += f"<mark style='background-color:{color}; color: black; padding: 2px 4px; border-radius: 4px; font-weight: 500;'>{word} <small style='opacity: 0.7;'>[{entity['entity_group']}]</small></mark>"
|
| 69 |
prev_end = end
|
| 70 |
htext += text_input[prev_end:] + "</div>"
|
|
@@ -72,9 +87,9 @@ def adr_predict(x):
|
|
| 72 |
htext = "<p style='color: black;'>NER processing error.</p>"
|
| 73 |
|
| 74 |
label_output = {"Severe Reaction": float(scores[1]), "Non-severe Reaction": float(scores[0])}
|
|
|
|
| 75 |
return label_output, local_plot, htext
|
| 76 |
|
| 77 |
-
# FIX: Added !important tags to ensure Gradio's dark mode doesn't override the white background and black text
|
| 78 |
custom_css = """
|
| 79 |
.gradio-container { font-family: 'Inter', system-ui, sans-serif; }
|
| 80 |
.main-header { text-align: center; margin-bottom: 2rem; }
|
|
@@ -115,7 +130,8 @@ with gr.Blocks(title="ADR Detector") as demo:
|
|
| 115 |
with gr.TabItem("Medical Entities"):
|
| 116 |
htext = gr.HTML(label="NER Mapping", elem_classes="output-box")
|
| 117 |
with gr.TabItem("Model Logic (SHAP)"):
|
| 118 |
-
|
|
|
|
| 119 |
|
| 120 |
gr.Markdown("---")
|
| 121 |
gr.Markdown("Disclaimer: This tool is for research purposes only and does not constitute medical advice.")
|
|
|
|
| 36 |
|
| 37 |
scores = torch.softmax(output.logits, dim=-1)[0].detach().cpu().numpy()
|
| 38 |
|
| 39 |
+
# -------------------------------------------------------------
|
| 40 |
+
# NEW SHAP BAR PLOT LOGIC
|
| 41 |
+
# -------------------------------------------------------------
|
| 42 |
try:
|
| 43 |
shap_values = explainer([text_input])
|
| 44 |
+
|
| 45 |
+
# Clear any previous Matplotlib figures to avoid overlapping
|
| 46 |
+
plt.close('all')
|
| 47 |
+
|
| 48 |
+
# Generate the standard SHAP bar plot. show=False prevents it from popping up
|
| 49 |
+
shap.plots.bar(shap_values[0], show=False)
|
| 50 |
+
|
| 51 |
+
# Capture the current figure to send to Gradio
|
| 52 |
+
local_plot = plt.gcf()
|
| 53 |
+
|
| 54 |
except Exception as e:
|
| 55 |
+
# Fallback if SHAP fails: return an empty figure with an error message
|
| 56 |
+
plt.close('all')
|
| 57 |
+
local_plot = plt.figure(figsize=(6, 2))
|
| 58 |
+
plt.text(0.5, 0.5, f"SHAP error: {e}", ha='center', va='center', color='red')
|
| 59 |
+
plt.axis('off')
|
| 60 |
+
# -------------------------------------------------------------
|
| 61 |
|
| 62 |
try:
|
| 63 |
res = ner_pipe(text_input)
|
|
|
|
| 71 |
'Biological_structure':'#d9d9d9'
|
| 72 |
}
|
| 73 |
|
|
|
|
| 74 |
htext = "<div style='line-height: 2.0; font-size: 1.1em; color: black;'>"
|
| 75 |
prev_end = 0
|
| 76 |
res = sorted(res, key=lambda x: x['start'])
|
|
|
|
| 80 |
color = entity_colors.get(entity['entity_group'], '#f3f3f3')
|
| 81 |
|
| 82 |
htext += f"{text_input[prev_end:start]}"
|
|
|
|
| 83 |
htext += f"<mark style='background-color:{color}; color: black; padding: 2px 4px; border-radius: 4px; font-weight: 500;'>{word} <small style='opacity: 0.7;'>[{entity['entity_group']}]</small></mark>"
|
| 84 |
prev_end = end
|
| 85 |
htext += text_input[prev_end:] + "</div>"
|
|
|
|
| 87 |
htext = "<p style='color: black;'>NER processing error.</p>"
|
| 88 |
|
| 89 |
label_output = {"Severe Reaction": float(scores[1]), "Non-severe Reaction": float(scores[0])}
|
| 90 |
+
|
| 91 |
return label_output, local_plot, htext
|
| 92 |
|
|
|
|
| 93 |
custom_css = """
|
| 94 |
.gradio-container { font-family: 'Inter', system-ui, sans-serif; }
|
| 95 |
.main-header { text-align: center; margin-bottom: 2rem; }
|
|
|
|
| 130 |
with gr.TabItem("Medical Entities"):
|
| 131 |
htext = gr.HTML(label="NER Mapping", elem_classes="output-box")
|
| 132 |
with gr.TabItem("Model Logic (SHAP)"):
|
| 133 |
+
# CHANGED: gr.HTML -> gr.Plot
|
| 134 |
+
local_plot = gr.Plot(label='Feature Importance', elem_classes="output-box")
|
| 135 |
|
| 136 |
gr.Markdown("---")
|
| 137 |
gr.Markdown("Disclaimer: This tool is for research purposes only and does not constitute medical advice.")
|