Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,37 @@
|
|
| 1 |
import os
|
| 2 |
import zipfile
|
| 3 |
-
from flask import Flask, jsonify
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
FIG_DIR = "FIG"
|
| 8 |
ZIP_FILE = "FIG.zip"
|
| 9 |
|
| 10 |
-
# 自动解压 FIG.zip
|
| 11 |
if not os.path.exists(FIG_DIR):
|
| 12 |
if os.path.exists(ZIP_FILE):
|
| 13 |
with zipfile.ZipFile(ZIP_FILE, 'r') as zip_ref:
|
| 14 |
zip_ref.extractall(FIG_DIR)
|
| 15 |
-
print("✅ 解压
|
| 16 |
else:
|
| 17 |
-
print("⚠️
|
| 18 |
|
|
|
|
| 19 |
@app.route("/")
|
| 20 |
-
def
|
| 21 |
-
return "✅
|
| 22 |
|
|
|
|
| 23 |
@app.route("/extract")
|
| 24 |
def extract():
|
| 25 |
-
base_url = "https://sferewqw--poster.hf.space/file/FIG"
|
| 26 |
figures = []
|
|
|
|
| 27 |
|
| 28 |
if not os.path.exists(FIG_DIR):
|
| 29 |
-
return jsonify({"error": "
|
| 30 |
|
| 31 |
for filename in os.listdir(FIG_DIR):
|
| 32 |
-
if filename.lower().endswith((".png", ".jpg", ".jpeg")):
|
| 33 |
figures.append({
|
| 34 |
"filename": filename,
|
| 35 |
"url": f"{base_url}/{filename}",
|
|
@@ -38,6 +40,12 @@ def extract():
|
|
| 38 |
|
| 39 |
return jsonify(figures)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
if __name__ == "__main__":
|
| 42 |
app.run(host="0.0.0.0", port=7860)
|
| 43 |
|
|
|
|
| 1 |
import os
|
| 2 |
import zipfile
|
| 3 |
+
from flask import Flask, jsonify, send_from_directory
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
FIG_DIR = "FIG"
|
| 8 |
ZIP_FILE = "FIG.zip"
|
| 9 |
|
| 10 |
+
# 自动解压 FIG.zip(如果 FIG 文件夹不存在)
|
| 11 |
if not os.path.exists(FIG_DIR):
|
| 12 |
if os.path.exists(ZIP_FILE):
|
| 13 |
with zipfile.ZipFile(ZIP_FILE, 'r') as zip_ref:
|
| 14 |
zip_ref.extractall(FIG_DIR)
|
| 15 |
+
print(f"✅ 解压 {ZIP_FILE} 成功,生成 {FIG_DIR}/ 文件夹")
|
| 16 |
else:
|
| 17 |
+
print(f"⚠️ {ZIP_FILE} 不存在,无法解压图像目录")
|
| 18 |
|
| 19 |
+
# 根路由:确认服务运行中
|
| 20 |
@app.route("/")
|
| 21 |
+
def index():
|
| 22 |
+
return "✅ Figure extraction service is running. Visit /extract to get JSON."
|
| 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": "❌ 图像目录 FIG/ 不存在"}), 500
|
| 32 |
|
| 33 |
for filename in os.listdir(FIG_DIR):
|
| 34 |
+
if filename.lower().endswith((".png", ".jpg", ".jpeg", ".gif")):
|
| 35 |
figures.append({
|
| 36 |
"filename": filename,
|
| 37 |
"url": f"{base_url}/{filename}",
|
|
|
|
| 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 |
|