Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,12 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import json
|
| 4 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
model = pipeline("token-classification", model="benchaffe/Bert-RAdam-Large", aggregation_strategy="simple")
|
| 7 |
|
|
@@ -21,25 +27,26 @@ def log_interaction(input_text, prediction):
|
|
| 21 |
"input": input_text,
|
| 22 |
"prediction": to_serializable(prediction)
|
| 23 |
}
|
| 24 |
-
with open(
|
| 25 |
f.write(json.dumps(log_entry) + "\n")
|
| 26 |
|
| 27 |
-
def
|
| 28 |
result = model(text)
|
| 29 |
log_interaction(text, result)
|
| 30 |
-
return result
|
| 31 |
|
| 32 |
with gr.Blocks() as demo:
|
| 33 |
gr.Markdown("## Biomedical Abbreviation Identifier")
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
demo.launch()
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import json
|
| 4 |
from datetime import datetime
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
LOG_FILE = "/tmp/log.jsonl"
|
| 8 |
+
|
| 9 |
+
if not os.path.exists(LOG_FILE):
|
| 10 |
+
with open(LOG_FILE, "w"): pass
|
| 11 |
|
| 12 |
model = pipeline("token-classification", model="benchaffe/Bert-RAdam-Large", aggregation_strategy="simple")
|
| 13 |
|
|
|
|
| 27 |
"input": input_text,
|
| 28 |
"prediction": to_serializable(prediction)
|
| 29 |
}
|
| 30 |
+
with open(LOG_FILE, "a") as f:
|
| 31 |
f.write(json.dumps(log_entry) + "\n")
|
| 32 |
|
| 33 |
+
def predict_and_log(text):
|
| 34 |
result = model(text)
|
| 35 |
log_interaction(text, result)
|
| 36 |
+
return result, gr.File.update(value=LOG_FILE)
|
| 37 |
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
gr.Markdown("## Biomedical Abbreviation Identifier")
|
| 40 |
+
|
| 41 |
+
input_box = gr.Textbox(label="Enter biomedical text")
|
| 42 |
+
output_box = gr.JSON(label="Model Prediction")
|
| 43 |
+
download_file = gr.File(label="Download Logs", value=LOG_FILE)
|
| 44 |
+
|
| 45 |
+
submit_btn = gr.Button("Submit")
|
| 46 |
+
submit_btn.click(
|
| 47 |
+
fn=predict_and_log,
|
| 48 |
+
inputs=input_box,
|
| 49 |
+
outputs=[output_box, download_file]
|
| 50 |
+
)
|
| 51 |
|
| 52 |
demo.launch()
|