Update app.py
Browse files
app.py
CHANGED
|
@@ -15,51 +15,68 @@ app = FastAPI()
|
|
| 15 |
@app.post("/execute/")
|
| 16 |
def install_and_execute(request: ExecutionRequest):
|
| 17 |
"""
|
| 18 |
-
Installs a
|
| 19 |
|
| 20 |
-
This endpoint
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
The
|
| 26 |
-
|
| 27 |
-
are also captured and returned.
|
| 28 |
"""
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
script_output = subprocess.check_output(
|
| 49 |
-
[sys.executable,
|
| 50 |
stderr=subprocess.STDOUT,
|
| 51 |
-
text=True
|
|
|
|
| 52 |
)
|
| 53 |
-
finally:
|
| 54 |
-
# Clean up the temporary file
|
| 55 |
-
os.remove(temp_script_path)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 15 |
@app.post("/execute/")
|
| 16 |
def install_and_execute(request: ExecutionRequest):
|
| 17 |
"""
|
| 18 |
+
Installs a package to a temporary directory and executes a Python script.
|
| 19 |
|
| 20 |
+
This endpoint addresses permission errors in restricted environments like
|
| 21 |
+
Hugging Face Spaces by:
|
| 22 |
+
1. Creating a temporary directory for the request.
|
| 23 |
+
2. Installing the specified pip package into this temporary directory using `pip install --target`.
|
| 24 |
+
3. Setting the PYTHONPATH environment variable to include this directory.
|
| 25 |
+
4. Executing the provided script, which can now import the installed package.
|
| 26 |
|
| 27 |
+
The temporary directory and its contents are automatically removed after the
|
| 28 |
+
request is complete.
|
|
|
|
| 29 |
"""
|
| 30 |
+
# Use a temporary directory that will be automatically cleaned up
|
| 31 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 32 |
+
pip_output = ""
|
| 33 |
+
script_output = ""
|
| 34 |
+
error = ""
|
| 35 |
|
| 36 |
+
try:
|
| 37 |
+
# 1. Install the package to the temporary directory
|
| 38 |
+
install_path = os.path.join(temp_dir, "packages")
|
| 39 |
+
os.makedirs(install_path)
|
| 40 |
+
|
| 41 |
+
pip_output = subprocess.check_output(
|
| 42 |
+
[
|
| 43 |
+
sys.executable,
|
| 44 |
+
"-m",
|
| 45 |
+
"pip",
|
| 46 |
+
"install",
|
| 47 |
+
request.package_name,
|
| 48 |
+
"--target",
|
| 49 |
+
install_path, # Install here
|
| 50 |
+
],
|
| 51 |
+
stderr=subprocess.STDOUT,
|
| 52 |
+
text=True
|
| 53 |
+
)
|
| 54 |
|
| 55 |
+
# 2. Create a temporary file for the user's script
|
| 56 |
+
script_path = os.path.join(temp_dir, "script.py")
|
| 57 |
+
with open(script_path, 'w') as temp_script:
|
| 58 |
+
temp_script.write(request.script_content)
|
| 59 |
|
| 60 |
+
# 3. Prepare the environment for the script execution
|
| 61 |
+
# This ensures the script can find the package in `install_path`
|
| 62 |
+
exec_env = os.environ.copy()
|
| 63 |
+
# Add the install path to the Python Path
|
| 64 |
+
exec_env["PYTHONPATH"] = install_path + os.pathsep + exec_env.get("PYTHONPATH", "")
|
| 65 |
+
|
| 66 |
+
# 4. Execute the Python script using the modified environment
|
| 67 |
script_output = subprocess.check_output(
|
| 68 |
+
[sys.executable, script_path],
|
| 69 |
stderr=subprocess.STDOUT,
|
| 70 |
+
text=True,
|
| 71 |
+
env=exec_env # Use the modified environment
|
| 72 |
)
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
except subprocess.CalledProcessError as e:
|
| 75 |
+
# If any command fails, capture its output as an error
|
| 76 |
+
error = e.output
|
| 77 |
|
| 78 |
+
return {
|
| 79 |
+
"pip_install_output": pip_output,
|
| 80 |
+
"script_execution_output": script_output,
|
| 81 |
+
"error_output": error
|
| 82 |
+
}
|