Spaces:
Running
Running
Initial upload via HfApi
Browse files- Dockerfile +18 -0
- README.md +3 -3
- app.py +57 -0
- requirements.txt +2 -0
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Start from a Python base image
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# Set the working directory inside the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy the requirements file and install Python packages
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
+
|
| 11 |
+
# Copy your application code
|
| 12 |
+
COPY app.py .
|
| 13 |
+
|
| 14 |
+
# Expose the port the app runs on (standard for HF Spaces)
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# Command to run the Flask application
|
| 18 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
title: Python Compiler
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
---
|
|
|
|
| 1 |
---
|
| 2 |
title: Python Compiler
|
| 3 |
+
emoji: 💻
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
---
|
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import tempfile
|
| 4 |
+
from flask import Flask, request, jsonify
|
| 5 |
+
from flask_cors import CORS
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
CORS(app)
|
| 9 |
+
|
| 10 |
+
@app.route('/')
|
| 11 |
+
def home():
|
| 12 |
+
return jsonify({
|
| 13 |
+
"status": "ok",
|
| 14 |
+
"message": "Python interpreter backend is running. Send POST requests to /api/run-python"
|
| 15 |
+
})
|
| 16 |
+
|
| 17 |
+
@app.route('/api/run-python', methods=['POST'])
|
| 18 |
+
def run_python():
|
| 19 |
+
code = request.json.get('code', '')
|
| 20 |
+
user_input = request.json.get('input', '')
|
| 21 |
+
|
| 22 |
+
if not code:
|
| 23 |
+
return jsonify({'error': 'No code provided.'}), 400
|
| 24 |
+
|
| 25 |
+
temp_file_path = None
|
| 26 |
+
try:
|
| 27 |
+
# Create a temporary file to write the Python code
|
| 28 |
+
with tempfile.NamedTemporaryFile(suffix=".py", delete=False, mode='w', encoding='utf-8') as temp_file:
|
| 29 |
+
temp_file.write(code)
|
| 30 |
+
temp_file_path = temp_file.name
|
| 31 |
+
|
| 32 |
+
# Run the Python script as a subprocess
|
| 33 |
+
run_process = subprocess.run(
|
| 34 |
+
['python', temp_file_path], # The command to run
|
| 35 |
+
input=user_input,
|
| 36 |
+
capture_output=True,
|
| 37 |
+
text=True,
|
| 38 |
+
timeout=15 # Set a timeout for safety
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Check if the process had errors
|
| 42 |
+
if run_process.returncode != 0:
|
| 43 |
+
return jsonify({'errors': run_process.stderr})
|
| 44 |
+
else:
|
| 45 |
+
return jsonify({'output': run_process.stdout})
|
| 46 |
+
|
| 47 |
+
except subprocess.TimeoutExpired:
|
| 48 |
+
return jsonify({'error': 'Code execution timed out (15 seconds limit).'}), 408
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return jsonify({'error': f"An unexpected error occurred: {str(e)}"}), 500
|
| 51 |
+
finally:
|
| 52 |
+
# Clean up the temporary file, even if an error occurred
|
| 53 |
+
if temp_file_path and os.path.exists(temp_file_path):
|
| 54 |
+
os.remove(temp_file_path)
|
| 55 |
+
|
| 56 |
+
if __name__ == '__main__':
|
| 57 |
+
app.run(host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
flask-cors
|