File size: 2,767 Bytes
798b4a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
app.py
Gradio entry point
"""

from __future__ import annotations

import tempfile
from pathlib import Path

import gradio as gr

from config import (
    DEFAULT_MAX_FILE_SIZE_MB,
    DEFAULT_MAX_TOKENS,
    IGNORE_DIRS_DEFAULT,
)
from core import build_context


def run(
    zip_file,
    ignored_dirs,
    output_format,
    max_tokens,
    max_size,
    include_hash,
    progress=gr.Progress(),
):

    progress(0.05, desc="Extracting ZIP")

    result = build_context(
        zip_path=zip_file,
        ignored_dirs=ignored_dirs,
        allowed_extensions=None,
        max_size_mb=max_size,
        output_format=output_format,
        max_tokens=max_tokens,
        include_hash=include_hash,
    )

    progress(0.8, desc="Writing output")

    temp = Path(tempfile.mkdtemp())

    downloads = []

    for i, chunk in enumerate(result["chunks"], start=1):

        if len(result["chunks"]) == 1:

            filename = (
                f"context.{result['extension']}"
            )

        else:

            filename = (
                f"context_part{i}.{result['extension']}"
            )

        file = temp / filename

        file.write_text(
            chunk,
            encoding="utf-8",
        )

        downloads.append(str(file))

    progress(1.0, desc="Done")

    return (
        result["statistics"],
        result["tree"],
        result["preview"],
        downloads,
    )


with gr.Blocks(title="Project Context Builder") as demo:

    gr.Markdown("# Project Context Builder")

    zip_file = gr.File(
        label="Project ZIP",
        file_types=[".zip"],
        type="filepath",
    )

    ignored = gr.Textbox(
        label="Ignored folders",
        value=IGNORE_DIRS_DEFAULT,
    )

    output = gr.Radio(
        ["Markdown", "XML"],
        value="Markdown",
        label="Output",
    )

    tokens = gr.Number(
        value=DEFAULT_MAX_TOKENS,
        label="Max Tokens (0 = unlimited)",
    )

    size = gr.Number(
        value=DEFAULT_MAX_FILE_SIZE_MB,
        label="Max File Size (MB)",
    )

    sha = gr.Checkbox(
        value=True,
        label="Include SHA256",
    )

    btn = gr.Button("Build")

    stats = gr.Markdown()

    tree = gr.Textbox(
        label="Project Tree",
        lines=15,
    )

    preview = gr.Textbox(
        label="Preview",
        lines=20,
    )

    downloads = gr.Files(
        label="Download",
    )

    btn.click(
        fn=run,
        inputs=[
            zip_file,
            ignored,
            output,
            tokens,
            size,
            sha,
        ],
        outputs=[
            stats,
            tree,
            preview,
            downloads,
        ],
    )

demo.queue()

if __name__ == "__main__":
    demo.launch()