Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,17 @@
|
|
| 1 |
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import subprocess
|
| 4 |
import sys
|
| 5 |
import io
|
| 6 |
import os
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
# Ensure Java and g++ are installed (for cloud environments)
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
# Function to execute Python code
|
| 14 |
def execute_python(code, user_input):
|
|
@@ -40,6 +43,9 @@ def execute_python(code, user_input):
|
|
| 40 |
|
| 41 |
# Function to execute Java code
|
| 42 |
def execute_java(code, user_input):
|
|
|
|
|
|
|
|
|
|
| 43 |
with open("Main.java", "w") as file:
|
| 44 |
file.write(code)
|
| 45 |
|
|
@@ -47,11 +53,14 @@ def execute_java(code, user_input):
|
|
| 47 |
if compile_process.returncode != 0:
|
| 48 |
return f"Compilation Error:\n{compile_process.stderr}"
|
| 49 |
|
| 50 |
-
run_process = subprocess.run(["java", "Main"],
|
| 51 |
return run_process.stdout if run_process.returncode == 0 else f"Runtime Error:\n{run_process.stderr}"
|
| 52 |
|
| 53 |
# Function to execute C++ code
|
| 54 |
def execute_cpp(code, user_input):
|
|
|
|
|
|
|
|
|
|
| 55 |
with open("main.cpp", "w") as file:
|
| 56 |
file.write(code)
|
| 57 |
|
|
@@ -62,7 +71,7 @@ def execute_cpp(code, user_input):
|
|
| 62 |
run_process = subprocess.run(["./main"], input=user_input.encode(), capture_output=True, text=True)
|
| 63 |
return run_process.stdout if run_process.returncode == 0 else f"Runtime Error:\n{run_process.stderr}"
|
| 64 |
|
| 65 |
-
# Streamlit UI
|
| 66 |
st.title("💻 Multi-Language Code Runner")
|
| 67 |
st.write("Write your Python, Java, or C++ code and get the correct output!")
|
| 68 |
|
|
@@ -87,6 +96,22 @@ if st.button("Run Code"):
|
|
| 87 |
else:
|
| 88 |
st.warning("⚠️ Please enter some code before running.")
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
# V1 without gemini api
|
| 91 |
|
| 92 |
# import streamlit as st
|
|
|
|
| 1 |
|
| 2 |
+
|
| 3 |
import streamlit as st
|
| 4 |
import subprocess
|
| 5 |
import sys
|
| 6 |
import io
|
| 7 |
import os
|
| 8 |
+
import shutil # For checking dependencies
|
| 9 |
+
import requests
|
| 10 |
+
import google.generativeai as genai # Gemini API
|
| 11 |
|
| 12 |
# Ensure Java and g++ are installed (for cloud environments)
|
| 13 |
+
if not shutil.which("javac"):
|
| 14 |
+
os.system("apt-get update && apt-get install -y default-jdk g++")
|
| 15 |
|
| 16 |
# Function to execute Python code
|
| 17 |
def execute_python(code, user_input):
|
|
|
|
| 43 |
|
| 44 |
# Function to execute Java code
|
| 45 |
def execute_java(code, user_input):
|
| 46 |
+
if not shutil.which("javac"):
|
| 47 |
+
return "Error: Java JDK is not installed. Please install it using 'apt-get install default-jdk'."
|
| 48 |
+
|
| 49 |
with open("Main.java", "w") as file:
|
| 50 |
file.write(code)
|
| 51 |
|
|
|
|
| 53 |
if compile_process.returncode != 0:
|
| 54 |
return f"Compilation Error:\n{compile_process.stderr}"
|
| 55 |
|
| 56 |
+
run_process = subprocess.run(["java", "Main"], input=user_input.encode(), capture_output=True, text=True)
|
| 57 |
return run_process.stdout if run_process.returncode == 0 else f"Runtime Error:\n{run_process.stderr}"
|
| 58 |
|
| 59 |
# Function to execute C++ code
|
| 60 |
def execute_cpp(code, user_input):
|
| 61 |
+
if not shutil.which("g++"):
|
| 62 |
+
return "Error: C++ Compiler is not installed. Please install it using 'apt-get install g++'."
|
| 63 |
+
|
| 64 |
with open("main.cpp", "w") as file:
|
| 65 |
file.write(code)
|
| 66 |
|
|
|
|
| 71 |
run_process = subprocess.run(["./main"], input=user_input.encode(), capture_output=True, text=True)
|
| 72 |
return run_process.stdout if run_process.returncode == 0 else f"Runtime Error:\n{run_process.stderr}"
|
| 73 |
|
| 74 |
+
# Streamlit UI for Code Execution
|
| 75 |
st.title("💻 Multi-Language Code Runner")
|
| 76 |
st.write("Write your Python, Java, or C++ code and get the correct output!")
|
| 77 |
|
|
|
|
| 96 |
else:
|
| 97 |
st.warning("⚠️ Please enter some code before running.")
|
| 98 |
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
# V1 without gemini api
|
| 116 |
|
| 117 |
# import streamlit as st
|