-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunLLM.py
More file actions
217 lines (170 loc) · 7.63 KB
/
RunLLM.py
File metadata and controls
217 lines (170 loc) · 7.63 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from radprompter import Prompt, RadPrompter, vLLMClient, OllamaClient, OpenAIClient
import os
import pandas as pd
import pandas as pd
import numpy as np
import pandas as pd
if __name__ == '__main__':
import argparse
# Set up command line argument parsing
parser = argparse.ArgumentParser(description="Run LLM model inference")
parser.add_argument('--model', type=str, default='llama3', help='Model name to use')
parser.add_argument('--server_port', type=int, default=1234, help='Server port number')
parser.add_argument('--interval', type=int, default=10, help='Interval for processing and negative value skips inference altogether')
parser.add_argument('--subset', type=str, default='', help='process a subset of rows. specify as <first>:<last> where those values are integers')
# Parse arguments
args = parser.parse_args()
MODEL = args.model
SERVER_PORT = args.server_port
INTERVAL = args.interval
SUBSET_STR = args.subset
if SUBSET_STR != '':
START_LINE, END_LINE = map(int, SUBSET_STR.split(':'))
else:
START_LINE = 0
END_LINE = -1
MODEL = "ruslanmv/Medical-Llama3-8B-GGUF"
INTERVAL = 200
prompt = Prompt("SIIM.toml")
# MODEL = 'mixtral:8x22b'
# get user's home directory in OS independent way
home = os.path.expanduser("~")
INPUT_FILE = home + '/Desktop/SIIMCombinedReports.xlsx'
model_name = MODEL.split("/")
OUTPUT_FILE = 'SIIM_Results_' + model_name[-1] + '.csv'
TEMP_OUT = 'output.csv'
if SERVER_PORT == 10000:
# use this command to port-forward from a server (roqril0006a) running vllm which will then appear on localhost port 10000:
# ssh -N -L localhost:10000:localhost:8000 bje01@roqril006a&
client = vLLMClient(
model=MODEL,
base_url="http://localhost:10000/v1",
temperature=0.0,
seed=42
)
elif SERVER_PORT == 11434:
client = OllamaClient(
model=MODEL,
base_url="http://localhost:11434/v1",
api_key="ollama",
temperature=0.0,
seed=42,
)
elif SERVER_PORT == 1234:
client = OpenAIClient(
model=MODEL,
base_url="http://localhost:1234/v1",
temperature=0.0,
api_key="lm-studio",
seed=42,
)
elif SERVER_PORT == 0:
client = OpenAIClient(
model="GPT-4o",
api_key="",
temperature=0.0,
seed=42
)
else:
print (f"Error--unknown server port {SERVER_PORT}")
exit(-1)
print (f"Using port {SERVER_PORT} with model {MODEL}.\nOutput will be {OUTPUT_FILE}")
# delete any prior output
if os.path.exists(TEMP_OUT):
os.remove(TEMP_OUT)
engine = RadPrompter(
client=client,
prompt=prompt,
hide_blocks=True,
output_file=TEMP_OUT,
)
#---
# now read in the reports from SIIMCombinedReports.xlsx into a pandas dataframe
# Load the Excel file into a DataFrame
reports_df = pd.read_excel(INPUT_FILE)
if END_LINE != -1: # they specified a subset
reports_df = reports_df.iloc[START_LINE:END_LINE]
if INTERVAL > 1: #specified a skip
reports_df = reports_df.iloc[::INTERVAL]
# strip spaces out of the FIndings column
reports_df['Findings'] = reports_df['Findings'].str.replace(' ', '')
# Remove any instance of '_x000D_' in the entire dataframe
reports_df = reports_df.replace('_x000D_', '', regex=True)
# Remove all newline characters and replace multiple spaces with a single space in the 'Report' column
reports_df['Report'] = reports_df['Report'].str.replace('\n', ' ')
reports_df['Report'] = reports_df['Report'].str.replace(r'\s+', ' ', regex=True)
reports_df = reports_df.replace({np.nan: 'No', 'None': 'No', 'Absent':'No', 'Present':'Yes'})
reports_df
#---
# Splitting the reports_df into separate dataframes based on the 'ExamClass' column
# Creating a dictionary to hold the dataframes for each category
categories = reports_df['ExamClass'].unique()
print (categories)
dfs = {category: reports_df[reports_df['ExamClass'] == category] for category in categories}
# Now dfs dictionary contains separate dataframes for each category in 'ExamClass'
# For example, to access the dataframe for 'Cervical Spine Fracture', you can use dfs['Cervical Spine Fracture']
dfs['Cervical Spine Fracture']
#---
# Summing up the number of rows with 'None' and not 'None' in the 'Findings' column for each category
# Initialize a dictionary to store the results
category_summary = {}
# Iterate over each category dataframe
for category, df in dfs.items():
unique_values = df['Findings'].unique() # Get unique values in 'Findings' column
unique_counts = df['Findings'].value_counts() # Count the number of each unique value
total_count = len(df) # Total number of rows
category_summary[category] = {
'Unique_Values': unique_values,
'Unique_Counts': unique_counts,
'Total': total_count
}
# Print the results for each column
for category, counts in category_summary.items():
print(f"Category: {category}")
for value, count in counts['Unique_Counts'].items():
print(f"{value}: {count} {count*100//counts['Total']}%")
print(f"Total: {counts['Total']}")
print()
#---
# Extract all reports from the 'Report' column and clean them by removing extra whitespace and blank lines
reports = [{'report': report.strip(), 'filename': category} for report, category in zip(reports_df['Report'], reports_df['ExamClass']) if report.strip()]
#---
if INTERVAL > -1:
print ('Doing inference...')
out=engine(reports)
print (f"Results should be in {TEMP_OUT}")
else:
print ("Skipped doing inference")
#---
output_df = pd.read_csv(TEMP_OUT, index_col='index')
# rename the colume in output_df from 'filename' to 'ExamClass'
out_df = output_df.rename(columns={'filename': 'ExamClass'})
# Delete the column with reports
out_df.drop(columns=['report'], inplace=True, axis=1)
# Merge the 'Findings' column from reports_df into output_df
out_df = out_df.join(reports_df['Findings'])
out_df
#---
if os.path.exists(OUTPUT_FILE):
os.remove(OUTPUT_FILE)
# sometimes the LLM gives a long answer, so remove all that
# Remove all lines after the first line of a cell (including the '\n') throughout the dataframe
for col_name in ['Pulmonary Embolism', 'Pneumonia', 'LiverMets', 'C1FX', 'C2FX', 'C3FX', 'C4FX', 'C5FX', 'C6FX', 'C7FX', 'GliomaStatus']:
col_del = col_name + '_response_1'
try:
out_df.drop(columns=[col_del], inplace=True, axis=1)
except:
pass
try:
out_df = output_df.rename(columns={col_name+'_reponse_0': col_name})
except:
pass
# Write the combined dataframe to a CSV fil
out_df.replace('Absent', 'No', inplace=True)
out_df.replace('Present', 'Yes', inplace=True)
out_df.to_csv(OUTPUT_FILE)
print('The below should show only results, no reports or other PHI. Please send this file back to BJE@mayo.edu')
out_df
##########################################################################
# This file was converted using nb2py: https://github.com/BardiaKh/nb2py #
##########################################################################