Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,35 @@
|
|
| 1 |
-
from flask import Flask, jsonify
|
| 2 |
import os
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
|
|
|
| 7 |
FIG_DIR = "/data/FIG"
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
| 11 |
if not os.path.exists(FIG_DIR):
|
| 12 |
os.makedirs(FIG_DIR, exist_ok=True)
|
| 13 |
-
with
|
| 14 |
zip_ref.extractall(FIG_DIR)
|
| 15 |
|
| 16 |
-
@app.route(
|
| 17 |
-
def
|
| 18 |
-
return "Figure extraction service is running. Visit /extract to get JSON."
|
| 19 |
|
| 20 |
-
@app.route(
|
| 21 |
-
def
|
| 22 |
results = []
|
| 23 |
-
for filename in
|
| 24 |
-
if filename.lower().endswith((
|
|
|
|
|
|
|
| 25 |
results.append({
|
| 26 |
-
"
|
| 27 |
-
"caption":
|
| 28 |
})
|
| 29 |
return jsonify(results)
|
| 30 |
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
+
from flask import Flask, jsonify
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from zipfile import ZipFile
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
+
# FIX: 使用 /data/ 路径来避免权限错误
|
| 10 |
FIG_DIR = "/data/FIG"
|
| 11 |
+
ZIP_PATH = "/app/FIG.zip" # 上传的图像包路径
|
| 12 |
|
| 13 |
+
# 解压 FIG.zip 到 FIG_DIR(只运行一次)
|
| 14 |
if not os.path.exists(FIG_DIR):
|
| 15 |
os.makedirs(FIG_DIR, exist_ok=True)
|
| 16 |
+
with ZipFile(ZIP_PATH, 'r') as zip_ref:
|
| 17 |
zip_ref.extractall(FIG_DIR)
|
| 18 |
|
| 19 |
+
@app.route('/')
|
| 20 |
+
def home():
|
| 21 |
+
return "✅ Figure extraction service is running. Visit /extract to get JSON."
|
| 22 |
|
| 23 |
+
@app.route('/extract')
|
| 24 |
+
def extract_figures():
|
| 25 |
results = []
|
| 26 |
+
for filename in os.listdir(FIG_DIR):
|
| 27 |
+
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
|
| 28 |
+
image_url = f"https://{os.environ.get('SPACE_ID')}.hf.space/file/FIG/{filename}"
|
| 29 |
+
caption = os.path.splitext(filename)[0]
|
| 30 |
results.append({
|
| 31 |
+
"image": image_url,
|
| 32 |
+
"caption": caption
|
| 33 |
})
|
| 34 |
return jsonify(results)
|
| 35 |
|