AdityaAdaki commited on
Commit
1265a74
·
1 Parent(s): 4ea3a39

Fix PORT environment variable issue

Browse files
Files changed (2) hide show
  1. Dockerfile +1 -0
  2. main.py +5 -34
Dockerfile CHANGED
@@ -46,6 +46,7 @@ COPY --chown=user . .
46
  ENV PATH="/home/user/.local/bin:${PATH}"
47
  ENV PYTHONPATH="/home/user/.local/lib/python3.9/site-packages:${PYTHONPATH}"
48
  ENV CHROME_BINARY_LOCATION="/usr/bin/google-chrome"
 
49
 
50
  # Run the application
51
  CMD ["python", "main.py"]
 
46
  ENV PATH="/home/user/.local/bin:${PATH}"
47
  ENV PYTHONPATH="/home/user/.local/lib/python3.9/site-packages:${PYTHONPATH}"
48
  ENV CHROME_BINARY_LOCATION="/usr/bin/google-chrome"
49
+ ENV PORT=7860
50
 
51
  # Run the application
52
  CMD ["python", "main.py"]
main.py CHANGED
@@ -29,6 +29,8 @@ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
29
  app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
30
  app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
31
 
 
 
32
  def allowed_file(filename):
33
  return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
34
 
@@ -329,58 +331,27 @@ def capture_screenshot():
329
  height=height
330
  )
331
 
332
- # Set the bounds
333
- bounds = map_state['bounds']
334
- m.fit_bounds([[bounds['south'], bounds['west']],
335
- [bounds['north'], bounds['east']]])
336
-
337
- # Add custom JavaScript to ensure correct zoom
338
- m.get_root().html.add_child(folium.Element(f"""
339
- <script>
340
- document.addEventListener('DOMContentLoaded', function() {{
341
- setTimeout(function() {{
342
- var map = document.querySelector('#map');
343
- if (map && map._leaflet_map) {{
344
- map._leaflet_map.setView([{center['lat']}, {center['lng']}], {zoom});
345
- }}
346
- }}, 1000);
347
- }});
348
- </script>
349
- """))
350
-
351
- # Save the map
352
  map_path = os.path.join(app.static_folder, 'temp_map.html')
353
  m.save(map_path)
354
 
355
- # Increase wait time to ensure map loads completely
356
  time.sleep(1)
357
 
358
  driver = setup_webdriver()
359
  try:
360
- driver.set_window_size(width + 50, height + 50) # Add padding to prevent scrollbars
361
- map_url = f"http://localhost:{app.config['PORT']}/static/temp_map.html"
362
  driver.get(map_url)
363
-
364
- # Wait for map to load and settle
365
  time.sleep(3)
366
-
367
- # Take screenshot
368
  driver.save_screenshot(filepath)
369
 
370
  if polygon_points and len(polygon_points) >= 3:
371
- # Create polygon cutout
372
  img = Image.open(filepath)
373
  mask = create_polygon_mask(img.size, polygon_points)
374
-
375
- # Create cutout image
376
  cutout = Image.new('RGBA', img.size, (0, 0, 0, 0))
377
  cutout.paste(img, mask=mask)
378
-
379
- # Save cutout
380
  cutout_filename = f"cutout_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
381
  cutout_filepath = os.path.join(SCREENSHOT_DIR, cutout_filename)
382
  cutout.save(cutout_filepath)
383
-
384
  return jsonify({
385
  'success': True,
386
  'screenshot_path': f'/static/screenshots/{filename}',
@@ -469,4 +440,4 @@ def upload_file():
469
  return jsonify({'error': 'Invalid file type'}), 400
470
 
471
  if __name__ == '__main__':
472
- app.run(host='0.0.0.0', port=7860)
 
29
  app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
30
  app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
31
 
32
+ PORT = int(os.getenv('PORT', 7860))
33
+
34
  def allowed_file(filename):
35
  return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
36
 
 
331
  height=height
332
  )
333
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
334
  map_path = os.path.join(app.static_folder, 'temp_map.html')
335
  m.save(map_path)
336
 
 
337
  time.sleep(1)
338
 
339
  driver = setup_webdriver()
340
  try:
341
+ driver.set_window_size(width + 50, height + 50)
342
+ map_url = f"http://localhost:{PORT}/static/temp_map.html"
343
  driver.get(map_url)
 
 
344
  time.sleep(3)
 
 
345
  driver.save_screenshot(filepath)
346
 
347
  if polygon_points and len(polygon_points) >= 3:
 
348
  img = Image.open(filepath)
349
  mask = create_polygon_mask(img.size, polygon_points)
 
 
350
  cutout = Image.new('RGBA', img.size, (0, 0, 0, 0))
351
  cutout.paste(img, mask=mask)
 
 
352
  cutout_filename = f"cutout_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
353
  cutout_filepath = os.path.join(SCREENSHOT_DIR, cutout_filename)
354
  cutout.save(cutout_filepath)
 
355
  return jsonify({
356
  'success': True,
357
  'screenshot_path': f'/static/screenshots/{filename}',
 
440
  return jsonify({'error': 'Invalid file type'}), 400
441
 
442
  if __name__ == '__main__':
443
+ app.run(host='0.0.0.0', port=PORT)