Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ import io
|
|
| 16 |
def geocode_csv(file):
|
| 17 |
"""Geocode cities from CSV file and return results"""
|
| 18 |
if not file:
|
| 19 |
-
return "β Upload CSV file", gr.DataFrame(),
|
| 20 |
|
| 21 |
try:
|
| 22 |
# Read CSV
|
|
@@ -24,7 +24,7 @@ def geocode_csv(file):
|
|
| 24 |
|
| 25 |
# Check columns
|
| 26 |
if 'name' not in df.columns:
|
| 27 |
-
return "β CSV must have 'name' column", gr.DataFrame(),
|
| 28 |
|
| 29 |
# Get country from filename
|
| 30 |
filename = file.name.split('/')[-1].replace('.csv', '').title()
|
|
@@ -67,15 +67,9 @@ def geocode_csv(file):
|
|
| 67 |
df.loc[idx, 'geocoded'] = False
|
| 68 |
results.append(f"β {city_name}: Error")
|
| 69 |
|
| 70 |
-
# Create CSV
|
| 71 |
csv_content = df.to_csv(index=False)
|
| 72 |
|
| 73 |
-
# Create temporary file
|
| 74 |
-
import tempfile
|
| 75 |
-
temp_file = tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False)
|
| 76 |
-
temp_file.write(csv_content)
|
| 77 |
-
temp_file.close()
|
| 78 |
-
|
| 79 |
# Summary
|
| 80 |
summary = f"""
|
| 81 |
## π Results for {filename}
|
|
@@ -90,10 +84,10 @@ def geocode_csv(file):
|
|
| 90 |
for result in results:
|
| 91 |
summary += f"{result}\n"
|
| 92 |
|
| 93 |
-
return summary, df,
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
-
return f"β Error: {str(e)}", gr.DataFrame(),
|
| 97 |
|
| 98 |
# Fixed interface
|
| 99 |
with gr.Blocks(title="Geo Tool") as app:
|
|
@@ -107,12 +101,12 @@ with gr.Blocks(title="Geo Tool") as app:
|
|
| 107 |
result_output = gr.Markdown(label="Results")
|
| 108 |
csv_output = gr.DataFrame(label="Geocoded Data", interactive=False)
|
| 109 |
|
| 110 |
-
|
| 111 |
|
| 112 |
search_btn.click(
|
| 113 |
fn=geocode_csv,
|
| 114 |
inputs=[file_input],
|
| 115 |
-
outputs=[result_output, csv_output,
|
| 116 |
)
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
|
|
|
| 16 |
def geocode_csv(file):
|
| 17 |
"""Geocode cities from CSV file and return results"""
|
| 18 |
if not file:
|
| 19 |
+
return "β Upload CSV file", gr.DataFrame(), ""
|
| 20 |
|
| 21 |
try:
|
| 22 |
# Read CSV
|
|
|
|
| 24 |
|
| 25 |
# Check columns
|
| 26 |
if 'name' not in df.columns:
|
| 27 |
+
return "β CSV must have 'name' column", gr.DataFrame(), ""
|
| 28 |
|
| 29 |
# Get country from filename
|
| 30 |
filename = file.name.split('/')[-1].replace('.csv', '').title()
|
|
|
|
| 67 |
df.loc[idx, 'geocoded'] = False
|
| 68 |
results.append(f"β {city_name}: Error")
|
| 69 |
|
| 70 |
+
# Create CSV content as text
|
| 71 |
csv_content = df.to_csv(index=False)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
# Summary
|
| 74 |
summary = f"""
|
| 75 |
## π Results for {filename}
|
|
|
|
| 84 |
for result in results:
|
| 85 |
summary += f"{result}\n"
|
| 86 |
|
| 87 |
+
return summary, df, csv_content
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
+
return f"β Error: {str(e)}", gr.DataFrame(), ""
|
| 91 |
|
| 92 |
# Fixed interface
|
| 93 |
with gr.Blocks(title="Geo Tool") as app:
|
|
|
|
| 101 |
result_output = gr.Markdown(label="Results")
|
| 102 |
csv_output = gr.DataFrame(label="Geocoded Data", interactive=False)
|
| 103 |
|
| 104 |
+
csv_text = gr.Textbox(label="π₯ CSV Data (copy and save as .csv file)", lines=10)
|
| 105 |
|
| 106 |
search_btn.click(
|
| 107 |
fn=geocode_csv,
|
| 108 |
inputs=[file_input],
|
| 109 |
+
outputs=[result_output, csv_output, csv_text]
|
| 110 |
)
|
| 111 |
|
| 112 |
if __name__ == "__main__":
|