Spaces:
Running
Running
import gradio as gr | |
import subprocess | |
import tempfile | |
import os | |
import shutil | |
from urllib.parse import urlparse | |
def validate_repo_url(url): | |
"""Validate if the input is a valid repository URL or GitHub shorthand.""" | |
if not url: | |
return False | |
# Check if it's a GitHub shorthand (user/repo) | |
if '/' in url and len(url.split('/')) == 2: | |
return True | |
try: | |
result = urlparse(url) | |
return all([result.scheme, result.netloc]) | |
except: | |
return False | |
def pack_repository(repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check): | |
"""Pack a repository using Repomix and return the output.""" | |
if not repo_url: | |
return "Error: Please provide a repository URL" | |
if not validate_repo_url(repo_url): | |
return "Error: Invalid repository URL format" | |
temp_dir = None | |
try: | |
# Create temporary directory that persists until we're done | |
temp_dir = tempfile.mkdtemp() | |
# Prepare command | |
cmd = ["npx", "repomix"] | |
# Add remote repository options | |
cmd.extend(["--remote", repo_url]) | |
if branch: | |
cmd.extend(["--remote-branch", branch]) | |
# Add style option | |
cmd.extend(["--style", output_style]) | |
# Add other options | |
if remove_comments: | |
cmd.append("--remove-comments") | |
if remove_empty_lines: | |
cmd.append("--remove-empty-lines") | |
if not security_check: | |
cmd.append("--no-security-check") | |
# Set output path | |
output_file = os.path.join(temp_dir, "repomix-output.txt") | |
cmd.extend(["-o", output_file]) | |
# Execute Repomix | |
result = subprocess.run( | |
cmd, | |
capture_output=True, | |
text=True, | |
cwd=temp_dir | |
) | |
if result.returncode != 0: | |
return f"Error running Repomix: {result.stderr}" | |
# Check if the file exists | |
if not os.path.exists(output_file): | |
return f"Error: Output file was not created. Repomix output: {result.stdout}\n{result.stderr}" | |
# Read the output file | |
try: | |
with open(output_file, 'r', encoding='utf-8') as f: | |
content = f.read() | |
return content | |
except Exception as e: | |
return f"Error reading output file: {str(e)}" | |
except Exception as e: | |
return f"Error: {str(e)}" | |
finally: | |
# Clean up temporary directory | |
if temp_dir and os.path.exists(temp_dir): | |
try: | |
shutil.rmtree(temp_dir) | |
except Exception as e: | |
print(f"Warning: Could not remove temporary directory: {str(e)}") | |
# 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") | |
output_text = gr.TextArea( | |
label="Output", | |
placeholder="Packed repository content will appear here...", | |
lines=20 | |
) | |
# Handle the pack button click | |
pack_button.click( | |
fn=pack_repository, | |
inputs=[repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check], | |
outputs=output_text | |
) | |
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 | |
### π Links | |
- [Repomix GitHub Repository](https://github.com/yamadashy/repomix) | |
- [Documentation](https://github.com/yamadashy/repomix#-quick-start) | |
""") | |
if __name__ == "__main__": | |
demo.launch() |