|
import streamlit as st |
|
import pandas as pd |
|
from transformers import pipeline, AutoConfig, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM, MistralForCausalLM |
|
from peft import PeftModel, PeftConfig |
|
|
|
|
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("squarelike/llama2-ko-medical-7b", trust_remote_code=True, padding_side="left") |
|
tokenizer.pad_token = tokenizer.eos_token |
|
tokenizer.padding_side = 'left' |
|
|
|
|
|
peft_config = PeftConfig.from_pretrained("squarelike/llama2-ko-medical-7b") |
|
peft_model = MistralForCausalLM.from_pretrained("squarelike/llama2-ko-medical-7b", trust_remote_code=True) |
|
peft_model = PeftModel.from_pretrained(peft_model, "squarelike/llama2-ko-medical-7b") |
|
|
|
|
|
uploaded_file = st.file_uploader("Choose a CSV file", type="csv") |
|
|
|
|
|
def prepare_context(data): |
|
|
|
data_str = data.to_string(index=False, header=False) |
|
|
|
|
|
input_ids = tokenizer.encode(data_str, return_tensors="pt") |
|
|
|
|
|
max_length = tokenizer.model_max_length |
|
if input_ids.shape[1] > max_length: |
|
input_ids = input_ids[:, :max_length] |
|
|
|
return input_ids |
|
|
|
if uploaded_file is not None: |
|
data = pd.read_csv(uploaded_file) |
|
st.write(data) |
|
|
|
|
|
context = prepare_context(data) |
|
generated_text = pipeline('text-generation', model=model)(context)[0]['generated_text'] |
|
st.write(generated_text) |
|
|
|
|
|
prompt = "You are an Electronic Health Records analyst with nursing school training. Please analyze patient data that you are provided here. Give an organized, step-by-step, formatted health records analysis. You will always be truthful and if you do nont know the answer say you do not know." |
|
|
|
if prompt: |
|
|
|
input_ids = tokenizer.encode(prompt, return_tensors="pt") |
|
|
|
|
|
generated_text = pipeline('text-generation', model=model)(input_ids=input_ids)[0]['generated_text'] |
|
st.write(generated_text) |
|
else: |
|
st.write("Please enter patient data") |
|
|
|
else: |
|
st.write("No file uploaded") |