Spaces:
Sleeping
Sleeping
Update translator.py
Browse files- translator.py +13 -51
translator.py
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 1 |
import requests
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
API_TOKEN = os.getenv("HF_API_TOKEN") # Get token from environment
|
| 6 |
|
| 7 |
-
|
| 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"
|
|
@@ -14,58 +12,22 @@ HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
|
|
| 14 |
|
| 15 |
def translate_code(code_snippet, source_lang, target_lang):
|
| 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."
|
| 48 |
|
| 49 |
-
|
| 50 |
-
return "⚠️ Error: Unauthorized. Check your API token."
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
return "⚠️ Error: Model is loading. Please wait and try again."
|
| 57 |
-
|
| 58 |
-
else:
|
| 59 |
-
return f"⚠️ Error {response.status_code}: {response.text}"
|
| 60 |
-
|
| 61 |
-
except requests.exceptions.RequestException as e:
|
| 62 |
-
return f"⚠️ Network Error: {str(e)}"
|
| 63 |
|
| 64 |
# Example usage
|
| 65 |
-
|
| 66 |
-
source_code = """
|
| 67 |
def add(a, b):
|
| 68 |
return a + b
|
| 69 |
"""
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
import os
|
| 3 |
|
| 4 |
+
# Your Hugging Face API token (Replace 'your_token_here' with your actual token)
|
|
|
|
| 5 |
|
| 6 |
+
API_TOKEN = os.getenv("HF_API_TOKEN")
|
|
|
|
| 7 |
|
| 8 |
# Define model and API endpoint
|
| 9 |
MODEL_ID = "bigcode/starcoder"
|
|
|
|
| 12 |
|
| 13 |
def translate_code(code_snippet, source_lang, target_lang):
|
| 14 |
"""
|
| 15 |
+
Translate code using Hugging Face API (No local download needed).
|
| 16 |
"""
|
| 17 |
+
prompt = f"Translate the following {source_lang} code to {target_lang}:\n\n{code_snippet}\n\nTranslated {target_lang} Code:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
response = requests.post(API_URL, headers=HEADERS, json={"inputs": prompt})
|
|
|
|
| 20 |
|
| 21 |
+
if response.status_code == 200:
|
| 22 |
+
return response.json()[0]["generated_text"]
|
| 23 |
+
else:
|
| 24 |
+
return f"Error: {response.status_code}, {response.text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Example usage
|
| 27 |
+
source_code = """
|
|
|
|
| 28 |
def add(a, b):
|
| 29 |
return a + b
|
| 30 |
"""
|
| 31 |
+
translated_code = translate_code(source_code, "Python", "Java")
|
| 32 |
+
print("Translated Java Code:\n", translated_code)
|
| 33 |
+
|