kenlkehl commited on
Commit
8a74c5f
·
verified ·
1 Parent(s): 0e923ad

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -27
app.py CHANGED
@@ -24,13 +24,6 @@ checker_pipe = pipeline('text-classification', 'ksg-dfci/TrialChecker', tokenize
24
 
25
 
26
 
27
- import gradio as gr
28
- import pandas as pd
29
- import torch
30
- import torch.nn.functional as F
31
- import tempfile
32
-
33
-
34
  import gradio as gr
35
  import pandas as pd
36
  import torch
@@ -43,7 +36,7 @@ import tempfile
43
  #
44
  # For example:
45
  # trial_spaces = pd.read_csv("some_file.csv")
46
- # embedding_model = SentenceTransformer("model-name")
47
  # trial_space_embeddings = torch.load("trial_space_embeddings.pt")
48
  # checker_pipe = pipeline(...)
49
  # etc.
@@ -51,7 +44,7 @@ import tempfile
51
  def match_clinical_trials_dropdown(patient_summary: str):
52
  """
53
  1) Runs the trial matching logic.
54
- 2) Returns a dict for the dropdown ({"choices": [...], "value": None}),
55
  plus a DataFrame for further use.
56
  """
57
  # 1. Encode user input
@@ -104,34 +97,31 @@ def match_clinical_trials_dropdown(patient_summary: str):
104
  'trial_checker_score'
105
  ]]
106
 
107
- # Build the dropdown choices, e.g. "NCT001 - Some Title"
108
  dropdown_options = []
109
  for _, row in out_df.iterrows():
110
  option_str = f"{row['nct_id']} - {row['trial_title']}"
111
  dropdown_options.append(option_str)
112
 
113
- # Return a dict that sets "choices" and resets "value" to None
114
- dropdown_dict = {
115
- "choices": dropdown_options,
116
- "value": None
117
- }
118
 
119
- # Returning (dropdown_dict, out_df) means the first output updates the dropdown,
120
- # the second output is stored in state.
121
- return dropdown_dict, out_df
122
 
123
  def show_selected_trial(selected_option: str, df: pd.DataFrame):
124
  """
125
- Given the selected dropdown option (e.g. "NCT001 - Some Title")
126
- and the DataFrame, find the row and build a text summary.
127
  """
128
  if not selected_option:
129
  return ""
130
 
131
- # Extract NCT ID from "NCT001 - Some Title"
132
  nct_id = selected_option.split(" - ")[0].strip()
133
 
134
- # Find the matching row
135
  row = df[df['nct_id'] == nct_id]
136
  if row.empty:
137
  return "No data found for the selected trial."
@@ -151,12 +141,13 @@ def show_selected_trial(selected_option: str, df: pd.DataFrame):
151
 
152
  def export_results(df: pd.DataFrame):
153
  """
154
- Saves the DataFrame to a temporary CSV file so Gradio can provide a download link.
155
  """
156
  temp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
157
  df.to_csv(temp.name, index=False)
158
  return temp.name
159
 
 
160
  custom_css = """
161
  #input_box textarea {
162
  width: 600px !important;
@@ -185,7 +176,7 @@ with gr.Blocks(css=custom_css) as demo:
185
  # We'll store the DataFrame in a State for CSV export + reference
186
  results_state = gr.State()
187
 
188
- # Dropdown: no initial choices, no value
189
  trial_dropdown = gr.Dropdown(
190
  label="Select a Trial",
191
  choices=[],
@@ -204,14 +195,14 @@ with gr.Blocks(css=custom_css) as demo:
204
  export_btn = gr.Button("Export Results")
205
  download_file = gr.File()
206
 
207
- # 1) "Find Matches" => set dropdown's choices and store the DataFrame
208
  submit_btn.click(
209
  fn=match_clinical_trials_dropdown,
210
  inputs=patient_summary_input,
211
  outputs=[trial_dropdown, results_state]
212
  )
213
 
214
- # 2) On dropdown change => show details
215
  trial_dropdown.change(
216
  fn=show_selected_trial,
217
  inputs=[trial_dropdown, results_state],
@@ -225,7 +216,7 @@ with gr.Blocks(css=custom_css) as demo:
225
  outputs=download_file
226
  )
227
 
228
- # Enable queue so "Processing..." shows if logic is slow
229
  demo.queue()
230
 
231
  if __name__ == "__main__":
 
24
 
25
 
26
 
 
 
 
 
 
 
 
27
  import gradio as gr
28
  import pandas as pd
29
  import torch
 
36
  #
37
  # For example:
38
  # trial_spaces = pd.read_csv("some_file.csv")
39
+ # embedding_model = SentenceTransformer("model-name", device="cuda")
40
  # trial_space_embeddings = torch.load("trial_space_embeddings.pt")
41
  # checker_pipe = pipeline(...)
42
  # etc.
 
44
  def match_clinical_trials_dropdown(patient_summary: str):
45
  """
46
  1) Runs the trial matching logic.
47
+ 2) Returns a gr.update(...) for the dropdown (setting its choices),
48
  plus a DataFrame for further use.
49
  """
50
  # 1. Encode user input
 
97
  'trial_checker_score'
98
  ]]
99
 
100
+ # Build the dropdown choices, e.g., "NCT001 - Some Title"
101
  dropdown_options = []
102
  for _, row in out_df.iterrows():
103
  option_str = f"{row['nct_id']} - {row['trial_title']}"
104
  dropdown_options.append(option_str)
105
 
106
+ # Return an update for the dropdown (choices + clear any initial value)
107
+ dropdown_update = gr.Dropdown.update(
108
+ choices=dropdown_options,
109
+ value=None # ensures we don't default to any item not in the list
110
+ )
111
 
112
+ return dropdown_update, out_df
 
 
113
 
114
  def show_selected_trial(selected_option: str, df: pd.DataFrame):
115
  """
116
+ 1) Given the selected dropdown option, e.g. "NCT001 - Some Title"
117
+ 2) Find the row in df and build a summary string.
118
  """
119
  if not selected_option:
120
  return ""
121
 
122
+ # Parse NCT ID from "NCT001 - Some Title"
123
  nct_id = selected_option.split(" - ")[0].strip()
124
 
 
125
  row = df[df['nct_id'] == nct_id]
126
  if row.empty:
127
  return "No data found for the selected trial."
 
141
 
142
  def export_results(df: pd.DataFrame):
143
  """
144
+ Saves the DataFrame to a temporary CSV file so Gradio can provide a downloadable link.
145
  """
146
  temp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
147
  df.to_csv(temp.name, index=False)
148
  return temp.name
149
 
150
+ # A little CSS for the input box
151
  custom_css = """
152
  #input_box textarea {
153
  width: 600px !important;
 
176
  # We'll store the DataFrame in a State for CSV export + reference
177
  results_state = gr.State()
178
 
179
+ # Dropdown (initially empty)
180
  trial_dropdown = gr.Dropdown(
181
  label="Select a Trial",
182
  choices=[],
 
195
  export_btn = gr.Button("Export Results")
196
  download_file = gr.File()
197
 
198
+ # 1) "Find Matches" => updates the dropdown choices and the state
199
  submit_btn.click(
200
  fn=match_clinical_trials_dropdown,
201
  inputs=patient_summary_input,
202
  outputs=[trial_dropdown, results_state]
203
  )
204
 
205
+ # 2) Selecting from the dropdown => shows more info
206
  trial_dropdown.change(
207
  fn=show_selected_trial,
208
  inputs=[trial_dropdown, results_state],
 
216
  outputs=download_file
217
  )
218
 
219
+ # Enable queue so "Processing..." is shown if logic is slow
220
  demo.queue()
221
 
222
  if __name__ == "__main__":