File size: 5,896 Bytes
4b247a4
 
064f6be
 
47db4ab
4b247a4
064f6be
 
 
 
 
 
 
f981774
064f6be
 
 
f981774
5f89689
064f6be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4154a1a
064f6be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47db4ab
064f6be
 
 
15fd1d7
 
d0ef7ad
eab0404
 
d0ef7ad
 
 
 
 
 
064f6be
aff925f
d0ef7ad
15fd1d7
 
4b247a4
f3168bc
4b247a4
39c823d
4b247a4
064f6be
4b247a4
 
064f6be
4b247a4
 
3b1a934
 
 
 
4b247a4
3b1a934
 
 
 
b29b7b1
4b247a4
064f6be
4b247a4
 
3b1a934
 
4b247a4
064f6be
b29b7b1
4b247a4
5f89689
064f6be
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
import gradio as gr
import torch
from peft import AutoPeftModelForCausalLM
from transformers import AutoTokenizer, GenerationConfig
import spaces 

# Load model and tokenizer
model_name = "nafisneehal/Llama-3.2-3B-bnb-4bit-finetuned-TrialBrain-BaselineFeatures-it"
load_in_4bit = True  # Set this to True as per your code requirements

# Initialize model and tokenizer
model = AutoPeftModelForCausalLM.from_pretrained(model_name, load_in_4bit=load_in_4bit)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Set device
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)

# Default values for system and user input
test_instruction_string = """
You're a clinical trial expert. For each trial query, list probable baseline features (each in backticks and comma-separated).
Baseline features are demographic characteristics used in primary outcome analysis, often shown by group in clinical publications.
"""

test_input_string = """
<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.

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:

* Histologically confirmed advanced solid tumor

  * Any histology allowed
* Refractory to standard therapy OR no standard therapy exists

  * Previously untreated non-small cell lung cancer allowed, provided abnormal liver function is present, defined as moderate (group 3) or severe (group 4)
* Measurable disease not required

  * Present measurable disease requires baseline measurements within 4 weeks of study entry
* Patients with acute hepatitis from viral or drug etiologies should recover to a stable baseline prior to study therapy
* History of brain metastasis allowed, provided the following criteria are met:

  * Metastasis has been controlled by radiotherapy or surgery
  * Patient is not currently on corticosteroids
  * Neurologic status is stable

PATIENT CHARACTERISTICS:

* Karnofsky performance status 70-100%
* Life expectancy ≥ 2 months
* ANC = 1,500/mm³
* Platelet count = 100,000/mm³
* Hemoglobin = 10 g/dL (transfusion to this level allowed)
* Creatinine \< 1.5 mg/dL OR creatinine clearance \> 60 mL/ min
* 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
* Not pregnant or nursing
* Negative pregnancy test
* Fertile patients must use effective contraception
* 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
* Must have ability to comply with study treatment and required tests
* Obstructive jaundice requires a drainage procedure prior to study treatment

PRIOR CONCURRENT THERAPY:

* See Disease Characteristics
* Recovered from prior therapy
* At least 3 weeks since prior chemotherapy (6 weeks for mitomycin or nitrosourea therapy)
* 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
"""

# Function to generate response
@spaces.GPU
def generate_response(system_instruction, user_input):
    # Prepare the input and move it to the correct device
    inputs = tokenizer([f"### Instruction:\n{system_instruction}\n### Input:\n{user_input}\n### Response:\n"], return_tensors="pt").to(device)

    # Define generation configuration
    meta_config = {
        "do_sample": False,
        "temperature": 0.0,
        "max_new_tokens": 256,
        "repetition_penalty": 1.2,
        "use_cache": True 
    }
    generation_config = GenerationConfig(**meta_config)

    # Generate response
    with torch.no_grad():
        outputs = model.generate(**inputs, generation_config=generation_config)
        decoded_output = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
        assistant_response = decoded_output.split("### Response:")[-1].strip()

    return assistant_response

# Gradio interface setup
with gr.Blocks() as demo:
    gr.Markdown("# Clinical Trial Chatbot")

    with gr.Row():
        # Left column for user inputs
        with gr.Column():
            system_instruction = gr.Textbox(
                value=test_instruction_string,
                placeholder="Enter system instruction here...",
                label="System Instruction"
            )
            user_input = gr.Textbox(
                value=test_input_string,
                placeholder="Type your message here...",
                label="Your Message"
            )
            submit_btn = gr.Button("Submit")

        # Right column for bot response
        with gr.Column():
            response_display = gr.Textbox(
                label="Bot Response", interactive=False, placeholder="Response will appear here."
            )

    # Link submit button to the function
    submit_btn.click(generate_response, [system_instruction, user_input], response_display)

# Launch the app with share=True to make it public
demo.launch()