skimlit / app.py
prosekutor's picture
chore: update script for keras 3 compatibility
ad507c1 verified
Raw History Blame Contribute Delete
2.4 kB
import gradio as gr
import tensorflow as tf
from tensorflow.keras.layers import TextVectorization
import pandas as pd
import re, string
model_path = "skimlit_max_trained_model"
@tf.keras.utils.register_keras_serializable(package="Custom", name=None)
def custom_standardization(input_data):
lowercase = tf.strings.lower(input_data)
stripped_html = tf.strings.regex_replace(lowercase, "", " ")
return tf.strings.regex_replace(
stripped_html, "[%s]" % re.escape(string.punctuation), ""
)
# Load SavedModel as inference-only layer
loaded_model = tf.keras.layers.TFSMLayer(model_path, call_endpoint="serving_default")
def get_results(abs_text):
text = ""
labels = ["BACKGROUND", "CONCLUSIONS", "METHODS", "OBJECTIVE", "RESULTS"]
sent_list = abs_text.split(sep=".")
sent_list.pop()
sent_list = [sentence.strip() for sentence in sent_list]
total_lines = len(sent_list)
# Prepare dataframe
final_list = []
for i, line in enumerate(sent_list):
final_list.append(
{"text": line, "line_number": i, "total_lines": total_lines}
)
df = pd.DataFrame(final_list)
# Features
chars = [" ".join(list(sentence)) for sentence in sent_list]
line_numbers_one_hot = tf.one_hot(df.line_number.to_numpy(), depth=15)
lines_total_one_hot = tf.one_hot(df.total_lines.to_numpy(), depth=20)
# Call TFSMLayer directly (no .predict)
preds = tf.argmax(
loaded_model(
(
line_numbers_one_hot,
lines_total_one_hot,
tf.constant(sent_list),
tf.constant(chars),
)
),
axis=1,
)
# Build formatted text output
for i in range(total_lines):
if i != 0 and preds[i] != preds[i - 1]:
text += f"\n\n{labels[preds[i]]}: {sent_list[i]}."
elif i == 0:
text += f"{labels[preds[i]]}: {sent_list[i]}."
else:
text += f"{sent_list[i]}."
return text
def skimlit_analysis(text):
return get_results(text)
intfc = gr.Interface(
fn=skimlit_analysis,
inputs=gr.Textbox(
label="Input abstract here",
lines=2,
placeholder="Input your abstract here (the model works best with ones from medical research papers)",
),
outputs=gr.Text(label="Skimmable Abstract", interactive=False),
)
intfc.launch()