ariankhalfani commited on
Commit
d781ad4
1 Parent(s): 2d00553

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -13
app.py CHANGED
@@ -9,6 +9,7 @@ from io import BytesIO
9
  import tempfile
10
  import pandas as pd
11
  import os
 
12
 
13
  # Load YOLOv8 model
14
  model = YOLO("best.pt")
@@ -203,30 +204,37 @@ def view_database():
203
 
204
  # Function to download database or HTML file
205
  def download_file(choice):
 
206
  directory = tempfile.gettempdir()
207
-
208
- # Ensure the directory exists
209
- if not os.path.exists(directory):
210
- os.makedirs(directory)
211
-
212
  db_file_path = os.path.join(directory, 'results.db')
213
 
214
  if choice == "Database (.db)":
215
- # Return the correct database file path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
  return db_file_path
217
 
218
  elif choice == "Database (.html)":
219
- # Check if the database file exists
220
- if not os.path.isfile(db_file_path):
221
- raise FileNotFoundError(f"Database file not found at path: {db_file_path}")
222
-
223
  # Connect to the SQLite database
224
  conn = sqlite3.connect(db_file_path)
225
-
226
  try:
227
  # Attempt to read the results table into a DataFrame
228
  df = pd.read_sql_query("SELECT * FROM results", conn)
229
  except pd.errors.DatabaseError as e:
 
230
  conn.close()
231
  raise ValueError("Table 'results' does not exist in the database.") from e
232
 
@@ -243,7 +251,7 @@ def download_file(choice):
243
  return html_file_path
244
  else:
245
  raise ValueError("Invalid choice. Please select a valid format.")
246
-
247
  # Initialize the database
248
  init_db()
249
 
@@ -309,7 +317,7 @@ with gr.Blocks() as demo:
309
  download_btn = gr.Button("Download")
310
  download_output = gr.File(label="Download File")
311
 
312
- download_btn.click(fn=download_interface, inputs=[download_choice], outputs=gr.File())
313
 
314
  # Launch the Gradio app
315
  demo.launch()
 
9
  import tempfile
10
  import pandas as pd
11
  import os
12
+ from pathlib import Path
13
 
14
  # Load YOLOv8 model
15
  model = YOLO("best.pt")
 
204
 
205
  # Function to download database or HTML file
206
  def download_file(choice):
207
+ # Define a temporary directory to store the database file
208
  directory = tempfile.gettempdir()
 
 
 
 
 
209
  db_file_path = os.path.join(directory, 'results.db')
210
 
211
  if choice == "Database (.db)":
212
+ # Ensure the database file exists
213
+ if not os.path.exists(db_file_path):
214
+ # Create and populate the database with dummy data if needed
215
+ conn = sqlite3.connect(db_file_path)
216
+ # Example table creation (if not already exists)
217
+ conn.execute('''CREATE TABLE IF NOT EXISTS results (
218
+ id INTEGER PRIMARY KEY,
219
+ name TEXT NOT NULL,
220
+ value TEXT NOT NULL
221
+ );''')
222
+ conn.execute("INSERT INTO results (name, value) VALUES ('example', '123')")
223
+ conn.commit()
224
+ conn.close()
225
+
226
+ # Return the database file path for download
227
  return db_file_path
228
 
229
  elif choice == "Database (.html)":
 
 
 
 
230
  # Connect to the SQLite database
231
  conn = sqlite3.connect(db_file_path)
232
+
233
  try:
234
  # Attempt to read the results table into a DataFrame
235
  df = pd.read_sql_query("SELECT * FROM results", conn)
236
  except pd.errors.DatabaseError as e:
237
+ # Handle the case where the table doesn't exist
238
  conn.close()
239
  raise ValueError("Table 'results' does not exist in the database.") from e
240
 
 
251
  return html_file_path
252
  else:
253
  raise ValueError("Invalid choice. Please select a valid format.")
254
+
255
  # Initialize the database
256
  init_db()
257
 
 
317
  download_btn = gr.Button("Download")
318
  download_output = gr.File(label="Download File")
319
 
320
+ download_btn.click(download_file, inputs=choice, outputs=download_file_output)
321
 
322
  # Launch the Gradio app
323
  demo.launch()