tracywong117 commited on
Commit
9543229
·
1 Parent(s): d9c298e

initial commit

Browse files

try if subprocess is work

Files changed (4) hide show
  1. Dockerfile +23 -0
  2. YourJavaProgram.java +34 -0
  3. app.py +34 -0
  4. requirements.txt +1 -0
Dockerfile ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.8-slim-buster
2
+
3
+ WORKDIR /app
4
+
5
+ # Install Java
6
+ RUN apt-get update && apt-get install -y openjdk-11-jdk
7
+
8
+ # Set the JAVA_HOME environment variable
9
+ ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64
10
+
11
+ # Copy the Python files and Java program to the Docker image
12
+ COPY app.py YourJavaProgram.java requirements.txt ./
13
+
14
+ # Install required Python dependencies
15
+ RUN pip install -r requirements.txt
16
+
17
+ # Compile the Java program
18
+ RUN javac YourJavaProgram.java
19
+
20
+ EXPOSE 7860
21
+
22
+ # Set the command to run the Python application
23
+ CMD ["python", "app.py"]
YourJavaProgram.java ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // YourJavaProgram.java
2
+
3
+ import java.io.FileWriter;
4
+ import java.io.IOException;
5
+
6
+ public class YourJavaProgram {
7
+ public static void main(String[] args) {
8
+ // Check if the input argument is provided
9
+ if (args.length < 1) {
10
+ System.out.println("Please provide an input argument.");
11
+ return;
12
+ }
13
+
14
+ // Parse the input argument as an integer
15
+ int number;
16
+ try {
17
+ number = Integer.parseInt(args[0]);
18
+ } catch (NumberFormatException e) {
19
+ System.out.println("Invalid input argument. Please provide an integer.");
20
+ return;
21
+ }
22
+
23
+ // Perform some operation or computation
24
+ int result = number * 2;
25
+
26
+ // Create a file writer for the output file
27
+ try (FileWriter writer = new FileWriter("output.txt")) {
28
+ // Write the result to the output file
29
+ writer.write(String.valueOf(result));
30
+ } catch (IOException e) {
31
+ e.printStackTrace();
32
+ }
33
+ }
34
+ }
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import subprocess
3
+
4
+ def run_java(input_value, output_file):
5
+ # Run the Java program using subprocess and redirect input/output
6
+ subprocess.run(["java", "YourJavaProgram", input_value])
7
+
8
+ # Read the content of the output file
9
+ with open(output_file, "r") as file:
10
+ result = file.read()
11
+
12
+ return result
13
+
14
+ def java_program_runner(input_file):
15
+ output_file = "output.txt"
16
+ with open(input_file, "r") as file:
17
+ input_value = file.read()
18
+ print(input_value)
19
+ run_java(input_value, output_file)
20
+ with open(output_file, "r") as file:
21
+ result = file.read()
22
+ print(result)
23
+ return result
24
+
25
+ iface = gr.Interface(
26
+ fn=java_program_runner,
27
+ inputs=gr.File(label="Input File"),
28
+ outputs="text",
29
+ title="Java Program Runner",
30
+ description="Runs a Java program and returns the result.",
31
+ theme="default",
32
+ )
33
+
34
+ iface.launch(server_name="0.0.0.0", server_port=7860)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio