Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,41 +26,27 @@ print("Attaching LoRA adapter...")
|
|
| 26 |
model = PeftModel.from_pretrained(base_model, "StudioIlios/icd10-model", token=hf_token)
|
| 27 |
print("Model loaded successfully!")
|
| 28 |
|
| 29 |
-
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
| 30 |
-
|
| 31 |
-
### Instruction:
|
| 32 |
-
Extract the exact ICD-10 code for the following clinical description. Return ONLY the code.
|
| 33 |
-
|
| 34 |
-
### Input:
|
| 35 |
-
{}
|
| 36 |
-
|
| 37 |
-
### Response:
|
| 38 |
-
"""
|
| 39 |
-
|
| 40 |
def predict(clinical_text):
|
| 41 |
-
|
| 42 |
-
prompt = alpaca_prompt.format(clinical_text)
|
| 43 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
| 44 |
-
|
| 45 |
-
input_token_length = inputs["input_ids"].shape[1]
|
| 46 |
|
| 47 |
with torch.no_grad():
|
| 48 |
outputs = model.generate(
|
| 49 |
**inputs,
|
| 50 |
-
max_new_tokens=
|
| 51 |
-
|
|
|
|
| 52 |
pad_token_id=tokenizer.eos_token_id
|
| 53 |
)
|
| 54 |
-
|
| 55 |
-
generated_tokens = outputs[0][input_token_length:]
|
| 56 |
-
final_answer = tokenizer.decode(generated_tokens, skip_special_tokens=True).strip()
|
| 57 |
|
| 58 |
-
icd_match = re.search(r'[A-Z][0-9][A-Z0-9](?:\.[A-Z0-9]{1,4})?',
|
| 59 |
|
| 60 |
if icd_match:
|
| 61 |
return icd_match.group(0)
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
demo = gr.Interface(
|
| 66 |
fn=predict,
|
|
|
|
| 26 |
model = PeftModel.from_pretrained(base_model, "StudioIlios/icd10-model", token=hf_token)
|
| 27 |
print("Model loaded successfully!")
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def predict(clinical_text):
|
| 30 |
+
inputs = tokenizer(clinical_text, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
with torch.no_grad():
|
| 33 |
outputs = model.generate(
|
| 34 |
**inputs,
|
| 35 |
+
max_new_tokens=50,
|
| 36 |
+
temperature=0.7,
|
| 37 |
+
do_sample=True,
|
| 38 |
pad_token_id=tokenizer.eos_token_id
|
| 39 |
)
|
| 40 |
+
full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
icd_match = re.search(r'\b[A-Z][0-9][A-Z0-9](?:\.[A-Z0-9]{1,4})?\b', full_output)
|
| 43 |
|
| 44 |
if icd_match:
|
| 45 |
return icd_match.group(0)
|
| 46 |
|
| 47 |
+
|
| 48 |
+
fallback = full_output.replace(clinical_text, "").strip()
|
| 49 |
+
return f"No code generated. Output was: {fallback}"
|
| 50 |
|
| 51 |
demo = gr.Interface(
|
| 52 |
fn=predict,
|