Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -82,6 +82,74 @@ class SafeGeocoder:
|
|
| 82 |
self.cache[location] = None
|
| 83 |
return None
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# Corrected model loading function based on official usage example
|
| 87 |
|
|
|
|
| 82 |
self.cache[location] = None
|
| 83 |
return None
|
| 84 |
|
| 85 |
+
def process_excel(file, places_column):
|
| 86 |
+
if file is None:
|
| 87 |
+
return None, "No file uploaded", None
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
if hasattr(file, 'name'):
|
| 91 |
+
df = pd.read_excel(file.name)
|
| 92 |
+
elif isinstance(file, bytes):
|
| 93 |
+
df = pd.read_excel(io.BytesIO(file))
|
| 94 |
+
else:
|
| 95 |
+
df = pd.read_excel(file)
|
| 96 |
+
|
| 97 |
+
print(f"Spalten in der Excel-Tabelle: {list(df.columns)}")
|
| 98 |
+
|
| 99 |
+
if places_column not in df.columns:
|
| 100 |
+
return None, f"Spalte '{places_column}' wurde in der Excel-Datei nicht gefunden. Verfügbare Spalten: {', '.join(df.columns)}", None
|
| 101 |
+
|
| 102 |
+
# Create coordinates columns
|
| 103 |
+
df['latitude'] = None
|
| 104 |
+
df['longitude'] = None
|
| 105 |
+
|
| 106 |
+
geocoder = SafeGeocoder()
|
| 107 |
+
coords = []
|
| 108 |
+
processed_count = 0
|
| 109 |
+
|
| 110 |
+
# Geocode each location and store coordinates in the DataFrame
|
| 111 |
+
for idx, row in df.iterrows():
|
| 112 |
+
if pd.isna(row[places_column]):
|
| 113 |
+
continue
|
| 114 |
+
|
| 115 |
+
location = str(row[places_column]).strip()
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
locations = [loc.strip() for loc in location.split(',') if loc.strip()]
|
| 119 |
+
if not locations:
|
| 120 |
+
locations = [location]
|
| 121 |
+
except:
|
| 122 |
+
locations = [location]
|
| 123 |
+
|
| 124 |
+
for loc in locations:
|
| 125 |
+
point = geocoder.get_coords(loc)
|
| 126 |
+
if point:
|
| 127 |
+
df.at[idx, 'latitude'] = point[0]
|
| 128 |
+
df.at[idx, 'longitude'] = point[1]
|
| 129 |
+
coords.append(point)
|
| 130 |
+
processed_count += 1
|
| 131 |
+
break # Use first successfully geocoded location
|
| 132 |
+
|
| 133 |
+
# Create the map
|
| 134 |
+
map_html, _ = create_map(df, places_column)
|
| 135 |
+
|
| 136 |
+
# Save the updated DataFrame to a new Excel file
|
| 137 |
+
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
|
| 138 |
+
processed_path = tmp.name
|
| 139 |
+
df.to_excel(processed_path, index=False)
|
| 140 |
+
|
| 141 |
+
total_locations = df[places_column].count()
|
| 142 |
+
success_rate = (processed_count / total_locations * 100) if total_locations > 0 else 0
|
| 143 |
+
|
| 144 |
+
stats = f"Gefunden: {processed_count} von {total_locations} Orten ({success_rate:.1f}%)"
|
| 145 |
+
|
| 146 |
+
return map_html, stats, processed_path
|
| 147 |
+
except Exception as e:
|
| 148 |
+
import traceback
|
| 149 |
+
trace = traceback.format_exc()
|
| 150 |
+
print(f"Error processing file: {e}\n{trace}")
|
| 151 |
+
return None, f"Fehler bei der Verarbeitung der Datei: {str(e)}", None
|
| 152 |
+
|
| 153 |
|
| 154 |
# Corrected model loading function based on official usage example
|
| 155 |
|