Spaces:
Runtime error
Runtime error
Add app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModel
|
3 |
+
|
4 |
+
# Load Med42
|
5 |
+
med42_model_name = "m42-health/med42-70b"
|
6 |
+
med42_tokenizer = AutoTokenizer.from_pretrained(med42_model_name)
|
7 |
+
med42_model = AutoModelForCausalLM.from_pretrained(med42_model_name)
|
8 |
+
|
9 |
+
# Load ClinicalBERT
|
10 |
+
clinicalbert_model_name = "medicalai/ClinicalBERT"
|
11 |
+
clinicalbert_tokenizer = AutoTokenizer.from_pretrained(clinicalbert_model_name)
|
12 |
+
clinicalbert_model = AutoModel.from_pretrained(clinicalbert_model_name)
|
13 |
+
|
14 |
+
# Define functions
|
15 |
+
def med42_qa(question):
|
16 |
+
inputs = med42_tokenizer(question, return_tensors="pt")
|
17 |
+
outputs = med42_model.generate(**inputs, max_length=200)
|
18 |
+
return med42_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
19 |
+
|
20 |
+
def analyze_ehr(text):
|
21 |
+
inputs = clinicalbert_tokenizer(text, return_tensors="pt")
|
22 |
+
embeddings = clinicalbert_model(**inputs).last_hidden_state
|
23 |
+
return f"ClinicalBERT generated embeddings with shape: {embeddings.shape}"
|
24 |
+
|
25 |
+
# Combine Gradio Interface
|
26 |
+
def combined_tool(input_text):
|
27 |
+
qa_result = med42_qa(input_text)
|
28 |
+
ehr_result = analyze_ehr(input_text)
|
29 |
+
return f"Med42 Answer:\n{qa_result}\n\nClinicalBERT Analysis:\n{ehr_result}"
|
30 |
+
|
31 |
+
# Build Gradio UI
|
32 |
+
interface = gr.Interface(
|
33 |
+
fn=combined_tool,
|
34 |
+
inputs="text",
|
35 |
+
outputs="text",
|
36 |
+
title="Healthcare AI Tool",
|
37 |
+
description="Use Med42 for medical Q&A and ClinicalBERT for EHR analysis."
|
38 |
+
)
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
interface.launch()
|