Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,104 @@
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
import streamlit as st
|
11 |
+
import requests
|
12 |
+
import os
|
13 |
+
|
14 |
+
# Load API token from environment
|
15 |
+
API_TOKEN = os.getenv("HF_API_TOKEN")
|
16 |
+
|
17 |
+
if not API_TOKEN:
|
18 |
+
st.error("⚠️ Hugging Face API token is missing! Set `HF_API_TOKEN` in your environment variables.")
|
19 |
+
st.stop() # Stop execution if the token is missing
|
20 |
+
|
21 |
+
# Define model API endpoint
|
22 |
+
MODEL_ID = "bigcode/starcoder"
|
23 |
+
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
24 |
+
HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
|
25 |
+
|
26 |
+
def translate_code(code_snippet, source_lang, target_lang):
|
27 |
+
"""Translates code from one language to another using Hugging Face API."""
|
28 |
+
prompt = f"""### Task: Convert {source_lang} code to {target_lang}.
|
29 |
+
|
30 |
+
{source_lang} Code:
|
31 |
+
```{source_lang.lower()}
|
32 |
+
{code_snippet}
|
33 |
+
```
|
34 |
+
|
35 |
+
Now convert it to {target_lang}:
|
36 |
+
|
37 |
+
```{target_lang.lower()}
|
38 |
+
"""
|
39 |
+
|
40 |
+
try:
|
41 |
+
response = requests.post(API_URL, headers=HEADERS, json={
|
42 |
+
"inputs": prompt,
|
43 |
+
"parameters": {
|
44 |
+
"max_new_tokens": 200,
|
45 |
+
"temperature": 0.2,
|
46 |
+
"top_k": 50,
|
47 |
+
}
|
48 |
+
})
|
49 |
+
|
50 |
+
if response.status_code == 200:
|
51 |
+
output = response.json()
|
52 |
+
|
53 |
+
if isinstance(output, list) and len(output) > 0:
|
54 |
+
generated_text = output[0].get("generated_text", "")
|
55 |
+
|
56 |
+
# Extract translated code only
|
57 |
+
translated_code = generated_text.split(f"```{target_lang.lower()}")[-1].strip()
|
58 |
+
translated_code = translated_code.replace("```", "").strip()
|
59 |
+
|
60 |
+
return translated_code if translated_code else "⚠️ Translation failed. No valid output received."
|
61 |
+
|
62 |
+
else:
|
63 |
+
return "⚠️ Unexpected response format from API."
|
64 |
+
|
65 |
+
elif response.status_code == 400:
|
66 |
+
return "⚠️ Error: Invalid request. Check input format."
|
67 |
+
|
68 |
+
elif response.status_code == 401:
|
69 |
+
return "⚠️ Error: Unauthorized. Check your API token."
|
70 |
+
|
71 |
+
elif response.status_code == 403:
|
72 |
+
return "⚠️ Error: Access forbidden. You may need special access to this model."
|
73 |
+
|
74 |
+
elif response.status_code == 503:
|
75 |
+
return "⚠️ Error: Model is loading. Please wait and try again."
|
76 |
+
|
77 |
+
else:
|
78 |
+
return f"⚠️ Error {response.status_code}: {response.text}"
|
79 |
+
|
80 |
+
except requests.exceptions.RequestException as e:
|
81 |
+
return f"⚠️ Network Error: {str(e)}"
|
82 |
+
|
83 |
+
# Streamlit UI
|
84 |
+
st.title("🔄 Code Translator using StarCoder")
|
85 |
+
st.write("Translate code between different programming languages using AI.")
|
86 |
+
|
87 |
+
# Define language options
|
88 |
+
languages = ["Python", "Java", "C", "C++"]
|
89 |
+
|
90 |
+
source_lang = st.selectbox("Select source language", languages)
|
91 |
+
target_lang = st.selectbox("Select target language", languages)
|
92 |
+
code_input = st.text_area("Enter your code here:", height=200)
|
93 |
+
|
94 |
+
if st.button("Translate"):
|
95 |
+
if source_lang == target_lang:
|
96 |
+
st.warning("⚠️ Source and target languages cannot be the same.")
|
97 |
+
elif code_input.strip():
|
98 |
+
with st.spinner("Translating..."):
|
99 |
+
translated_code = translate_code(code_input, source_lang, target_lang)
|
100 |
+
st.subheader(f"Translated {target_lang} Code:")
|
101 |
+
st.code(translated_code, language=target_lang.lower())
|
102 |
+
else:
|
103 |
+
st.warning("⚠️ Please enter some code before translating.")
|
104 |
+
|