ZaynZhu
commited on
Commit
·
991a405
1
Parent(s):
ddf421d
update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import subprocess, shutil, os, zipfile
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
ROOT = Path(__file__).resolve().parent
|
| 6 |
OUTPUT_DIR = ROOT / "output"
|
| 7 |
ZIP_PATH = ROOT / "output.zip"
|
|
|
|
| 8 |
|
| 9 |
def run_pipeline(model_name_t, model_name_v, result_dir, paper_latex_root, arxiv_url, openai_key, gemini_key):
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if ZIP_PATH.exists():
|
| 14 |
ZIP_PATH.unlink()
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
cmd = [
|
| 18 |
"python", "pipeline.py",
|
| 19 |
"--model_name_t", model_name_t,
|
|
@@ -21,29 +34,64 @@ def run_pipeline(model_name_t, model_name_v, result_dir, paper_latex_root, arxiv
|
|
| 21 |
"--result_dir", result_dir,
|
| 22 |
"--paper_latex_root", paper_latex_root,
|
| 23 |
"--arxiv_url", arxiv_url,
|
| 24 |
-
"--openai_key", openai_key,
|
| 25 |
-
"--gemini_key", gemini_key
|
| 26 |
]
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
except subprocess.TimeoutExpired:
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# 压缩 output 文件夹
|
| 40 |
with zipfile.ZipFile(ZIP_PATH, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 41 |
for root, dirs, files in os.walk(OUTPUT_DIR):
|
| 42 |
for file in files:
|
| 43 |
file_path = Path(root) / file
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# ===================== Gradio UI =====================
|
| 49 |
iface = gr.Interface(
|
|
@@ -58,12 +106,13 @@ iface = gr.Interface(
|
|
| 58 |
gr.Textbox(label="Gemini API Key", placeholder="AIza...", type="password"),
|
| 59 |
],
|
| 60 |
outputs=[
|
| 61 |
-
gr.Textbox(label="Logs"),
|
| 62 |
gr.File(label="Download Output (.zip)")
|
| 63 |
],
|
| 64 |
-
title="PaperShow Pipeline",
|
| 65 |
-
description="输入 arXiv 链接和参数,自动生成 slides + poster,结果打包下载。"
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import subprocess, shutil, os, zipfile, datetime
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
ROOT = Path(__file__).resolve().parent
|
| 6 |
OUTPUT_DIR = ROOT / "output"
|
| 7 |
ZIP_PATH = ROOT / "output.zip"
|
| 8 |
+
LOG_PATH = ROOT / "last_run.log"
|
| 9 |
|
| 10 |
def run_pipeline(model_name_t, model_name_v, result_dir, paper_latex_root, arxiv_url, openai_key, gemini_key):
|
| 11 |
+
start_time = datetime.datetime.now()
|
| 12 |
+
logs = [f"🚀 Starting pipeline at {start_time.strftime('%Y-%m-%d %H:%M:%S')}\n"]
|
| 13 |
+
|
| 14 |
+
# 🧩 确保 output 目录存在(避免 No output generated)
|
| 15 |
+
if not OUTPUT_DIR.exists():
|
| 16 |
+
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 17 |
+
logs.append(f"📁 Created output directory: {OUTPUT_DIR}\n")
|
| 18 |
+
|
| 19 |
+
# 🧹 清理旧输出(但保留空目录)
|
| 20 |
+
for item in OUTPUT_DIR.iterdir():
|
| 21 |
+
if item.is_dir():
|
| 22 |
+
shutil.rmtree(item)
|
| 23 |
+
else:
|
| 24 |
+
item.unlink()
|
| 25 |
if ZIP_PATH.exists():
|
| 26 |
ZIP_PATH.unlink()
|
| 27 |
+
logs.append("🧹 Cleaned previous output and zip files.\n")
|
| 28 |
|
| 29 |
+
# 构造命令
|
| 30 |
cmd = [
|
| 31 |
"python", "pipeline.py",
|
| 32 |
"--model_name_t", model_name_t,
|
|
|
|
| 34 |
"--result_dir", result_dir,
|
| 35 |
"--paper_latex_root", paper_latex_root,
|
| 36 |
"--arxiv_url", arxiv_url,
|
|
|
|
|
|
|
| 37 |
]
|
| 38 |
+
|
| 39 |
+
# 临时设置 API keys(供 pipeline 内部使用)
|
| 40 |
+
os.environ["OPENAI_API_KEY"] = openai_key or ""
|
| 41 |
+
os.environ["GEMINI_API_KEY"] = gemini_key or ""
|
| 42 |
+
|
| 43 |
+
logs.append(f"🧠 Running command: {' '.join(cmd)}\n")
|
| 44 |
+
|
| 45 |
try:
|
| 46 |
+
# 同时捕获 stdout + stderr
|
| 47 |
+
result = subprocess.run(
|
| 48 |
+
cmd, capture_output=True, text=True, timeout=1800
|
| 49 |
+
)
|
| 50 |
+
logs.append("\n======= STDOUT =======\n")
|
| 51 |
+
logs.append(result.stdout)
|
| 52 |
+
logs.append("\n======= STDERR =======\n")
|
| 53 |
+
logs.append(result.stderr)
|
| 54 |
except subprocess.TimeoutExpired:
|
| 55 |
+
msg = "❌ Pipeline timed out (30 min limit)."
|
| 56 |
+
logs.append(msg)
|
| 57 |
+
_write_logs(logs)
|
| 58 |
+
return msg, None
|
| 59 |
except Exception as e:
|
| 60 |
+
msg = f"❌ Pipeline error: {e}"
|
| 61 |
+
logs.append(msg)
|
| 62 |
+
_write_logs(logs)
|
| 63 |
+
return msg, None
|
| 64 |
|
| 65 |
+
# 检查输出目录
|
| 66 |
+
if not any(OUTPUT_DIR.iterdir()):
|
| 67 |
+
msg = "❌ No output generated. Please check logs below."
|
| 68 |
+
logs.append(msg)
|
| 69 |
+
_write_logs(logs)
|
| 70 |
+
return "\n".join(logs), None
|
| 71 |
|
| 72 |
# 压缩 output 文件夹
|
| 73 |
with zipfile.ZipFile(ZIP_PATH, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 74 |
for root, dirs, files in os.walk(OUTPUT_DIR):
|
| 75 |
for file in files:
|
| 76 |
file_path = Path(root) / file
|
| 77 |
+
arcname = file_path.relative_to(OUTPUT_DIR)
|
| 78 |
+
zipf.write(file_path, arcname=arcname)
|
| 79 |
+
logs.append(f"✅ Zipped output folder to {ZIP_PATH}\n")
|
| 80 |
+
|
| 81 |
+
end_time = datetime.datetime.now()
|
| 82 |
+
logs.append(f"🏁 Completed at {end_time.strftime('%Y-%m-%d %H:%M:%S')} (Duration: {(end_time - start_time).seconds}s)\n")
|
| 83 |
+
|
| 84 |
+
# 保存日志到文件
|
| 85 |
+
_write_logs(logs)
|
| 86 |
+
|
| 87 |
+
return "\n".join(logs), ZIP_PATH
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def _write_logs(logs):
|
| 91 |
+
"""将日志写入文件,便于 HF Logs 窗口调试"""
|
| 92 |
+
with open(LOG_PATH, "w", encoding="utf-8") as f:
|
| 93 |
+
f.write("\n".join(logs))
|
| 94 |
+
|
| 95 |
|
| 96 |
# ===================== Gradio UI =====================
|
| 97 |
iface = gr.Interface(
|
|
|
|
| 106 |
gr.Textbox(label="Gemini API Key", placeholder="AIza...", type="password"),
|
| 107 |
],
|
| 108 |
outputs=[
|
| 109 |
+
gr.Textbox(label="Logs", lines=30, max_lines=50),
|
| 110 |
gr.File(label="Download Output (.zip)")
|
| 111 |
],
|
| 112 |
+
title="📄 PaperShow Pipeline",
|
| 113 |
+
description="输入 arXiv 链接和参数,自动生成 slides + poster,结果打包下载。",
|
| 114 |
+
allow_flagging="never",
|
| 115 |
)
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|