Update app.py
Browse files
app.py
CHANGED
@@ -28,22 +28,14 @@ model = tf.keras.models.load_model("java_to_python_seq2seq_model.h5")
|
|
28 |
java_vectorizer = TextVectorization(max_tokens=vocab_size, output_sequence_length=sequence_length)
|
29 |
python_vectorizer = TextVectorization(max_tokens=vocab_size, output_sequence_length=sequence_length)
|
30 |
|
31 |
-
#
|
32 |
java_vectorizer.adapt(tf.data.Dataset.from_tensor_slices(["public class Main { public static void main(String[] args) {} }"]))
|
33 |
python_vectorizer.adapt(tf.data.Dataset.from_tensor_slices(["def main():\n pass"]))
|
34 |
|
35 |
-
# Reverse lookup for Python vocab
|
36 |
python_vocab = python_vectorizer.get_vocabulary()
|
37 |
index_to_word = dict(enumerate(python_vocab))
|
38 |
|
39 |
-
|
40 |
-
"""Greedy decoding of the prediction."""
|
41 |
-
pred_ids = tf.argmax(pred, axis=-1).numpy()[0]
|
42 |
-
tokens = [index_to_word.get(i, "") for i in pred_ids]
|
43 |
-
code = " ".join(tokens).replace("[UNK]", "").strip()
|
44 |
-
return code
|
45 |
-
|
46 |
-
# --- Translation Functions ---
|
47 |
|
48 |
def fallback_translate_with_gemini(code_snippet, source_lang, target_lang):
|
49 |
prompt = f"""You are a code translation expert. Convert the following {source_lang} code to {target_lang}:
|
@@ -61,10 +53,9 @@ def fallback_translate_with_gemini(code_snippet, source_lang, target_lang):
|
|
61 |
return f"Gemini API Error: {str(e)}"
|
62 |
|
63 |
def translate_with_local_model(code_snippet):
|
64 |
-
"""Local seq2seq Java→Python translation."""
|
65 |
try:
|
66 |
java_seq = java_vectorizer(tf.constant([code_snippet]))
|
67 |
-
python_in = tf.constant([[1] + [0] * (sequence_length - 1)])
|
68 |
translated_tokens = []
|
69 |
|
70 |
for i in range(sequence_length):
|
@@ -85,7 +76,6 @@ def translate_with_local_model(code_snippet):
|
|
85 |
return f"Local Model Error: {str(e)}"
|
86 |
|
87 |
def translate_code(code_snippet, source_lang, target_lang):
|
88 |
-
"""Hugging Face translation."""
|
89 |
prompt = f"Translate the following {source_lang} code to {target_lang}:\n\n{code_snippet}\n\nTranslated {target_lang} Code:\n"
|
90 |
response = requests.post(API_URL, headers=HEADERS, json={
|
91 |
"inputs": prompt,
|
@@ -102,13 +92,14 @@ def translate_code(code_snippet, source_lang, target_lang):
|
|
102 |
# --- Streamlit UI ---
|
103 |
|
104 |
st.title("🔄 Programming Language Translator")
|
105 |
-
st.write("Translate code between programming languages using 3-tier
|
106 |
|
107 |
languages = ["Python", "Java", "C++", "C"]
|
108 |
source_lang = st.selectbox("Select source language", languages)
|
109 |
target_lang = st.selectbox("Select target language", languages)
|
110 |
code_input = st.text_area("Enter your code here:", height=200)
|
111 |
|
|
|
112 |
if "translate_attempts" not in st.session_state:
|
113 |
st.session_state.translate_attempts = 0
|
114 |
st.session_state.translated_code = ""
|
@@ -119,12 +110,15 @@ if st.button("Translate"):
|
|
119 |
attempt = st.session_state.translate_attempts
|
120 |
|
121 |
with st.spinner(f"Translating..."):
|
|
|
122 |
if attempt == 1:
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
126 |
else:
|
127 |
-
|
|
|
128 |
|
129 |
st.subheader("Translated Code:")
|
130 |
st.code(st.session_state.translated_code, language=target_lang.lower())
|
@@ -143,7 +137,6 @@ if st.button("Translate"):
|
|
143 |
|
144 |
|
145 |
|
146 |
-
|
147 |
# version1: Without Trained model.
|
148 |
|
149 |
# import streamlit as st
|
|
|
28 |
java_vectorizer = TextVectorization(max_tokens=vocab_size, output_sequence_length=sequence_length)
|
29 |
python_vectorizer = TextVectorization(max_tokens=vocab_size, output_sequence_length=sequence_length)
|
30 |
|
31 |
+
# Dummy adaptation to initialize
|
32 |
java_vectorizer.adapt(tf.data.Dataset.from_tensor_slices(["public class Main { public static void main(String[] args) {} }"]))
|
33 |
python_vectorizer.adapt(tf.data.Dataset.from_tensor_slices(["def main():\n pass"]))
|
34 |
|
|
|
35 |
python_vocab = python_vectorizer.get_vocabulary()
|
36 |
index_to_word = dict(enumerate(python_vocab))
|
37 |
|
38 |
+
# --- Translator Functions ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
def fallback_translate_with_gemini(code_snippet, source_lang, target_lang):
|
41 |
prompt = f"""You are a code translation expert. Convert the following {source_lang} code to {target_lang}:
|
|
|
53 |
return f"Gemini API Error: {str(e)}"
|
54 |
|
55 |
def translate_with_local_model(code_snippet):
|
|
|
56 |
try:
|
57 |
java_seq = java_vectorizer(tf.constant([code_snippet]))
|
58 |
+
python_in = tf.constant([[1] + [0] * (sequence_length - 1)])
|
59 |
translated_tokens = []
|
60 |
|
61 |
for i in range(sequence_length):
|
|
|
76 |
return f"Local Model Error: {str(e)}"
|
77 |
|
78 |
def translate_code(code_snippet, source_lang, target_lang):
|
|
|
79 |
prompt = f"Translate the following {source_lang} code to {target_lang}:\n\n{code_snippet}\n\nTranslated {target_lang} Code:\n"
|
80 |
response = requests.post(API_URL, headers=HEADERS, json={
|
81 |
"inputs": prompt,
|
|
|
92 |
# --- Streamlit UI ---
|
93 |
|
94 |
st.title("🔄 Programming Language Translator")
|
95 |
+
st.write("Translate code between programming languages using 3-tier logic:")
|
96 |
|
97 |
languages = ["Python", "Java", "C++", "C"]
|
98 |
source_lang = st.selectbox("Select source language", languages)
|
99 |
target_lang = st.selectbox("Select target language", languages)
|
100 |
code_input = st.text_area("Enter your code here:", height=200)
|
101 |
|
102 |
+
# State initialization
|
103 |
if "translate_attempts" not in st.session_state:
|
104 |
st.session_state.translate_attempts = 0
|
105 |
st.session_state.translated_code = ""
|
|
|
110 |
attempt = st.session_state.translate_attempts
|
111 |
|
112 |
with st.spinner(f"Translating..."):
|
113 |
+
# First click
|
114 |
if attempt == 1:
|
115 |
+
if source_lang == "Java" and target_lang == "Python":
|
116 |
+
st.session_state.translated_code = translate_with_local_model(code_input)
|
117 |
+
else:
|
118 |
+
st.session_state.translated_code = translate_code(code_input, source_lang, target_lang)
|
119 |
else:
|
120 |
+
# Second and later attempts -> Gemini
|
121 |
+
st.session_state.translated_code = fallback_translate_with_gemini(code_input, source_lang, target_lang)
|
122 |
|
123 |
st.subheader("Translated Code:")
|
124 |
st.code(st.session_state.translated_code, language=target_lang.lower())
|
|
|
137 |
|
138 |
|
139 |
|
|
|
140 |
# version1: Without Trained model.
|
141 |
|
142 |
# import streamlit as st
|