Spaces:
Build error
Build error
File size: 18,158 Bytes
0aee03f c60ac27 0aee03f c60ac27 0aee03f |
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
from flask import Flask, jsonify, request, render_template
import pandas as pd
from flask_cors import CORS
import os
import json
import torch
from sentence_transformers import SentenceTransformer
import logging
from transformers import GPT2LMHeadModel, GPT2Tokenizer
from datetime import datetime
from transformers import BertTokenizer, BertForSequenceClassification
import random
import re
app = Flask(__name__)
app.json.sort_keys = False
CORS(app)
# Configure logging
logging.basicConfig(level=logging.DEBUG)
# Load the SentenceTransformer model
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
print("---"*30)
print(device)
# Load tokenizer and model
loaded_model = BertForSequenceClassification.from_pretrained('saved_model')
loaded_tokenizer = BertTokenizer.from_pretrained('saved_model')
tokenizer = GPT2Tokenizer.from_pretrained("checkpoint-15000")
model_gpt = GPT2LMHeadModel.from_pretrained("checkpoint-15000")
model_gpt.to(device)
print("===="*20)
# df_case = pd.read_csv('case_clustering24.csv', on_bad_lines="skip")
grouped = pd.read_csv('grouped_22_23_24.csv', on_bad_lines="skip")
from openai import OpenAI
api_key = "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"
#api_key = "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"
client = OpenAI(api_key = api_key)
def ask_gpt(question):
# Using the text completion model
response = client.chat.completions.create(
model = "gpt-4o-mini",
messages = [
{"role":"system","content":"you are a helpful assistant."},
{"role":"user","content": question}
]
)
return response.choices[0].message.content
class DataFrameManager:
def __init__(self, file_path):
self.file_path = file_path
self.df = pd.DataFrame() # Initialize an empty DataFrame
self.load_dataframe()
def load_dataframe(self):
if os.path.exists(self.file_path):
constant_date = "2024-01-01"
constant_policy_number = "POL123456"
constant_status = "Active"
self.df = pd.read_csv(self.file_path)
self.df['date'] = constant_date
self.df['policy_number'] = constant_policy_number
self.df['status'] = constant_status
self.df = self.df.rename(columns={
'note_id': 'id',
'cleaned_comments': 'summary',
'summarized_text': 'suggested_summary'
})
else:
print(f"File not found: {self.file_path}")
def get_dataframe(self):
return self.df.copy().head(200)
df_manager = DataFrameManager('client_notes_Sneha.csv')
# Define a function for text generation
def generate_text(prompt_text, max_length=100,num_return_sequences=10):
# Tokenize the prompt text and convert to tensor
input_ids = tokenizer(prompt_text, return_tensors="pt").input_ids.to(device)
attention_mask = tokenizer(
prompt_text, return_tensors="pt").attention_mask.to(device)
print("........")
try:
# Move input_ids and attention_mask tensor to GPU
input_ids = input_ids.to(device)
attention_mask = attention_mask.to(device)
outputs = model_gpt.generate(
input_ids=input_ids,
attention_mask=attention_mask,
pad_token_id=tokenizer.pad_token_id,
#max_length=10,
max_new_tokens=3,
num_beams=50,
temperature=0.7,
top_k=50,
top_p=0.9,
do_sample=True,
num_return_sequences=num_return_sequences
)
print(outputs)
print(",,,")
# Decode the generated text
generated_texts = [tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
print(generated_texts)
unique_texts = list(set(generated_texts))
return unique_texts[:5]
except Exception as e:
print(str(e))
@app.route('/get-csv')
def get_csv():
df = df_manager.get_dataframe()
data = df.to_dict(orient='records')
return jsonify(data)
@app.route('/search_notes', methods=['POST'])
def search_notes():
request_data = request.get_json()
claim_id_to_search = request_data.get('name', '')
full_df = df_manager.get_dataframe()
# print("DataFrame columns:", full_df.columns) # Debug output
# print("DataFrame first few rows:", full_df.head()) # Debug output
if claim_id_to_search:
try:
claim_id_to_search = int(claim_id_to_search) # Convert the claim ID from string to integer
# print("Searching for ID:", claim_id_to_search) # Debug output
filtered_df = full_df[full_df['id'] == claim_id_to_search]
# print("Filtered DataFrame:", filtered_df) # Debug output
if filtered_df.empty:
print("No matching records found, returning full DataFrame.")
return jsonify({})
else:
return jsonify(filtered_df.to_dict(orient='records'))
except ValueError as e:
print(e)
return jsonify({"error": "Invalid claim ID format"}), 400
else:
print("No claim ID provided, returning full DataFrame.")
return jsonify({})
@app.route('/get-similarity', methods=['POST'])
def get_similarity():
data = request.json
logging.debug(f"Received payload: {data}")
if not data or 'id' not in data:
return jsonify({'error': 'No valid data provided'}), 400
note_id = data['id']
logging.debug(f"Note ID: {note_id}")
df = df_manager.get_dataframe()
print(df.columns)
filtered_df = df[df['id'] == note_id]
if filtered_df.empty:
return jsonify({'error': 'No matching record found'}), 404
row = filtered_df.iloc[0]
summarized_text = row['suggested_summary']
logging.debug(f"Summarized Text: {summarized_text}")
# Encode the target summarized_text
target_embedding = model.encode(summarized_text, convert_to_tensor=True, device=device).unsqueeze(0)
# Calculate similarities with all entries in the suggested_summary column
similarities = []
for index, row in df.iterrows():
text = row['suggested_summary']
embedding = model.encode(text, convert_to_tensor=True, device=device).unsqueeze(0)
similarity = torch.nn.functional.cosine_similarity(target_embedding, embedding).item()
similarities.append({
'id': row['id'],
'status': row['status'],
'policy_number': row['policy_number'],
'date': row['date_created'].split(' ')[0],
'summary': row['summary'],
'suggested_summary': text,
'similarity': similarity
})
# Convert the results to a dataframe
similarity_df = pd.DataFrame(similarities)
# Sort the dataframe by similarity in descending order
similarity_df = similarity_df.sort_values(by='similarity', ascending=False)
print(similarity_df.head())
print(similarity_df.columns)
# Convert the dataframe to a dictionary
result = similarity_df.to_dict(orient='records')
return jsonify(result), 200
@app.route('/autocomplete', methods=['GET'])
def auto_complete():
data = request.args
print("-----------------------")
print(data)
prompt = data.get('prompt', '')
print(prompt)
if not prompt:
return jsonify({"error":"No prompt Provided"}),400
try:
print("====")
generated_texts = generate_text(prompt)
print(generated_texts)
return jsonify({'generated_text': generated_texts})
except Exception as e:
return jsonify({"error":str(e)}),500
@app.route('/assign-case-id', methods=['POST'])
def classify_claim():
data = request.json
case_id = int(data['case_id'])
# claim_line_id = data['claim_line_id']
diagnosis = data['diagnosis']
claim_line_note = data['claim_line_notes']
print(data['service_date'])
service_date = datetime.strptime(data['service_date'], "%Y-%m-%d")
print("++++++"*30)
print("dddddddd")
# Convert all `case_id` values in `grouped` to integers
grouped['case_id'] = grouped['case_id'].astype(int)
record = grouped[grouped['case_id'] == case_id]
# Check if case_id is present
if case_id not in grouped['case_id'].values:
new_case_id = random.randint(100000, 999999)
return jsonify({"message": f"New Case: Customer id {case_id} not found. \n"
f"A new case has been created with Case ID: {new_case_id}." })
# Check if the record is empty and return an appropriate response
if record.empty:
return jsonify({"error": "No record found for the given case_id"}), 404
print("--"*2)
print(record['service_date'])
# Compare service_date
existing_service_date = datetime.strptime(eval(record['service_date'].values[0])[-1], "%Y-%m-%d")
#existing_service_date = datetime.strptime(eval(record['service_date'].values[0])[-1],'%d-%m-%Y')
print("-")
print(existing_service_date)
print("-")
print(service_date)
#
is_recent = (service_date - existing_service_date).days < 90
print(is_recent)
if case_id in grouped['case_id'].values and not is_recent:
return jsonify({
"message": (
f"New case (Customer id {case_id} found, however service date is more than 90 days), "
f"Last service date: {existing_service_date.strftime('%Y-%m-%d')}"
)
})
# history for bert
past_claims_data = {}
for _, row in record.iterrows():
case_id = row['case_id'] # Extract the case_id for reference
num_claims = len(row['service_date'])
# Create sequences of claims within the same case
for i in range(1, num_claims):
row['claim_line_note'] = str([i for i in eval(row['claim_line_note'],{'nan':'nan'}) if i != 'nan'])
input_sequence = (
f"Diagnosis History: {', '.join(map(str, eval(row['diagnosis'])))}, "
f"Claim Line Notes History: {', '.join(map(str, eval(row['claim_line_note'])))}, "
f"Service Dates History: {', '.join(map(str, eval(row['service_date'])))}")
past_claims_data["input_sequence"]= input_sequence
# history for llm
past_claims_data_llm = {}
for _, row in record.iterrows():
case_id = row['case_id'] # Extract the case_id for reference
num_claims = len(row['service_date'])
# Create sequences of claims within the same case
for i in range(1, num_claims):
row['claim_line_note'] = str([i for i in eval(row['claim_line_note'],{'nan':'nan'}) if i != 'nan'])
past_claims_data_llm["Diagnosis History"]= ', '.join(map(str, eval(row['diagnosis'])))
past_claims_data_llm["Claim Line Notes"]= ', '.join(map(str, eval(row['claim_line_note'])))
print("***********************Past claim History***********************")
print(past_claims_data['input_sequence'])
print()
# new claim info
new_claim = (
f"New Diagnosis: {', '.join(map(str, [diagnosis]))}, "
f"New Claim Line Note: {', '.join(map(str, [claim_line_note]))}, "
f"New Service Date: {', '.join(map(str, [service_date]))}"
)
print("***********************New claim Data***********************")
print(new_claim)
print("***********************")
# Tokenize the test data
inputs = loaded_tokenizer(past_claims_data['input_sequence'], new_claim, padding=True, truncation=True, return_tensors="pt")
# Get model predictions
with torch.no_grad():
outputs = loaded_model(**inputs)
predictions = torch.argmax(outputs.logits, dim=-1)
bert_probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
pred_label = predictions.tolist()[0]
new_case_id = random.randint(100000, 999999)
new_claim_id = random.randint(100000, 999999)
# Generate final output based on prediction
if pred_label == 1:
# New Case: Generate a 6-digit random case ID
final_output = (
f"New Claim ID: {new_claim_id}."
f"New Case: A new case has been created with Case ID: {new_case_id}. "
f"The diagnosis and claim notes indicate it's a New Case. "
f"Diagnosis: {diagnosis}, Claim Line Note: {claim_line_note}, Service Date: {service_date.strftime('%Y-%m-%d')}."
)
else:
# Follow-up Case: Add reasoning
final_output = (
f"New Claim ID: {new_claim_id}."
f"Follow-up Case: The claim has been classified as a follow-up case for Case ID: {case_id}. "
f"The diagnosis and claim notes indicate a follow-up claim, and the service date is within 30 days of the last service date."
)
## LLM
system_prompt = """Respond to the human as helpfully and accurately as possible. You are an expert in analyzing medical claims. Your task is to compare the new claim with past claims and determine if the New Claim is a "Follow-up Claim" (related to an existing issue) or a "Different Claim" (a separate, unrelated issue).
To make this determination, carefully analyze both the below diagnosis and the claim line notes for patterns, similarities, or differences.
**Existing Claims:**
- Diagnosis: "{past_claims_data_diagnosis}"
- Claim Line Note: "{past_claims_data_claim_line_note}"
**New Claim:**
- Diagnosis: "{diagnosis}"
- Claim Line Note: "{claim_line_note}"
Use a json blob to output a confidence sore along with a reasoning.
Valid "category" values: Follow-up Case, New Case
Valid "confidence_score" values: 1-100
Provide only ONE action per $JSON_BLOB, as shown:
```
{{
"action": $CATEGORY_NAME
"confidence_score": "$CONFIDENCE_SCORE",
"reasoning": $REASONING
}}
```
Begin! Reminder to ALWAYS respond with a valid json blob of a single action.
Respond directly if appropriate. Format is Action:```$JSON_BLOB```"""
system_prompt = system_prompt.format(diagnosis=diagnosis, claim_line_note=claim_line_note,
past_claims_data_diagnosis=past_claims_data_llm["Diagnosis History"],
past_claims_data_claim_line_note=past_claims_data_llm["Claim Line Notes"])
# Get the LLM's response
llm_response = ask_gpt(system_prompt)
print(llm_response)
# Function to extract the JSON blob from the LLM response
def extract_json_blobs(response):
try:
# Use regex to find all JSON blobs within the backticks
matches = re.findall(r'\{.*?\}', response, re.DOTALL)
# matches = re.findall(r'\{(?:[^{}]|(?R))*\}', response)
json_blobs = [json.loads(match) for match in matches]
for blob in json_blobs:
if 'confidence_score' in blob:
blob['confidence_score'] = float(blob['confidence_score']) / 100
return json_blobs
except json.JSONDecodeError as e:
print(f"Error in parsing JSON: {e}")
return []
final_output_llm = extract_json_blobs(llm_response)[0]
print(final_output_llm)
# Assign weights to BERT and LLM responses (adjust as per requirement)
json_confidence_follow_up = final_output_llm['confidence_score'] if final_output_llm['action'] == 'Follow-up Case' else 1-final_output_llm['confidence_score']
json_confidence_new_case = final_output_llm['confidence_score'] if final_output_llm['action'] == 'New Case' else 1-final_output_llm['confidence_score']
# Extract BERT probabilities
bert_confidence_follow_up = bert_probabilities[0][0].item()
bert_confidence_new_case = bert_probabilities[0][1].item()
# (simple average)
combined_confidence_follow_up = 0.35 * bert_confidence_follow_up + 0.65 * json_confidence_follow_up
combined_confidence_new_case = 0.35 * bert_confidence_new_case + 0.65 * json_confidence_new_case
final_prediction = "New Case" if combined_confidence_new_case > combined_confidence_follow_up else "Follow-Up Case"
if final_prediction == "Follow-Up Case":
# Construct the response with Markdown-style formatting
return jsonify({
#"BERT Prediction": "New Case" if pred_label == 1 else "Follow-Up Case",
#"LLM Prediction": final_output_llm["action"],
"Ensembled model Prediction": final_prediction,
"New Claim ID": new_claim_id,
"Weighted Confidence Score": round(max(combined_confidence_follow_up, combined_confidence_new_case), 2),
"Reasoning": final_output_llm['reasoning']
})
else:
return jsonify({
# "BERT Prediction": "New Case" if pred_label == 1 else "Follow-Up Case",
# "LLM Prediction": final_output_llm["action"],
"Ensembled models Prediction": final_prediction,
"New Claim ID": new_claim_id,
"Weighted Confidence Score": round(max(combined_confidence_follow_up, combined_confidence_new_case), 2),
"New Case ID": new_case_id,
"Reasoning": final_output_llm['reasoning']
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True) |