umar-ts commited on
Commit
bd2f22b
·
verified ·
1 Parent(s): c91f206

Upload requirements.txt

Browse files
Files changed (3) hide show
  1. app.py +63 -0
  2. requirements.txt +2 -0
  3. service.py +115 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ else:
26
+ print("Unknown stage")
27
+ raise gr.Error('Unknown stage encountered')
28
+ except:
29
+ print("GPT error encounters")
30
+ raise gr.Error('Something went wrong please try again')
31
+ else:
32
+ print("Unknown stage parameters")
33
+ raise gr.Error('Provide appropriate parameters and try again')
34
+
35
+ def pasteFunctionality():
36
+ print("Hello word")
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.FOLLOWUP_ASSESSMENT.value,Stage.DETAILED_EVALUATION.value,Stage.DISCHARGE_SUMMARY.value,Stage.LONG_TERM_GOALS.value]
49
+
50
+ stage = gr.Dropdown(choices=stage_choices, label="Stage")
51
+
52
+ # Create a button component to copy goals
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
+
58
+ # Create a Gradio interface
59
+ gr.Interface(fn=process_text,
60
+ inputs=[input_text, stage, medical_history],
61
+ outputs=[output_text_1,output_text_2,output_text_3],
62
+ title="Medical Model Interface",
63
+ allow_flagging='never').launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ python-dotenv
service.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ from enum import Enum
4
+ from dotenv import load_dotenv
5
+ from prompts import Prompts
6
+ import json
7
+
8
+ # Load environment variables from .env file
9
+ load_dotenv()
10
+
11
+ # Access environment variables
12
+ API_KEY = os.environ['CHAT_GPT_KEY']
13
+
14
+ class Stage(Enum):
15
+ INITIAL_ASSESSMENT = "Initial Assessment"
16
+ FOLLOWUP_ASSESSMENT = "Follow Up Assessment"
17
+ EVALUATION = "Evaluation"
18
+ DETAILED_EVALUATION = "Detailed Evaluation"
19
+ PROGRESS_NOTE = "Progress Note"
20
+ DISCHARGE_SUMMARY = "Discharge Summary"
21
+ SHORT_TERM_GOALS = "Short Term Goals"
22
+ LONG_TERM_GOALS = "Long Term Goals"
23
+
24
+
25
+ class GPT_Service:
26
+ def __init__(self):
27
+ self.api_key = API_KEY
28
+ self.base_url = 'https://api.openai.com/v1/chat/completions'
29
+
30
+ def request_gpt_for_response(self, system_prompt, userPrompt):
31
+ headers = {
32
+ 'Content-Type': 'application/json',
33
+ 'Authorization': f'Bearer {self.api_key}'
34
+ }
35
+ data = {
36
+ 'model': 'gpt-3.5-turbo-1106',
37
+ 'temperature': 0.3,
38
+ 'messages': [
39
+ {
40
+ 'role': 'system',
41
+ 'content': system_prompt
42
+ },
43
+ {
44
+ 'role': 'user',
45
+ 'content': userPrompt
46
+ }
47
+ ]
48
+ }
49
+ try:
50
+ response = requests.post(
51
+ self.base_url,
52
+ json=data,
53
+ headers=headers,
54
+ )
55
+ response.raise_for_status()
56
+ return response.json()['choices'][0]['message']['content']
57
+ except requests.exceptions.RequestException as e:
58
+ raise ValueError(f"Error: {e}")
59
+
60
+ def formulateUserPrompt(self, assessmentPhase, keywords, medicalHistory):
61
+ patientData= f"""
62
+ Given we are in the {assessmentPhase} phase with a patient who has articulated experiences of {keywords}.
63
+ {f"Patient has the following medical history: {medicalHistory}" if medicalHistory is not None and medicalHistory != ""
64
+ else ""}
65
+ """
66
+ print(patientData)
67
+ return patientData
68
+
69
+ def processOutputResults(self,jsonOutput):
70
+ preprocessed_json_string = jsonOutput.replace('\n', '')
71
+ data = json.loads(preprocessed_json_string)
72
+ variants = [value for value in data.values()]
73
+ # Fetch first three items from the array or pad with empty strings if not enough items are available
74
+ paddedVariants = variants[:3] + [''] * (3 - len(variants))
75
+ return paddedVariants
76
+
77
+ def getInitialAssessment(self, keywords,medicalHistory):
78
+ userPrompt= self.formulateUserPrompt(Stage.INITIAL_ASSESSMENT.value,keywords,medicalHistory)
79
+ response= self.request_gpt_for_response(Prompts.INITIAL_ASSESSMENT_PROMPT.value,userPrompt)
80
+ return self.processOutputResults(response)
81
+
82
+ def getFollowUpAssessment(self, keywords, medicalHistory):
83
+ userPrompt= self.formulateUserPrompt(Stage.FOLLOWUP_ASSESSMENT.value,keywords,medicalHistory)
84
+ response= self.request_gpt_for_response(Prompts.FOLLOWUP_ASSESSMENT_PROMPT.value,userPrompt)
85
+ return self.processOutputResults(response)
86
+
87
+ def getEvaluation(self, keywords, medicalHistory):
88
+ userPrompt= self.formulateUserPrompt(Stage.EVALUATION.value,keywords,medicalHistory)
89
+ response= self.request_gpt_for_response(Prompts.EVALUATION_PROMPT.value,userPrompt)
90
+ return self.processOutputResults(response)
91
+
92
+ def getDetailedEvaluation(self, keywords,medicalHistory):
93
+ userPrompt= self.formulateUserPrompt(Stage.DETAILED_EVALUATION.value,keywords,medicalHistory)
94
+ response= self.request_gpt_for_response(Prompts.DETAILED_EVALUATION_PROMPT.value,userPrompt)
95
+ return self.processOutputResults(response)
96
+
97
+ def getProgressNote(self, keywords,medicalHistory):
98
+ userPrompt= self.formulateUserPrompt(Stage.PROGRESS_NOTE.value,keywords,medicalHistory)
99
+ response = self.request_gpt_for_response(Prompts.PROGRESS_NOTE_PROMPT.value,userPrompt)
100
+ return self.processOutputResults(response)
101
+
102
+ def getDischargeSummary(self, keywords,medicalHistory):
103
+ userPrompt= self.formulateUserPrompt(Stage.DISCHARGE_SUMMARY.value,keywords,medicalHistory)
104
+ response = self.request_gpt_for_response(Prompts.DISCHARGE_SUMMARY_PROMPT.value,userPrompt)
105
+ return self.processOutputResults(response)
106
+
107
+ def getShortTermGoals(self, keywords,medicalHistory):
108
+ userPrompt= self.formulateUserPrompt(Stage.SHORT_TERM_GOALS.value,keywords,medicalHistory)
109
+ response = self.request_gpt_for_response(Prompts.SHORT_TERM_GOALS_PROMPT.value,userPrompt)
110
+ return self.processOutputResults(response)
111
+
112
+ def getLongTermGoals(self, keywords,medicalHistory):
113
+ userPrompt= self.formulateUserPrompt(Stage.LONG_TERM_GOALS.value,keywords,medicalHistory)
114
+ response = self.request_gpt_for_response(Prompts.LONG_TERM_GOALS_PROMPT.value,userPrompt)
115
+ return self.processOutputResults(response)