Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import git
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
import shutil
|
| 6 |
+
|
| 7 |
+
def is_text_file(file_path):
|
| 8 |
+
"""Check if a file is likely to be a text file."""
|
| 9 |
+
try:
|
| 10 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 11 |
+
f.read(1024) # Try to read the first 1024 bytes
|
| 12 |
+
return True
|
| 13 |
+
except UnicodeDecodeError:
|
| 14 |
+
return False
|
| 15 |
+
except Exception:
|
| 16 |
+
return False
|
| 17 |
+
|
| 18 |
+
def clone_and_concatenate(repo_url):
|
| 19 |
+
# Create a temporary directory
|
| 20 |
+
temp_dir = tempfile.mkdtemp()
|
| 21 |
+
output_file = os.path.join(temp_dir, "concatenated_files.txt")
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
# Clone the repository
|
| 25 |
+
git.Repo.clone_from(repo_url, temp_dir)
|
| 26 |
+
|
| 27 |
+
# Walk through all files in the repository
|
| 28 |
+
with open(output_file, 'w', encoding='utf-8') as outfile:
|
| 29 |
+
for root, dirs, files in os.walk(temp_dir):
|
| 30 |
+
# Skip .git directory
|
| 31 |
+
if '.git' in dirs:
|
| 32 |
+
dirs.remove('.git')
|
| 33 |
+
|
| 34 |
+
for file in files:
|
| 35 |
+
if file == "concatenated_files.txt":
|
| 36 |
+
continue
|
| 37 |
+
|
| 38 |
+
file_path = os.path.join(root, file)
|
| 39 |
+
relative_path = os.path.relpath(file_path, temp_dir)
|
| 40 |
+
|
| 41 |
+
# Check if the file is a text file
|
| 42 |
+
if is_text_file(file_path):
|
| 43 |
+
try:
|
| 44 |
+
with open(file_path, 'r', encoding='utf-8') as infile:
|
| 45 |
+
outfile.write(f"\n\n{'='*80}\n")
|
| 46 |
+
outfile.write(f"File: {relative_path}\n")
|
| 47 |
+
outfile.write(f"{'='*80}\n\n")
|
| 48 |
+
outfile.write(infile.read())
|
| 49 |
+
except Exception as e:
|
| 50 |
+
outfile.write(f"\nError reading file {relative_path}: {str(e)}\n")
|
| 51 |
+
|
| 52 |
+
except Exception as e:
|
| 53 |
+
# Clean up the temporary directory
|
| 54 |
+
shutil.rmtree(temp_dir)
|
| 55 |
+
return None, f"Error: {str(e)}"
|
| 56 |
+
|
| 57 |
+
return output_file, "Repository cloned and files concatenated successfully!"
|
| 58 |
+
|
| 59 |
+
# Create the Gradio interface
|
| 60 |
+
iface = gr.Interface(
|
| 61 |
+
fn=clone_and_concatenate,
|
| 62 |
+
inputs=gr.Textbox(label="Git Repository URL"),
|
| 63 |
+
outputs=[
|
| 64 |
+
gr.File(label="Concatenated Files"),
|
| 65 |
+
gr.Textbox(label="Status")
|
| 66 |
+
],
|
| 67 |
+
title="Git Repository File Concatenator",
|
| 68 |
+
description="Enter a Git repository URL to clone it and concatenate all text files into a single downloadable file.",
|
| 69 |
+
examples=[["https://github.com/username/repository"]]
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Launch the app
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
iface.launch()
|