添加了后台运行的功能以及密钥设置
Browse files- .gitignore +2 -0
- Dockerfile +22 -1
- README.md +6 -3
- awake.py +81 -0
- build.sh +4 -0
- init_config.py +27 -0
- models/.gitkeep +0 -0
- start.sh +12 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
|
2 |
+
.env
|
Dockerfile
CHANGED
@@ -1,3 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
FROM python:3.12
|
2 |
|
3 |
WORKDIR /app
|
@@ -22,4 +29,18 @@ USER appuser
|
|
22 |
ENV PATH="/home/appuser/.local/bin:${PATH}"
|
23 |
RUN pip install --user pdf2zh
|
24 |
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
ARG USERNAME
|
4 |
+
ARG PASSWORD
|
5 |
+
ARG OPENAI_API_KEY
|
6 |
+
|
7 |
+
|
8 |
FROM python:3.12
|
9 |
|
10 |
WORKDIR /app
|
|
|
29 |
ENV PATH="/home/appuser/.local/bin:${PATH}"
|
30 |
RUN pip install --user pdf2zh
|
31 |
|
32 |
+
WORKDIR /app
|
33 |
+
COPY . .
|
34 |
+
|
35 |
+
USER root
|
36 |
+
RUN mkdir -p /home/appuser/.cache/babeldoc/models/ && \
|
37 |
+
chmod -R 777 /home/appuser/.cache/ && \
|
38 |
+
mv models/doclayout_yolo_docstructbench_imgsz1024.onnx /home/appuser/.cache/babeldoc/models/
|
39 |
+
|
40 |
+
# ENV HF_ENDPOINT=https://hf-mirror.com
|
41 |
+
# ENV HF_HUB_OFFLINE=1
|
42 |
+
|
43 |
+
USER appuser
|
44 |
+
EXPOSE 7860
|
45 |
+
|
46 |
+
CMD ["/bin/bash", "start.sh"]
|
README.md
CHANGED
@@ -1,9 +1,12 @@
|
|
1 |
---
|
2 |
-
title: PDFMathTranslate
|
|
|
|
|
|
|
3 |
sdk: docker
|
4 |
pinned: false
|
5 |
-
license:
|
6 |
-
short_description:
|
7 |
---
|
8 |
|
9 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: PDFMathTranslate
|
3 |
+
emoji: 📚
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: pink
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
+
license: MIT
|
9 |
+
short_description: https://github.com/Byaidu/PDFMathTranslate
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
awake.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import random
|
3 |
+
import math
|
4 |
+
from itertools import cycle
|
5 |
+
|
6 |
+
def cpu_intensive_task():
|
7 |
+
"""随机选择一个CPU密集型任务执行"""
|
8 |
+
tasks = [
|
9 |
+
_calculate_primes,
|
10 |
+
_matrix_multiplication,
|
11 |
+
_fibonacci_calculation,
|
12 |
+
_pi_calculation
|
13 |
+
]
|
14 |
+
task = random.choice(tasks)
|
15 |
+
task()
|
16 |
+
|
17 |
+
def _calculate_primes():
|
18 |
+
"""计算质数"""
|
19 |
+
n = random.randint(100000, 1000000)
|
20 |
+
sieve = [True] * (n + 1)
|
21 |
+
sieve[0:2] = [False, False]
|
22 |
+
for i in range(2, int(math.sqrt(n)) + 1):
|
23 |
+
if sieve[i]:
|
24 |
+
sieve[i*i : n+1 : i] = [False] * len(sieve[i*i : n+1 : i])
|
25 |
+
|
26 |
+
def _matrix_multiplication():
|
27 |
+
"""矩阵乘法"""
|
28 |
+
size = random.randint(100, 300)
|
29 |
+
matrix = [[random.random() for _ in range(size)] for _ in range(size)]
|
30 |
+
result = [[0] * size for _ in range(size)]
|
31 |
+
for i in range(size):
|
32 |
+
for j in range(size):
|
33 |
+
for k in range(size):
|
34 |
+
result[i][j] += matrix[i][k] * matrix[k][j]
|
35 |
+
|
36 |
+
def _fibonacci_calculation():
|
37 |
+
"""斐波那契数列计算"""
|
38 |
+
n = random.randint(300000, 500000)
|
39 |
+
a, b = 0, 1
|
40 |
+
for _ in range(n):
|
41 |
+
a, b = b, a + b
|
42 |
+
|
43 |
+
def _pi_calculation():
|
44 |
+
"""蒙特卡洛法计算π近似值"""
|
45 |
+
iterations = 10000000
|
46 |
+
count = 0
|
47 |
+
for _ in range(iterations):
|
48 |
+
x = random.random()
|
49 |
+
y = random.random()
|
50 |
+
if x*x + y*y <= 1:
|
51 |
+
count += 1
|
52 |
+
pi = 4 * count / iterations
|
53 |
+
|
54 |
+
def main():
|
55 |
+
try:
|
56 |
+
# 初始随机延迟 (5~15 分钟)
|
57 |
+
initial_delay = random.randint(5, 15)
|
58 |
+
time.sleep(initial_delay * 60)
|
59 |
+
|
60 |
+
# 创建间隔时间循环(10-300秒)
|
61 |
+
intervals = cycle([random.randint(10, 300) for _ in range(50)])
|
62 |
+
|
63 |
+
while True:
|
64 |
+
start_time = time.time()
|
65 |
+
print(f"开始CPU密集型任务 @ {time.strftime('%Y-%m-%d %H:%M:%S')}")
|
66 |
+
|
67 |
+
cpu_intensive_task()
|
68 |
+
|
69 |
+
elapsed = time.time() - start_time
|
70 |
+
print(f"任务完成,耗时: {elapsed:.2f}秒")
|
71 |
+
|
72 |
+
# 动态调整间隔时间
|
73 |
+
delay = next(intervals)
|
74 |
+
print(f"下次任务将在 {delay} 分钟后执行...")
|
75 |
+
time.sleep(delay * 60)
|
76 |
+
|
77 |
+
except KeyboardInterrupt:
|
78 |
+
print("\n任务调度已停止")
|
79 |
+
|
80 |
+
if __name__ == "__main__":
|
81 |
+
main()
|
build.sh
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
docker build -t pdf-math-translator .
|
3 |
+
docker run --rm -t -p 7860:7860 --name pdf --env-file .env pdf-math-translator
|
4 |
+
# docker exec -it pdf /bin/bash
|
init_config.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
|
5 |
+
OPENAI_API_KEY: str = os.environ.get("OPENAI_API_KEY", "")
|
6 |
+
|
7 |
+
if not OPENAI_API_KEY:
|
8 |
+
print("You Should Set OPENAI_API_KEY Enviroment.")
|
9 |
+
exit(-1)
|
10 |
+
|
11 |
+
config = {
|
12 |
+
"USE_MODELSCOPE": "0",
|
13 |
+
"translators": [
|
14 |
+
{
|
15 |
+
"name": "openai",
|
16 |
+
"envs": {
|
17 |
+
# 设置环境变量
|
18 |
+
"OPENAI_BASE_URL": "https://megatrump-oneapi.hf.space/v1",
|
19 |
+
"OPENAI_API_KEY": OPENAI_API_KEY,
|
20 |
+
"OPENAI_MODEL": "gpt-4o-mini",
|
21 |
+
}
|
22 |
+
}
|
23 |
+
]
|
24 |
+
}
|
25 |
+
|
26 |
+
with open("config.json", "w") as f:
|
27 |
+
json.dump(config, f)
|
models/.gitkeep
ADDED
File without changes
|
start.sh
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# 根据环境变量初始化用户名以及密码
|
3 |
+
echo "${USERNAME},${PASSWORD}" > users.txt
|
4 |
+
|
5 |
+
# 启动一个后台任务避免 Space 进入 Sleep 状态
|
6 |
+
python3 awake.py &
|
7 |
+
|
8 |
+
# 初始化环境变量
|
9 |
+
python3 init_config.py
|
10 |
+
|
11 |
+
# 启动 Server
|
12 |
+
pdf2zh -i --config config.json --serverport 7860 -t 5 --authorized users.txt
|