Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,152 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def run_search(age, sex, state, keywords):
|
| 5 |
-
|
|
|
|
| 6 |
user_age=age,
|
| 7 |
user_sex=sex,
|
| 8 |
user_state=state,
|
| 9 |
-
user_keywords=keywords
|
|
|
|
| 10 |
)
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
"
|
| 17 |
-
"
|
|
|
|
|
|
|
| 18 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
with gr.Row():
|
| 21 |
age_input = gr.Number(label="Your Age", value=30)
|
| 22 |
-
sex_input = gr.Dropdown(["Male", "Female"], label="Sex", value="
|
| 23 |
|
| 24 |
with gr.Row():
|
| 25 |
-
state_input = gr.
|
| 26 |
-
keywords_input = gr.Textbox(label="Keywords (comma separated)", placeholder="e.g.,
|
| 27 |
|
| 28 |
search_btn = gr.Button("Search Trials")
|
| 29 |
|
| 30 |
output_table = gr.Dataframe(label="Matching Trials", interactive=False)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
search_btn.click(
|
| 33 |
-
fn=
|
| 34 |
inputs=[age_input, sex_input, state_input, keywords_input],
|
| 35 |
-
outputs=output_table
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from main2 import search_trials # Your updated search_trials includes summary generation
|
| 4 |
+
|
| 5 |
+
PAGE_SIZE = 5
|
| 6 |
|
| 7 |
def run_search(age, sex, state, keywords):
|
| 8 |
+
# Run search WITHOUT generating summaries initially
|
| 9 |
+
df = search_trials(
|
| 10 |
user_age=age,
|
| 11 |
user_sex=sex,
|
| 12 |
user_state=state,
|
| 13 |
+
user_keywords=keywords,
|
| 14 |
+
generate_summaries=False # generate summaries page-wise
|
| 15 |
)
|
| 16 |
+
if df.empty:
|
| 17 |
+
return pd.DataFrame(), 0, None
|
| 18 |
+
total_pages = (len(df) + PAGE_SIZE - 1) // PAGE_SIZE
|
| 19 |
+
page_df = df.iloc[:PAGE_SIZE].copy()
|
| 20 |
+
page_df['LaymanSummary'] = "" # empty summary placeholder
|
| 21 |
+
return page_df, total_pages, df
|
| 22 |
+
|
| 23 |
+
def generate_summary_for_row(row):
|
| 24 |
+
# Use the generate_summary helper inside search_trials function, or reimplement here if needed
|
| 25 |
+
# Since generate_summary is inside search_trials, just call search_trials with generate_summaries=True on 1 row doesn't work.
|
| 26 |
+
# So, for simplicity, re-implement the summary logic here or expose generate_summary separately.
|
| 27 |
+
# But easiest: call search_trials with generate_summaries=True on page data and extract LaymanSummary.
|
| 28 |
+
# To avoid overhead, let's generate summaries for the page using search_trials with generate_summaries=True
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
def load_page(page_num, full_df):
|
| 32 |
+
if full_df is None or full_df.empty:
|
| 33 |
+
return pd.DataFrame()
|
| 34 |
+
start = page_num * PAGE_SIZE
|
| 35 |
+
end = start + PAGE_SIZE
|
| 36 |
+
page_df = full_df.iloc[start:end].copy()
|
| 37 |
+
# Generate summaries for current page only using your own generate_summary inside search_trials
|
| 38 |
+
# Since generate_summary is local inside search_trials, call search_trials with this subset and generate_summaries=True
|
| 39 |
|
| 40 |
+
# Create minimal subset dataframe similar to full_df slice for summary generation
|
| 41 |
+
page_df_with_summaries = search_trials(
|
| 42 |
+
user_age=0, # dummy values; ignored because filtering is done on df subset
|
| 43 |
+
user_sex="all",
|
| 44 |
+
user_state="all",
|
| 45 |
+
user_keywords=[],
|
| 46 |
+
generate_summaries=True
|
| 47 |
)
|
| 48 |
+
# The above won't work as is because it re-filters dataset; instead do it manually:
|
| 49 |
+
|
| 50 |
+
# Workaround: Re-apply generate_summary function here explicitly for each row
|
| 51 |
+
# Re-implement generate_summary here from your main2.py for page_df only:
|
| 52 |
+
import re
|
| 53 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 54 |
+
import numpy as np
|
| 55 |
+
|
| 56 |
+
def split_sentences(text):
|
| 57 |
+
return re.split(r'(?<=[.!?])\s+', text.strip())
|
| 58 |
+
|
| 59 |
+
def build_input_text(row):
|
| 60 |
+
text_parts = [
|
| 61 |
+
f"Intervention Name: {row.get('InterventionName', '')}",
|
| 62 |
+
f"Intervention Description: {row.get('InterventionDescription', '')}",
|
| 63 |
+
f"Brief Summary: {row.get('BriefSummary', '')}",
|
| 64 |
+
f"Primary Outcome Measure: {row.get('PrimaryOutcomeMeasure', '')}",
|
| 65 |
+
f"Primary Outcome Description: {row.get('PrimaryOutcomeDescription', '')}",
|
| 66 |
+
f"Start Date: {row.get('StartDate', '')}",
|
| 67 |
+
f"Detailed Description: {row.get('DetailedDescription', '')}",
|
| 68 |
+
]
|
| 69 |
+
return " ".join([part for part in text_parts if part.strip()])
|
| 70 |
+
|
| 71 |
+
def generate_summary(row, num_sentences=5):
|
| 72 |
+
text = build_input_text(row)
|
| 73 |
+
if not text.strip():
|
| 74 |
+
return ""
|
| 75 |
+
sentences = split_sentences(text)
|
| 76 |
+
if len(sentences) <= num_sentences:
|
| 77 |
+
return " ".join(sentences)
|
| 78 |
+
vectorizer = TfidfVectorizer(stop_words="english")
|
| 79 |
+
tfidf_matrix = vectorizer.fit_transform(sentences)
|
| 80 |
+
scores = np.array(tfidf_matrix.sum(axis=1)).flatten()
|
| 81 |
+
top_indices = scores.argsort()[-num_sentences:][::-1]
|
| 82 |
+
top_indices = sorted(top_indices)
|
| 83 |
+
summary_sentences = [sentences[i] for i in top_indices]
|
| 84 |
+
return " ".join(summary_sentences)
|
| 85 |
+
|
| 86 |
+
page_df['LaymanSummary'] = page_df.apply(generate_summary, axis=1)
|
| 87 |
+
return page_df
|
| 88 |
+
|
| 89 |
+
def update_page_controls(page_num, total_pages):
|
| 90 |
+
prev_visible = gr.update(visible=page_num > 0)
|
| 91 |
+
next_visible = gr.update(visible=page_num < total_pages - 1)
|
| 92 |
+
page_text = f"Page {page_num + 1} of {total_pages}" if total_pages > 0 else ""
|
| 93 |
+
return prev_visible, next_visible, page_text
|
| 94 |
+
|
| 95 |
+
def on_search(age, sex, state, keywords):
|
| 96 |
+
df_page, total_pages, full_df = run_search(age, sex, state, keywords)
|
| 97 |
+
page_num = 0
|
| 98 |
+
if not df_page.empty:
|
| 99 |
+
df_page = load_page(page_num, full_df)
|
| 100 |
+
prev_vis, next_vis, page_text = update_page_controls(page_num, total_pages)
|
| 101 |
+
return df_page, page_text, prev_vis, next_vis, page_num, total_pages, full_df
|
| 102 |
+
|
| 103 |
+
def on_page_change(increment, page_num, total_pages, full_df):
|
| 104 |
+
if full_df is None or full_df.empty:
|
| 105 |
+
return pd.DataFrame(), "", gr.update(visible=False), gr.update(visible=False), 0
|
| 106 |
+
new_page = max(0, min(page_num + increment, total_pages - 1))
|
| 107 |
+
page_df = load_page(new_page, full_df)
|
| 108 |
+
prev_vis, next_vis, page_text = update_page_controls(new_page, total_pages)
|
| 109 |
+
return page_df, page_text, prev_vis, next_vis, new_page
|
| 110 |
+
|
| 111 |
+
with gr.Blocks() as demo:
|
| 112 |
+
gr.Markdown("# Clinical Trials Search Tool with Pagination")
|
| 113 |
|
| 114 |
with gr.Row():
|
| 115 |
age_input = gr.Number(label="Your Age", value=30)
|
| 116 |
+
sex_input = gr.Dropdown(["Male", "Female", "All"], label="Sex", value="All")
|
| 117 |
|
| 118 |
with gr.Row():
|
| 119 |
+
state_input = gr.Textbox(label="State (full name or abbreviation)", placeholder="e.g., California")
|
| 120 |
+
keywords_input = gr.Textbox(label="Keywords (comma separated)", placeholder="e.g., Cancer, Diabetes")
|
| 121 |
|
| 122 |
search_btn = gr.Button("Search Trials")
|
| 123 |
|
| 124 |
output_table = gr.Dataframe(label="Matching Trials", interactive=False)
|
| 125 |
|
| 126 |
+
total_pages_text = gr.Textbox(value="", interactive=False)
|
| 127 |
+
prev_btn = gr.Button("Previous Page")
|
| 128 |
+
next_btn = gr.Button("Next Page")
|
| 129 |
+
|
| 130 |
+
page_num_state = gr.State(0)
|
| 131 |
+
total_pages_state = gr.State(0)
|
| 132 |
+
full_results_state = gr.State(None)
|
| 133 |
+
|
| 134 |
search_btn.click(
|
| 135 |
+
fn=on_search,
|
| 136 |
inputs=[age_input, sex_input, state_input, keywords_input],
|
| 137 |
+
outputs=[output_table, total_pages_text, prev_btn, next_btn, page_num_state, total_pages_state, full_results_state]
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
next_btn.click(
|
| 141 |
+
fn=on_page_change,
|
| 142 |
+
inputs=[gr.State(1), page_num_state, total_pages_state, full_results_state],
|
| 143 |
+
outputs=[output_table, total_pages_text, prev_btn, next_btn, page_num_state]
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
prev_btn.click(
|
| 147 |
+
fn=on_page_change,
|
| 148 |
+
inputs=[gr.State(-1), page_num_state, total_pages_state, full_results_state],
|
| 149 |
+
outputs=[output_table, total_pages_text, prev_btn, next_btn, page_num_state]
|
| 150 |
)
|
| 151 |
|
| 152 |
if __name__ == "__main__":
|