Spaces:
Sleeping
Sleeping
Update translator.py
Browse files- translator.py +21 -8
translator.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
import requests
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 6 |
|
| 7 |
if not API_TOKEN:
|
| 8 |
-
raise ValueError("⚠️ Error: Hugging Face API token is missing! Set
|
| 9 |
|
| 10 |
# Define model and API endpoint
|
| 11 |
MODEL_ID = "bigcode/starcoder"
|
|
@@ -16,19 +16,32 @@ def translate_code(code_snippet, source_lang, target_lang):
|
|
| 16 |
"""
|
| 17 |
Translate code using Hugging Face API.
|
| 18 |
"""
|
| 19 |
-
prompt = f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
try:
|
| 22 |
response = requests.post(API_URL, headers=HEADERS, json={"inputs": prompt})
|
| 23 |
|
|
|
|
| 24 |
if response.status_code == 200:
|
| 25 |
response_json = response.json()
|
|
|
|
|
|
|
| 26 |
if isinstance(response_json, list) and len(response_json) > 0:
|
| 27 |
generated_text = response_json[0].get("generated_text", "")
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
else:
|
| 31 |
-
return "⚠️ Error: Unexpected response format
|
| 32 |
|
| 33 |
elif response.status_code == 400:
|
| 34 |
return "⚠️ Error: Invalid request. Check input format."
|
|
@@ -40,7 +53,7 @@ def translate_code(code_snippet, source_lang, target_lang):
|
|
| 40 |
return "⚠️ Error: Access forbidden. You may need special access to this model."
|
| 41 |
|
| 42 |
elif response.status_code == 503:
|
| 43 |
-
return "⚠️ Error: Model is loading. Please wait
|
| 44 |
|
| 45 |
else:
|
| 46 |
return f"⚠️ Error {response.status_code}: {response.text}"
|
|
|
|
| 1 |
import requests
|
| 2 |
import os
|
| 3 |
|
| 4 |
+
# Set Hugging Face API Token
|
| 5 |
+
API_TOKEN = os.getenv("HF_API_TOKEN") # Get token from environment
|
| 6 |
|
| 7 |
if not API_TOKEN:
|
| 8 |
+
raise ValueError("⚠️ Error: Hugging Face API token is missing! Set HF_API_TOKEN.")
|
| 9 |
|
| 10 |
# Define model and API endpoint
|
| 11 |
MODEL_ID = "bigcode/starcoder"
|
|
|
|
| 16 |
"""
|
| 17 |
Translate code using Hugging Face API.
|
| 18 |
"""
|
| 19 |
+
prompt = f"""
|
| 20 |
+
Translate the following {source_lang} code to {target_lang}:
|
| 21 |
+
|
| 22 |
+
{code_snippet}
|
| 23 |
+
|
| 24 |
+
Translated {target_lang} Code:
|
| 25 |
+
"""
|
| 26 |
|
| 27 |
try:
|
| 28 |
response = requests.post(API_URL, headers=HEADERS, json={"inputs": prompt})
|
| 29 |
|
| 30 |
+
# Check response status
|
| 31 |
if response.status_code == 200:
|
| 32 |
response_json = response.json()
|
| 33 |
+
|
| 34 |
+
# Ensure proper response format
|
| 35 |
if isinstance(response_json, list) and len(response_json) > 0:
|
| 36 |
generated_text = response_json[0].get("generated_text", "")
|
| 37 |
+
if generated_text:
|
| 38 |
+
# Extract translated code
|
| 39 |
+
translated_code = generated_text.split(f"Translated {target_lang} Code:")[-1].strip()
|
| 40 |
+
return translated_code
|
| 41 |
+
else:
|
| 42 |
+
return "⚠️ Error: No translated text received."
|
| 43 |
else:
|
| 44 |
+
return "⚠️ Error: Unexpected response format."
|
| 45 |
|
| 46 |
elif response.status_code == 400:
|
| 47 |
return "⚠️ Error: Invalid request. Check input format."
|
|
|
|
| 53 |
return "⚠️ Error: Access forbidden. You may need special access to this model."
|
| 54 |
|
| 55 |
elif response.status_code == 503:
|
| 56 |
+
return "⚠️ Error: Model is loading. Please wait and try again."
|
| 57 |
|
| 58 |
else:
|
| 59 |
return f"⚠️ Error {response.status_code}: {response.text}"
|