File size: 3,152 Bytes
1277820
d062874
 
b542780
 
03c2a65
b542780
 
 
 
 
 
7489434
b542780
 
 
 
 
 
 
fcd755f
 
b542780
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#pip install transformers 


from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, HfArgumentParser,TrainingArguments,pipeline, logging, TextStreamer, MistralForCausalLM
from peft import LoraConfig, PeftModel, prepare_model_for_kbit_training, get_peft_model,AutoPeftModelForCausalLM
import os,torch, platform, warnings
from datasets import load_dataset
from trl import SFTTrainer
from huggingface_hub import notebook_login
import fire
import streamlit as st

#git clone https://huggingface.co/spaces/J4Lee/RadiantScriptor


st.set_page_config(page_title= "Reports generation from Radiological Image ")

@st.cache(allow_output_mutation=True)
def get_model():
    #device = "cuda" # the device to load the model onto
    model = AutoModelForCausalLM.from_pretrained("MariamAde/Mistral_finetuned_base2")
    tokenizer = AutoTokenizer.from_pretrained("MariamAde/Mistral_finetuned_base2")
    return tokenizer, model
    


tokenizer, model = get_model()

def generate_report(labels): #,model,tokenizer):
    # Tokenize the input labels
    inputs = tokenizer(labels, return_tensors="pt") #.to(device)
    #model.to(device)
    # Generate output using the model
    output = model.generate(**inputs)
    # Decode the output sentences
    sentences = tokenizer.decode(output[0], skip_special_tokens=True)
    return sentences

# Streamlit interface
st.title("Radiology Report Generator")

# User input for finding labels
labels = st.text_input("Enter Finding Labels:")


if st.button("Generate Report"):

    # Generate the radiology report
    report = generate_report(labels) #,model,tokenizer)
    # Display the report
    st.text_area("Generated Report:", value=report, height=300)


    
    
    # option 1) Mistral Usage tip 
    
# @st.cache(allow_output_mutation=True)
# def get_model():
#     #device = "cuda" # the device to load the model onto
#     model = AutoModelForCausalLM.from_pretrained("MariamAde/Mistral_finetuned_v2")
#     tokenizer = AutoTokenizer.from_pretrained("MariamAde/Mistral_finetuned_v2")
#     return tokenizer, model
    
    
    # option 2) 
    
# @st.cache(allow_output_mutation=True)
# def get_model():
#     tokenizer = LlamaTokenizer.from_pretrained("J4Lee/Medalpaca_finetuned_test")
#     model = MistralForCausalLM.from_pretrained("J4Lee/Medalpaca_finetuned_test")
#     return tokenizer, model



    # option 3) 
    
# @st.cache(allow_output_mutation=True)
# def get_model():
#     base_model, new_model = "mistralai/Mistral-7B-v0.1" , "inferenceanalytics/radmistral_7b"

#     base_model_reload = AutoModelForCausalLM.from_pretrained(
#         base_model, low_cpu_mem_usage=True,
#         return_dict=True,torch_dtype=torch.bfloat16,
#         device_map= 'auto')
    
#     model = PeftModel.from_pretrained(base_model_reload, new_model)
#     model = merged_model.merge_and_unload()

#     # Reload tokenizer
#     tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)
#     tokenizer.pad_token = tokenizer.eos_token
#     tokenizer.padding_side = "right"
#     return tokenizer, model

# DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# DEVICE