Spaces:
Sleeping
Sleeping
Delete utils.py
Browse files
utils.py
DELETED
@@ -1,88 +0,0 @@
|
|
1 |
-
import re
|
2 |
-
import ast
|
3 |
-
|
4 |
-
def extract_code_blocks(text):
|
5 |
-
"""Extract code blocks from a markdown-formatted text"""
|
6 |
-
# Pattern to match code blocks with ```python or ``` markers
|
7 |
-
pattern = r'```(?:python)?\s*([\s\S]*?)```'
|
8 |
-
matches = re.findall(pattern, text)
|
9 |
-
|
10 |
-
# If no code blocks found, try to extract the entire text as code
|
11 |
-
if not matches and text.strip():
|
12 |
-
# Check if the text looks like Python code (has common imports or patterns)
|
13 |
-
if re.search(r'import\s+\w+|def\s+\w+\(|class\s+\w+:|if\s+__name__\s*==\s*[\'"]__main__[\'"]:', text):
|
14 |
-
return [text.strip()]
|
15 |
-
|
16 |
-
return [match.strip() for match in matches]
|
17 |
-
|
18 |
-
def sanitize_code(code):
|
19 |
-
"""Remove potentially harmful operations from the code"""
|
20 |
-
# Basic sanitization - replace known harmful functions
|
21 |
-
harmful_patterns = [
|
22 |
-
(r'__import__\([\'"]os[\'"]\)', 'None'),
|
23 |
-
(r'exec\(', 'print('),
|
24 |
-
(r'eval\(', 'print('),
|
25 |
-
(r'open\(.*,.*[\'"]w[\'"].*\)', 'open("safe_file.txt", "r")'),
|
26 |
-
(r'subprocess\.\w+\(', 'print('),
|
27 |
-
(r'os\.system\(', 'print('),
|
28 |
-
(r'os\.popen\(', 'print('),
|
29 |
-
(r'os\.unlink\(', 'print('),
|
30 |
-
(r'os\.remove\(', 'print('),
|
31 |
-
(r'shutil\.rmtree\(', 'print('),
|
32 |
-
]
|
33 |
-
|
34 |
-
sanitized_code = code
|
35 |
-
for pattern, replacement in harmful_patterns:
|
36 |
-
sanitized_code = re.sub(pattern, replacement, sanitized_code)
|
37 |
-
|
38 |
-
return sanitized_code
|
39 |
-
|
40 |
-
def validate_gradio_code(code):
|
41 |
-
"""Validate that the code only uses Gradio and safe libraries"""
|
42 |
-
try:
|
43 |
-
# Parse the code into an AST
|
44 |
-
tree = ast.parse(code)
|
45 |
-
|
46 |
-
# Check imports
|
47 |
-
for node in ast.walk(tree):
|
48 |
-
if isinstance(node, ast.Import):
|
49 |
-
for name in node.names:
|
50 |
-
if name.name not in ['gradio', 'numpy', 'pandas', 'matplotlib', 'PIL', 'os', 'io', 'base64',
|
51 |
-
'time', 'datetime', 'json', 'random', 'math', 'sys', 're', 'pathlib',
|
52 |
-
'collections', 'typing', 'warnings']:
|
53 |
-
return False, f"Unauthorized import: {name.name}"
|
54 |
-
|
55 |
-
elif isinstance(node, ast.ImportFrom):
|
56 |
-
if node.module not in ['gradio', 'numpy', 'pandas', 'matplotlib', 'PIL', 'os', 'io', 'base64',
|
57 |
-
'time', 'datetime', 'json', 'random', 'math', 'sys', 're', 'pathlib',
|
58 |
-
'collections', 'typing', 'warnings', None]:
|
59 |
-
return False, f"Unauthorized import from: {node.module}"
|
60 |
-
|
61 |
-
# Basic check for potentially harmful OS operations
|
62 |
-
code_str = code.lower()
|
63 |
-
harmful_operations = [
|
64 |
-
'subprocess', 'system(', 'popen(', 'execve(', 'fork(', 'chmod(',
|
65 |
-
'rmdir(', 'remove(', 'unlink(', 'rmtree(', 'shutil.rm', 'socket',
|
66 |
-
'urllib.request', 'requests', 'http', 'ftp', 'telnet', 'eval(', 'exec('
|
67 |
-
]
|
68 |
-
|
69 |
-
for op in harmful_operations:
|
70 |
-
if op in code_str:
|
71 |
-
return False, f"Potentially harmful operation detected: {op}"
|
72 |
-
|
73 |
-
# Check for launch parameters
|
74 |
-
launch_pattern = r'\.launch\s*\(([^)]*)\)'
|
75 |
-
launch_matches = re.findall(launch_pattern, code)
|
76 |
-
|
77 |
-
for match in launch_matches:
|
78 |
-
if 'debug=' in match and 'debug=False' not in match:
|
79 |
-
return False, "Debug mode is not allowed"
|
80 |
-
if 'share=' in match and 'share=False' not in match and 'share=True' in match:
|
81 |
-
return False, "Share mode must be set to False"
|
82 |
-
|
83 |
-
return True, None
|
84 |
-
|
85 |
-
except SyntaxError as e:
|
86 |
-
return False, f"Syntax error in the code: {str(e)}"
|
87 |
-
except Exception as e:
|
88 |
-
return False, f"Error validating code: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|