masif umar-ts commited on
Commit
1b80b66
·
verified ·
1 Parent(s): da45c37

Update app.py (#9)

Browse files

- Update app.py (916ca956de3c231596b3c726f69111e009773bf2)


Co-authored-by: Umar <umar-ts@users.noreply.huggingface.co>

Files changed (1) hide show
  1. app.py +115 -45
app.py CHANGED
@@ -1,29 +1,67 @@
1
  import gradio as gr
2
  from service import GPT_Service, Stage
3
 
4
- # Define the function to be called when the button is clicked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  def process_text(keywords, stage, medical_history):
6
- if(keywords.strip()!='' and stage!=None and len(stage)!=0):
7
  try:
8
  gptService=GPT_Service()
9
  if stage == Stage.INITIAL_ASSESSMENT.value:
10
- return gptService.getInitialAssessment(keywords,medical_history)
 
11
  elif stage == Stage.FOLLOWUP_ASSESSMENT.value:
12
- return gptService.getFollowUpAssessment(keywords,medical_history)
 
13
  elif stage == Stage.EVALUATION.value:
14
- return gptService.getEvaluation(keywords,medical_history)
 
15
  elif stage == Stage.DETAILED_EVALUATION.value:
16
- return gptService.getDetailedEvaluation(keywords,medical_history)
 
17
  elif stage == Stage.PROGRESS_NOTE.value:
18
- return gptService.getProgressNote(keywords,medical_history)
 
19
  elif stage == Stage.DISCHARGE_SUMMARY.value:
20
- return gptService.getDischargeSummary(keywords,medical_history)
 
21
  elif stage == Stage.SHORT_TERM_GOALS.value:
22
- return gptService.getShortTermGoals(keywords,medical_history)
 
23
  elif stage == Stage.LONG_TERM_GOALS.value:
24
- return gptService.getLongTermGoals(keywords,medical_history)
 
25
  elif stage == Stage.RECOMMENDATION.value:
26
- return gptService.getRecommendation(keywords,medical_history)
 
27
  else:
28
  print("Unknown stage")
29
  raise gr.Error('Unknown stage encountered')
@@ -33,46 +71,78 @@ def process_text(keywords, stage, medical_history):
33
  else:
34
  print("Unknown stage parameters")
35
  raise gr.Error('Provide appropriate parameters and try again')
36
-
37
-
38
- # Create text input components with labels
39
- input_text = gr.Textbox(lines=7, label="Sample keywords", placeholder="Enter sample keywords here")
40
- medical_history = gr.Textbox(lines=7, label="Medical History", placeholder="Enter medical history here")
41
-
42
- # Populating stage choices and creating dropdown component with label
43
-
44
- # To add all the stages
45
- # stage_choices = [stage.value for stage in Stage]
46
-
47
- # To add few stages
48
- stage_choices = [Stage.INITIAL_ASSESSMENT.value,Stage.SHORT_TERM_GOALS.value,Stage.LONG_TERM_GOALS.value, Stage.RECOMMENDATION.value]
49
 
50
- stage = gr.Dropdown(choices=stage_choices, label="Stage")
 
 
51
 
52
- # Create output containers
53
- output_text_1 = gr.Textbox(lines=6, label="Variant 1", show_copy_button=True)
54
- output_text_2 = gr.Textbox(lines=6, label="Variant 2", show_copy_button=True)
55
- output_text_3 = gr.Textbox(lines=6, label="Variant 3", show_copy_button=True)
56
 
57
- title_html = """
58
- <div style="display: flex; align-items: center; width=100%; background-color:#FFC90E; overflow:auto;">
59
- <img src='https://i.ibb.co/gPFskvg/LOGO-1.png' style='height: 90px; width:90px; margin-left: 20px;'>
60
- <span style="margin-left: 20px;font-size:30px">
61
- <span>Patient Assessment Documentation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
62
- </span>
63
- </div>
64
 
65
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- # Create a Gradio interface
69
- interface=gr.Interface(fn=process_text,
70
- inputs=[input_text, stage, medical_history],
71
- outputs=[output_text_1,output_text_2,output_text_3],
72
- title=title_html,
73
- theme=gr.themes.Default(),
74
- allow_flagging='never')
75
 
76
- interface.launch()
 
 
 
 
 
 
 
77
 
 
 
 
 
78
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from service import GPT_Service, Stage
3
 
4
+
5
+ title_html = """
6
+ <div style="display: flex; align-items: center; width=100%; background-color:#FFC90E; border-radius:10px;">
7
+ <img src='https://i.ibb.co/gPFskvg/LOGO-1.png' style='height: 90px; width:90px; margin-left: 20px;'>
8
+ <span style="margin-left: 20px;font-size:30px">
9
+ <span>Patient Assessment Documentation</span>
10
+ </span>
11
+ </div>
12
+
13
+ """
14
+
15
+ css = """
16
+ #subButton, #rephraseBtn {
17
+ background-color: #EA580C !important;
18
+ background: #EA580C !important;
19
+ color: #FFFFFF !important;
20
+ border: none !important; /* Remove the border */
21
+ transition: background-color 0.3s ease; /* Add transition for smoother hover effect */
22
+ }
23
+
24
+ #subButton:hover, #rephraseBtn:hover {
25
+ color: #EA580C !important;
26
+ background-color: #FFC90E !important; /* Change to a lighter background color on hover */
27
+ }
28
+
29
+ """
30
+
31
+ def split_based_on_commas(string):
32
+ return [substring.strip() for substring in string.split(",")]
33
+
34
  def process_text(keywords, stage, medical_history):
35
+ if(keywords.strip()!='' and stage!=None and len(stage)!=0 and len(split_based_on_commas(keywords))<=5):
36
  try:
37
  gptService=GPT_Service()
38
  if stage == Stage.INITIAL_ASSESSMENT.value:
39
+ options = gptService.getInitialAssessment(keywords,medical_history)
40
+ return gr.update(choices=options, value="", interactive=True)
41
  elif stage == Stage.FOLLOWUP_ASSESSMENT.value:
42
+ options = gptService.getFollowUpAssessment(keywords,medical_history)
43
+ return gr.update(choices=options, value="", interactive=True)
44
  elif stage == Stage.EVALUATION.value:
45
+ options = gptService.getEvaluation(keywords,medical_history)
46
+ return gr.update(choices=options, value="", interactive=True)
47
  elif stage == Stage.DETAILED_EVALUATION.value:
48
+ options = gptService.getDetailedEvaluation(keywords,medical_history)
49
+ return gr.update(choices=options, value="", interactive=True)
50
  elif stage == Stage.PROGRESS_NOTE.value:
51
+ options = gptService.getProgressNote(keywords,medical_history)
52
+ return gr.update(choices=options, value="", interactive=True)
53
  elif stage == Stage.DISCHARGE_SUMMARY.value:
54
+ options = gptService.getDischargeSummary(keywords,medical_history)
55
+ return gr.update(choices=options, value="", interactive=True)
56
  elif stage == Stage.SHORT_TERM_GOALS.value:
57
+ options = gptService.getShortTermGoals(keywords,medical_history)
58
+ return gr.update(choices=options, value="", interactive=True)
59
  elif stage == Stage.LONG_TERM_GOALS.value:
60
+ options = gptService.getLongTermGoals(keywords,medical_history)
61
+ return gr.update(choices=options, value="", interactive=True)
62
  elif stage == Stage.RECOMMENDATION.value:
63
+ options = gptService.getRecommendation(keywords,medical_history)
64
+ return gr.update(choices=options, value="", interactive=True)
65
  else:
66
  print("Unknown stage")
67
  raise gr.Error('Unknown stage encountered')
 
71
  else:
72
  print("Unknown stage parameters")
73
  raise gr.Error('Provide appropriate parameters and try again')
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
+
76
+ def clear_input(response):
77
+ return gr.update(value="")
78
 
 
 
 
 
79
 
80
+ def getPatientAssessment(sentences,stage, medical_history):
 
 
 
 
 
 
81
 
82
+ if(sentences is None or len(sentences) == 0 or stage is None):
83
+ raise gr.Error('Provide appropriate parameters and try again')
84
+ else:
85
+ try:
86
+ gptService=GPT_Service()
87
+ if stage == Stage.INITIAL_ASSESSMENT.value:
88
+ return gptService.getDetailInitialAssessment(medical_history,sentences)
89
+ elif stage == Stage.SHORT_TERM_GOALS.value:
90
+ return gptService.getDetailShortTermGoals(medical_history,sentences)
91
+ elif stage == Stage.LONG_TERM_GOALS.value:
92
+ return gptService.getDetailLongTermGoals(medical_history,sentences)
93
+ elif stage == Stage.RECOMMENDATION.value:
94
+ return gptService.getDetailRecommendation(medical_history,sentences)
95
+ else:
96
+ print("Unknown stage")
97
+ raise gr.Error('Unknown stage encountered')
98
+ except:
99
+ print("GPT error encounters")
100
+ raise gr.Error('Something went wrong please try again')
101
+
102
+
103
+ def getRephrasedSentences(sentences):
104
+ if (sentences is None or sentences.strip() == ''):
105
+ raise gr.Error('Provide appropriate parameters and try again')
106
+ else:
107
+ try:
108
+ gptService=GPT_Service()
109
+ return gptService.getRephrasedPatientAssessment(sentences)
110
+ except:
111
+ raise gr.Error('Something went wrong please try again')
112
+
113
 
114
+ with gr.Blocks(css=css) as demo:
115
+ gr.HTML(title_html)
116
+ gr.HTML("<h2>Step 1:- Patient Assessment Input</h2>")
117
+ with gr.Row():
118
+ keywords = gr.Textbox(label="Keywords", placeholder="Write comma separated sample keywords (max=5)")
119
+ medicalHistory = gr.Textbox(label="Medical History", placeholder="Enter patient history")
120
+ stage_choices = [Stage.INITIAL_ASSESSMENT.value,Stage.SHORT_TERM_GOALS.value,Stage.LONG_TERM_GOALS.value, Stage.RECOMMENDATION.value]
121
+ stage = gr.Dropdown(choices=stage_choices, label="Assessment Stage")
122
+
123
+ with gr.Row():
124
+ clearInputFields = gr.ClearButton(value="Clear Inputs",components=[keywords,medicalHistory,stage])
125
+ responseButton = gr.Button("Submit",elem_id="subButton")
126
 
 
 
 
 
 
 
 
127
 
128
+ with gr.Column(scale=2):
129
+ gr.HTML("<h2>Step 2:- Keyword-Driven Sentence Analysis</h2>")
130
+ selectedSentence = gr.Dropdown(choices=[], show_label=False,interactive=False,multiselect=True)
131
+
132
+
133
+ with gr.Row():
134
+ clearSelectedSentences = gr.Button(value="Clear Selection")
135
+ detailAssessmentButton = gr.Button("Generate Summary",elem_id="rephraseBtn",)
136
 
137
+ with gr.Column():
138
+ gr.HTML("<h2>Step 3:- Patient Assessment Summary</h2>")
139
+ patientDetailAssessment=gr.Textbox(show_label=False, interactive=False, show_copy_button=True,lines=15)
140
+ rephraseButton = gr.Button("I don't like response, Generate New :)",elem_id="rephraseBtn",)
141
 
142
+ responseButton.click(process_text,[keywords,stage,medicalHistory],[selectedSentence])
143
+ detailAssessmentButton.click(getPatientAssessment,[selectedSentence,stage,medicalHistory],patientDetailAssessment)
144
+ rephraseButton.click(getPatientAssessment,[selectedSentence,stage,medicalHistory],patientDetailAssessment)
145
+ clearSelectedSentences.click(clear_input,inputs=[selectedSentence],outputs=[selectedSentence])
146
+
147
+
148
+ demo.launch(share=True)