Spaces:
Build error
Build error
Update Dockerfile
Browse files- Dockerfile +50 -75
Dockerfile
CHANGED
@@ -72,152 +72,127 @@ RUN mkdir -p /app/templates \
|
|
72 |
&& mkdir -p /app/outputs \
|
73 |
&& mkdir -p /app/temp
|
74 |
|
75 |
-
#
|
76 |
-
COPY app.py /app/
|
77 |
COPY requirements.txt /app/ 2>/dev/null || echo "gradio" > /app/requirements.txt
|
|
|
78 |
|
79 |
-
#
|
80 |
-
RUN
|
81 |
-
|
82 |
-
|
83 |
-
RUN cat > /app/app.py << 'EOF'
|
84 |
-
import gradio as gr
|
85 |
import subprocess
|
86 |
import tempfile
|
87 |
import os
|
88 |
import shutil
|
89 |
from pathlib import Path
|
90 |
|
91 |
-
def compile_latex(latex_code, compiler
|
92 |
-
"""编译LaTeX代码并返回PDF"""
|
93 |
with tempfile.TemporaryDirectory() as temp_dir:
|
94 |
# 写入LaTeX文件
|
95 |
-
tex_file = os.path.join(temp_dir, "document.tex")
|
96 |
-
with open(tex_file, "w", encoding
|
97 |
f.write(latex_code)
|
98 |
|
99 |
try:
|
100 |
# 编译LaTeX
|
101 |
result = subprocess.run(
|
102 |
-
[compiler, "-interaction=nonstopmode", "-output-directory", temp_dir, tex_file],
|
103 |
capture_output=True,
|
104 |
text=True,
|
105 |
timeout=30
|
106 |
)
|
107 |
|
108 |
-
pdf_file = os.path.join(temp_dir, "document.pdf")
|
109 |
-
log_file = os.path.join(temp_dir, "document.log")
|
110 |
|
111 |
# 读取日志
|
112 |
-
log_content = ""
|
113 |
if os.path.exists(log_file):
|
114 |
-
with open(log_file, "r", encoding
|
115 |
log_content = f.read()
|
116 |
|
117 |
if os.path.exists(pdf_file):
|
118 |
# 复制PDF到输出目录
|
119 |
-
output_pdf = "/app/outputs/output.pdf"
|
120 |
shutil.copy2(pdf_file, output_pdf)
|
121 |
-
return output_pdf, f"
|
122 |
else:
|
123 |
-
return None, f"
|
124 |
|
125 |
except subprocess.TimeoutExpired:
|
126 |
-
return None, "编译超时(30
|
127 |
except Exception as e:
|
128 |
-
return None, f"编译出错: {str(e)}"
|
129 |
|
130 |
# 创建Gradio界面
|
131 |
def create_interface():
|
132 |
-
with gr.Blocks(title
|
133 |
-
gr.Markdown("# LaTeX
|
134 |
-
gr.Markdown("基于教程中的完整texlive环境,支持XeLaTeX
|
135 |
|
136 |
with gr.Row():
|
137 |
with gr.Column():
|
138 |
latex_input = gr.Textbox(
|
139 |
-
label
|
140 |
-
placeholder
|
141 |
lines=20,
|
142 |
-
value
|
143 |
-
|
144 |
-
|
145 |
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
|
150 |
-
|
151 |
-
|
152 |
|
153 |
-
|
154 |
这是一个支持中文的LaTeX文档示例。
|
155 |
|
156 |
-
|
157 |
爱因斯坦的质能方程:$E = mc^2$
|
158 |
|
159 |
-
|
160 |
)
|
161 |
|
162 |
compiler = gr.Radio(
|
163 |
-
choices=["xelatex", "pdflatex", "lualatex"],
|
164 |
-
value
|
165 |
-
label
|
166 |
)
|
167 |
|
168 |
-
compile_btn = gr.Button("编译PDF", variant
|
169 |
|
170 |
with gr.Column():
|
171 |
-
output_file = gr.File(label
|
172 |
-
log_output = gr.Textbox(label
|
173 |
|
174 |
compile_btn.click(
|
175 |
fn=compile_latex,
|
176 |
inputs=[latex_input, compiler],
|
177 |
outputs=[output_file, log_output]
|
178 |
)
|
179 |
-
|
180 |
-
# 示例模板
|
181 |
-
gr.Markdown("## 示例模板")
|
182 |
-
with gr.Row():
|
183 |
-
gr.Examples(
|
184 |
-
examples=[
|
185 |
-
["简单中文文档", """\\documentclass{article}
|
186 |
-
\\usepackage{xeCJK}
|
187 |
-
\\setCJKmainfont{SimSun}
|
188 |
-
\\title{测试文档}
|
189 |
-
\\begin{document}
|
190 |
-
\\maketitle
|
191 |
-
你好,世界!
|
192 |
-
\\end{document}"""],
|
193 |
-
["数学公式", """\\documentclass{article}
|
194 |
-
\\usepackage{xeCJK,amsmath}
|
195 |
-
\\setCJKmainfont{SimSun}
|
196 |
-
\\begin{document}
|
197 |
-
\\section{数学公式}
|
198 |
-
行内公式:$\\alpha + \\beta = \\gamma$
|
199 |
-
|
200 |
-
行间公式:
|
201 |
-
$$\\int_{-\\infty}^{\\infty} e^{-x^2} dx = \\sqrt{\\pi}$$
|
202 |
-
\\end{document}"""]
|
203 |
-
],
|
204 |
-
inputs=[gr.Textbox(visible=False), latex_input]
|
205 |
-
)
|
206 |
|
207 |
return demo
|
208 |
|
209 |
-
if __name__ == "__main__":
|
210 |
# 确保输出目录存在
|
211 |
-
os.makedirs("/app/outputs", exist_ok=True)
|
212 |
|
213 |
# 启动应用
|
214 |
demo = create_interface()
|
215 |
demo.launch(
|
216 |
-
server_name
|
217 |
server_port=7860,
|
218 |
share=False
|
219 |
)
|
220 |
-
|
|
|
|
|
|
|
|
|
221 |
|
222 |
# 设置权限
|
223 |
RUN chmod +x /app/app.py
|
|
|
72 |
&& mkdir -p /app/outputs \
|
73 |
&& mkdir -p /app/temp
|
74 |
|
75 |
+
# 如果有本地requirements.txt,复制并安装
|
|
|
76 |
COPY requirements.txt /app/ 2>/dev/null || echo "gradio" > /app/requirements.txt
|
77 |
+
RUN if [ -f /app/requirements.txt ]; then pip3 install --no-cache-dir -r /app/requirements.txt; fi
|
78 |
|
79 |
+
# 创建Python应用文件
|
80 |
+
RUN python3 -c "
|
81 |
+
import os
|
82 |
+
app_code = '''import gradio as gr
|
|
|
|
|
83 |
import subprocess
|
84 |
import tempfile
|
85 |
import os
|
86 |
import shutil
|
87 |
from pathlib import Path
|
88 |
|
89 |
+
def compile_latex(latex_code, compiler=\"xelatex\"):
|
90 |
+
\"\"\"编译LaTeX代码并返回PDF\"\"\"
|
91 |
with tempfile.TemporaryDirectory() as temp_dir:
|
92 |
# 写入LaTeX文件
|
93 |
+
tex_file = os.path.join(temp_dir, \"document.tex\")
|
94 |
+
with open(tex_file, \"w\", encoding=\"utf-8\") as f:
|
95 |
f.write(latex_code)
|
96 |
|
97 |
try:
|
98 |
# 编译LaTeX
|
99 |
result = subprocess.run(
|
100 |
+
[compiler, \"-interaction=nonstopmode\", \"-output-directory\", temp_dir, tex_file],
|
101 |
capture_output=True,
|
102 |
text=True,
|
103 |
timeout=30
|
104 |
)
|
105 |
|
106 |
+
pdf_file = os.path.join(temp_dir, \"document.pdf\")
|
107 |
+
log_file = os.path.join(temp_dir, \"document.log\")
|
108 |
|
109 |
# 读取日志
|
110 |
+
log_content = \"\"
|
111 |
if os.path.exists(log_file):
|
112 |
+
with open(log_file, \"r\", encoding=\"utf-8\", errors=\"ignore\") as f:
|
113 |
log_content = f.read()
|
114 |
|
115 |
if os.path.exists(pdf_file):
|
116 |
# 复制PDF到输出目录
|
117 |
+
output_pdf = \"/app/outputs/output.pdf\"
|
118 |
shutil.copy2(pdf_file, output_pdf)
|
119 |
+
return output_pdf, f\"编译成功!\\n\\n编译日志:\\n{log_content}\"
|
120 |
else:
|
121 |
+
return None, f\"编译失败!\\n\\n错误信息:\\n{result.stderr}\\n\\n日志:\\n{log_content}\"
|
122 |
|
123 |
except subprocess.TimeoutExpired:
|
124 |
+
return None, \"编译超时(30秒)\"
|
125 |
except Exception as e:
|
126 |
+
return None, f\"编译出错: {str(e)}\"
|
127 |
|
128 |
# 创建Gradio界面
|
129 |
def create_interface():
|
130 |
+
with gr.Blocks(title=\"LaTeX编译器 - 支持中文\") as demo:
|
131 |
+
gr.Markdown(\"# LaTeX编译器(支持中文字体)\")
|
132 |
+
gr.Markdown(\"基于教程中的完整texlive环境,支持XeLaTeX编译中文文档\")
|
133 |
|
134 |
with gr.Row():
|
135 |
with gr.Column():
|
136 |
latex_input = gr.Textbox(
|
137 |
+
label=\"LaTeX代码\",
|
138 |
+
placeholder=\"请输入LaTeX代码...\",
|
139 |
lines=20,
|
140 |
+
value=\"\"\"\\\\documentclass{article}
|
141 |
+
\\\\usepackage{xeCJK}
|
142 |
+
\\\\setCJKmainfont{SimSun}
|
143 |
|
144 |
+
\\\\title{中文LaTeX示例}
|
145 |
+
\\\\author{作者姓名}
|
146 |
+
\\\\date{\\\\today}
|
147 |
|
148 |
+
\\\\begin{document}
|
149 |
+
\\\\maketitle
|
150 |
|
151 |
+
\\\\section{介绍}
|
152 |
这是一个支持中文的LaTeX文档示例。
|
153 |
|
154 |
+
\\\\section{数学公式}
|
155 |
爱因斯坦的质能方程:$E = mc^2$
|
156 |
|
157 |
+
\\\\end{document}\"\"\"
|
158 |
)
|
159 |
|
160 |
compiler = gr.Radio(
|
161 |
+
choices=[\"xelatex\", \"pdflatex\", \"lualatex\"],
|
162 |
+
value=\"xelatex\",
|
163 |
+
label=\"编译器选择\"
|
164 |
)
|
165 |
|
166 |
+
compile_btn = gr.Button(\"编译PDF\", variant=\"primary\")
|
167 |
|
168 |
with gr.Column():
|
169 |
+
output_file = gr.File(label=\"生成的PDF\")
|
170 |
+
log_output = gr.Textbox(label=\"编译日志\", lines=15, interactive=False)
|
171 |
|
172 |
compile_btn.click(
|
173 |
fn=compile_latex,
|
174 |
inputs=[latex_input, compiler],
|
175 |
outputs=[output_file, log_output]
|
176 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
|
178 |
return demo
|
179 |
|
180 |
+
if __name__ == \"__main__\":
|
181 |
# 确保输出目录存在
|
182 |
+
os.makedirs(\"/app/outputs\", exist_ok=True)
|
183 |
|
184 |
# 启动应用
|
185 |
demo = create_interface()
|
186 |
demo.launch(
|
187 |
+
server_name=\"0.0.0.0\",
|
188 |
server_port=7860,
|
189 |
share=False
|
190 |
)
|
191 |
+
'''
|
192 |
+
|
193 |
+
with open('/app/app.py', 'w', encoding='utf-8') as f:
|
194 |
+
f.write(app_code)
|
195 |
+
"
|
196 |
|
197 |
# 设置权限
|
198 |
RUN chmod +x /app/app.py
|