Spaces:
Runtime error
Runtime error
import os | |
import fnmatch | |
import streamlit as st | |
import sys | |
import base64 | |
def get_ignore_list(ignore_file_path): | |
ignore_list = [] | |
with open(ignore_file_path, 'r') as ignore_file: | |
for line in ignore_file: | |
if sys.platform == "win32": | |
line = line.replace("/", "\\") | |
ignore_list.append(line.strip()) | |
return ignore_list | |
def should_ignore(file_path, ignore_list): | |
for pattern in ignore_list: | |
if fnmatch.fnmatch(file_path, pattern): | |
return True | |
return False | |
def process_repository(repo_path, ignore_list): | |
structure = "Project Directory Structure:\n" | |
contents = "" | |
for root, dirs, files in os.walk(repo_path): | |
level = root.replace(repo_path, '').count(os.sep) | |
indent = ' ' * level | |
subindent = ' ' * (level + 1) | |
relative_root = os.path.relpath(root, repo_path) | |
if not should_ignore(relative_root, ignore_list): | |
structure += '{}- {}\n'.format(indent, os.path.basename(root)) | |
for file in files: | |
relative_file_path = os.path.relpath(os.path.join(root, file), repo_path) | |
if not should_ignore(relative_file_path, ignore_list): | |
structure += '{}- {}\n'.format(subindent, file) | |
file_path = os.path.join(root, file) | |
with open(file_path, 'r', errors='ignore') as file: | |
file_contents = file.read() | |
contents += "-" * 4 + "\n" | |
contents += f"{relative_file_path}\n" | |
contents += f"{file_contents}\n" | |
structure += "End Project Directory Structure Visual\n\n" | |
return structure, contents | |
def download_button(object_to_download, download_filename, button_text): | |
if isinstance(object_to_download, bytes): | |
pass | |
elif isinstance(object_to_download, str): | |
object_to_download = object_to_download.encode('utf-8') | |
else: | |
raise ValueError(f"object_to_download must be a str or bytes, got {type(object_to_download)}") | |
try: | |
b64 = base64.b64encode(object_to_download).decode() | |
href = f'<a href="data:file/txt;base64,{b64}" download="{download_filename}" class="btn btn-primary" role="button">{button_text}</a>' | |
st.markdown(href, unsafe_allow_html=True) | |
except Exception as e: | |
st.error(f"An error occurred while creating the download link: {e}") | |
# Updated Streamlit UI to include new features | |
st.title("Git Repository Loader with Web UI") | |
# Organizing input and buttons using columns | |
col1, col2 = st.columns([3, 2]) | |
with col1: | |
uploaded_files = st.file_uploader("Drag and drop a folder or click to select a directory:", accept_multiple_files=True) | |
with col2: | |
process_button = st.button("Process Repository") | |
if process_button and uploaded_files: | |
with st.spinner('Processing the repository...'): | |
# Temporary directory to store uploaded files | |
with tempfile.TemporaryDirectory() as temp_dir: | |
# Save uploaded files to the temporary directory | |
for uploaded_file in uploaded_files: | |
file_path = os.path.join(temp_dir, uploaded_file.name) | |
with open(file_path, "wb") as file: | |
file.write(uploaded_file.getbuffer()) | |
ignore_file_path = os.path.join(temp_dir, ".gptignore") | |
if not os.path.exists(ignore_file_path): | |
HERE = os.path.dirname(os.path.abspath(__file__)) | |
ignore_file_path = os.path.join(HERE, ".gptignore") | |
if os.path.exists(ignore_file_path): | |
ignore_list = get_ignore_list(ignore_file_path) | |
else: | |
ignore_list = [] | |
structure, contents = process_repository(temp_dir, ignore_list) | |
full_output = f"### Project Directory Structure\n{structure}\n### Files Content\n{contents}" | |
st.markdown("### Output") | |
download_button(full_output, "repository_output.md", "Download Output as Markdown") | |
with st.expander("View Project Directory Structure"): | |
st.text(structure) | |
with st.expander("View Files Content"): | |
st.text(contents) | |
st.success("Process completed. Review the results above.") | |