akashkumar12 commited on
Commit
6756e6a
·
verified ·
1 Parent(s): fc2add9

Update app.py

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