File size: 3,370 Bytes
6609537
7949e50
6609537
 
 
 
 
 
 
 
 
01e0394
6609537
 
e3e93d3
6609537
 
 
7949e50
6609537
e3e93d3
6609537
 
 
e3e93d3
6609537
7949e50
6609537
e3e93d3
6609537
 
 
 
3e86e45
6609537
 
 
e3e93d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#
import streamlit as st
import sys
import io

def execute_code(code):
    """Execute the given code and return the output or error message."""
    old_stdout = sys.stdout  # Backup original stdout
    redirected_output = io.StringIO()  # Create a new string buffer
    sys.stdout = redirected_output  # Redirect stdout to buffer

    try:
        exec(code, {})  # Execute the user's code safely
        output = redirected_output.getvalue()  # Get the output from buffer
    except Exception as e:
        output = f"Error: {str(e)}"  # Capture and display any errors
    finally:
        sys.stdout = old_stdout  # Restore original stdout

    return output.strip()  # Return cleaned output

# Streamlit UI
st.title("💻 Code Runner")
st.write("Write your code and get the correct output!")

code_input = st.text_area("Enter your Python code:", height=200)

if st.button("Run Code"):
    if code_input.strip():
        with st.spinner("Executing..."):
            output = execute_code(code_input)  # Execute user code
        st.subheader("Output:")
        st.code(output, language="plaintext")
    else:
        st.warning("⚠️ Please enter some Python 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.")