Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,40 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
MODEL_ID = "Salesforce/codet5p-770m" # CodeT5+ (Recommended)
|
| 12 |
-
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
| 13 |
-
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 14 |
-
|
| 15 |
-
# Initialize Gemini API
|
| 16 |
-
genai.configure(api_key='AIzaSyBkc8CSEhyYwZAuUiJfzF1Xtns-RYmBOpg')
|
| 17 |
-
|
| 18 |
-
def translate_code(code_snippet, source_lang, target_lang):
|
| 19 |
-
"""Translate code using Hugging Face API."""
|
| 20 |
-
prompt = f"Translate the following {source_lang} code to {target_lang}:\n\n{code_snippet}\n\nTranslated {target_lang} Code:\n"
|
| 21 |
-
|
| 22 |
-
response = requests.post(API_URL, headers=HEADERS, json={
|
| 23 |
-
"inputs": prompt,
|
| 24 |
-
"parameters": {
|
| 25 |
-
"max_new_tokens": 150,
|
| 26 |
-
"temperature": 0.2,
|
| 27 |
-
"top_k": 50
|
| 28 |
-
}
|
| 29 |
-
})
|
| 30 |
-
|
| 31 |
-
if response.status_code == 200:
|
| 32 |
-
generated_text = response.json()[0]["generated_text"]
|
| 33 |
-
translated_code = generated_text.split(f"Translated {target_lang} Code:\n")[-1].strip()
|
| 34 |
-
return translated_code
|
| 35 |
-
else:
|
| 36 |
-
return f"Error: {response.status_code}, {response.text}"
|
| 37 |
-
|
| 38 |
-
def fallback_translate_with_gemini(code_snippet, source_lang, target_lang):
|
| 39 |
-
"""Fallback function using Gemini API for translation."""
|
| 40 |
-
prompt = f"""You are a code translation expert. Convert the following {source_lang} code to {target_lang}:
|
| 41 |
-
|
| 42 |
-
{code_snippet}
|
| 43 |
-
Ensure the translation is accurate and follows {target_lang} best practices.
|
| 44 |
-
Do not give any explaination. only give the translated code.
|
| 45 |
-
"""
|
| 46 |
try:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
return response.text.strip() if response else "Translation failed."
|
| 50 |
except Exception as e:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
#
|
| 54 |
-
st.title("🔄 Programming Language Translator")
|
| 55 |
-
st.write("Translate code between different programming languages using AI.")
|
| 56 |
|
| 57 |
-
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
if "translate_attempts" not in st.session_state:
|
| 65 |
-
st.session_state.translate_attempts = 0
|
| 66 |
-
st.session_state.translated_code = ""
|
| 67 |
|
| 68 |
-
if st.button("
|
| 69 |
if code_input.strip():
|
| 70 |
-
st.
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
st.session_state.translated_code = translate_code(code_input, source_lang, target_lang)
|
| 75 |
-
else:
|
| 76 |
-
# Second attempt uses Gemini API
|
| 77 |
-
st.session_state.translated_code = fallback_translate_with_gemini(code_input, source_lang, target_lang)
|
| 78 |
-
|
| 79 |
-
st.subheader("Translated Code:")
|
| 80 |
-
st.code(st.session_state.translated_code, language=target_lang.lower())
|
| 81 |
else:
|
| 82 |
-
st.warning("⚠️ Please enter some code before
|
|
|
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
|
|
|
|
| 1 |
+
#
|
| 2 |
import streamlit as st
|
| 3 |
+
import sys
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
def execute_code(code):
|
| 7 |
+
"""Execute the given code and return the output or error message."""
|
| 8 |
+
old_stdout = sys.stdout # Backup original stdout
|
| 9 |
+
redirected_output = io.StringIO() # Create a new string buffer
|
| 10 |
+
sys.stdout = redirected_output # Redirect stdout to buffer
|
| 11 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
+
exec(code, {}) # Execute the user's code safely
|
| 14 |
+
output = redirected_output.getvalue() # Get the output from buffer
|
|
|
|
| 15 |
except Exception as e:
|
| 16 |
+
output = f"Error: {str(e)}" # Capture and display any errors
|
| 17 |
+
finally:
|
| 18 |
+
sys.stdout = old_stdout # Restore original stdout
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
return output.strip() # Return cleaned output
|
| 21 |
|
| 22 |
+
# Streamlit UI
|
| 23 |
+
st.title("💻 Code Runner")
|
| 24 |
+
st.write("Write your code and get the correct output!")
|
| 25 |
|
| 26 |
+
code_input = st.text_area("Enter your Python code:", height=200)
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
if st.button("Run Code"):
|
| 29 |
if code_input.strip():
|
| 30 |
+
with st.spinner("Executing..."):
|
| 31 |
+
output = execute_code(code_input) # Execute user code
|
| 32 |
+
st.subheader("Output:")
|
| 33 |
+
st.code(output, language="plaintext")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
else:
|
| 35 |
+
st.warning("⚠️ Please enter some Python code before running.")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
|
| 39 |
|
| 40 |
|