Spaces:
Sleeping
Sleeping
ariankhalfani
commited on
Commit
•
e65f39b
1
Parent(s):
813521c
Update app.py
Browse files
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,
|
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 |
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
|
211 |
return temp_file.name
|
212 |
else:
|
213 |
-
|
214 |
|
215 |
# Initialize the database
|
216 |
init_db()
|
217 |
|
218 |
-
# Gradio
|
219 |
-
|
220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
with gr.Row():
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
|
|
|
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()
|