iyadsultan commited on
Commit
3a9e81b
·
1 Parent(s): ac86d10
Files changed (2) hide show
  1. Dockerfile +7 -5
  2. app.py +34 -19
Dockerfile CHANGED
@@ -2,16 +2,18 @@ FROM python:3.10-slim
2
 
3
  WORKDIR /app
4
 
5
- # Create data directory with proper permissions first
6
- RUN mkdir -p /data && \
7
- chmod -R 755 /data
8
 
9
  COPY requirements.txt .
10
  RUN pip install --no-cache-dir -r requirements.txt
11
 
12
  COPY . .
13
 
14
- # Ensure the data directory persists
15
- VOLUME /data
 
16
 
 
17
  CMD ["python", "app.py"]
 
2
 
3
  WORKDIR /app
4
 
5
+ # Create writable directories
6
+ RUN mkdir -p /tmp/human_notes_evaluator && \
7
+ chmod -R 777 /tmp/human_notes_evaluator
8
 
9
  COPY requirements.txt .
10
  RUN pip install --no-cache-dir -r requirements.txt
11
 
12
  COPY . .
13
 
14
+ # Make sure environment variables are set
15
+ ENV DATA_DIR=/tmp/human_notes_evaluator
16
+ ENV PYTHONUNBUFFERED=1
17
 
18
+ # Start the application
19
  CMD ["python", "app.py"]
app.py CHANGED
@@ -46,7 +46,7 @@ NOTE_ORIGINS = [
46
  "I am not sure"
47
  ]
48
 
49
- DATA_DIR = os.environ.get('DATA_DIR', 'data') # Changed from '.' to 'data'
50
  ERROR_LOG = [] # Store recent errors for debugging
51
 
52
  def log_error(error_msg):
@@ -223,25 +223,40 @@ def index():
223
 
224
  if file and '.' in file.filename and file.filename.rsplit('.', 1)[1].lower() == 'csv':
225
  try:
226
- # Create a temporary file path
227
- temp_path = os.path.join(DATA_DIR, 'temp_upload.csv')
228
-
229
- # Save the uploaded file
230
- file.save(temp_path)
231
-
232
- # Validate the CSV format
233
- df = load_and_validate_csv(temp_path)
234
-
235
- # If valid, move to documents.csv
236
- documents_path = os.path.join(DATA_DIR, 'documents.csv')
237
- shutil.move(temp_path, documents_path)
238
-
239
- # Set session cookie
240
- session['evaluator_name'] = evaluator_name
241
-
242
- flash("File uploaded successfully!")
243
- return redirect(url_for('evaluate'))
244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  except Exception as e:
246
  log_error(f"Error during file upload: {str(e)}")
247
  flash(f"Error during file upload: {str(e)}. Please try again.")
 
46
  "I am not sure"
47
  ]
48
 
49
+ DATA_DIR = os.environ.get('DATA_DIR', '/tmp/human_notes_evaluator')
50
  ERROR_LOG = [] # Store recent errors for debugging
51
 
52
  def log_error(error_msg):
 
223
 
224
  if file and '.' in file.filename and file.filename.rsplit('.', 1)[1].lower() == 'csv':
225
  try:
226
+ # Read file directly from memory instead of saving to disk first
227
+ file_content = file.read()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
 
229
+ # Parse CSV from memory
230
+ try:
231
+ # Try to detect encoding
232
+ encoding = chardet.detect(file_content)['encoding']
233
+ log_error(f"Detected encoding: {encoding}")
234
+
235
+ # Load CSV from in-memory content
236
+ csv_buffer = io.StringIO(file_content.decode(encoding))
237
+ df = pd.read_csv(csv_buffer)
238
+
239
+ # Validate the dataframe
240
+ required_columns = ['filename', 'description', 'mrn', 'note']
241
+ missing_columns = [col for col in required_columns if col not in df.columns]
242
+
243
+ if missing_columns:
244
+ raise ValueError(f"Missing required columns: {missing_columns}")
245
+
246
+ # Write to documents.csv only after validation succeeded
247
+ documents_path = os.path.join(DATA_DIR, 'documents.csv')
248
+ df.to_csv(documents_path, index=False)
249
+
250
+ # Set session cookie
251
+ session['evaluator_name'] = evaluator_name
252
+
253
+ flash("File uploaded successfully!")
254
+ return redirect(url_for('evaluate'))
255
+
256
+ except Exception as inner_e:
257
+ log_error(f"Error parsing CSV data: {str(inner_e)}")
258
+ raise ValueError(f"Error parsing CSV data: {str(inner_e)}")
259
+
260
  except Exception as e:
261
  log_error(f"Error during file upload: {str(e)}")
262
  flash(f"Error during file upload: {str(e)}. Please try again.")