Spaces:
Runtime error
Runtime error
Initial upload via HfApi
Browse files- Dockerfile +23 -0
- README.md +3 -3
- app.py +81 -0
- requirements.txt +2 -0
Dockerfile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Start from a Python base image
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# Install the GNU C++ compiler (g++)
|
| 5 |
+
# build-essential is a common package that includes g++, make, and other tools
|
| 6 |
+
RUN apt-get update && apt-get install -y g++
|
| 7 |
+
|
| 8 |
+
# Set the working directory inside the container
|
| 9 |
+
WORKDIR /app
|
| 10 |
+
|
| 11 |
+
# Copy the requirements file and install Python packages
|
| 12 |
+
# (This file will be the same as your Java project)
|
| 13 |
+
COPY requirements.txt .
|
| 14 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 15 |
+
|
| 16 |
+
# Copy your application code
|
| 17 |
+
COPY app.py .
|
| 18 |
+
|
| 19 |
+
# Expose the port the app runs on (standard for HF Spaces)
|
| 20 |
+
EXPOSE 7860
|
| 21 |
+
|
| 22 |
+
# Command to run the Flask application
|
| 23 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
title: Cpp Compiler
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
---
|
|
|
|
| 1 |
---
|
| 2 |
title: Cpp Compiler
|
| 3 |
+
emoji: 🌍
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
---
|
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import stat
|
| 3 |
+
import subprocess
|
| 4 |
+
from flask import Flask, request, jsonify
|
| 5 |
+
from flask_cors import CORS
|
| 6 |
+
import uuid # Import the uuid library to create unique filenames
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
CORS(app)
|
| 10 |
+
|
| 11 |
+
@app.route('/')
|
| 12 |
+
def home():
|
| 13 |
+
return jsonify({
|
| 14 |
+
"status": "ok",
|
| 15 |
+
"message": "C++ compiler backend is running. Send POST requests to /api/run-cpp"
|
| 16 |
+
})
|
| 17 |
+
|
| 18 |
+
@app.route('/api/run-cpp', methods=['POST'])
|
| 19 |
+
def run_cpp():
|
| 20 |
+
code = request.json.get('code', '')
|
| 21 |
+
user_input = request.json.get('input', '')
|
| 22 |
+
|
| 23 |
+
if not code:
|
| 24 |
+
return jsonify({'error': 'No code provided.'}), 400
|
| 25 |
+
|
| 26 |
+
# **CRITICAL FIX: Use the /tmp directory for all file operations.**
|
| 27 |
+
# This directory is guaranteed to be writable in Linux container environments.
|
| 28 |
+
# A unique name prevents conflicts if two requests happen at once.
|
| 29 |
+
unique_name = str(uuid.uuid4())
|
| 30 |
+
temp_dir = "/tmp"
|
| 31 |
+
source_file_path = os.path.join(temp_dir, f"{unique_name}.cpp")
|
| 32 |
+
executable_path = os.path.join(temp_dir, unique_name)
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
# Write the user's code to the temporary .cpp file in /tmp
|
| 36 |
+
with open(source_file_path, "w") as f:
|
| 37 |
+
f.write(code)
|
| 38 |
+
|
| 39 |
+
# --- Compilation Step ---
|
| 40 |
+
compile_process = subprocess.run(
|
| 41 |
+
['g++', '-std=c++17', source_file_path, '-o', executable_path],
|
| 42 |
+
capture_output=True, text=True, timeout=10
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
if compile_process.returncode != 0:
|
| 46 |
+
return jsonify({'compileErrors': compile_process.stderr})
|
| 47 |
+
|
| 48 |
+
# --- Set Execute Permissions ---
|
| 49 |
+
# This is still necessary for the compiled file in /tmp.
|
| 50 |
+
st = os.stat(executable_path)
|
| 51 |
+
os.chmod(executable_path, st.st_mode | stat.S_IEXEC)
|
| 52 |
+
|
| 53 |
+
# --- Execution Step ---
|
| 54 |
+
# Execute the program using its full, absolute path.
|
| 55 |
+
run_process = subprocess.run(
|
| 56 |
+
[executable_path], # No './' is needed when using the full path
|
| 57 |
+
input=user_input,
|
| 58 |
+
capture_output=True, text=True, timeout=15
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
if run_process.returncode != 0:
|
| 62 |
+
return jsonify({'runErrors': run_process.stderr})
|
| 63 |
+
else:
|
| 64 |
+
return jsonify({'output': run_process.stdout})
|
| 65 |
+
|
| 66 |
+
except subprocess.TimeoutExpired:
|
| 67 |
+
return jsonify({'error': 'Execution timed out. Your program took too long to run or has an infinite loop.'})
|
| 68 |
+
except Exception as e:
|
| 69 |
+
# This will catch any other unexpected errors.
|
| 70 |
+
return jsonify({'error': f"An unexpected server error occurred: {str(e)}"})
|
| 71 |
+
|
| 72 |
+
finally:
|
| 73 |
+
# --- Cleanup Step ---
|
| 74 |
+
# Always clean up the files from the /tmp directory to be a good citizen.
|
| 75 |
+
if os.path.exists(source_file_path):
|
| 76 |
+
os.remove(source_file_path)
|
| 77 |
+
if os.path.exists(executable_path):
|
| 78 |
+
os.remove(executable_path)
|
| 79 |
+
|
| 80 |
+
if __name__ == '__main__':
|
| 81 |
+
app.run(host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask
|
| 2 |
+
Flask-Cors
|