Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- main.py +71 -0
- pi.txt +0 -0
- requirements.txt +58 -0
main.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import docx
|
| 2 |
+
from docx.shared import Pt
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Helper to format circled numbers 1-20
|
| 6 |
+
CIRCLED = {
|
| 7 |
+
i: chr(9311 + i) for i in range(1, 21)
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
def load_pi(path="pi.txt"):
|
| 11 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 12 |
+
return f.read().strip()
|
| 13 |
+
|
| 14 |
+
def make_doc(start_seg, end_seg, pi_path="pi.txt"):
|
| 15 |
+
# Segments start from 1
|
| 16 |
+
pi = load_pi(pi_path)
|
| 17 |
+
doc = docx.Document()
|
| 18 |
+
|
| 19 |
+
style = doc.styles["Normal"]
|
| 20 |
+
font = style.font #type: ignore
|
| 21 |
+
font.name = "Malgun Gothic"
|
| 22 |
+
font.size = Pt(10)
|
| 23 |
+
|
| 24 |
+
# Calculate the starting offset for the first line
|
| 25 |
+
first_offset = (start_seg // 10) * 10
|
| 26 |
+
last_offset = ((end_seg - 1) // 10) * 10 # adjust to prevent extra line
|
| 27 |
+
|
| 28 |
+
for offset in range(first_offset, last_offset + 1, 10):
|
| 29 |
+
line_segments = []
|
| 30 |
+
line_start = max(offset + 1, start_seg)
|
| 31 |
+
line_end = min(offset + 10, end_seg)
|
| 32 |
+
for s in range(line_start, line_end + 1):
|
| 33 |
+
idx = (s - 1) * 4
|
| 34 |
+
if idx + 4 > len(pi):
|
| 35 |
+
break
|
| 36 |
+
seg = pi[idx:idx+4]
|
| 37 |
+
n = s - offset # position in line
|
| 38 |
+
circ = CIRCLED.get(n, str(n))
|
| 39 |
+
line_segments.append(f"{seg}{circ}")
|
| 40 |
+
|
| 41 |
+
if line_segments:
|
| 42 |
+
p = doc.add_paragraph()
|
| 43 |
+
run = p.add_run(str(offset))
|
| 44 |
+
run.bold = True
|
| 45 |
+
p.add_run(" | " + " ".join(line_segments))
|
| 46 |
+
|
| 47 |
+
out_name = "pi_segments.docx"
|
| 48 |
+
doc.save(out_name)
|
| 49 |
+
return out_name
|
| 50 |
+
|
| 51 |
+
with gr.Blocks() as demo:
|
| 52 |
+
gr.Markdown("## π Segment DOCX Generator")
|
| 53 |
+
gr.Markdown("This web app generates a DOCX file showing π digits divided into 4-digit segments.\n\n"
|
| 54 |
+
"Segments are numbered starting from 1. Each line contains up to 10 segments, and the line starts with the offset (multiple of 10) in bold.\n\n"
|
| 55 |
+
"The circled number after each segment indicates its position within the line. You can use them as a checkbox after you memorized it.")
|
| 56 |
+
|
| 57 |
+
start_in = gr.Number(label="Start Segment (min 1)", value=1)
|
| 58 |
+
end_in = gr.Number(label="End Segment (max 250000)", value=100)
|
| 59 |
+
out = gr.File(label="Generated DOCX")
|
| 60 |
+
|
| 61 |
+
def run_app(a, b):
|
| 62 |
+
pi_file_path = "pi.txt"
|
| 63 |
+
if b > 250000:
|
| 64 |
+
raise gr.Error("End segment cannot exceed 250,000.")
|
| 65 |
+
return make_doc(int(a), int(b), pi_file_path)
|
| 66 |
+
|
| 67 |
+
btn = gr.Button("Generate")
|
| 68 |
+
btn.click(run_app, [start_in, end_in], out)
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
demo.launch()
|
pi.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==24.1.0
|
| 2 |
+
annotated-doc==0.0.4
|
| 3 |
+
annotated-types==0.7.0
|
| 4 |
+
anyio==4.11.0
|
| 5 |
+
audioop-lts==0.2.2
|
| 6 |
+
brotli==1.2.0
|
| 7 |
+
certifi==2025.11.12
|
| 8 |
+
click==8.3.1
|
| 9 |
+
colorama==0.4.6
|
| 10 |
+
fastapi==0.121.2
|
| 11 |
+
ffmpy==1.0.0
|
| 12 |
+
filelock==3.20.0
|
| 13 |
+
fsspec==2025.10.0
|
| 14 |
+
gradio==5.49.1
|
| 15 |
+
gradio_client==1.13.3
|
| 16 |
+
groovy==0.1.2
|
| 17 |
+
h11==0.16.0
|
| 18 |
+
hf-xet==1.2.0
|
| 19 |
+
httpcore==1.0.9
|
| 20 |
+
httpx==0.28.1
|
| 21 |
+
huggingface_hub==1.1.4
|
| 22 |
+
idna==3.11
|
| 23 |
+
Jinja2==3.1.6
|
| 24 |
+
lxml==6.0.2
|
| 25 |
+
markdown-it-py==4.0.0
|
| 26 |
+
MarkupSafe==3.0.3
|
| 27 |
+
mdurl==0.1.2
|
| 28 |
+
numpy==2.3.5
|
| 29 |
+
orjson==3.11.4
|
| 30 |
+
packaging==25.0
|
| 31 |
+
pandas==2.3.3
|
| 32 |
+
pillow==11.3.0
|
| 33 |
+
pydantic==2.11.10
|
| 34 |
+
pydantic_core==2.33.2
|
| 35 |
+
pydub==0.25.1
|
| 36 |
+
Pygments==2.19.2
|
| 37 |
+
python-dateutil==2.9.0.post0
|
| 38 |
+
python-docx==1.2.0
|
| 39 |
+
python-multipart==0.0.20
|
| 40 |
+
pytz==2025.2
|
| 41 |
+
PyYAML==6.0.3
|
| 42 |
+
rich==14.2.0
|
| 43 |
+
ruff==0.14.5
|
| 44 |
+
safehttpx==0.1.7
|
| 45 |
+
semantic-version==2.10.0
|
| 46 |
+
shellingham==1.5.4
|
| 47 |
+
six==1.17.0
|
| 48 |
+
sniffio==1.3.1
|
| 49 |
+
starlette==0.49.3
|
| 50 |
+
tomlkit==0.13.3
|
| 51 |
+
tqdm==4.67.1
|
| 52 |
+
typer==0.20.0
|
| 53 |
+
typer-slim==0.20.0
|
| 54 |
+
typing-inspection==0.4.2
|
| 55 |
+
typing_extensions==4.15.0
|
| 56 |
+
tzdata==2025.2
|
| 57 |
+
uvicorn==0.38.0
|
| 58 |
+
websockets==15.0.1
|