takatorury commited on
Commit
4068748
·
verified ·
1 Parent(s): 30d42bb

Create Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +233 -0
Dockerfile ADDED
@@ -0,0 +1,233 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 轻量级LaTeX处理器,适用于Hugging Face Spaces
2
+ FROM ubuntu:22.04
3
+
4
+ # 设置环境变量
5
+ ENV DEBIAN_FRONTEND=noninteractive
6
+ ENV TZ=Asia/Shanghai
7
+ ENV PYTHONUNBUFFERED=1
8
+
9
+ # 安装系统依赖
10
+ RUN apt-get update && apt-get install -y \
11
+ # 基础工具
12
+ curl \
13
+ wget \
14
+ git \
15
+ unzip \
16
+ # Python环境
17
+ python3 \
18
+ python3-pip \
19
+ python3-venv \
20
+ # LaTeX环境
21
+ texlive-full \
22
+ texlive-lang-chinese \
23
+ texlive-xetex \
24
+ texlive-luatex \
25
+ # 字体支持
26
+ fonts-noto-cjk \
27
+ fonts-noto-cjk-extra \
28
+ fonts-wqy-microhei \
29
+ fonts-wqy-zenhei \
30
+ # PDF工具
31
+ poppler-utils \
32
+ # 其他工具
33
+ pandoc \
34
+ imagemagick \
35
+ && rm -rf /var/lib/apt/lists/*
36
+
37
+ # 安装中文字体(基于教程中提到的字体)
38
+ RUN mkdir -p /usr/share/fonts/chinese
39
+ COPY fonts/ /usr/share/fonts/chinese/ 2>/dev/null || true
40
+
41
+ # 如果没有本地字体文件,下载一些常用中文字体
42
+ RUN cd /usr/share/fonts/chinese && \
43
+ # 下载思源字体
44
+ wget -q https://github.com/adobe-fonts/source-han-sans/releases/download/2.004R/SourceHanSansCN.zip && \
45
+ unzip -q SourceHanSansCN.zip && \
46
+ rm SourceHanSansCN.zip && \
47
+ # 更新字体缓存
48
+ fc-cache -fv
49
+
50
+ # 设置工作目录
51
+ WORKDIR /app
52
+
53
+ # 安装Python依赖
54
+ RUN pip3 install --no-cache-dir \
55
+ gradio \
56
+ streamlit \
57
+ fastapi \
58
+ uvicorn \
59
+ python-multipart \
60
+ aiofiles \
61
+ jinja2 \
62
+ PyPDF2 \
63
+ reportlab \
64
+ matplotlib \
65
+ pillow \
66
+ numpy \
67
+ pandas
68
+
69
+ # 创建应用目录结构
70
+ RUN mkdir -p /app/templates \
71
+ && mkdir -p /app/uploads \
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
+ # 如果有requirements.txt,安装额外依赖
80
+ RUN if [ -f requirements.txt ]; then pip3 install --no-cache-dir -r requirements.txt; fi
81
+
82
+ # 创建简单的LaTeX处理应用
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="xelatex"):
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="utf-8") as f:
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="utf-8", errors="ignore") as f:
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"编译成功!\n\n编译日志:\n{log_content}"
122
+ else:
123
+ return None, f"编译失败!\n\n错误信息:\n{result.stderr}\n\n日志:\n{log_content}"
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="LaTeX编译器 - 支持中文") as demo:
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="LaTeX代码",
140
+ placeholder="请输入LaTeX代码...",
141
+ lines=20,
142
+ value="""\\documentclass{article}
143
+ \\usepackage{xeCJK}
144
+ \\setCJKmainfont{SimSun}
145
+
146
+ \\title{中文LaTeX示例}
147
+ \\author{作者姓名}
148
+ \\date{\\today}
149
+
150
+ \\begin{document}
151
+ \\maketitle
152
+
153
+ \\section{介绍}
154
+ 这是一个支持中文的LaTeX文档示例。
155
+
156
+ \\section{数学公式}
157
+ 爱因斯坦的质能方程:$E = mc^2$
158
+
159
+ \\end{document}"""
160
+ )
161
+
162
+ compiler = gr.Radio(
163
+ choices=["xelatex", "pdflatex", "lualatex"],
164
+ value="xelatex",
165
+ label="编译器选择"
166
+ )
167
+
168
+ compile_btn = gr.Button("编译PDF", variant="primary")
169
+
170
+ with gr.Column():
171
+ output_file = gr.File(label="生��的PDF")
172
+ log_output = gr.Textbox(label="编译日志", lines=15, interactive=False)
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="0.0.0.0",
217
+ server_port=7860,
218
+ share=False
219
+ )
220
+ EOF
221
+
222
+ # 设置权限
223
+ RUN chmod +x /app/app.py
224
+
225
+ # 暴露端口
226
+ EXPOSE 7860
227
+
228
+ # 健康检查
229
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
230
+ CMD curl -f http://localhost:7860/health || exit 1
231
+
232
+ # 启动命令
233
+ CMD ["python3", "/app/app.py"]