Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,75 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from
|
4 |
-
import
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
# Define variables for the model and tokenizer
|
14 |
-
model = None
|
15 |
-
tokenizer = None
|
16 |
-
|
17 |
-
def load_model(model_name):
|
18 |
-
global model, tokenizer
|
19 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
20 |
-
|
21 |
-
# Clear previous model from memory
|
22 |
-
if model:
|
23 |
-
del model
|
24 |
-
torch.cuda.empty_cache()
|
25 |
-
|
26 |
-
try:
|
27 |
-
# Load the specified model and move it to the device
|
28 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16).to(device)
|
29 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
30 |
-
print(f"Loaded model: {model_name}")
|
31 |
-
except ValueError as e:
|
32 |
-
print(f"Failed to load model '{model_name}': {e}")
|
33 |
-
model = None # Reset if loading fails
|
34 |
-
tokenizer = None
|
35 |
-
|
36 |
-
# Load the initial model
|
37 |
-
current_model_name = model_list[0]
|
38 |
-
load_model(current_model_name)
|
39 |
|
40 |
# Default values for system and user input
|
41 |
-
test_instruction_string = """
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
# Define generation configuration
|
63 |
meta_config = {
|
@@ -70,7 +82,7 @@ def generate_response(system_instruction, user_input):
|
|
70 |
}
|
71 |
generation_config = GenerationConfig(**meta_config)
|
72 |
|
73 |
-
# Generate response
|
74 |
with torch.no_grad():
|
75 |
outputs = model.generate(**inputs, generation_config=generation_config)
|
76 |
decoded_output = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
@@ -78,12 +90,12 @@ def generate_response(system_instruction, user_input):
|
|
78 |
|
79 |
return assistant_response
|
80 |
|
81 |
-
|
82 |
# Gradio interface setup
|
83 |
with gr.Blocks() as demo:
|
84 |
-
gr.Markdown("# Clinical Trial Chatbot
|
85 |
|
86 |
with gr.Row():
|
|
|
87 |
with gr.Column():
|
88 |
system_instruction = gr.Textbox(
|
89 |
value=test_instruction_string,
|
@@ -97,15 +109,14 @@ with gr.Blocks() as demo:
|
|
97 |
)
|
98 |
submit_btn = gr.Button("Submit")
|
99 |
|
|
|
100 |
with gr.Column():
|
101 |
-
model_dropdown = gr.Dropdown(choices=model_list, value=current_model_name, label="Select Model")
|
102 |
response_display = gr.Textbox(
|
103 |
label="Bot Response", interactive=False, placeholder="Response will appear here."
|
104 |
)
|
105 |
|
106 |
-
# Link
|
107 |
-
model_dropdown.change(lambda m: load_model(m), inputs=model_dropdown, outputs=[])
|
108 |
submit_btn.click(generate_response, [system_instruction, user_input], response_display)
|
109 |
|
110 |
# Launch the app with share=True to make it public
|
111 |
-
demo.launch(
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from peft import AutoPeftModelForCausalLM
|
4 |
+
from transformers import AutoTokenizer, GenerationConfig
|
5 |
|
6 |
+
# Load model and tokenizer
|
7 |
+
model_name = "nafisneehal/Llama-3.2-3B-bnb-4bit-finetuned-TrialBrain-BaselineFeatures-it"
|
8 |
+
load_in_4bit = True # Set this to True as per your code requirements
|
9 |
+
|
10 |
+
# Initialize model and tokenizer
|
11 |
+
model = AutoPeftModelForCausalLM.from_pretrained(model_name, load_in_4bit=load_in_4bit)
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
13 |
|
14 |
+
# Set device
|
15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
+
model.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# Default values for system and user input
|
19 |
+
test_instruction_string = """
|
20 |
+
You're a clinical trial expert. For each trial query, list probable baseline features (each in backticks and comma-separated).
|
21 |
+
Baseline features are demographic characteristics used in primary outcome analysis, often shown by group in clinical publications.
|
22 |
+
"""
|
23 |
+
|
24 |
+
test_input_string = """
|
25 |
+
<Title:>Vinorelbine in Treating Patients With Advanced Solid Tumors That Have Not Responded to Treatment and Liver Dysfunction <BriefSummary:>RATIONALE: Drugs used in chemotherapy, such as vinorelbine, work in different ways to stop the growth of tumor cells, either by killing the cells or by stopping them from dividing.
|
26 |
+
|
27 |
+
PURPOSE: This pilot trial is studying the side effects and best dose of vinorelbine in treating patients with advanced solid tumors that have not responded to treatment and liver dysfunction. <EligibilityCriteria:>DISEASE CHARACTERISTICS:
|
28 |
+
|
29 |
+
* Histologically confirmed advanced solid tumor
|
30 |
+
|
31 |
+
* Any histology allowed
|
32 |
+
* Refractory to standard therapy OR no standard therapy exists
|
33 |
+
|
34 |
+
* Previously untreated non-small cell lung cancer allowed, provided abnormal liver function is present, defined as moderate (group 3) or severe (group 4)
|
35 |
+
* Measurable disease not required
|
36 |
|
37 |
+
* Present measurable disease requires baseline measurements within 4 weeks of study entry
|
38 |
+
* Patients with acute hepatitis from viral or drug etiologies should recover to a stable baseline prior to study therapy
|
39 |
+
* History of brain metastasis allowed, provided the following criteria are met:
|
40 |
+
|
41 |
+
* Metastasis has been controlled by radiotherapy or surgery
|
42 |
+
* Patient is not currently on corticosteroids
|
43 |
+
* Neurologic status is stable
|
44 |
+
|
45 |
+
PATIENT CHARACTERISTICS:
|
46 |
+
|
47 |
+
* Karnofsky performance status 70-100%
|
48 |
+
* Life expectancy ≥ 2 months
|
49 |
+
* ANC = 1,500/mm³
|
50 |
+
* Platelet count = 100,000/mm³
|
51 |
+
* Hemoglobin = 10 g/dL (transfusion to this level allowed)
|
52 |
+
* Creatinine \< 1.5 mg/dL OR creatinine clearance \> 60 mL/ min
|
53 |
+
* Patients with EKG evidence of first- or second-degree AV block or left or right bundle branch block are ineligible for the lidocaine bolus, but may otherwise be treated on this protocol
|
54 |
+
* Not pregnant or nursing
|
55 |
+
* Negative pregnancy test
|
56 |
+
* Fertile patients must use effective contraception
|
57 |
+
* No concurrent illness (e.g., cardiovascular, pulmonary, or central nervous system) that is poorly controlled or of such severity that the investigator deems unwise to enter the patient on protocol
|
58 |
+
* Must have ability to comply with study treatment and required tests
|
59 |
+
* Obstructive jaundice requires a drainage procedure prior to study treatment
|
60 |
+
|
61 |
+
PRIOR CONCURRENT THERAPY:
|
62 |
+
|
63 |
+
* See Disease Characteristics
|
64 |
+
* Recovered from prior therapy
|
65 |
+
* At least 3 weeks since prior chemotherapy (6 weeks for mitomycin or nitrosourea therapy)
|
66 |
+
* No prior radiotherapy to \> 30% of the bone marrow or more than standard adjuvant pelvic radiotherapy for rectal cancer <Conditions:>Lung Cancer, Unspecified Adult Solid Tumor, Protocol Specific, <Interventions:>indocyanine green, lidocaine, vinorelbine ditartrate, high performance liquid chromatography, intracellular fluorescence polarization analysis, liquid chromatography, mass spectrometry, pharmacological study <StudyType:>INTERVENTIONAL <PrimaryOutcomes:>Area Under the Curve, Number of Participants With Grade 3 and 4 Toxicities <OverallStatus:>COMPLETED
|
67 |
+
"""
|
68 |
+
|
69 |
+
# Function to generate response
|
70 |
+
def generate_response(system_instruction, user_input):
|
71 |
+
# Prepare the input and move it to the correct device
|
72 |
+
inputs = tokenizer([f"### Instruction:\n{system_instruction}\n### Input:\n{user_input}\n### Response:\n"], return_tensors="pt").to(device)
|
73 |
|
74 |
# Define generation configuration
|
75 |
meta_config = {
|
|
|
82 |
}
|
83 |
generation_config = GenerationConfig(**meta_config)
|
84 |
|
85 |
+
# Generate response
|
86 |
with torch.no_grad():
|
87 |
outputs = model.generate(**inputs, generation_config=generation_config)
|
88 |
decoded_output = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
|
|
90 |
|
91 |
return assistant_response
|
92 |
|
|
|
93 |
# Gradio interface setup
|
94 |
with gr.Blocks() as demo:
|
95 |
+
gr.Markdown("# Clinical Trial Chatbot")
|
96 |
|
97 |
with gr.Row():
|
98 |
+
# Left column for user inputs
|
99 |
with gr.Column():
|
100 |
system_instruction = gr.Textbox(
|
101 |
value=test_instruction_string,
|
|
|
109 |
)
|
110 |
submit_btn = gr.Button("Submit")
|
111 |
|
112 |
+
# Right column for bot response
|
113 |
with gr.Column():
|
|
|
114 |
response_display = gr.Textbox(
|
115 |
label="Bot Response", interactive=False, placeholder="Response will appear here."
|
116 |
)
|
117 |
|
118 |
+
# Link submit button to the function
|
|
|
119 |
submit_btn.click(generate_response, [system_instruction, user_input], response_display)
|
120 |
|
121 |
# Launch the app with share=True to make it public
|
122 |
+
demo.launch()
|