File size: 10,229 Bytes
f343cdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbaf100
 
 
 
 
 
 
 
 
 
883e4d2
 
db975bb
883e4d2
 
 
 
 
 
 
 
 
f343cdb
db975bb
f343cdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac0125e
f343cdb
 
 
 
 
 
 
 
 
 
 
 
99d83cb
f343cdb
d00a519
f343cdb
99d83cb
f343cdb
d00a519
65a6124
 
f343cdb
 
 
 
 
 
 
 
 
 
 
9f0c4fb
f343cdb
 
ac0125e
f343cdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d85b3ec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""
Ryan Tietjen
Sep 2024
Demo application for paper abstract fragmentaion demonstration
"""
import gradio as gr
import tensorflow as tf
from tensorflow import keras
from keras import layers
from timeit import default_timer as timer
from process_input import split_abstract
from process_input import split_abstract_original
from process_input import split_sentences_by_characters
import pandas as pd
import tensorflow_hub as hub
from model import EmbeddingLayer
from process_input import encode_labels


sample_list = []
example1 =  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.
Forty-three bruxism patients were randomly allocated to 3 groups : acupuncture , needle contact for 10 seconds , no treatment ( control ).
Helkimo 's clinical dysfunction index ( CDI ) and anamnestic dysfunction index ( ADI ) were used to assess the functional state of the masticatory system.
The 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 ).
The electromyographical variations in the 3 groups were studied with t test for independent samples.
Acupuncture and needle contact were superior to control in reducing the muscle hypertonicity of all muscles except SCM.
In the comparison between acupuncture and needle contact the former showed better results only for the right TA and left DA ( p = 0.000 ).
In 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.
The auricular area we chose for stimulation was never used before for the purpose of relaxing masticatory muscles.
Acupuncture and needle contact for 10 seconds showed similar effects."""
example2 = """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 ) .
A 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 .
Outcome measures included pain reduction and improvement in function scores and systemic inflammation markers .
Pain was assessed using the visual analog pain scale ( 0-100 mm ) .
Secondary 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 ) .
Serum levels of interleukin 1 ( IL-1 ) , IL-6 , tumor necrosis factor ( TNF ) - , and high-sensitivity C-reactive protein ( hsCRP ) were measured .
There 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 .
The 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 .
Further , 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 .
These differences remained significant at 12 weeks .
The 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 ) .
Low-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 ) ."""
sample_list.append(example1)
sample_list.append(example2)

def format_non_empty_lists(objective, background, methods, results, conclusion):
    """
    This function checks each provided list and formats a string with the list name and its contents
    only if the list is not empty.
    
    Parameters:
    - objective (list): List containing sentences classified as 'Objective'.
    - background (list): List containing sentences classified as 'Background'.
    - methods (list): List containing sentences classified as 'Methods'.
    - results (list): List containing sentences classified as 'Results'.
    - conclusion (list): List containing sentences classified as 'Conclusion'.
    
    Returns:
    - str: A formatted string that contains the non-empty list names and their contents.
    """
    
    output = ""
    lists = {
        'Objective': objective,
        'Background': background,
        'Methods': methods,
        'Results': results,
        'Conclusion': conclusion
    }
    
    for name, content in lists.items():
        if content:  # Check if the list is not empty
            output += f"{name}:\n"  # Append the category name followed by a newline
            for item in content:
                output += f"  - {item}\n"  # Append each item in the list, formatted as a list
            
            output += "\n"  # Append a newline for better separation between categories

    return output.strip() 

def fragment_single_abstract(abstract):
    """
    Processes a single abstract by fragmenting it into structured sections based on predefined categories
    such as Objective, Methods, Results, Conclusions, and Background. The function utilizes a pre-trained Keras model
    to predict the category of each sentence in the abstract.

    The process involves several steps:
    1. Splitting the abstract into sentences.
    2. Encoding these sentences using a custom embedding layer.
    3. Classifying each sentence into one of the predefined categories.
    4. Grouping the sentences by their predicted categories.

    Parameters:
    abstract (str): The abstract text that needs to be processed and categorized.

    Returns:
    tuple: A tuple containing two elements:
        - A dictionary with keys as the category names ('Objective', 'Background', 'Methods', 'Results', 'Conclusions')
          and values as lists of sentences belonging to these categories. Only non-empty categories are returned.
        - The time taken to process the abstract (in seconds).

    Example:
    ```python
    abstract_text = "This study aims to evaluate the effectiveness of..."
    categorized_abstract, processing_time = fragment_single_abstract(abstract_text)
    print("Categorized Abstract:", categorized_abstract)
    print("Processing Time:", processing_time)
    ```

    Note:
    - This function assumes that a Keras model 'test.keras' and a custom embedding layer 'EmbeddingLayer'
      are available and correctly configured to be loaded.
    - The function uses pandas for data manipulation, TensorFlow for machine learning operations,
      and TensorFlow's data API for batching and prefetching data for model predictions.
    """
    start_time = timer()

    original_abstract = split_abstract_original(abstract)
    df_original = pd.DataFrame(original_abstract)
    sentences_original = df_original["text"].tolist()

    abstract_split = split_abstract(abstract)
    df = pd.DataFrame(abstract_split)
    sentences = df["text"].tolist()
    labels = encode_labels(df["target"])

    objective = []
    background = []
    methods = []
    results = []
    conclusion = []

    embed_layer = EmbeddingLayer()
    model = tf.keras.models.load_model("200k_10_epochs.keras", custom_objects={'EmbeddingLayer': embed_layer})

    data_by_character = split_sentences_by_characters(sentences)
    line_numbers = tf.one_hot(df["line_number"].to_numpy(), depth=15)
    total_line_numbers = tf.one_hot(df["total_lines"].to_numpy(), depth=20)
    
    sentences_dataset = tf.data.Dataset.from_tensor_slices((line_numbers, total_line_numbers, sentences, data_by_character))
    labels_dataset = tf.data.Dataset.from_tensor_slices(labels) 
    dataset = tf.data.Dataset.zip((sentences_dataset, labels_dataset)).batch(32).prefetch(tf.data.AUTOTUNE)

    predictions = tf.argmax(model.predict(dataset), axis=1)

    for i, prediction in enumerate(predictions):
        if prediction == 3:
            objective.append(sentences_original[i])
        elif prediction == 2:
            methods.append(sentences_original[i])
        elif prediction == 4:
            results.append(sentences_original[i])
        elif prediction == 1:
            conclusion.append(sentences_original[i])
        elif prediction == 0:
            background.append(sentences_original[i])

    end_time = timer()

    return format_non_empty_lists(objective, background, methods, results, conclusion), end_time - start_time



title = "Paper Abstract Fragmentation With TensorFlow by Ryan Tietjen"
description = f"""
This app will take the abstract of a paper and break it down into five categories: objective, background, methods, results, and conclusion. 
The 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 architecture
was based off of ["Neural Networks for Joint Sentence Classification in Medical Paper Abstracts."](https://arxiv.org/pdf/1612.05251)

This 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).

How to use:

-Paste the given abstract into the box below.

-Make sure to separate each sentence by a new line (this helps avoid ambiguity).

-Click submit, and allow the model to run!
"""

demo = gr.Interface(
    fn=fragment_single_abstract,
    inputs=gr.Textbox(lines=10, placeholder="Enter abstract here..."),
    outputs=[
        gr.Textbox(label="Fragmented Abstract"),
        gr.Number(label="Time to process (s)"),
    ],
    examples=sample_list,
    title=title,
    description=description,
)


demo.launch(share=False)