File size: 1,873 Bytes
6f74dd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3ff628
6f74dd4
 
 
 
a886fc9
6f74dd4
 
 
a886fc9
 
17baaab
 
a886fc9
 
6f74dd4
 
 
 
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
import gradio as gr
from repo2txt.decoder import (
    clone_repo,
    get_directory_structure,
    extract_all_files_contents,
    write_output_file,
    cleanup,
)


def process_repository(repo_url_or_shorthand):
    """Process the GitHub repository and return the content of the output file."""
    # Define the directory to clone into
    clone_dir = "temp_repo"
    output_file = "output.txt"

    try:
        # Clone the repository
        clone_repo(repo_url_or_shorthand, clone_dir)

        # Get directory structure and file contents
        directory_structure = get_directory_structure(clone_dir)
        file_contents = extract_all_files_contents(clone_dir)

        # Write output to file
        write_output_file(output_file, directory_structure, file_contents)

        # Read the content of the output file
        with open(output_file, "r", encoding="utf-8") as file:
            output_content = file.read()

        # Cleanup
        cleanup(clone_dir)
        # Return the output file path for Gradio
        return output_content, output_file

    except Exception as e:
        return f"An error occurred: {e}", None


# Define Gradio interface
with gr.Blocks(theme="gradio/soft", title="repo2txt") as demo:
    gr.Markdown("# repo2txt")

    with gr.Row():
        repo_url_input = gr.Textbox(
            label="GitHub Repository URL or Shorthand",placeholder="e.g., user/repo or https://github.com/user/repo")

    process_button = gr.Button("Process Repository")

    with gr.Row():
        txt_output = gr.File(label="Download txt file")
    with gr.Row():
        result_output = gr.Textbox(label="Result",lines=1,placeholder="Processing result will be shown here")

    process_button.click(process_repository, inputs=repo_url_input, outputs=[result_output, txt_output])

# Launch the interface
if __name__ == "__main__":
    demo.launch()