fix requirements txt generation
Browse files7 unmodified lines891011121314151617120 unmodified lines138139140141142143315 unmodified lines45946046146246346446546646746846947034 unmodified lines5055065075085095105115127 unmodified linesimport uuidimport tempfileimport shutilfrom typing import Dict, List, Optional, Tuplefrom pathlib import Pathfrom huggingface_hub import HfApidef parse_html_code(code: str) -> str:120 unmodified linesreturn Nonedef parse_multi_file_python_output(code: str) -> Dict[str, str]:"""Parse multi-file Python output (e.g., Gradio, Streamlit)"""files = {}315 unmodified linesfile_path.parent.mkdir(parents=True, exist_ok=True)file_path.write_text(content, encoding='utf-8')# Ensure requirements.txt existsif "requirements.txt" not in files:if language == "gradio":(temp_path / "requirements.txt").write_text("gradio>=4.0.0\n", encoding='utf-8')elif language == "streamlit":(temp_path / "requirements.txt").write_text("streamlit>=1.30.0\n", encoding='utf-8')# Create Dockerfile if neededif sdk == "docker":34 unmodified linesfile_path.parent.mkdir(parents=True, exist_ok=True)file_path.write_text(content, encoding='utf-8')if "requirements.txt" not in files:(temp_path / "requirements.txt").write_text("gradio>=4.0.0\n", encoding='utf-8')# Don't create README - HuggingFace will auto-generate it# We'll add the anycoder tag after deployment7 unmodified lines8910111213141516171819120 unmodified lines140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372315 unmodified lines68868969069169269369469569669769869970070170270370470570670770870934 unmodified lines7447457467477487497507517527537547557567577587597607617 unmodified linesimport uuidimport tempfileimport shutilimport astfrom typing import Dict, List, Optional, Tuplefrom pathlib import Pathfrom huggingface_hub import HfApifrom backend_models import get_inference_client, get_real_model_iddef parse_html_code(code: str) -> str:120 unmodified linesreturn Nonedef strip_tool_call_markers(text):"""Remove TOOL_CALL markers and thinking tags that some LLMs add to their output."""if not text:return text# Remove [TOOL_CALL] and [/TOOL_CALL] markerstext = re.sub(r'\[/?TOOL_CALL\]', '', text, flags=re.IGNORECASE)# Remove <think> and </think> tags and their contenttext = re.sub(r'<think>[\s\S]*?</think>', '', text, flags=re.IGNORECASE)# Remove any remaining unclosed <think> tags at the starttext = re.sub(r'^<think>[\s\S]*?(?=\n|$)', '', text, flags=re.IGNORECASE | re.MULTILINE)# Remove any remaining </think> tagstext = re.sub(r'</think>', '', text, flags=re.IGNORECASE)# Remove standalone }} that appears with tool calls# Only remove if it's on its own line or at the endtext = re.sub(r'^\s*\}\}\s*$', '', text, flags=re.MULTILINE)return text.strip()def remove_code_block(text):"""Remove code block markers from text."""# First strip any tool call markerstext = strip_tool_call_markers(text)# Try to match code blocks with language markerspatterns = [r'```(?:html|HTML)\n([\s\S]+?)\n```', # Match ```html or ```HTMLr'```\n([\s\S]+?)\n```', # Match code blocks without language markersr'```([\s\S]+?)```' # Match code blocks without line breaks]for pattern in patterns:match = re.search(pattern, text, re.DOTALL)if match:extracted = match.group(1).strip()# Remove a leading language marker line (e.g., 'python') if presentif extracted.split('\n', 1)[0].strip().lower() in ['python', 'html', 'css', 'javascript', 'json', 'c', 'cpp', 'markdown', 'latex', 'jinja2', 'typescript', 'yaml', 'dockerfile', 'shell', 'r', 'sql']:return extracted.split('\n', 1)[1] if '\n' in extracted else ''return extracted# If no code block is found, return as-isreturn text.strip()def extract_import_statements(code):"""Extract import statements from generated code."""import_statements = []# Built-in Python modules to excludebuiltin_modules = {'os', 'sys', 'json', 'time', 'datetime', 'random', 'math', 're', 'collections','itertools', 'functools', 'pathlib', 'urllib', 'http', 'email', 'html', 'xml','csv', 'tempfile', 'shutil', 'subprocess', 'threading', 'multiprocessing','asyncio', 'logging', 'typing', 'base64', 'hashlib', 'secrets', 'uuid','copy', 'pickle', 'io', 'contextlib', 'warnings', 'sqlite3', 'gzip', 'zipfile','tarfile', 'socket', 'ssl', 'platform', 'getpass', 'pwd', 'grp', 'stat','glob', 'fnmatch', 'linecache', 'traceback', 'inspect', 'keyword', 'token','tokenize', 'ast', 'code', 'codeop', 'dis', 'py_compile', 'compileall','importlib', 'pkgutil', 'modulefinder', 'runpy', 'site', 'sysconfig'}try:# Try to parse as Python ASTtree = ast.parse(code)for node in ast.walk(tree):if isinstance(node, ast.Import):for alias in node.names:module_name = alias.name.split('.')[0]if module_name not in builtin_modules and not module_name.startswith('_'):import_statements.append(f"import {alias.name}")elif isinstance(node, ast.ImportFrom):if node.module:module_name = node.module.split('.')[0]if module_name not in builtin_modules and not module_name.startswith('_'):names = [alias.name for alias in node.names]import_statements.append(f"from {node.module} import {', '.join(names)}")except SyntaxError:# Fallback: use regex to find import statementsfor line in code.split('\n'):line = line.strip()if line.startswith('import ') or line.startswith('from '):# Check if it's not a builtin moduleif line.startswith('import '):module_name = line.split()[1].split('.')[0]elif line.startswith('from '):module_name = line.split()[1].split('.')[0]if module_name not in builtin_modules and not module_name.startswith('_'):import_statements.append(line)return list(set(import_statements)) # Remove duplicatesdef generate_requirements_txt_with_llm(import_statements):"""Generate requirements.txt content using LLM based on import statements."""if not import_statements:return "# No additional dependencies required\n"# Use a lightweight model for this tasktry:client = get_inference_client("zai-org/GLM-4.6", "auto")actual_model_id = get_real_model_id("zai-org/GLM-4.6")imports_text = '\n'.join(import_statements)prompt = f"""Based on the following Python import statements, generate a comprehensive requirements.txt file with all necessary and commonly used related packages:{imports_text}Instructions:- Include the direct packages needed for the imports- Include commonly used companion packages and dependencies for better functionality- Use correct PyPI package names (e.g., PIL -> Pillow, sklearn -> scikit-learn)- IMPORTANT: For diffusers, ALWAYS use: git+https://github.com/huggingface/diffusers- IMPORTANT: For transformers, ALWAYS use: git+https://github.com/huggingface/transformers- IMPORTANT: If diffusers is installed, also include transformers and sentencepiece as they usually go together- Examples of comprehensive dependencies:* diffusers often needs: git+https://github.com/huggingface/transformers, sentencepiece, accelerate, torch, tokenizers* transformers often needs: accelerate, torch, tokenizers, datasets* gradio often needs: requests, Pillow for image handling* pandas often needs: numpy, openpyxl for Excel files* matplotlib often needs: numpy, pillow for image saving* sklearn often needs: numpy, scipy, joblib* streamlit often needs: pandas, numpy, requests* opencv-python often needs: numpy, pillow* fastapi often needs: uvicorn, pydantic* torch often needs: torchvision, torchaudio (if doing computer vision/audio)- Include packages for common file formats if relevant (openpyxl, python-docx, PyPDF2)- Do not include Python built-in modules- Do not specify versions unless there are known compatibility issues- One package per line- If no external packages are needed, return "# No additional dependencies required"🚨 CRITICAL OUTPUT FORMAT:- Output ONLY the package names, one per line (plain text format)- Do NOT use markdown formatting (no ```, no bold, no headings, no lists)- Do NOT add any explanatory text before or after the package list- Do NOT wrap the output in code blocks- Just output raw package names as they would appear in requirements.txtGenerate a comprehensive requirements.txt that ensures the application will work smoothly:"""messages = [{"role": "system", "content": "You are a Python packaging expert specializing in creating comprehensive, production-ready requirements.txt files. Output ONLY plain text package names without any markdown formatting, code blocks, or explanatory text. Your goal is to ensure applications work smoothly by including not just direct dependencies but also commonly needed companion packages, popular extensions, and supporting libraries that developers typically need together."},{"role": "user", "content": prompt}]response = client.chat.completions.create(model=actual_model_id,messages=messages,max_tokens=1024,temperature=0.1)requirements_content = response.choices[0].message.content.strip()# Clean up the response in case it includes extra formattingif '```' in requirements_content:requirements_content = remove_code_block(requirements_content)# Enhanced cleanup for markdown and formattinglines = requirements_content.split('\n')clean_lines = []for line in lines:stripped_line = line.strip()# Skip lines that are markdown formattingif (stripped_line == '```' orstripped_line.startswith('```') orstripped_line.startswith('#') and not stripped_line.startswith('# ') or # Skip markdown headers but keep commentsstripped_line.startswith('**') or # Skip bold textstripped_line.startswith('*') and not stripped_line[1:2].isalnum() or # Skip markdown lists but keep package names starting with *stripped_line.startswith('-') and not stripped_line[1:2].isalnum() or # Skip markdown lists but keep package names starting with -stripped_line.startswith('===') or # Skip section dividersstripped_line.startswith('---') or # Skip horizontal rulesstripped_line.lower().startswith('here') or # Skip explanatory textstripped_line.lower().startswith('this') or # Skip explanatory textstripped_line.lower().startswith('the') or # Skip explanatory textstripped_line.lower().startswith('based on') or # Skip explanatory textstripped_line == ''): # Skip empty lines unless they're at natural boundariescontinue# Keep lines that look like valid package specifications# Valid lines: package names, git+https://, comments starting with "# "if (stripped_line.startswith('# ') or # Valid commentsstripped_line.startswith('git+') or # Git dependenciesstripped_line[0].isalnum() or # Package names start with alphanumeric'==' in stripped_line or # Version specifications'>=' in stripped_line or # Version specifications'<=' in stripped_line): # Version specificationsclean_lines.append(line)requirements_content = '\n'.join(clean_lines).strip()# Ensure it ends with a newlineif requirements_content and not requirements_content.endswith('\n'):requirements_content += '\n'return requirements_content if requirements_content else "# No additional dependencies required\n"except Exception as e:# Fallback: simple extraction with basic mappingprint(f"[Deploy] Warning: LLM requirements generation failed: {e}, using fallback")dependencies = set()special_cases = {'PIL': 'Pillow','sklearn': 'scikit-learn','skimage': 'scikit-image','bs4': 'beautifulsoup4'}for stmt in import_statements:if stmt.startswith('import '):module_name = stmt.split()[1].split('.')[0]package_name = special_cases.get(module_name, module_name)dependencies.add(package_name)elif stmt.startswith('from '):module_name = stmt.split()[1].split('.')[0]package_name = special_cases.get(module_name, module_name)dependencies.add(package_name)if dependencies:return '\n'.join(sorted(dependencies)) + '\n'else:return "# No additional dependencies required\n"def parse_multi_file_python_output(code: str) -> Dict[str, str]:"""Parse multi-file Python output (e.g., Gradio, Streamlit)"""files = {}315 unmodified linesfile_path.parent.mkdir(parents=True, exist_ok=True)file_path.write_text(content, encoding='utf-8')# Ensure requirements.txt exists - generate from imports if missingif "requirements.txt" not in files:# Get the main app file (app.py for gradio, streamlit_app.py or app.py for streamlit)main_app = files.get('streamlit_app.py') or files.get('app.py', '')if main_app:print(f"[Deploy] Generating requirements.txt from imports in {language} app")import_statements = extract_import_statements(main_app)requirements_content = generate_requirements_txt_with_llm(import_statements)(temp_path / "requirements.txt").write_text(requirements_content, encoding='utf-8')print(f"[Deploy] Generated requirements.txt with {len(requirements_content.splitlines())} lines")else:# Fallback to minimal requirements if no app file foundif language == "gradio":(temp_path / "requirements.txt").write_text("gradio>=4.0.0\n", encoding='utf-8')elif language == "streamlit":(temp_path / "requirements.txt").write_text("streamlit>=1.30.0\n", encoding='utf-8')# Create Dockerfile if neededif sdk == "docker":34 unmodified linesfile_path.parent.mkdir(parents=True, exist_ok=True)file_path.write_text(content, encoding='utf-8')# Generate requirements.txt from imports if missingif "requirements.txt" not in files:main_app = files.get('app.py', '')if main_app:print(f"[Deploy] Generating requirements.txt from imports in default app")import_statements = extract_import_statements(main_app)requirements_content = generate_requirements_txt_with_llm(import_statements)(temp_path / "requirements.txt").write_text(requirements_content, encoding='utf-8')print(f"[Deploy] Generated requirements.txt with {len(requirements_content.splitlines())} lines")else:# Fallback to minimal requirements if no app file found(temp_path / "requirements.txt").write_text("gradio>=4.0.0\n", encoding='utf-8')# Don't create README - HuggingFace will auto-generate it# We'll add the anycoder tag after deployment
backend_deploy.py+255-6