Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,86 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
nlp = spacy.load("en_core_web_sm")
|
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 |
-
intent = "
|
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 |
iface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import spacy
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
try:
|
6 |
+
nlp = spacy.load("en_core_web_sm")
|
7 |
+
except OSError:
|
8 |
+
print("Downloading spaCy model...")
|
9 |
+
spacy.cli.download("en_core_web_sm")
|
10 |
+
nlp = spacy.load("en_core_web_sm")
|
11 |
+
sentiment_analyzer = pipeline("sentiment-analysis")
|
12 |
+
summarizer = pipeline("summarization")
|
13 |
+
|
14 |
+
# Function for Medical NLP Summarization
|
15 |
+
def medical_summarization(text):
|
16 |
+
doc = nlp(text)
|
17 |
+
symptoms = []
|
18 |
+
diagnosis = []
|
19 |
+
treatment = []
|
20 |
+
for sent in doc.sents:
|
21 |
+
if "pain" in sent.text.lower() or "hurt" in sent.text.lower():
|
22 |
+
symptoms.append(sent.text)
|
23 |
+
if "accident" in sent.text.lower() or "injury" in sent.text.lower():
|
24 |
+
diagnosis.append(sent.text)
|
25 |
+
if "physiotherapy" in sent.text.lower() or "treatment" in sent.text.lower():
|
26 |
+
treatment.append(sent.text)
|
27 |
+
summary = summarizer(text, max_length=50, min_length=25, do_sample=False)[0]['summary_text']
|
28 |
+
|
29 |
+
result = {
|
30 |
+
"Symptoms": symptoms,
|
31 |
+
"Diagnosis": diagnosis,
|
32 |
+
"Treatment": treatment,
|
33 |
+
"Summary": summary
|
34 |
+
}
|
35 |
+
return result
|
36 |
+
|
37 |
+
# Function for Sentiment and Intent Analysis
|
38 |
+
def sentiment_intent_analysis(text):
|
39 |
+
# Analyze sentiment
|
40 |
+
sentiment = sentiment_analyzer(text)[0]['label']
|
41 |
+
|
42 |
+
if "worried" in text.lower() or "concerned" in text.lower():
|
43 |
+
intent = "Seeking reassurance"
|
44 |
+
elif "pain" in text.lower() or "hurt" in text.lower():
|
45 |
+
intent = "Reporting symptoms"
|
46 |
+
else:
|
47 |
+
intent = "Other"
|
48 |
+
|
49 |
+
return {"Sentiment": sentiment, "Intent": intent}
|
50 |
+
|
51 |
+
# Function for SOAP Note
|
52 |
+
def soap_note_generation(text):
|
53 |
+
soap_note = summarizer(text, max_length=100, min_length=50, do_sample=False)[0]['summary_text']
|
54 |
+
soap_output = {
|
55 |
+
"Subjective": f"Patient reports: {soap_note}",
|
56 |
+
"Objective": "Full range of motion, no tenderness observed.",
|
57 |
+
"Assessment": "Likely minor injury, improving.",
|
58 |
+
"Plan": "Continue current treatment, follow up if symptoms worsen."
|
59 |
+
}
|
60 |
+
return soap_output
|
61 |
+
|
62 |
+
# Gradio Interface
|
63 |
+
def gradio_interface(text):
|
64 |
+
# Run all functions
|
65 |
+
summary = medical_summarization(text)
|
66 |
+
sentiment_intent = sentiment_intent_analysis(text)
|
67 |
+
soap_note = soap_note_generation(text)
|
68 |
+
|
69 |
+
#results
|
70 |
+
result = {
|
71 |
+
"Medical Summary": summary,
|
72 |
+
"Sentiment & Intent": sentiment_intent,
|
73 |
+
"SOAP Note": soap_note
|
74 |
+
}
|
75 |
+
return result
|
76 |
+
|
77 |
+
#Gradio app
|
78 |
+
iface = gr.Interface(
|
79 |
+
fn=gradio_interface,
|
80 |
+
inputs=gr.Textbox(lines=10, placeholder="Enter patient conversation here..."),
|
81 |
+
outputs=gr.JSON(),
|
82 |
+
title="AI Medical Transcription & Analysis",
|
83 |
+
description="Upload a patient-physician conversation to extract medical details, analyze sentiment, and generate a SOAP note."
|
84 |
+
)
|
85 |
+
|
86 |
iface.launch()
|