import gradio as gr import re, os, zipfile, tempfile def parse_ai_response(text): files = [] pattern = r"```([a-zA-Z0-9]+)?\n([\s\S]*?)```" matches = re.findall(pattern, text) lines = text.splitlines() inferred_names = [] for i, line in enumerate(lines): if re.search(r"(`.*?`|\.js|\.html|\.css|\.py)", line): m = re.search(r"([A-Za-z0-9_\-]+\.[A-Za-z0-9]+)", line) if m: inferred_names.append(m.group(1)) for i, (lang, code) in enumerate(matches): filename = inferred_names[i] if i < len(inferred_names) else f"file_{i+1}.{lang or 'txt'}" files.append((filename, code)) return files def create_zip(files): temp_dir = tempfile.mkdtemp() zip_path = os.path.join(temp_dir, "project.zip") with zipfile.ZipFile(zip_path, "w") as zipf: for name, content in files: file_path = os.path.join(temp_dir, name) os.makedirs(os.path.dirname(file_path), exist_ok=True) with open(file_path, "w", encoding="utf-8") as f: f.write(content) zipf.write(file_path, arcname=name) return zip_path def process_response(text): files = parse_ai_response(text) zip_path = create_zip(files) return zip_path interface = gr.Interface( fn=process_response, inputs=gr.Textbox(lines=30, label="Paste AI project response here"), outputs=gr.File(label="Download generated ZIP"), title="AI Project Packager", description="Paste an AI-generated project (like a Node.js or Python app) and this tool will parse, assemble, and zip the files for you." ) if __name__ == "__main__": interface.launch()