rairo commited on
Commit
9fcc741
·
verified ·
1 Parent(s): 6eb56d5

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +6 -8
main.py CHANGED
@@ -22,14 +22,15 @@ app = Flask(__name__)
22
  CORS(app)
23
 
24
  # --- App Configuration ---
25
- # --- FIX 1: Switched to a persistent file-based SQLite database ---
26
- # This ensures data survives between requests on Hugging Face Spaces.
27
- DB_FOLDER = 'data'
28
  DB_PATH = os.path.join(DB_FOLDER, 'products.db')
29
- os.makedirs(DB_FOLDER, exist_ok=True)
 
30
  app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_PATH}'
31
  app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
32
- app.config['UPLOAD_FOLDER'] = 'uploads'
33
  os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
34
 
35
  # --- File Upload Configuration ---
@@ -124,8 +125,6 @@ def process_uploaded_file(filepath, filename):
124
 
125
  try:
126
  file_ext = filename.rsplit('.', 1)[1].lower()
127
- # --- FIX 2: Robustly parse the second column (index 1) for names ---
128
- # The user's uploaded `list.csv` clearly has the product name in the second column.
129
  if file_ext == 'csv':
130
  df = pd.read_csv(filepath, header=None, usecols=[1], names=['product_name'])
131
  elif file_ext in ['xls', 'xlsx']:
@@ -166,7 +165,6 @@ def process_uploaded_file(filepath, filename):
166
  "primary_category": best_hs_desc or "N/A", "status": ""
167
  }
168
  try:
169
- # Each operation needs its own app context to interact with the database
170
  with app.app_context():
171
  existing_product = Product.query.filter_by(name=validated_name).first()
172
  if existing_product:
 
22
  CORS(app)
23
 
24
  # --- App Configuration ---
25
+ # --- FIX: Use an absolute path for the database to ensure correct file location on server environments ---
26
+ basedir = os.path.abspath(os.path.dirname(__file__))
27
+ DB_FOLDER = os.path.join(basedir, 'data')
28
  DB_PATH = os.path.join(DB_FOLDER, 'products.db')
29
+ os.makedirs(DB_FOLDER, exist_ok=True) # Ensure the data directory exists
30
+
31
  app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_PATH}'
32
  app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
33
+ app.config['UPLOAD_FOLDER'] = os.path.join(basedir, 'uploads') # Also make upload folder absolute
34
  os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
35
 
36
  # --- File Upload Configuration ---
 
125
 
126
  try:
127
  file_ext = filename.rsplit('.', 1)[1].lower()
 
 
128
  if file_ext == 'csv':
129
  df = pd.read_csv(filepath, header=None, usecols=[1], names=['product_name'])
130
  elif file_ext in ['xls', 'xlsx']:
 
165
  "primary_category": best_hs_desc or "N/A", "status": ""
166
  }
167
  try:
 
168
  with app.app_context():
169
  existing_product = Product.query.filter_by(name=validated_name).first()
170
  if existing_product: