update requirements for gradio and streamlit
Browse files2029 unmodified lines2030203120322033203420352036203720382039519 unmodified lines25592560256125622563256425652566250 unmodified lines281728182819282028212822282328242029 unmodified lines# Deploy to Spaces logicdef wrap_html_in_gradio_app(html_code):# Escape triple quotes for safe embeddingsafe_html = html_code.replace('"""', r'\"\"\"')return ('import gradio as gr\n\n''def show_html():\n'f' return """{safe_html}"""\n\n'519 unmodified linesexist_ok=True)# Upload the user's code to src/streamlit_app.py (for both new and existing spaces)import tempfilewith tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:f.write(code)temp_path = f.name250 unmodified linesimport osos.unlink(temp_path)else:file_name = "app.py"import tempfilewith tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False) as f:f.write(code)temp_path = f.name2029 unmodified lines20302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190519 unmodified lines271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745250 unmodified lines29962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130322029 unmodified lines# Deploy to Spaces logicdef extract_import_statements(code):"""Extract import statements from generated code."""import astimport reimport_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("Qwen/Qwen3-Coder-480B-A35B", "auto")imports_text = '\n'.join(import_statements)prompt = f"""Based on the following Python import statements, generate a requirements.txt file with the necessary PyPI packages:{imports_text}Instructions:- Only include external packages that need to be installed via pip- Do not include Python built-in modules- Use the correct PyPI package names (e.g., cv2 -> opencv-python, PIL -> Pillow, sklearn -> scikit-learn)- Do not specify versions unless absolutely necessary for compatibility- One package per line- If no external packages are needed, return "# No additional dependencies required"Requirements.txt:"""messages = [{"role": "system", "content": "You are a Python packaging expert. Generate accurate requirements.txt files based on import statements."},{"role": "user", "content": prompt}]response = client.chat.completions.create(model="Qwen/Qwen3-Coder-480B-A35B",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:# Extract content between code blockslines = requirements_content.split('\n')in_code_block = Falseclean_lines = []for line in lines:if line.strip().startswith('```'):in_code_block = not in_code_blockcontinueif in_code_block:clean_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 mappingdependencies = set()special_cases = {'cv2': 'opencv-python','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 wrap_html_in_gradio_app(html_code):# Escape triple quotes for safe embeddingsafe_html = html_code.replace('"""', r'\"\"\"')# Extract import statements and generate requirements.txt with LLMimport_statements = extract_import_statements(html_code)requirements_comment = ""if import_statements:requirements_content = generate_requirements_txt_with_llm(import_statements)requirements_comment = ("# Generated requirements.txt content (create this file manually if needed):\n"+ '\n'.join(f"# {line}" for line in requirements_content.strip().split('\n')) + '\n\n')return (f'{requirements_comment}''import gradio as gr\n\n''def show_html():\n'f' return """{safe_html}"""\n\n'519 unmodified linesexist_ok=True)# Generate and upload requirements.txt for Streamlit appsimport_statements = extract_import_statements(code)requirements_content = generate_requirements_txt_with_llm(import_statements)import tempfile# Upload requirements.txt firsttry:with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False) as f:f.write(requirements_content)requirements_temp_path = f.nameapi.upload_file(path_or_fileobj=requirements_temp_path,path_in_repo="requirements.txt",repo_id=repo_id,repo_type="space")except Exception as e:error_msg = str(e)if "403 Forbidden" in error_msg and "write token" in error_msg:return gr.update(value=f"Error uploading requirements.txt: Permission denied. Please ensure you have write access to {repo_id} and your token has the correct permissions.", visible=True)else:return gr.update(value=f"Error uploading requirements.txt: {e}", visible=True)finally:import osif 'requirements_temp_path' in locals():os.unlink(requirements_temp_path)# Upload the user's code to src/streamlit_app.py (for both new and existing spaces)with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:f.write(code)temp_path = f.name250 unmodified linesimport osos.unlink(temp_path)else:# Generate and upload requirements.txt for Gradio appsimport_statements = extract_import_statements(code)requirements_content = generate_requirements_txt_with_llm(import_statements)import tempfile# Upload requirements.txt firsttry:with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False) as f:f.write(requirements_content)requirements_temp_path = f.nameapi.upload_file(path_or_fileobj=requirements_temp_path,path_in_repo="requirements.txt",repo_id=repo_id,repo_type="space")except Exception as e:error_msg = str(e)if "403 Forbidden" in error_msg and "write token" in error_msg:return gr.update(value=f"Error uploading requirements.txt: Permission denied. Please ensure you have write access to {repo_id} and your token has the correct permissions.", visible=True)else:return gr.update(value=f"Error uploading requirements.txt: {e}", visible=True)finally:import osif 'requirements_temp_path' in locals():os.unlink(requirements_temp_path)# Now upload the main app.py filefile_name = "app.py"with tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False) as f:f.write(code)temp_path = f.name
app.py+210-2