NikilDGr8 commited on
Commit
58af7df
·
verified ·
1 Parent(s): f058590

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -69
app.py CHANGED
@@ -25,20 +25,6 @@ questions = [
25
  "Please give all the clinical findings which were listed"
26
  ]
27
 
28
- # List of form fields in the correct order
29
- form_fields = [
30
- "Age",
31
- "Gender",
32
- "Chief complaint",
33
- "Medical history",
34
- "Dental history",
35
- "Clinical Findings",
36
- "Referred to",
37
- "Treatment plan",
38
- "Calculus",
39
- "Stains"
40
- ]
41
-
42
  # Oral Health Assessment Form
43
  oral_health_assessment_form = [
44
  "Doctor’s Name",
@@ -65,7 +51,6 @@ def generate_answer(question: str, context: str) -> str:
65
  def transcribe_audio(audio_path: str) -> str:
66
  print(f"Received audio file at: {audio_path}")
67
 
68
- # Check if the file exists and is not empty
69
  if not os.path.exists(audio_path):
70
  return "Error: Audio file does not exist."
71
 
@@ -73,14 +58,11 @@ def transcribe_audio(audio_path: str) -> str:
73
  return "Error: Audio file is empty."
74
 
75
  try:
76
- # Transcribe the audio file using AssemblyAI
77
  transcriber = aai.Transcriber()
78
  print("Starting transcription...")
79
  transcript = transcriber.transcribe(audio_path)
80
-
81
  print("Transcription process completed.")
82
 
83
- # Handle the transcription result
84
  if transcript.status == aai.TranscriptStatus.error:
85
  print(f"Error during transcription: {transcript.error}")
86
  return transcript.error
@@ -94,57 +76,44 @@ def transcribe_audio(audio_path: str) -> str:
94
  return str(e)
95
 
96
  # Function to fill in the answers for the text boxes
97
- def fill_textboxes(context: str) -> dict:
98
  answers = []
99
  for question in questions:
100
  answer = generate_answer(question, context)
101
  answers.append(answer)
102
 
103
- # Map answers to form fields in the correct order
104
- return {
105
- "Age": answers[0] if len(answers) > 0 else "",
106
- "Gender": answers[1] if len(answers) > 1 else "",
107
- "Chief complaint": answers[2] if len(answers) > 2 else "",
108
- "Medical history": answers[3] if len(answers) > 3 else "",
109
- "Dental history": answers[4] if len(answers) > 4 else "",
110
- "Clinical Findings": answers[5] if len(answers) > 5 else "",
111
- "Referred to": "", # Default value
112
- "Treatment plan": "", # Default value
113
- "Calculus": "", # Default value
114
- "Stains": "", # Default value
115
- "Doctor’s Name": "", # Default value
116
- "Location": "" # Default value
117
- }
118
 
119
  # Supabase configuration
120
  supabase: Client = create_client(url, key)
121
 
122
  # Main Gradio app function
123
- def handle_transcription(audio: str, doctor_name: str, location: str) -> dict:
124
  context = transcribe_audio(audio)
125
  if "Error" in context:
126
- return {field: context for field in form_fields}
127
 
128
  answers = fill_textboxes(context)
129
- answers.update({
130
- "Doctor’s Name": doctor_name,
131
- "Location": location
132
- })
133
 
134
- return {
135
- "Age": answers.get("Age", ""),
136
- "Gender": answers.get("Gender", ""),
137
- "Chief complaint": answers.get("Chief complaint", ""),
138
- "Medical history": answers.get("Medical history", ""),
139
- "Dental history": answers.get("Dental history", ""),
140
- "Clinical Findings": answers.get("Clinical Findings", ""),
141
- "Referred to": answers.get("Referred to", ""),
142
- "Treatment plan": answers.get("Treatment plan", ""),
143
- "Calculus": answers.get("Calculus", ""),
144
- "Stains": answers.get("Stains", ""),
145
- "Doctor’s Name": doctor_name,
146
- "Location": location
147
- }
148
 
149
  def save_answers(doctor_name: str, location: str, patient_name: str, age: str, gender: str, chief_complaint: str, medical_history: str, dental_history: str, clinical_findings: str, treatment_plan: str, referred_to: str, calculus: str, stains: str) -> str:
150
  current_datetime = datetime.now().isoformat()
@@ -166,7 +135,6 @@ def save_answers(doctor_name: str, location: str, patient_name: str, age: str, g
166
  }
167
  print("Saved answers:", answers_dict)
168
 
169
- # Insert data into Supabase
170
  try:
171
  response = supabase.table('oral_health_assessments').insert(answers_dict).execute()
172
  print("Data inserted into Supabase:", response.data)
@@ -177,28 +145,21 @@ def save_answers(doctor_name: str, location: str, patient_name: str, age: str, g
177
 
178
  # Function to download table as CSV
179
  def download_table_to_csv() -> Optional[str]:
180
- # Fetch data from Supabase table
181
  response = supabase.table("oral_health_assessments").select("*").execute()
182
 
183
- # Check if data is available
184
  if not response.data:
185
  print("No data found in the table.")
186
  return None
187
 
188
  data = response.data
189
-
190
- # Prepare CSV data
191
  csv_data = []
192
 
193
- # Add header row
194
  if len(data) > 0:
195
  csv_data.append(data[0].keys())
196
 
197
- # Add data rows
198
  for row in data:
199
  csv_data.append(row.values())
200
 
201
- # Save CSV data to file (replace 'your_table.csv' with desired filename)
202
  csv_file = "your_table.csv"
203
  with open(csv_file, "w", newline='') as f:
204
  writer = csv.writer(f)
@@ -218,7 +179,6 @@ with gr.Blocks() as demo:
218
  gr.Markdown("# OHA Form Filler App")
219
 
220
  with gr.Tabs() as tabs:
221
- # Default tab for Doctor's Name and Location
222
  with gr.Tab("Doctor Info"):
223
  doctor_name_input = gr.Textbox(label="Doctor's Name", interactive=True)
224
  location_input = gr.Textbox(label="Location", interactive=True)
@@ -230,7 +190,6 @@ with gr.Blocks() as demo:
230
 
231
  submit_button.click(fn=submit_info, inputs=[doctor_name_input, location_input], outputs=info_output)
232
 
233
- # Second tab for OHA Form
234
  with gr.Tab("OHA Form"):
235
  audio_input = gr.Audio(type="filepath", label="Record your audio", elem_id="audio_input")
236
  transcribe_button = gr.Button("Transcribe and Generate Form", elem_id="transcribe_button", interactive=False)
@@ -259,12 +218,25 @@ with gr.Blocks() as demo:
259
  oha_output = gr.Textbox(label="OHA Output", value="", interactive=False)
260
  save_button = gr.Button("Save to Supabase", elem_id="save_button", interactive=True)
261
 
262
- transcribe_button.click(fn=handle_transcription, inputs=[audio_input, doctor_name_display, location_display], outputs=textboxes_left + textboxes_right + [oha_output])
263
- save_button.click(fn=save_answers, inputs=[doctor_name_display, location_display, patient_name_input] + textboxes_left + textboxes_right, outputs=oha_output)
 
 
 
 
 
 
 
 
 
 
 
264
 
265
  with gr.Tab("Download Data"):
266
  download_button = gr.Button("Download CSV")
267
- download_output = gr.File()
268
- download_button.click(fn=gradio_download, outputs=download_output)
 
269
 
270
- demo.launch()
 
 
25
  "Please give all the clinical findings which were listed"
26
  ]
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  # Oral Health Assessment Form
29
  oral_health_assessment_form = [
30
  "Doctor’s Name",
 
51
  def transcribe_audio(audio_path: str) -> str:
52
  print(f"Received audio file at: {audio_path}")
53
 
 
54
  if not os.path.exists(audio_path):
55
  return "Error: Audio file does not exist."
56
 
 
58
  return "Error: Audio file is empty."
59
 
60
  try:
 
61
  transcriber = aai.Transcriber()
62
  print("Starting transcription...")
63
  transcript = transcriber.transcribe(audio_path)
 
64
  print("Transcription process completed.")
65
 
 
66
  if transcript.status == aai.TranscriptStatus.error:
67
  print(f"Error during transcription: {transcript.error}")
68
  return transcript.error
 
76
  return str(e)
77
 
78
  # Function to fill in the answers for the text boxes
79
+ def fill_textboxes(context: str) -> list:
80
  answers = []
81
  for question in questions:
82
  answer = generate_answer(question, context)
83
  answers.append(answer)
84
 
85
+ # Map answers to form fields in the correct order and return as a list
86
+ return [
87
+ answers[0] if len(answers) > 0 else "",
88
+ answers[1] if len(answers) > 1 else "",
89
+ answers[2] if len(answers) > 2 else "",
90
+ answers[3] if len(answers) > 3 else "",
91
+ answers[4] if len(answers) > 4 else "",
92
+ answers[5] if len(answers) > 5 else "",
93
+ "", # Referred to
94
+ "", # Treatment plan
95
+ "", # Calculus
96
+ "", # Stains
97
+ "", # Doctor’s Name
98
+ "", # Location
99
+ ]
100
 
101
  # Supabase configuration
102
  supabase: Client = create_client(url, key)
103
 
104
  # Main Gradio app function
105
+ def handle_transcription(audio: str, doctor_name: str, location: str) -> list:
106
  context = transcribe_audio(audio)
107
  if "Error" in context:
108
+ return [context] * 13 # Return error for all fields
109
 
110
  answers = fill_textboxes(context)
 
 
 
 
111
 
112
+ # Insert Doctor’s Name and Location in the appropriate fields
113
+ answers[-2] = doctor_name
114
+ answers[-1] = location
115
+
116
+ return answers
 
 
 
 
 
 
 
 
 
117
 
118
  def save_answers(doctor_name: str, location: str, patient_name: str, age: str, gender: str, chief_complaint: str, medical_history: str, dental_history: str, clinical_findings: str, treatment_plan: str, referred_to: str, calculus: str, stains: str) -> str:
119
  current_datetime = datetime.now().isoformat()
 
135
  }
136
  print("Saved answers:", answers_dict)
137
 
 
138
  try:
139
  response = supabase.table('oral_health_assessments').insert(answers_dict).execute()
140
  print("Data inserted into Supabase:", response.data)
 
145
 
146
  # Function to download table as CSV
147
  def download_table_to_csv() -> Optional[str]:
 
148
  response = supabase.table("oral_health_assessments").select("*").execute()
149
 
 
150
  if not response.data:
151
  print("No data found in the table.")
152
  return None
153
 
154
  data = response.data
 
 
155
  csv_data = []
156
 
 
157
  if len(data) > 0:
158
  csv_data.append(data[0].keys())
159
 
 
160
  for row in data:
161
  csv_data.append(row.values())
162
 
 
163
  csv_file = "your_table.csv"
164
  with open(csv_file, "w", newline='') as f:
165
  writer = csv.writer(f)
 
179
  gr.Markdown("# OHA Form Filler App")
180
 
181
  with gr.Tabs() as tabs:
 
182
  with gr.Tab("Doctor Info"):
183
  doctor_name_input = gr.Textbox(label="Doctor's Name", interactive=True)
184
  location_input = gr.Textbox(label="Location", interactive=True)
 
190
 
191
  submit_button.click(fn=submit_info, inputs=[doctor_name_input, location_input], outputs=info_output)
192
 
 
193
  with gr.Tab("OHA Form"):
194
  audio_input = gr.Audio(type="filepath", label="Record your audio", elem_id="audio_input")
195
  transcribe_button = gr.Button("Transcribe and Generate Form", elem_id="transcribe_button", interactive=False)
 
218
  oha_output = gr.Textbox(label="OHA Output", value="", interactive=False)
219
  save_button = gr.Button("Save to Supabase", elem_id="save_button", interactive=True)
220
 
221
+ # Update the transcription and form fields when the transcribe button is clicked
222
+ transcribe_button.click(
223
+ fn=handle_transcription,
224
+ inputs=[audio_input, doctor_name_input, location_input],
225
+ outputs=textboxes_left + textboxes_right + [doctor_name_display, location_display]
226
+ )
227
+
228
+ # Save the form data to Supabase when the save button is clicked
229
+ save_button.click(
230
+ fn=save_answers,
231
+ inputs=[doctor_name_input, location_input] + textboxes_left + textboxes_right,
232
+ outputs=[oha_output]
233
+ )
234
 
235
  with gr.Tab("Download Data"):
236
  download_button = gr.Button("Download CSV")
237
+ download_output = gr.File(label="Download the CSV File", interactive=False)
238
+
239
+ download_button.click(fn=gradio_download, inputs=[], outputs=download_output)
240
 
241
+ # Launch the Gradio app
242
+ demo.launch(share=True)