Spaces:
Sleeping
Sleeping
fix
Browse files- app.py +11 -6
- create_map_poster.py +4 -4
app.py
CHANGED
|
@@ -5,6 +5,7 @@ Gradio Web Interface for City Map Poster Generator
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
import json
|
|
|
|
| 8 |
|
| 9 |
# Monkey-patch gradio_client bug before importing gradio
|
| 10 |
# Fix for: TypeError: argument of type 'bool' is not iterable
|
|
@@ -253,7 +254,10 @@ def generate_poster(
|
|
| 253 |
|
| 254 |
# Generate output filename (using English/Slugified names)
|
| 255 |
en_city = translate(selected_location, "en")
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
try:
|
| 259 |
# Wrap the generator to yield status updates and final result
|
|
@@ -289,17 +293,17 @@ def generate_poster(
|
|
| 289 |
elif "Done" in status:
|
| 290 |
status_display = "完成!"
|
| 291 |
|
| 292 |
-
yield None, f"⏳ {status_display}"
|
| 293 |
|
| 294 |
progress(1.0, desc="完成!")
|
| 295 |
|
| 296 |
-
yield output_file, f"✅ 海报生成成功!保存至: {output_file}"
|
| 297 |
|
| 298 |
except Exception as e:
|
| 299 |
import traceback
|
| 300 |
|
| 301 |
traceback.print_exc()
|
| 302 |
-
yield None, f"❌ 生成失败: {str(e)}"
|
| 303 |
|
| 304 |
|
| 305 |
def update_provinces(country, lang="en"):
|
|
@@ -532,6 +536,7 @@ def create_interface():
|
|
| 532 |
output_status = gr.Textbox(label="状态", interactive=False)
|
| 533 |
|
| 534 |
download_btn = gr.DownloadButton(label="📥 下载海报", visible=False)
|
|
|
|
| 535 |
|
| 536 |
# --- Event Handlers ---
|
| 537 |
|
|
@@ -677,10 +682,10 @@ def create_interface():
|
|
| 677 |
lang_radio,
|
| 678 |
layers_checkbox,
|
| 679 |
],
|
| 680 |
-
outputs=[output_image, output_status],
|
| 681 |
).then(
|
| 682 |
fn=on_generate_complete,
|
| 683 |
-
inputs=[
|
| 684 |
outputs=[output_image, output_status, download_btn],
|
| 685 |
)
|
| 686 |
|
|
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
import json
|
| 8 |
+
import tempfile
|
| 9 |
|
| 10 |
# Monkey-patch gradio_client bug before importing gradio
|
| 11 |
# Fix for: TypeError: argument of type 'bool' is not iterable
|
|
|
|
| 254 |
|
| 255 |
# Generate output filename (using English/Slugified names)
|
| 256 |
en_city = translate(selected_location, "en")
|
| 257 |
+
temp_dir = tempfile.gettempdir()
|
| 258 |
+
output_file = cmp.generate_output_filename(
|
| 259 |
+
en_city, theme_name, output_format, directory=temp_dir
|
| 260 |
+
)
|
| 261 |
|
| 262 |
try:
|
| 263 |
# Wrap the generator to yield status updates and final result
|
|
|
|
| 293 |
elif "Done" in status:
|
| 294 |
status_display = "完成!"
|
| 295 |
|
| 296 |
+
yield None, f"⏳ {status_display}", None
|
| 297 |
|
| 298 |
progress(1.0, desc="完成!")
|
| 299 |
|
| 300 |
+
yield output_file, f"✅ 海报生成成功!保存至: {output_file}", output_file
|
| 301 |
|
| 302 |
except Exception as e:
|
| 303 |
import traceback
|
| 304 |
|
| 305 |
traceback.print_exc()
|
| 306 |
+
yield None, f"❌ 生成失败: {str(e)}", None
|
| 307 |
|
| 308 |
|
| 309 |
def update_provinces(country, lang="en"):
|
|
|
|
| 536 |
output_status = gr.Textbox(label="状态", interactive=False)
|
| 537 |
|
| 538 |
download_btn = gr.DownloadButton(label="📥 下载海报", visible=False)
|
| 539 |
+
poster_state = gr.State()
|
| 540 |
|
| 541 |
# --- Event Handlers ---
|
| 542 |
|
|
|
|
| 682 |
lang_radio,
|
| 683 |
layers_checkbox,
|
| 684 |
],
|
| 685 |
+
outputs=[output_image, output_status, poster_state],
|
| 686 |
).then(
|
| 687 |
fn=on_generate_complete,
|
| 688 |
+
inputs=[poster_state, output_status],
|
| 689 |
outputs=[output_image, output_status, download_btn],
|
| 690 |
)
|
| 691 |
|
create_map_poster.py
CHANGED
|
@@ -43,18 +43,18 @@ def load_fonts():
|
|
| 43 |
FONTS = load_fonts()
|
| 44 |
|
| 45 |
|
| 46 |
-
def generate_output_filename(city, theme_name, output_format):
|
| 47 |
"""
|
| 48 |
Generate unique output filename with city, theme, and datetime.
|
| 49 |
"""
|
| 50 |
-
if not os.path.exists(
|
| 51 |
-
os.makedirs(
|
| 52 |
|
| 53 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 54 |
city_slug = city.lower().replace(" ", "_")
|
| 55 |
ext = output_format.lower()
|
| 56 |
filename = f"{city_slug}_{theme_name}_{timestamp}.{ext}"
|
| 57 |
-
return os.path.join(
|
| 58 |
|
| 59 |
|
| 60 |
def get_available_themes():
|
|
|
|
| 43 |
FONTS = load_fonts()
|
| 44 |
|
| 45 |
|
| 46 |
+
def generate_output_filename(city, theme_name, output_format, directory=POSTERS_DIR):
|
| 47 |
"""
|
| 48 |
Generate unique output filename with city, theme, and datetime.
|
| 49 |
"""
|
| 50 |
+
if not os.path.exists(directory):
|
| 51 |
+
os.makedirs(directory)
|
| 52 |
|
| 53 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 54 |
city_slug = city.lower().replace(" ", "_")
|
| 55 |
ext = output_format.lower()
|
| 56 |
filename = f"{city_slug}_{theme_name}_{timestamp}.{ext}"
|
| 57 |
+
return os.path.join(directory, filename)
|
| 58 |
|
| 59 |
|
| 60 |
def get_available_themes():
|