Spaces:
Sleeping
Sleeping
Update translator.py
Browse files- translator.py +26 -4
translator.py
CHANGED
@@ -9,7 +9,9 @@ def get_api_token():
|
|
9 |
return api_token
|
10 |
|
11 |
API_TOKEN = get_api_token()
|
12 |
-
|
|
|
|
|
13 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
14 |
HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
|
15 |
|
@@ -17,17 +19,37 @@ def translate_code(code_snippet, source_lang, target_lang):
|
|
17 |
"""Translate code using the Hugging Face API."""
|
18 |
prompt = (
|
19 |
f"### Task: Convert {source_lang} code to {target_lang} code.\n\n"
|
20 |
-
f"### {source_lang} Code:\n\n{code_snippet}\n\n"
|
21 |
-
f"### {target_lang} Code:\n"
|
22 |
)
|
23 |
|
24 |
try:
|
25 |
response = requests.post(API_URL, headers=HEADERS, json={"inputs": prompt})
|
|
|
26 |
if response.status_code == 200:
|
27 |
result = response.json()
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
else:
|
30 |
return f"⚠️ API Error {response.status_code}: {response.text}"
|
|
|
31 |
except requests.exceptions.RequestException as e:
|
32 |
return f"⚠️ Network Error: {str(e)}"
|
33 |
|
|
|
9 |
return api_token
|
10 |
|
11 |
API_TOKEN = get_api_token()
|
12 |
+
|
13 |
+
# Use StarCoder or CodeLlama for better code translation
|
14 |
+
MODEL_ID = "bigcode/starcoder" # Alternative: "codellama/CodeLlama-7b-hf"
|
15 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
16 |
HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
|
17 |
|
|
|
19 |
"""Translate code using the Hugging Face API."""
|
20 |
prompt = (
|
21 |
f"### Task: Convert {source_lang} code to {target_lang} code.\n\n"
|
22 |
+
f"### {source_lang} Code:\n```{source_lang.lower()}\n{code_snippet}\n```\n\n"
|
23 |
+
f"### {target_lang} Code:\n```{target_lang.lower()}\n"
|
24 |
)
|
25 |
|
26 |
try:
|
27 |
response = requests.post(API_URL, headers=HEADERS, json={"inputs": prompt})
|
28 |
+
|
29 |
if response.status_code == 200:
|
30 |
result = response.json()
|
31 |
+
|
32 |
+
# Ensure we extract the generated code correctly
|
33 |
+
if isinstance(result, list) and result:
|
34 |
+
generated_text = result[0].get("generated_text", "")
|
35 |
+
|
36 |
+
# Extract translated code properly
|
37 |
+
translated_code = generated_text.split(f"### {target_lang} Code:")[-1].strip()
|
38 |
+
return translated_code if translated_code else "⚠️ No translated code received."
|
39 |
+
|
40 |
+
return "⚠️ Unexpected API response format."
|
41 |
+
|
42 |
+
elif response.status_code == 400:
|
43 |
+
return "⚠️ Error: Bad request. Check your input."
|
44 |
+
elif response.status_code == 401:
|
45 |
+
return "⚠️ Error: Unauthorized. Check your API token."
|
46 |
+
elif response.status_code == 403:
|
47 |
+
return "⚠️ Error: Access Forbidden. You may need special model access."
|
48 |
+
elif response.status_code == 503:
|
49 |
+
return "⚠️ Error: Model is loading. Please wait and try again."
|
50 |
else:
|
51 |
return f"⚠️ API Error {response.status_code}: {response.text}"
|
52 |
+
|
53 |
except requests.exceptions.RequestException as e:
|
54 |
return f"⚠️ Network Error: {str(e)}"
|
55 |
|