Spaces:
Sleeping
Sleeping
Update translator.py
Browse files- translator.py +16 -47
translator.py
CHANGED
|
@@ -1,63 +1,32 @@
|
|
| 1 |
import requests
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
|
| 5 |
-
"""Fetch API token from environment variable."""
|
| 6 |
-
api_token = os.getenv("HF_API_TOKEN")
|
| 7 |
-
if not api_token:
|
| 8 |
-
raise ValueError("⚠️ API Token is missing! Please set HF_API_TOKEN as an environment variable.")
|
| 9 |
-
return api_token
|
| 10 |
|
| 11 |
-
API_TOKEN =
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
MODEL_ID = "bigcode/starcoder"
|
| 15 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
| 16 |
HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 17 |
|
| 18 |
def translate_code(code_snippet, source_lang, target_lang):
|
| 19 |
-
"""
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
f"### {target_lang} Code:\n```{target_lang.lower()}\n"
|
| 24 |
-
)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
response = requests.post(API_URL, headers=HEADERS, json={"inputs": prompt})
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 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 |
|
| 56 |
# Example usage
|
| 57 |
-
|
| 58 |
-
source_code = """
|
| 59 |
def add(a, b):
|
| 60 |
return a + b
|
| 61 |
"""
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 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"
|
| 10 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
| 11 |
HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 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)
|