Spaces:
Sleeping
Sleeping
| import os | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| repo_dir = "Plastic-App" | |
| if not os.path.exists(repo_dir): | |
| print("Cloning the Plastic-App repository...") | |
| try: | |
| subprocess.run( | |
| ["git", "clone", "https://github.com/vectorplasticity/Plastic-App.git"], | |
| check=True, | |
| capture_output=True, | |
| text=True | |
| ) | |
| print("Repository cloned successfully.") | |
| except subprocess.CalledProcessError as e: | |
| print(f"Error cloning repository: {e.stderr}") | |
| sys.exit(1) | |
| else: | |
| print("Repository already exists. Skipping clone.") | |
| os.chdir(repo_dir) | |
| print(f"Changed directory to: {os.getcwd()}") | |
| try: | |
| subprocess.run( | |
| [sys.executable, "-m", "pip", "install", "-e", "."], | |
| check=True, | |
| capture_output=True, | |
| text=True | |
| ) | |
| print("Package installed successfully.") | |
| except subprocess.CalledProcessError as e: | |
| print(f"Error installing package: {e.stderr}") | |
| sys.exit(1) | |
| index_path = Path("plastic_app/index.html") | |
| app_path = Path("plastic_app/app.py") | |
| try: | |
| file_content = index_path.read_text() | |
| original_block = """<form id="folder-form"> | |
| <label for="folder-input">Option 2: Select a folder</label> | |
| <input type="file" id="folder-input" webkitdirectory directory multiple required> | |
| <button type="submit">Analyze Folder</button> | |
| </form>""" | |
| replacement_block = """<form id="folder-form"> | |
| <label for="folder-input">Option 2: Select a folder</label> | |
| <input type="file" id="folder-input" webkitdirectory directory multiple required> | |
| <button type="submit">Analyze Folder</button> | |
| </form> | |
| <form id="repo-form"> | |
| <label for="repo-input">Option 3: Select a Repo URL#branch</label> | |
| <input id="repo-input" name="repo-input"> | |
| <button type="submit">Analyze Repo</button> | |
| </form>""" | |
| if original_block in file_content: | |
| modified_content = file_content.replace(original_block, replacement_block) | |
| index_path.write_text(modified_content) | |
| print("Script modified successfully.") | |
| else: | |
| print("Warning: Error") | |
| except Exception as e: | |
| print(f"An error occurred while modifying the script: {e}") | |
| sys.exit(1) | |
| try: | |
| file_content = index_path.read_text() | |
| original_block = """ const folderForm = document.getElementById('folder-form'); | |
| """ | |
| replacement_block = """ const folderForm = document.getElementById('folder-form'); | |
| const repoForm = document.getElementById('repo-form'); | |
| """ | |
| if original_block in file_content: | |
| modified_content = file_content.replace(original_block, replacement_block) | |
| index_path.write_text(modified_content) | |
| print("Script modified successfully.") | |
| else: | |
| print("Warning: Error") | |
| except Exception as e: | |
| print(f"An error occurred while modifying the script: {e}") | |
| sys.exit(1) | |
| try: | |
| file_content = index_path.read_text() | |
| original_block = """ zipForm.addEventListener('submit', function(e) { e.preventDefault(); uploadAndRender(new FormData(this)); }); | |
| """ | |
| replacement_block = """ zipForm.addEventListener('submit', function(e) { e.preventDefault(); uploadAndRender(new FormData(this)); }); | |
| repoForm.addEventListener('submit', function(e) { e.preventDefault(); uploadAndRender(new FormData(this)); }); | |
| """ | |
| if original_block in file_content: | |
| modified_content = file_content.replace(original_block, replacement_block) | |
| index_path.write_text(modified_content) | |
| print("Script modified successfully.") | |
| else: | |
| print("Warning: Error") | |
| except Exception as e: | |
| print(f"An error occurred while modifying the script: {e}") | |
| sys.exit(1) | |
| try: | |
| file_content = index_path.read_text() | |
| if original_block in file_content: | |
| modified_content = file_content.replace(original_block, replacement_block) | |
| index_path.write_text(modified_content) | |
| print("Script modified successfully.") | |
| else: | |
| print("Warning: Error") | |
| except Exception as e: | |
| print(f"An error occurred while modifying the script: {e}") | |
| sys.exit(1) | |
| try: | |
| file_content = app_path.read_text() | |
| original_block = """@app.route('/upload', methods=['POST']) | |
| def upload_project(): | |
| session_id = str(uuid.uuid4()) | |
| session_dir = os.path.join(SESSION_STORAGE, session_id) | |
| os.makedirs(session_dir) | |
| try: | |
| project_path = session_dir | |
| if 'zipfile' in request.files: | |
| file = request.files['zipfile'] | |
| zip_path = os.path.join(session_dir, secure_filename(file.filename)) | |
| file.save(zip_path) | |
| with zipfile.ZipFile(zip_path, 'r') as zip_ref: | |
| zip_ref.extractall(project_path) | |
| elif 'files[]' in request.files: | |
| files = request.files.getlist('files[]') | |
| if not files or not files[0].filename: | |
| raise ValueError("No files selected in folder.") | |
| for file in files: | |
| relative_path = file.filename | |
| if '..' in relative_path.split(os.path.sep): raise ValueError("Invalid path detected.") | |
| abs_path = os.path.join(project_path, relative_path) | |
| os.makedirs(os.path.dirname(abs_path), exist_ok=True) | |
| file.save(abs_path)""" | |
| replacement_block = """@app.route('/upload', methods=['POST']) | |
| def upload_project(): | |
| session_id = str(uuid.uuid4()) | |
| session_dir = os.path.join(SESSION_STORAGE, session_id) | |
| os.makedirs(session_dir) | |
| try: | |
| project_path = session_dir | |
| if 'zipfile' in request.files: | |
| file = request.files['zipfile'] | |
| zip_path = os.path.join(session_dir, secure_filename(file.filename)) | |
| file.save(zip_path) | |
| with zipfile.ZipFile(zip_path, 'r') as zip_ref: | |
| zip_ref.extractall(project_path) | |
| elif 'files[]' in request.files: | |
| files = request.files.getlist('files[]') | |
| if not files or not files[0].filename: | |
| raise ValueError("No files selected in folder.") | |
| for file in files: | |
| relative_path = file.filename | |
| if '..' in relative_path.split(os.path.sep): raise ValueError("Invalid path detected.") | |
| abs_path = os.path.join(project_path, relative_path) | |
| os.makedirs(os.path.dirname(abs_path), exist_ok=True) | |
| file.save(abs_path) | |
| elif 'http' in request.form.get('repo-input', ''): | |
| repo_url = request.form.get('repo-input') | |
| print('http found') | |
| print(repo_url) | |
| if repo_url.startswith("https://github"): | |
| import requests | |
| if "#" in repo_url: | |
| branch = repo_url.split("#")[1] | |
| else: branch = "main" | |
| zip_repo = f'{repo_url}/archive/refs/heads/{branch}.zip' | |
| response = requests.get(zip_repo) | |
| response.raise_for_status() | |
| zip_filename = secure_filename(zip_repo.split('/')[-1]) | |
| zip_path = os.path.join(session_dir, zip_filename.split("#")[0]) | |
| with open(zip_path, 'wb') as f: | |
| f.write(response.content) | |
| with zipfile.ZipFile(zip_path, 'r') as zip_ref: | |
| zip_ref.extractall(project_path) | |
| elif "https://huggingface.co".strip() or "https://www.huggingface".strip() in repo_url: | |
| print("HF REPO: ", repo_url) | |
| from repo_to_zip import main as repo_to_zip_main | |
| zip_filename=repo_url.split("/")[-1] + ".zip" | |
| zip_path = os.path.join(session_dir, zip_filename) | |
| repo_to_zip_main(repo_url, zip_path) | |
| with zipfile.ZipFile(zip_path, 'r') as zip_ref: | |
| zip_ref.extractall(project_path)""" | |
| if original_block in file_content: | |
| modified_content = file_content.replace(original_block, replacement_block) | |
| app_path.write_text(modified_content) | |
| print("Script modified successfully.") | |
| else: | |
| print("Warning: Error") | |
| except Exception as e: | |
| print(f"An error occurred while modifying the script: {e}") | |
| sys.exit(1) | |
| try: | |
| file_content = app_path.read_text() | |
| original_block = """def main(): | |
| app.run(debug=True, port=5000)""" | |
| replacement_block = """def main(): | |
| app.run(debug=True, host='0.0.0.0', port=7860)""" | |
| if original_block in file_content: | |
| modified_content = file_content.replace(original_block, replacement_block) | |
| app_path.write_text(modified_content) | |
| print("Script modified successfully.") | |
| else: | |
| print("Warning: Error") | |
| except Exception as e: | |
| print(f"An error occurred while modifying the script: {e}") | |
| sys.exit(1) | |
| #from plastic_app import app as pa | |
| import os | |
| os.system("plastic-app") |