Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import subprocess | |
| import sys | |
| import io | |
| import os | |
| import shutil # For checking dependencies | |
| import requests | |
| import google.generativeai as genai # Gemini API | |
| # Ensure Java and g++ are installed (for cloud environments) | |
| if not shutil.which("javac"): | |
| os.system("apt-get update && apt-get install -y default-jdk g++") | |
| # Function to execute Python code | |
| def execute_python(code, user_input): | |
| old_stdout = sys.stdout | |
| redirected_output = io.StringIO() | |
| sys.stdout = redirected_output | |
| input_values = user_input.strip().split("\n") | |
| input_counter = 0 | |
| def mock_input(prompt=""): | |
| nonlocal input_counter | |
| if input_counter < len(input_values): | |
| value = input_values[input_counter] | |
| input_counter += 1 | |
| return value | |
| else: | |
| raise ValueError("Not enough inputs provided.") | |
| try: | |
| exec(code, {"input": mock_input}) | |
| output = redirected_output.getvalue() | |
| except Exception as e: | |
| output = f"Error: {str(e)}" | |
| finally: | |
| sys.stdout = old_stdout | |
| return output.strip() | |
| # Function to execute Java code | |
| def execute_java(code, user_input): | |
| if not shutil.which("javac"): | |
| return "Error: Java JDK is not installed. Please install it using 'apt-get install default-jdk'." | |
| with open("Main.java", "w") as file: | |
| file.write(code) | |
| compile_process = subprocess.run(["javac", "Main.java"], capture_output=True, text=True) | |
| if compile_process.returncode != 0: | |
| return f"Compilation Error:\n{compile_process.stderr}" | |
| run_process = subprocess.run(["java", "Main"], input=user_input.encode(), capture_output=True, text=True) | |
| return run_process.stdout if run_process.returncode == 0 else f"Runtime Error:\n{run_process.stderr}" | |
| # Function to execute C++ code | |
| def execute_cpp(code, user_input): | |
| if not shutil.which("g++"): | |
| return "Error: C++ Compiler is not installed. Please install it using 'apt-get install g++'." | |
| with open("main.cpp", "w") as file: | |
| file.write(code) | |
| compile_process = subprocess.run(["g++", "main.cpp", "-o", "main"], capture_output=True, text=True) | |
| if compile_process.returncode != 0: | |
| return f"Compilation Error:\n{compile_process.stderr}" | |
| run_process = subprocess.run(["./main"], input=user_input.encode(), capture_output=True, text=True) | |
| return run_process.stdout if run_process.returncode == 0 else f"Runtime Error:\n{run_process.stderr}" | |
| # Streamlit UI for Code Execution | |
| st.title("๐ป Multi-Language Code Runner") | |
| st.write("Write your Python, Java, or C++ code and get the correct output!") | |
| languages = ["Python", "Java", "C++"] | |
| selected_lang = st.selectbox("Select Language:", languages) | |
| code_input = st.text_area("Enter your code:", height=200) | |
| user_input = st.text_area("Enter input values (one per line):", height=100) | |
| if st.button("Run Code"): | |
| if code_input.strip(): | |
| with st.spinner("Executing..."): | |
| if selected_lang == "Python": | |
| output = execute_python(code_input, user_input) | |
| elif selected_lang == "Java": | |
| output = execute_java(code_input, user_input) | |
| elif selected_lang == "C++": | |
| output = execute_cpp(code_input, user_input) | |
| st.subheader("Output:") | |
| st.code(output, language="plaintext") | |
| else: | |
| st.warning("โ ๏ธ Please enter some code before running.") | |
| # V1 without gemini api | |
| # import streamlit as st | |
| # import requests | |
| # import os # Import os to access environment variables | |
| # # Get API token from environment variable | |
| # API_TOKEN = os.getenv("HF_API_TOKEN") | |
| # # Change MODEL_ID to a better model | |
| # MODEL_ID = "Salesforce/codet5p-770m" # CodeT5+ (Recommended) | |
| # # MODEL_ID = "bigcode/starcoder2-15b" # StarCoder2 | |
| # # MODEL_ID = "bigcode/starcoder" | |
| # API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}" | |
| # HEADERS = {"Authorization": f"Bearer {API_TOKEN}"} | |
| # def translate_code(code_snippet, source_lang, target_lang): | |
| # """Translate code using Hugging Face API securely.""" | |
| # prompt = f"Translate the following {source_lang} code to {target_lang}:\n\n{code_snippet}\n\nTranslated {target_lang} Code:\n" | |
| # response = requests.post(API_URL, headers=HEADERS, json={ | |
| # "inputs": prompt, | |
| # "parameters": { | |
| # "max_new_tokens": 150, | |
| # "temperature": 0.2, | |
| # "top_k": 50 | |
| # # "stop": ["\n\n", "#", "//", "'''"] | |
| # } | |
| # }) | |
| # if response.status_code == 200: | |
| # generated_text = response.json()[0]["generated_text"] | |
| # translated_code = generated_text.split(f"Translated {target_lang} Code:\n")[-1].strip() | |
| # return translated_code | |
| # else: | |
| # return f"Error: {response.status_code}, {response.text}" | |
| # # Streamlit UI | |
| # st.title("๐ Code Translator using StarCoder") | |
| # st.write("Translate code between different programming languages using AI.") | |
| # languages = ["Python", "Java", "C++", "C"] | |
| # source_lang = st.selectbox("Select source language", languages) | |
| # target_lang = st.selectbox("Select target language", languages) | |
| # code_input = st.text_area("Enter your code here:", height=200) | |
| # if st.button("Translate"): | |
| # if code_input.strip(): | |
| # with st.spinner("Translating..."): | |
| # translated_code = translate_code(code_input, source_lang, target_lang) | |
| # st.subheader("Translated Code:") | |
| # st.code(translated_code, language=target_lang.lower()) | |
| # else: | |
| # st.warning("โ ๏ธ Please enter some code before translating.") |