Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,31 @@
|
|
| 1 |
import os
|
| 2 |
import zipfile
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
FIG_DIR = "FIG"
|
| 8 |
ZIP_FILE = "FIG.zip"
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
if not os.path.exists(FIG_DIR):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
print(f"⚠️ {ZIP_FILE} 不存在,无法解压图像目录")
|
| 18 |
|
| 19 |
-
# 根路由:确认服务运行中
|
| 20 |
@app.route("/")
|
| 21 |
def index():
|
| 22 |
-
return "✅
|
| 23 |
|
| 24 |
-
# 提供 JSON 图像信息
|
| 25 |
@app.route("/extract")
|
| 26 |
def extract():
|
| 27 |
figures = []
|
| 28 |
base_url = "https://sferewqw--poster.hf.space/file/FIG"
|
| 29 |
-
|
| 30 |
if not os.path.exists(FIG_DIR):
|
| 31 |
-
return jsonify({"error": "
|
| 32 |
|
| 33 |
for filename in os.listdir(FIG_DIR):
|
| 34 |
if filename.lower().endswith((".png", ".jpg", ".jpeg", ".gif")):
|
|
@@ -40,12 +37,5 @@ def extract():
|
|
| 40 |
|
| 41 |
return jsonify(figures)
|
| 42 |
|
| 43 |
-
# 显式文件访问(可选)
|
| 44 |
-
@app.route("/file/FIG/<path:filename>")
|
| 45 |
-
def serve_figure(filename):
|
| 46 |
-
return send_from_directory(FIG_DIR, filename)
|
| 47 |
-
|
| 48 |
-
# 启动 Flask
|
| 49 |
if __name__ == "__main__":
|
| 50 |
app.run(host="0.0.0.0", port=7860)
|
| 51 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import zipfile
|
| 3 |
+
import json
|
| 4 |
+
from flask import Flask, jsonify
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
FIG_DIR = "FIG"
|
| 9 |
ZIP_FILE = "FIG.zip"
|
| 10 |
|
| 11 |
+
# 解压 FIG.zip(仅首次)
|
| 12 |
+
if not os.path.exists(FIG_DIR) and os.path.exists(ZIP_FILE):
|
| 13 |
+
with zipfile.ZipFile(ZIP_FILE, 'r') as zip_ref:
|
| 14 |
+
zip_ref.extractall(FIG_DIR)
|
| 15 |
+
print("✅ 解压完成,生成 FIG/ 文件夹")
|
| 16 |
+
else:
|
| 17 |
+
print("⚠️ FIG 文件夹已存在或 ZIP 不存在")
|
|
|
|
| 18 |
|
|
|
|
| 19 |
@app.route("/")
|
| 20 |
def index():
|
| 21 |
+
return "✅ Service running. Try /extract"
|
| 22 |
|
|
|
|
| 23 |
@app.route("/extract")
|
| 24 |
def extract():
|
| 25 |
figures = []
|
| 26 |
base_url = "https://sferewqw--poster.hf.space/file/FIG"
|
|
|
|
| 27 |
if not os.path.exists(FIG_DIR):
|
| 28 |
+
return jsonify({"error": "FIG folder not found."}), 500
|
| 29 |
|
| 30 |
for filename in os.listdir(FIG_DIR):
|
| 31 |
if filename.lower().endswith((".png", ".jpg", ".jpeg", ".gif")):
|
|
|
|
| 37 |
|
| 38 |
return jsonify(figures)
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
if __name__ == "__main__":
|
| 41 |
app.run(host="0.0.0.0", port=7860)
|
|
|