Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,33 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
-
import os
|
4 |
|
5 |
# Get API token from environment variable
|
6 |
-
API_TOKEN = os.getenv("HF_API_TOKEN") #
|
7 |
-
if not API_TOKEN:
|
8 |
-
st.error("⚠️ API Token is missing! Set HF_API_TOKEN in your environment.")
|
9 |
-
st.stop()
|
10 |
-
|
11 |
MODEL_ID = "bigcode/starcoder"
|
12 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
13 |
HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
|
14 |
|
15 |
def translate_code(code_snippet, source_lang, target_lang):
|
16 |
-
"""Translate code using Hugging Face API
|
17 |
-
prompt = f""
|
18 |
-
Translate the following {source_lang} code to {target_lang}:
|
19 |
-
|
20 |
-
{code_snippet}
|
21 |
-
|
22 |
-
Translated {target_lang} Code:
|
23 |
-
"""
|
24 |
|
25 |
response = requests.post(API_URL, headers=HEADERS, json={
|
26 |
-
"inputs": prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
})
|
28 |
|
29 |
if response.status_code == 200:
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
generated_text = response_json[0].get("generated_text", "").strip()
|
34 |
-
|
35 |
-
if generated_text:
|
36 |
-
# Extract only the translated code
|
37 |
-
translated_code = generated_text.split(f"Translated {target_lang} Code:")[-1].strip()
|
38 |
-
return translated_code
|
39 |
-
else:
|
40 |
-
return "⚠️ Error: No translated text received."
|
41 |
-
|
42 |
-
else:
|
43 |
-
return "⚠️ Error: Unexpected response format."
|
44 |
-
|
45 |
-
elif response.status_code == 400:
|
46 |
-
return "⚠️ Error: Invalid request. Check input format."
|
47 |
-
|
48 |
-
elif response.status_code == 401:
|
49 |
-
return "⚠️ Error: Unauthorized. Check your API token."
|
50 |
-
|
51 |
-
elif response.status_code == 403:
|
52 |
-
return "⚠️ Error: Access forbidden. You may need special access to this model."
|
53 |
-
|
54 |
-
elif response.status_code == 503:
|
55 |
-
return "⚠️ Error: Model is loading. Please wait and try again."
|
56 |
-
|
57 |
else:
|
58 |
-
return f"
|
59 |
|
60 |
# Streamlit UI
|
61 |
st.title("🔄 Code Translator using StarCoder")
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
+
import os # Import os to access environment variables
|
4 |
|
5 |
# Get API token from environment variable
|
6 |
+
API_TOKEN = os.getenv("HF_API_TOKEN") # Fetch token securely
|
|
|
|
|
|
|
|
|
7 |
MODEL_ID = "bigcode/starcoder"
|
8 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
9 |
HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
|
10 |
|
11 |
def translate_code(code_snippet, source_lang, target_lang):
|
12 |
+
"""Translate code using Hugging Face API securely."""
|
13 |
+
prompt = f"Translate the following {source_lang} code to {target_lang}:\n\n{code_snippet}\n\nTranslated {target_lang} Code:\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
response = requests.post(API_URL, headers=HEADERS, json={
|
16 |
+
"inputs": prompt,
|
17 |
+
"parameters": {
|
18 |
+
"max_new_tokens": 150,
|
19 |
+
"temperature": 0.2,
|
20 |
+
"top_k": 50,
|
21 |
+
"stop": ["\n\n", "#", "//", "'''"]
|
22 |
+
}
|
23 |
})
|
24 |
|
25 |
if response.status_code == 200:
|
26 |
+
generated_text = response.json()[0]["generated_text"]
|
27 |
+
translated_code = generated_text.split(f"Translated {target_lang} Code:\n")[-1].strip()
|
28 |
+
return translated_code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
else:
|
30 |
+
return f"Error: {response.status_code}, {response.text}"
|
31 |
|
32 |
# Streamlit UI
|
33 |
st.title("🔄 Code Translator using StarCoder")
|