Spaces:
Running
Running
import gradio as gr | |
import subprocess | |
import tempfile | |
import os | |
import shutil | |
from urllib.parse import urlparse | |
import re | |
def get_repo_details(repo_url): | |
"""Extract username and repo name from GitHub URL or shorthand.""" | |
# Handle GitHub shorthand (user/repo) | |
if '/' in repo_url and len(repo_url.split('/')) == 2: | |
username, repo = repo_url.split('/') | |
return username, repo.replace('.git', '') | |
# Handle full GitHub URL | |
try: | |
# Parse URL and path | |
parsed = urlparse(repo_url) | |
path_parts = parsed.path.strip('/').split('/') | |
# Extract username and repo | |
if len(path_parts) >= 2: | |
username = path_parts[0] | |
repo = path_parts[1].replace('.git', '') | |
return username, repo | |
except: | |
pass | |
return None, None | |
def format_filename(repo_url, branch): | |
"""Generate filename based on repository details.""" | |
username, repo_name = get_repo_details(repo_url) | |
if not username or not repo_name: | |
return "repository-output.txt" | |
# Clean and format branch name | |
branch = branch if branch else "main" | |
branch = re.sub(r'[^\w\-\.]', '-', branch) | |
return f"{username}-{repo_name}-{branch}.txt" | |
[Previous helper functions remain the same] | |
# Create the Gradio interface | |
with gr.Blocks(title="Repomix Web Interface", theme=gr.themes.Soft()) as demo: | |
gr.Markdown(""" | |
# π¦ Repomix Web Interface | |
Pack your GitHub repository into a single, AI-friendly file. Perfect for use with LLMs like Claude, ChatGPT, and Gemini. | |
Enter a GitHub repository URL (e.g., `https://github.com/user/repo`) or shorthand format (e.g., `user/repo`). | |
""") | |
with gr.Row(): | |
with gr.Column(): | |
repo_url = gr.Textbox( | |
label="Repository URL or GitHub Shorthand", | |
placeholder="e.g., https://github.com/user/repo or user/repo" | |
) | |
branch = gr.Textbox( | |
label="Branch/Tag/Commit (optional)", | |
placeholder="e.g., main, master, v1.0.0" | |
) | |
with gr.Row(): | |
output_style = gr.Radio( | |
choices=["plain", "xml", "markdown"], | |
value="plain", | |
label="Output Style" | |
) | |
with gr.Row(): | |
remove_comments = gr.Checkbox( | |
label="Remove Comments", | |
value=False | |
) | |
remove_empty_lines = gr.Checkbox( | |
label="Remove Empty Lines", | |
value=False | |
) | |
security_check = gr.Checkbox( | |
label="Enable Security Check", | |
value=True | |
) | |
pack_button = gr.Button("Pack Repository", variant="primary") | |
with gr.Row(): | |
output_text = gr.TextArea( | |
label="Output", | |
placeholder="Packed repository content will appear here...", | |
lines=20, | |
show_copy_button=True | |
) | |
# Add download button | |
with gr.Row(): | |
download_button = gr.File( | |
label="Download Output", | |
file_count="single", | |
type="file", | |
interactive=False | |
) | |
# Handle the pack button click | |
def on_pack(repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check): | |
content = pack_repository(repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check) | |
# Generate file for download if content is not an error message | |
if not content.startswith("Error"): | |
filename = format_filename(repo_url, branch) | |
temp_path = tempfile.mktemp(suffix='.txt') | |
with open(temp_path, 'w', encoding='utf-8') as f: | |
f.write(content) | |
return content, temp_path | |
return content, None | |
pack_button.click( | |
fn=on_pack, | |
inputs=[repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check], | |
outputs=[output_text, download_button] | |
) | |
gr.Markdown(""" | |
### π Notes | |
- The packed output is optimized for use with AI models | |
- Security check helps identify potentially sensitive information | |
- Different output styles (plain, XML, markdown) are available for different use cases | |
- Click the copy icon in the output box to copy the content to clipboard | |
- Use the download button to save the output as a text file | |
### π Links | |
- [Repomix GitHub Repository](https://github.com/yamadashy/repomix) | |
- [Documentation](https://github.com/yamadashy/repomix#-quick-start) | |
""") | |
if __name__ == "__main__": | |
demo.launch() |