Spaces:
Sleeping
Sleeping
ariankhalfani
commited on
Commit
•
813521c
1
Parent(s):
99de52c
Update app.py
Browse files
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,
|
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", "
|
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,
|
200 |
rows = c.fetchall()
|
201 |
conn.close()
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
|
|
207 |
else:
|
208 |
-
|
209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
|
211 |
# Initialize the database
|
212 |
init_db()
|
213 |
|
214 |
-
#
|
215 |
-
with gr.Blocks() as
|
216 |
-
|
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.
|
230 |
-
|
231 |
-
|
232 |
-
|
|
|
|
|
|
|
233 |
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|