Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,40 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
import torch.nn.functional as F
|
5 |
-
import os
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
if os.path.exists(directory_path):
|
11 |
-
for file_name in os.listdir(directory_path):
|
12 |
-
if file_name.endswith(".txt"):
|
13 |
-
file_path = os.path.join(directory_path, file_name)
|
14 |
-
with open(file_path, "r", encoding="utf-8") as file:
|
15 |
-
for line in file:
|
16 |
-
parts = line.strip().split(maxsplit=1)
|
17 |
-
if len(parts) == 2:
|
18 |
-
code = parts[0].strip()
|
19 |
-
description = parts[1].strip()
|
20 |
-
codes[code] = description
|
21 |
-
else:
|
22 |
-
print(f"Directory {directory_path} does not exist!")
|
23 |
-
return codes
|
24 |
-
|
25 |
-
# Load ICD and CPT codes
|
26 |
-
ICD_CODES = load_codes_from_files("./codes/icd_txt_files/", "ICD")
|
27 |
-
CPT_CODES = load_codes_from_files("./codes/cpt_txt_files/", "CPT")
|
28 |
-
|
29 |
-
# Check if codes were loaded
|
30 |
-
if not ICD_CODES or not CPT_CODES:
|
31 |
-
raise ValueError("No ICD or CPT codes were loaded. Please check your files and directory structure.")
|
32 |
|
33 |
# Load tokenizer and model
|
34 |
tokenizer = AutoTokenizer.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
|
35 |
-
model = AutoModelForSequenceClassification.from_pretrained("emilyalsentzer/Bio_ClinicalBERT", num_labels=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# Prediction function
|
38 |
def predict_codes(text):
|
@@ -57,22 +59,21 @@ def predict_codes(text):
|
|
57 |
# Get probabilities
|
58 |
probs = F.softmax(logits, dim=1)
|
59 |
|
60 |
-
# Get top 3 predictions
|
61 |
-
top_k =
|
62 |
-
|
|
|
|
|
|
|
63 |
|
64 |
# Format results
|
65 |
result = "Recommended ICD-10 Codes:\n"
|
66 |
-
for i,
|
67 |
-
|
68 |
-
description = ICD_CODES[code]
|
69 |
-
result += f"{i+1}. {code}: {description} (Confidence: {prob.item():.2f})\n"
|
70 |
|
71 |
result += "\nRecommended CPT Codes:\n"
|
72 |
-
for i,
|
73 |
-
|
74 |
-
description = CPT_CODES[code]
|
75 |
-
result += f"{i+1}. {code}: {description} (Confidence: {prob.item():.2f})\n"
|
76 |
|
77 |
return result
|
78 |
|
|
|
1 |
+
import requests
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
5 |
import torch.nn.functional as F
|
|
|
6 |
|
7 |
+
# API endpoints (replace with actual API endpoints)
|
8 |
+
ICD_API_URL = "https://icd10api.com/"
|
9 |
+
CPT_API_URL = "https://api.ama-assn.org/cpt/codes" # Example, requires AMA license
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Load tokenizer and model
|
12 |
tokenizer = AutoTokenizer.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
|
13 |
+
model = AutoModelForSequenceClassification.from_pretrained("emilyalsentzer/Bio_ClinicalBERT", num_labels=1000) # Adjust num_labels as needed
|
14 |
+
|
15 |
+
# Function to fetch ICD codes from API
|
16 |
+
def fetch_icd_codes(query):
|
17 |
+
try:
|
18 |
+
response = requests.get(f"{ICD_API_URL}?desc={query}")
|
19 |
+
if response.status_code == 200:
|
20 |
+
return response.json() # Adjust based on API response format
|
21 |
+
else:
|
22 |
+
return []
|
23 |
+
except Exception as e:
|
24 |
+
print(f"Error fetching ICD codes: {e}")
|
25 |
+
return []
|
26 |
+
|
27 |
+
# Function to fetch CPT codes from API
|
28 |
+
def fetch_cpt_codes(query):
|
29 |
+
try:
|
30 |
+
response = requests.get(f"{CPT_API_URL}?desc={query}")
|
31 |
+
if response.status_code == 200:
|
32 |
+
return response.json() # Adjust based on API response format
|
33 |
+
else:
|
34 |
+
return []
|
35 |
+
except Exception as e:
|
36 |
+
print(f"Error fetching CPT codes: {e}")
|
37 |
+
return []
|
38 |
|
39 |
# Prediction function
|
40 |
def predict_codes(text):
|
|
|
59 |
# Get probabilities
|
60 |
probs = F.softmax(logits, dim=1)
|
61 |
|
62 |
+
# Get top 3 predictions
|
63 |
+
top_k = torch.topk(probs, k=3)
|
64 |
+
|
65 |
+
# Fetch ICD and CPT codes using APIs
|
66 |
+
icd_results = fetch_icd_codes(text)
|
67 |
+
cpt_results = fetch_cpt_codes(text)
|
68 |
|
69 |
# Format results
|
70 |
result = "Recommended ICD-10 Codes:\n"
|
71 |
+
for i, code in enumerate(icd_results[:3]): # Show top 3 ICD codes
|
72 |
+
result += f"{i+1}. {code.get('code', 'Unknown')}: {code.get('description', 'No description')}\n"
|
|
|
|
|
73 |
|
74 |
result += "\nRecommended CPT Codes:\n"
|
75 |
+
for i, code in enumerate(cpt_results[:3]): # Show top 3 CPT codes
|
76 |
+
result += f"{i+1}. {code.get('code', 'Unknown')}: {code.get('description', 'No description')}\n"
|
|
|
|
|
77 |
|
78 |
return result
|
79 |
|