Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|
| 13 |
+
if os.path.exists(ZIP_FILE):
|
| 14 |
+
with zipfile.ZipFile(ZIP_FILE, 'r') as zip_ref:
|
| 15 |
+
zip_ref.extractall(FIG_DIR)
|
| 16 |
+
print(f"✅ 解压 {ZIP_FILE} 成功,生成 {FIG_DIR}/ 文件夹")
|
| 17 |
+
else:
|
| 18 |
+
print(f"⚠️ {ZIP_FILE} 不存在,无法解压图像目录")
|
| 19 |
+
|
| 20 |
+
@app.route("/")
|
| 21 |
+
def index():
|
| 22 |
+
return "✅ Figure extraction service is running. Visit /extract to get JSON."
|
| 23 |
+
|
| 24 |
+
@app.route("/extract")
|
| 25 |
+
def extract():
|
| 26 |
+
figures = []
|
| 27 |
+
base_url = "https://hf.space/embed/ASC8384/P2P/file/FIG"
|
| 28 |
+
|
| 29 |
+
if not os.path.exists(FIG_DIR):
|
| 30 |
+
return jsonify({"error": "FIG folder not found."}), 500
|
| 31 |
+
|
| 32 |
+
for filename in os.listdir(FIG_DIR):
|
| 33 |
+
if filename.lower().endswith((".png", ".jpg", ".jpeg", ".gif")):
|
| 34 |
+
figures.append({
|
| 35 |
+
"filename": filename,
|
| 36 |
+
"url": f"{base_url}/{filename}",
|
| 37 |
+
"caption": f"Figure: {filename}"
|
| 38 |
+
})
|
| 39 |
+
|
| 40 |
+
return jsonify(figures)
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
app.run(host="0.0.0.0", port=7860)
|