Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -347,20 +347,18 @@ def extract_info(template, text):
|
|
347 |
print(f"Error in extract_info: {e}\n{trace}")
|
348 |
return f"❌ Fehler: {str(e)}", "{}"
|
349 |
@spaces.GPU
|
|
|
350 |
def create_map(df, location_col):
|
351 |
-
#
|
352 |
-
with open("map_debug.log", "w") as
|
353 |
-
|
354 |
-
log_file.write(f"Data shape: {df.shape}, Location column: {location_col}\n")
|
355 |
|
356 |
-
# Create the map
|
357 |
m = folium.Map(
|
358 |
location=[20, 0],
|
359 |
zoom_start=2,
|
360 |
control_scale=True
|
361 |
)
|
362 |
|
363 |
-
# Add base map and controls
|
364 |
folium.TileLayer(
|
365 |
tiles=MAP_TILES["GreenMap"]["url"],
|
366 |
attr=MAP_TILES["GreenMap"]["attr"],
|
@@ -372,78 +370,70 @@ def create_map(df, location_col):
|
|
372 |
Fullscreen().add_to(m)
|
373 |
MeasureControl(position='topright', primary_length_unit='kilometers').add_to(m)
|
374 |
|
375 |
-
# Setup for geocoding and markers
|
376 |
geocoder = SafeGeocoder()
|
377 |
coords = []
|
378 |
marker_cluster = MarkerCluster(name="Locations").add_to(m)
|
379 |
processed_count = 0
|
380 |
|
381 |
-
|
382 |
-
|
383 |
-
log_file.write("\nStarting to process locations...\n")
|
384 |
|
385 |
-
# Process each row in dataframe
|
386 |
for idx, row in df.iterrows():
|
387 |
-
# Log the current row we're processing
|
388 |
-
with open("map_debug.log", "a") as log_file:
|
389 |
-
log_file.write(f"\nProcessing row {idx}\n")
|
390 |
-
|
391 |
if pd.isna(row[location_col]):
|
392 |
-
with open("map_debug.log", "a") as log_file:
|
393 |
-
log_file.write(f" - Empty location, skipping\n")
|
394 |
continue
|
395 |
|
396 |
location = str(row[location_col]).strip()
|
397 |
-
with open("map_debug.log", "a") as log_file:
|
398 |
-
log_file.write(f" - Location: {location}\n")
|
399 |
|
400 |
-
#
|
|
|
|
|
|
|
401 |
additional_info = ""
|
402 |
for col in df.columns:
|
403 |
if col != location_col and not pd.isna(row[col]):
|
404 |
additional_info += f"<br><b>{col}:</b> {row[col]}"
|
405 |
|
406 |
-
# Split location if it contains multiple comma-separated places
|
407 |
try:
|
408 |
locations = [loc.strip() for loc in location.split(',') if loc.strip()]
|
409 |
if not locations:
|
410 |
locations = [location]
|
411 |
except Exception as e:
|
412 |
-
with open("map_debug.log", "a") as
|
413 |
-
|
414 |
locations = [location]
|
|
|
|
|
|
|
|
|
415 |
|
416 |
-
with open("map_debug.log", "a") as log_file:
|
417 |
-
log_file.write(f" - Parsed locations: {locations}\n")
|
418 |
-
|
419 |
-
# Process each individual location
|
420 |
for loc in locations:
|
421 |
-
|
422 |
-
|
|
|
|
|
423 |
|
424 |
-
|
425 |
-
point = geocoder.get_coords(loc)
|
426 |
-
|
427 |
-
with open("map_debug.log", "a") as log_file:
|
428 |
-
log_file.write(f" - Coordinates: {point}\n")
|
429 |
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
|
|
|
|
|
|
|
|
|
|
437 |
</div>
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
# Add the marker to the map
|
446 |
-
try:
|
447 |
folium.Marker(
|
448 |
location=point,
|
449 |
popup=folium.Popup(popup_content, max_width=300),
|
@@ -451,25 +441,27 @@ def create_map(df, location_col):
|
|
451 |
icon=folium.Icon(color="blue", icon="info-sign")
|
452 |
).add_to(marker_cluster)
|
453 |
|
|
|
|
|
|
|
|
|
454 |
coords.append(point)
|
455 |
processed_count += 1
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
|
|
|
|
|
|
462 |
|
463 |
# Fit map bounds if we have coordinates
|
464 |
if coords:
|
465 |
m.fit_bounds(coords)
|
466 |
|
467 |
-
#
|
468 |
-
with open("map_debug.log", "a") as log_file:
|
469 |
-
log_file.write(f"\nMap creation completed at {time.strftime('%Y-%m-%d %H:%M:%S')}\n")
|
470 |
-
log_file.write(f"Processed {processed_count} locations\n")
|
471 |
-
|
472 |
-
# Add custom CSS for better display
|
473 |
custom_css = """
|
474 |
<style>
|
475 |
@import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap');
|
@@ -487,6 +479,11 @@ def create_map(df, location_col):
|
|
487 |
"""
|
488 |
m.get_root().header.add_child(folium.Element(custom_css))
|
489 |
|
|
|
|
|
|
|
|
|
|
|
490 |
return m._repr_html_(), processed_count
|
491 |
|
492 |
custom_css = """
|
|
|
347 |
print(f"Error in extract_info: {e}\n{trace}")
|
348 |
return f"❌ Fehler: {str(e)}", "{}"
|
349 |
@spaces.GPU
|
350 |
+
@spaces.GPU
|
351 |
def create_map(df, location_col):
|
352 |
+
# Start a simple log to track execution
|
353 |
+
with open("map_debug.log", "w") as log:
|
354 |
+
log.write(f"Starting map creation at {time.strftime('%Y-%m-%d %H:%M:%S')}\n")
|
|
|
355 |
|
|
|
356 |
m = folium.Map(
|
357 |
location=[20, 0],
|
358 |
zoom_start=2,
|
359 |
control_scale=True
|
360 |
)
|
361 |
|
|
|
362 |
folium.TileLayer(
|
363 |
tiles=MAP_TILES["GreenMap"]["url"],
|
364 |
attr=MAP_TILES["GreenMap"]["attr"],
|
|
|
370 |
Fullscreen().add_to(m)
|
371 |
MeasureControl(position='topright', primary_length_unit='kilometers').add_to(m)
|
372 |
|
|
|
373 |
geocoder = SafeGeocoder()
|
374 |
coords = []
|
375 |
marker_cluster = MarkerCluster(name="Locations").add_to(m)
|
376 |
processed_count = 0
|
377 |
|
378 |
+
with open("map_debug.log", "a") as log:
|
379 |
+
log.write(f"Processing {len(df)} rows from dataframe\n")
|
|
|
380 |
|
|
|
381 |
for idx, row in df.iterrows():
|
|
|
|
|
|
|
|
|
382 |
if pd.isna(row[location_col]):
|
|
|
|
|
383 |
continue
|
384 |
|
385 |
location = str(row[location_col]).strip()
|
|
|
|
|
386 |
|
387 |
+
# Log the location being processed
|
388 |
+
with open("map_debug.log", "a") as log:
|
389 |
+
log.write(f"Processing location: {location}\n")
|
390 |
+
|
391 |
additional_info = ""
|
392 |
for col in df.columns:
|
393 |
if col != location_col and not pd.isna(row[col]):
|
394 |
additional_info += f"<br><b>{col}:</b> {row[col]}"
|
395 |
|
|
|
396 |
try:
|
397 |
locations = [loc.strip() for loc in location.split(',') if loc.strip()]
|
398 |
if not locations:
|
399 |
locations = [location]
|
400 |
except Exception as e:
|
401 |
+
with open("map_debug.log", "a") as log:
|
402 |
+
log.write(f"Error splitting location '{location}': {str(e)}\n")
|
403 |
locations = [location]
|
404 |
+
|
405 |
+
# Log the parsed locations
|
406 |
+
with open("map_debug.log", "a") as log:
|
407 |
+
log.write(f"Split into locations: {locations}\n")
|
408 |
|
|
|
|
|
|
|
|
|
409 |
for loc in locations:
|
410 |
+
try:
|
411 |
+
# Log the current location
|
412 |
+
with open("map_debug.log", "a") as log:
|
413 |
+
log.write(f"Getting coordinates for: {loc}\n")
|
414 |
|
415 |
+
point = geocoder.get_coords(loc)
|
|
|
|
|
|
|
|
|
416 |
|
417 |
+
if point:
|
418 |
+
# Log the found coordinates
|
419 |
+
with open("map_debug.log", "a") as log:
|
420 |
+
log.write(f"Found coordinates for {loc}: {point}\n")
|
421 |
+
|
422 |
+
# Create popup HTML - this is the part that was disappearing
|
423 |
+
popup_content = f"""
|
424 |
+
<div style="min-width: 200px; max-width: 300px">
|
425 |
+
<h4 style="font-family: 'Source Sans Pro', sans-serif; margin-bottom: 5px;">{loc}</h4>
|
426 |
+
<div style="font-family: 'Source Sans Pro', sans-serif; font-size: 14px;">
|
427 |
+
{additional_info}
|
428 |
+
</div>
|
429 |
</div>
|
430 |
+
"""
|
431 |
+
|
432 |
+
# Log popup creation
|
433 |
+
with open("map_debug.log", "a") as log:
|
434 |
+
log.write(f"Created popup for {loc}\n")
|
435 |
+
|
436 |
+
# Add marker with popup
|
|
|
|
|
437 |
folium.Marker(
|
438 |
location=point,
|
439 |
popup=folium.Popup(popup_content, max_width=300),
|
|
|
441 |
icon=folium.Icon(color="blue", icon="info-sign")
|
442 |
).add_to(marker_cluster)
|
443 |
|
444 |
+
# Log marker addition
|
445 |
+
with open("map_debug.log", "a") as log:
|
446 |
+
log.write(f"Added marker for {loc}\n")
|
447 |
+
|
448 |
coords.append(point)
|
449 |
processed_count += 1
|
450 |
+
else:
|
451 |
+
# Log geocoding failure
|
452 |
+
with open("map_debug.log", "a") as log:
|
453 |
+
log.write(f"No coordinates found for {loc}\n")
|
454 |
+
except Exception as e:
|
455 |
+
# Log any errors in processing this location
|
456 |
+
with open("map_debug.log", "a") as log:
|
457 |
+
log.write(f"Error processing {loc}: {str(e)}\n")
|
458 |
+
log.write(traceback.format_exc() + "\n")
|
459 |
|
460 |
# Fit map bounds if we have coordinates
|
461 |
if coords:
|
462 |
m.fit_bounds(coords)
|
463 |
|
464 |
+
# Custom CSS for map
|
|
|
|
|
|
|
|
|
|
|
465 |
custom_css = """
|
466 |
<style>
|
467 |
@import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap');
|
|
|
479 |
"""
|
480 |
m.get_root().header.add_child(folium.Element(custom_css))
|
481 |
|
482 |
+
# Log completion
|
483 |
+
with open("map_debug.log", "a") as log:
|
484 |
+
log.write(f"Map creation completed at {time.strftime('%Y-%m-%d %H:%M:%S')}\n")
|
485 |
+
log.write(f"Processed {processed_count} locations\n")
|
486 |
+
|
487 |
return m._repr_html_(), processed_count
|
488 |
|
489 |
custom_css = """
|