ariankhalfani commited on
Commit
e65f39b
1 Parent(s): 813521c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -39
app.py CHANGED
@@ -170,12 +170,12 @@ def submit_result(name, patient_id, input_image, predicted_image, result):
170
  def view_database():
171
  conn = sqlite3.connect('results.db')
172
  c = conn.cursor()
173
- c.execute("SELECT name, patient_id, result FROM results")
174
  rows = c.fetchall()
175
  conn.close()
176
 
177
  # Convert to pandas DataFrame for better display in Gradio
178
- df = pd.DataFrame(rows, columns=["Name", "Patient ID", "Result"])
179
 
180
  return df
181
 
@@ -184,19 +184,6 @@ def download_file(choice):
184
  if choice == "Database (.db)":
185
  # Provide the path to the database file
186
  return 'results.db'
187
- elif choice == "Database (.html)":
188
- # Export the database as an HTML file
189
- conn = sqlite3.connect('results.db')
190
- c = conn.cursor()
191
- c.execute("SELECT name, patient_id, result FROM results")
192
- rows = c.fetchall()
193
- conn.close()
194
-
195
- df = pd.DataFrame(rows, columns=["Name", "Patient ID", "Result"])
196
-
197
- with tempfile.NamedTemporaryFile(delete=False, suffix='.html') as temp_file:
198
- df.to_html(temp_file.name, index=False)
199
- return temp_file.name
200
  else:
201
  conn = sqlite3.connect('results.db')
202
  c = conn.cursor()
@@ -207,35 +194,73 @@ def download_file(choice):
207
  image_bytes = row[0]
208
  with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
209
  temp_file.write(image_bytes)
210
- temp_file.flush() # Ensure all data is written before closing the file
211
  return temp_file.name
212
  else:
213
- return None
214
 
215
  # Initialize the database
216
  init_db()
217
 
218
- # Gradio interface
219
- with gr.Blocks() as interface:
220
- gr.Markdown("## Image Prediction System")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
 
 
 
 
 
 
 
 
 
 
222
  with gr.Row():
223
- with gr.Column():
224
- name_input = gr.Textbox(label="Name", placeholder="Enter patient name")
225
- patient_id_input = gr.Textbox(label="Patient ID", placeholder="Enter patient ID")
226
- input_image = gr.Image(source="upload", type="pil", label="Input Image")
227
- submit_button = gr.Button("Submit")
228
- database_view = gr.DataFrame(label="Database View")
229
-
230
- with gr.Column():
231
- predicted_image_output = gr.Image(type="pil", label="Predicted Image")
232
- result_text_output = gr.Textbox(label="Raw Result")
233
- download_choice = gr.Radio(["Database (.db)", "Database (.html)", "Predicted Image (.png)"], label="Download Options")
234
- download_button = gr.File(label="Download", interactive=True)
235
-
236
- submit_button.click(predict_image, inputs=[input_image, name_input, patient_id_input], outputs=[predicted_image_output, result_text_output])
237
- submit_button.click(submit_result, inputs=[name_input, patient_id_input, input_image, predicted_image_output, result_text_output], outputs=None)
238
- gr.Button("View Database").click(view_database, outputs=database_view)
239
- download_button.upload(download_file, inputs=download_choice, outputs=download_button)
240
-
241
- interface.launch()
 
 
170
  def view_database():
171
  conn = sqlite3.connect('results.db')
172
  c = conn.cursor()
173
+ c.execute("SELECT name, patient_id, input_image, predicted_image FROM results")
174
  rows = c.fetchall()
175
  conn.close()
176
 
177
  # Convert to pandas DataFrame for better display in Gradio
178
+ df = pd.DataFrame(rows, columns=["Name", "Patient ID", "Input Image", "Predicted Image"])
179
 
180
  return df
181
 
 
184
  if choice == "Database (.db)":
185
  # Provide the path to the database file
186
  return 'results.db'
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  else:
188
  conn = sqlite3.connect('results.db')
189
  c = conn.cursor()
 
194
  image_bytes = row[0]
195
  with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
196
  temp_file.write(image_bytes)
197
+ temp_file.flush() # Ensure all data is written before closing
198
  return temp_file.name
199
  else:
200
+ raise FileNotFoundError("No images found in the database.")
201
 
202
  # Initialize the database
203
  init_db()
204
 
205
+ # Gradio Interface
206
+ def interface(name, patient_id, input_image):
207
+ if input_image is None:
208
+ return "Please upload an image."
209
+
210
+ output_image, raw_result = predict_image(input_image, name, patient_id)
211
+ submit_status = submit_result(name, patient_id, input_image, output_image, raw_result)
212
+
213
+ return output_image, raw_result, submit_status
214
+
215
+ # View Database Function
216
+ def view_db_interface():
217
+ df = view_database()
218
+ return df
219
+
220
+ # Download Function
221
+ def download_interface(choice):
222
+ try:
223
+ file_path = download_file(choice)
224
+ with open(file_path, "rb") as file:
225
+ return file.read(), file_path.split('/')[-1]
226
+ except FileNotFoundError as e:
227
+ return str(e), None
228
+
229
+ # Gradio Blocks
230
+ with gr.Blocks() as demo:
231
+ with gr.Column():
232
+ gr.Markdown("# Cataract Detection System")
233
+ gr.Markdown("Upload an image to detect cataract and add patient details.")
234
+ gr.Image("PR_curve.png", label="Model PR Curve")
235
+ gr.Markdown("This application uses YOLOv8 with mAP=0.981")
236
 
237
+ with gr.Column():
238
+ name = gr.Textbox(label="Name")
239
+ patient_id = gr.Textbox(label="Patient ID")
240
+ input_image = gr.Image(type="pil", label="Upload an Image", image_mode="RGB")
241
+
242
+ with gr.Column():
243
+ submit_btn = gr.Button("Submit")
244
+ output_image = gr.Image(type="pil", label="Predicted Image")
245
+
246
  with gr.Row():
247
+ raw_result = gr.Textbox(label="Raw Result", lines=5)
248
+ submit_status = gr.Textbox(label="Submission Status")
249
+
250
+ submit_btn.click(fn=interface, inputs=[name, patient_id, input_image], outputs=[output_image, raw_result, submit_status])
251
+
252
+ with gr.Column():
253
+ view_db_btn = gr.Button("View Database")
254
+ db_output = gr.Dataframe(label="Database Records")
255
+
256
+ view_db_btn.click(fn=view_db_interface, inputs=[], outputs=[db_output])
257
+
258
+ with gr.Column():
259
+ download_choice = gr.Radio(["Database (.db)", "Predicted Image (.png)"], label="Choose the file to download:")
260
+ download_btn = gr.Button("Download")
261
+ download_output = gr.File(label="Download File")
262
+
263
+ download_btn.click(fn=download_interface, inputs=[download_choice], outputs=[download_output])
264
+
265
+ # Launch the Gradio app
266
+ demo.launch()