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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -43
app.py CHANGED
@@ -170,21 +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, input_image, predicted_image, 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", "Input Image", "Predicted Image", "Raw Result"])
179
-
180
- # Decode images from BLOB and convert to displayable format
181
- def decode_image(image_blob):
182
- image_np = np.frombuffer(image_blob, dtype=np.uint8)
183
- image = cv2.imdecode(image_np, cv2.IMREAD_COLOR)
184
- return image
185
-
186
- df["Input Image"] = df["Input Image"].apply(lambda x: decode_image(x))
187
- df["Predicted Image"] = df["Predicted Image"].apply(lambda x: decode_image(x))
188
 
189
  return df
190
 
@@ -194,46 +185,57 @@ def download_file(choice):
194
  # Provide the path to the database file
195
  return 'results.db'
196
  elif choice == "Database (.html)":
 
197
  conn = sqlite3.connect('results.db')
198
  c = conn.cursor()
199
- c.execute("SELECT name, patient_id, input_image, predicted_image, result FROM results")
200
  rows = c.fetchall()
201
  conn.close()
202
- df = pd.DataFrame(rows, columns=["Name", "Patient ID", "Input Image", "Predicted Image", "Raw Result"])
203
- html = df.to_html()
204
- with tempfile.NamedTemporaryFile(delete=False, suffix='.html') as f:
205
- f.write(html.encode())
206
- return f.name
 
207
  else:
208
- # Handle other download options if necessary
209
- pass
 
 
 
 
 
 
 
 
 
 
 
210
 
211
  # Initialize the database
212
  init_db()
213
 
214
- # Define the Gradio interface
215
- with gr.Blocks() as demo:
216
- with gr.Tab("YOLOv8 Inference"):
217
- with gr.Row():
218
- input_image = gr.Image(label="Input Image", type="pil")
219
- with gr.Row():
220
- name = gr.Textbox(label="Patient Name")
221
- patient_id = gr.Textbox(label="Patient ID")
222
- with gr.Row():
223
- submit_button = gr.Button("Submit")
224
- predicted_image = gr.Image(label="Predicted Image")
225
- with gr.Row():
226
- result = gr.Textbox(label="Raw Result", lines=5)
227
- submit_button.click(predict_image, inputs=[input_image, name, patient_id], outputs=[predicted_image, result])
228
 
229
- with gr.Tab("View Database"):
230
- view_button = gr.Button("View Database")
231
- database_output = gr.DataFrame(label="Database Records")
232
- view_button.click(view_database, outputs=database_output)
 
 
 
233
 
234
- download_choice = gr.Radio(["Database (.db)", "Database (.html)", "Predicted Image (.png)"], label="Choose the file to download:")
235
- download_button = gr.Button("Download")
236
- download_button.click(download_file, inputs=download_choice, outputs=gr.File())
237
-
238
- # Launch the interface
239
- demo.launch()
 
 
 
 
 
 
 
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
 
 
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()
203
+ c.execute("SELECT predicted_image FROM results ORDER BY id DESC LIMIT 1")
204
+ row = c.fetchone()
205
+ conn.close()
206
+ if row:
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()