Spaces:
Running
Running
Initial upload via HfApi
Browse files- Dockerfile +21 -0
- README.md +3 -3
- app.py +71 -0
- requirements.txt +2 -0
Dockerfile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Start from a Python base image
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# Install the Java Development Kit (JDK)
|
| 5 |
+
RUN apt-get update && apt-get install -y default-jdk
|
| 6 |
+
|
| 7 |
+
# Set the working directory inside the container
|
| 8 |
+
WORKDIR /app
|
| 9 |
+
|
| 10 |
+
# Copy the requirements file and install Python packages
|
| 11 |
+
COPY requirements.txt .
|
| 12 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 13 |
+
|
| 14 |
+
# Copy your application code
|
| 15 |
+
COPY app.py .
|
| 16 |
+
|
| 17 |
+
# Expose the port the app runs on (standard for HF Spaces)
|
| 18 |
+
EXPOSE 7860
|
| 19 |
+
|
| 20 |
+
# Command to run the Flask application
|
| 21 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
title: Java Compiler
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
---
|
|
|
|
| 1 |
---
|
| 2 |
title: Java Compiler
|
| 3 |
+
emoji: ๐
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: gray
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
---
|
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import re # NEW: Import the 're' module for regular expressions
|
| 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": "Java compiler backend is running. Send POST requests to /api/run-java"
|
| 15 |
+
})
|
| 16 |
+
|
| 17 |
+
@app.route('/api/run-java', methods=['POST'])
|
| 18 |
+
def run_java():
|
| 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 |
+
# --- MODIFIED LOGIC: Find the public class name ---
|
| 26 |
+
class_name = "Main" # Default class name
|
| 27 |
+
# Regex to find "public class TheClassName"
|
| 28 |
+
match = re.search(r'public\s+class\s+([a-zA-Z0-9_]+)', code)
|
| 29 |
+
if match:
|
| 30 |
+
class_name = match.group(1)
|
| 31 |
+
|
| 32 |
+
file_path = f"{class_name}.java"
|
| 33 |
+
# --- END OF MODIFIED LOGIC ---
|
| 34 |
+
|
| 35 |
+
with open(file_path, "w") as f:
|
| 36 |
+
f.write(code)
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
compile_process = subprocess.run(
|
| 40 |
+
['javac', file_path],
|
| 41 |
+
capture_output=True, text=True, timeout=10
|
| 42 |
+
)
|
| 43 |
+
if compile_process.returncode != 0:
|
| 44 |
+
return jsonify({'compileErrors': compile_process.stderr})
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return jsonify({'error': f"An unexpected error occurred during compilation: {str(e)}"})
|
| 47 |
+
finally:
|
| 48 |
+
# We need to clean up the .java file even if compilation fails
|
| 49 |
+
if os.path.exists(file_path):
|
| 50 |
+
os.remove(file_path)
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
run_process = subprocess.run(
|
| 54 |
+
['java', class_name],
|
| 55 |
+
input=user_input,
|
| 56 |
+
capture_output=True, text=True, timeout=15
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
if run_process.returncode != 0:
|
| 60 |
+
return jsonify({'runErrors': run_process.stderr})
|
| 61 |
+
else:
|
| 62 |
+
return jsonify({'output': run_process.stdout})
|
| 63 |
+
finally:
|
| 64 |
+
# The .java file is already removed above. Now remove the .class file.
|
| 65 |
+
class_file = f"{class_name}.class"
|
| 66 |
+
if os.path.exists(class_file):
|
| 67 |
+
os.remove(class_file)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
if __name__ == '__main__':
|
| 71 |
+
app.run(host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask
|
| 2 |
+
Flask-Cors
|