Spaces:
Runtime error
Runtime error
Edward J. Schwartz
commited on
Commit
·
32078b8
1
Parent(s):
bc895a0
First attempt at interpretation
Browse files- app.py +18 -0
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
import os
|
| 4 |
import re
|
|
@@ -83,6 +84,8 @@ with gr.Blocks() as demo:
|
|
| 83 |
with gr.Row(visible=True) as result:
|
| 84 |
disassembly = gr.Textbox(label="Disassembly", lines=20)
|
| 85 |
clazz = gr.Label()
|
|
|
|
|
|
|
| 86 |
|
| 87 |
example_widget = gr.Examples(
|
| 88 |
examples=[f.path for f in os.scandir(os.path.join(os.path.dirname(__file__), "examples"))],
|
|
@@ -116,9 +119,24 @@ with gr.Blocks() as demo:
|
|
| 116 |
return {disassembly: gr.Textbox.update(value=disassembly_str),
|
| 117 |
clazz: gr.Label.update(top_k)
|
| 118 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
file_widget.change(file_change_fn, file_widget, [col, fun_dropdown, all_dis_state])
|
| 121 |
|
| 122 |
fun_dropdown.change(function_change_fn, [fun_dropdown, all_dis_state], [disassembly, clazz])
|
| 123 |
|
|
|
|
|
|
|
| 124 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import shap
|
| 3 |
|
| 4 |
import os
|
| 5 |
import re
|
|
|
|
| 84 |
with gr.Row(visible=True) as result:
|
| 85 |
disassembly = gr.Textbox(label="Disassembly", lines=20)
|
| 86 |
clazz = gr.Label()
|
| 87 |
+
interpret_button = gr.Button("Interpret")
|
| 88 |
+
interpretation = gr.components.Interpretation(disassembly)
|
| 89 |
|
| 90 |
example_widget = gr.Examples(
|
| 91 |
examples=[f.path for f in os.scandir(os.path.join(os.path.dirname(__file__), "examples"))],
|
|
|
|
| 119 |
return {disassembly: gr.Textbox.update(value=disassembly_str),
|
| 120 |
clazz: gr.Label.update(top_k)
|
| 121 |
}
|
| 122 |
+
|
| 123 |
+
def interpretation_function(text):
|
| 124 |
+
explainer = shap.Explainer(model)
|
| 125 |
+
shap_values = explainer([text])
|
| 126 |
+
|
| 127 |
+
# Dimensions are (batch size, text size, number of classes)
|
| 128 |
+
# Since we care about positive sentiment, use index 1
|
| 129 |
+
scores = list(zip(shap_values.data[0], shap_values.values[0, :, 1]))
|
| 130 |
+
# Scores contains (word, score) pairs
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
# Format expected by gr.components.Interpretation
|
| 134 |
+
return {"original": text, "interpretation": scores}
|
| 135 |
|
| 136 |
file_widget.change(file_change_fn, file_widget, [col, fun_dropdown, all_dis_state])
|
| 137 |
|
| 138 |
fun_dropdown.change(function_change_fn, [fun_dropdown, all_dis_state], [disassembly, clazz])
|
| 139 |
|
| 140 |
+
interpret_button.click(interpretation_function, disassembly, interpretation)
|
| 141 |
+
|
| 142 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
gradio
|
| 2 |
pandas
|
| 3 |
numpy >= 1.22.4
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
pandas
|
| 3 |
numpy >= 1.22.4
|
| 4 |
+
shap
|