CoolFace
Apppublic

RyanTietjen/Paper-Fragmentation

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
app.py194 linesDownload Raw Back to root
1"""2Ryan Tietjen3Sep 20244Demo application for paper abstract fragmentaion demonstration5"""6import gradio as gr7import tensorflow as tf8from tensorflow import keras9from keras import layers10from timeit import default_timer as timer11from process_input import split_abstract12from process_input import split_abstract_original13from process_input import split_sentences_by_characters14import pandas as pd15import tensorflow_hub as hub16from model import EmbeddingLayer17from process_input import encode_labels18 19 20sample_list = []21example1 =  f"""The aim of this study was to verify in bruxism patients the possible efficacy of auricular stimulation in reducing the hypertonicity of some masticatory muscles.22Forty-three bruxism patients were randomly allocated to 3 groups : acupuncture , needle contact for 10 seconds , no treatment ( control ).23Helkimo 's clinical dysfunction index ( CDI ) and anamnestic dysfunction index ( ADI ) were used to assess the functional state of the masticatory system.24The resting electrical activity of the anterior temporalis ( AT ) , masseter ( MM ) , digastric ( DA ) and sternocleidomastoid ( SCM ) muscles was measured , according to Jankelson , with surface electrodes at baseline , after stimulation and continually for 30 minutes ( 120 measurements in total ).25The electromyographical variations in the 3 groups were studied with t test for independent samples.26Acupuncture and needle contact were superior to control in reducing the muscle hypertonicity of all muscles except SCM.27In the comparison between acupuncture and needle contact the former showed better results only for the right TA and left DA ( p = 0.000 ).28In this study it was possible to measure the efficacy of the stimulation of only one point or area , which is an ideal model for research in acupuncture.29The auricular area we chose for stimulation was never used before for the purpose of relaxing masticatory muscles.30Acupuncture and needle contact for 10 seconds showed similar effects."""31example2 = """To investigate the efficacy of 6 weeks of daily low-dose oral prednisolone in improving pain , mobility , and systemic low-grade inflammation in the short term and whether the effect would be sustained at 12 weeks in older adults with moderate to severe knee osteoarthritis ( OA ) .32A total of 125 patients with primary knee OA were randomized 1:1 ; 63 received 7.5 mg/day of prednisolone and 62 received placebo for 6 weeks .33Outcome measures included pain reduction and improvement in function scores and systemic inflammation markers .34Pain was assessed using the visual analog pain scale ( 0-100 mm ) .35Secondary outcome measures included the Western Ontario and McMaster Universities Osteoarthritis Index scores , patient global assessment ( PGA ) of the severity of knee OA , and 6-min walk distance ( 6MWD ) .36Serum levels of interleukin 1 ( IL-1 ) , IL-6 , tumor necrosis factor ( TNF ) - , and high-sensitivity C-reactive protein ( hsCRP ) were measured .37There was a clinically relevant reduction in the intervention group compared to the placebo group for knee pain , physical function , PGA , and 6MWD at 6 weeks .38The mean difference between treatment arms ( 95 % CI ) was 10.9 ( 4.8-18 .0 ) , p < 0.001 ; 9.5 ( 3.7-15 .4 ) , p < 0.05 ; 15.7 ( 5.3-26 .1 ) , p < 0.001 ; and 86.9 ( 29.8-144 .1 ) , p < 0.05 , respectively .39Further , there was a clinically relevant reduction in the serum levels of IL-1 , IL-6 , TNF - , and hsCRP at 6 weeks in the intervention group when compared to the placebo group .40These differences remained significant at 12 weeks .41The Outcome Measures in Rheumatology Clinical Trials-Osteoarthritis Research Society International responder rate was 65 % in the intervention group and 34 % in the placebo group ( p < 0.05 ) .42Low-dose oral prednisolone had both a short-term and a longer sustained effect resulting in less knee pain , better physical function , and attenuation of systemic inflammation in older patients with knee OA ( ClinicalTrials.gov identifier NCT01619163 ) ."""43sample_list.append(example1)44sample_list.append(example2)45 46def format_non_empty_lists(objective, background, methods, results, conclusion):47    """48    This function checks each provided list and formats a string with the list name and its contents49    only if the list is not empty.50    51    Parameters:52    - objective (list): List containing sentences classified as 'Objective'.53    - background (list): List containing sentences classified as 'Background'.54    - methods (list): List containing sentences classified as 'Methods'.55    - results (list): List containing sentences classified as 'Results'.56    - conclusion (list): List containing sentences classified as 'Conclusion'.57    58    Returns:59    - str: A formatted string that contains the non-empty list names and their contents.60    """61    62    output = ""63    lists = {64        'Objective': objective,65        'Background': background,66        'Methods': methods,67        'Results': results,68        'Conclusion': conclusion69    }70    71    for name, content in lists.items():72        if content:  # Check if the list is not empty73            output += f"{name}:\n"  # Append the category name followed by a newline74            for item in content:75                output += f"  - {item}\n"  # Append each item in the list, formatted as a list76            77            output += "\n"  # Append a newline for better separation between categories78 79    return output.strip() 80 81def fragment_single_abstract(abstract):82    """83    Processes a single abstract by fragmenting it into structured sections based on predefined categories84    such as Objective, Methods, Results, Conclusions, and Background. The function utilizes a pre-trained Keras model85    to predict the category of each sentence in the abstract.86 87    The process involves several steps:88    1. Splitting the abstract into sentences.89    2. Encoding these sentences using a custom embedding layer.90    3. Classifying each sentence into one of the predefined categories.91    4. Grouping the sentences by their predicted categories.92 93    Parameters:94    abstract (str): The abstract text that needs to be processed and categorized.95 96    Returns:97    tuple: A tuple containing two elements:98        - A dictionary with keys as the category names ('Objective', 'Background', 'Methods', 'Results', 'Conclusions')99          and values as lists of sentences belonging to these categories. Only non-empty categories are returned.100        - The time taken to process the abstract (in seconds).101 102    Example:103    ```python104    abstract_text = "This study aims to evaluate the effectiveness of..."105    categorized_abstract, processing_time = fragment_single_abstract(abstract_text)106    print("Categorized Abstract:", categorized_abstract)107    print("Processing Time:", processing_time)108    ```109 110    Note:111    - This function assumes that a Keras model 'test.keras' and a custom embedding layer 'EmbeddingLayer'112      are available and correctly configured to be loaded.113    - The function uses pandas for data manipulation, TensorFlow for machine learning operations,114      and TensorFlow's data API for batching and prefetching data for model predictions.115    """116    start_time = timer()117 118    original_abstract = split_abstract_original(abstract)119    df_original = pd.DataFrame(original_abstract)120    sentences_original = df_original["text"].tolist()121 122    abstract_split = split_abstract(abstract)123    df = pd.DataFrame(abstract_split)124    sentences = df["text"].tolist()125    labels = encode_labels(df["target"])126 127    objective = []128    background = []129    methods = []130    results = []131    conclusion = []132 133    embed_layer = EmbeddingLayer()134    model = tf.keras.models.load_model("200k_10_epochs.keras", custom_objects={'EmbeddingLayer': embed_layer})135 136    data_by_character = split_sentences_by_characters(sentences)137    line_numbers = tf.one_hot(df["line_number"].to_numpy(), depth=15)138    total_line_numbers = tf.one_hot(df["total_lines"].to_numpy(), depth=20)139    140    sentences_dataset = tf.data.Dataset.from_tensor_slices((line_numbers, total_line_numbers, sentences, data_by_character))141    labels_dataset = tf.data.Dataset.from_tensor_slices(labels) 142    dataset = tf.data.Dataset.zip((sentences_dataset, labels_dataset)).batch(32).prefetch(tf.data.AUTOTUNE)143 144    predictions = tf.argmax(model.predict(dataset), axis=1)145 146    for i, prediction in enumerate(predictions):147        if prediction == 3:148            objective.append(sentences_original[i])149        elif prediction == 2:150            methods.append(sentences_original[i])151        elif prediction == 4:152            results.append(sentences_original[i])153        elif prediction == 1:154            conclusion.append(sentences_original[i])155        elif prediction == 0:156            background.append(sentences_original[i])157 158    end_time = timer()159 160    return format_non_empty_lists(objective, background, methods, results, conclusion), end_time - start_time161 162 163 164title = "Paper Abstract Fragmentation With TensorFlow by Ryan Tietjen"165description = f"""166This app will take the abstract of a paper and break it down into five categories: objective, background, methods, results, and conclusion. 167The dataset used can be found in the [PubMed 200k RCT]("https://arxiv.org/pdf/1710.06071") and in [this repo](https://github.com/Franck-Dernoncourt/pubmed-rct). The model architecture168was based off of ["Neural Networks for Joint Sentence Classification in Medical Paper Abstracts."](https://arxiv.org/pdf/1612.05251)169 170This model achieved a testing accuracy of 88.2% and a F1 score of 88%. For the whole project, please visit [my GitHub](https://github.com/RyanTietjen/Paper-Fragmentation).171 172How to use:173 174-Paste the given abstract into the box below.175 176-Make sure to separate each sentence by a new line (this helps avoid ambiguity).177 178-Click submit, and allow the model to run!179"""180 181demo = gr.Interface(182    fn=fragment_single_abstract,183    inputs=gr.Textbox(lines=10, placeholder="Enter abstract here..."),184    outputs=[185        gr.Textbox(label="Fragmented Abstract"),186        gr.Number(label="Time to process (s)"),187    ],188    examples=sample_list,189    title=title,190    description=description,191)192 193 194demo.launch(share=False)