Commit ·
293fb2f
1
Parent(s): 4a092c4
Remove interpretation to upgrade gradio
Browse files- app.py +1 -32
- requirements.txt +2 -3
app.py
CHANGED
|
@@ -1,17 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import shap
|
| 3 |
-
import transformers
|
| 4 |
|
| 5 |
import os
|
| 6 |
import re
|
| 7 |
import subprocess
|
| 8 |
-
import sys
|
| 9 |
import tempfile
|
| 10 |
|
| 11 |
model = gr.load("ejschwartz/oo-method-test-model-bylibrary", src="models")
|
| 12 |
|
| 13 |
-
model_interp = transformers.pipeline("text-classification", "ejschwartz/oo-method-test-model-bylibrary")
|
| 14 |
-
|
| 15 |
def get_all_dis(bname, addrs=None):
|
| 16 |
|
| 17 |
anafile = tempfile.NamedTemporaryFile(prefix=os.path.basename(bname) + "_", suffix=".bat_ana")
|
|
@@ -89,8 +84,6 @@ with gr.Blocks() as demo:
|
|
| 89 |
disassembly = gr.Textbox(label="Disassembly", lines=20)
|
| 90 |
with gr.Column():
|
| 91 |
clazz = gr.Label()
|
| 92 |
-
interpret_button = gr.Button("Interpret (very slow)")
|
| 93 |
-
interpretation = gr.components.Interpretation(disassembly)
|
| 94 |
|
| 95 |
example_widget = gr.Examples(
|
| 96 |
examples=[f.path for f in os.scandir(os.path.join(os.path.dirname(__file__), "examples"))],
|
|
@@ -124,35 +117,11 @@ with gr.Blocks() as demo:
|
|
| 124 |
|
| 125 |
return {disassembly: gr.Textbox.update(value=disassembly_str),
|
| 126 |
clazz: gr.Label.update(top_k),
|
| 127 |
-
# I can't figure out how to hide this
|
| 128 |
-
#interpretation: {}
|
| 129 |
}
|
| 130 |
-
|
| 131 |
-
# XXX: Ideally we'd use the gr.load model, which uses the huggingface
|
| 132 |
-
# inference API. But shap library appears to use information in the
|
| 133 |
-
# transformers pipeline, and I don't feel like figuring out how to
|
| 134 |
-
# reimplement that, so we'll just use a regular transformers pipeline here
|
| 135 |
-
# for interpretation.
|
| 136 |
-
def interpretation_function(text, progress=gr.Progress(track_tqdm=True)):
|
| 137 |
-
|
| 138 |
-
progress(0, desc="Interpreting function")
|
| 139 |
-
explainer = shap.Explainer(model_interp)
|
| 140 |
-
shap_values = explainer([text])
|
| 141 |
-
|
| 142 |
-
# Dimensions are (batch size, text size, number of classes)
|
| 143 |
-
# Since we care about positive sentiment, use index 1
|
| 144 |
-
scores = list(zip(shap_values.data[0], shap_values.values[0, :, 1]))
|
| 145 |
-
# Scores contains (word, score) pairs
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
# Format expected by gr.components.Interpretation
|
| 149 |
-
return {"original": text, "interpretation": scores}
|
| 150 |
|
| 151 |
file_widget.change(file_change_fn, file_widget, [col, fun_dropdown, all_dis_state])
|
| 152 |
|
| 153 |
-
fun_dropdown.change(function_change_fn, [fun_dropdown, all_dis_state], [disassembly, clazz
|
| 154 |
-
|
| 155 |
-
interpret_button.click(interpretation_function, disassembly, interpretation)
|
| 156 |
|
| 157 |
demo.queue()
|
| 158 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
import subprocess
|
|
|
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
model = gr.load("ejschwartz/oo-method-test-model-bylibrary", src="models")
|
| 9 |
|
|
|
|
|
|
|
| 10 |
def get_all_dis(bname, addrs=None):
|
| 11 |
|
| 12 |
anafile = tempfile.NamedTemporaryFile(prefix=os.path.basename(bname) + "_", suffix=".bat_ana")
|
|
|
|
| 84 |
disassembly = gr.Textbox(label="Disassembly", lines=20)
|
| 85 |
with gr.Column():
|
| 86 |
clazz = gr.Label()
|
|
|
|
|
|
|
| 87 |
|
| 88 |
example_widget = gr.Examples(
|
| 89 |
examples=[f.path for f in os.scandir(os.path.join(os.path.dirname(__file__), "examples"))],
|
|
|
|
| 117 |
|
| 118 |
return {disassembly: gr.Textbox.update(value=disassembly_str),
|
| 119 |
clazz: gr.Label.update(top_k),
|
|
|
|
|
|
|
| 120 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
file_widget.change(file_change_fn, file_widget, [col, fun_dropdown, all_dis_state])
|
| 123 |
|
| 124 |
+
fun_dropdown.change(function_change_fn, [fun_dropdown, all_dis_state], [disassembly, clazz])
|
|
|
|
|
|
|
| 125 |
|
| 126 |
demo.queue()
|
| 127 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
requirements.txt
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
-
gradio
|
| 2 |
pandas
|
| 3 |
numpy >= 1.22.4
|
| 4 |
-
|
| 5 |
-
transformers[torch]
|
|
|
|
| 1 |
+
gradio
|
| 2 |
pandas
|
| 3 |
numpy >= 1.22.4
|
| 4 |
+
transformers
|
|
|