Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,62 +4,58 @@ import os
|
|
| 4 |
|
| 5 |
# Get API token from environment variable
|
| 6 |
API_TOKEN = os.getenv("HF_API_TOKEN") # Ensure you set this in your environment
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 with improved prompt."""
|
| 13 |
-
prompt = f"""
|
| 14 |
-
|
| 15 |
-
Example Translation:
|
| 16 |
-
Python:
|
| 17 |
-
```python
|
| 18 |
-
name = input("Enter your name: ")
|
| 19 |
-
print("Hello, " + name)
|
| 20 |
-
```
|
| 21 |
-
|
| 22 |
-
Java:
|
| 23 |
-
```java
|
| 24 |
-
import java.util.Scanner;
|
| 25 |
-
public class Main {{
|
| 26 |
-
public static void main(String[] args) {{
|
| 27 |
-
Scanner scanner = new Scanner(System.in);
|
| 28 |
-
System.out.print("Enter your name: ");
|
| 29 |
-
String name = scanner.nextLine();
|
| 30 |
-
System.out.println("Hello, " + name);
|
| 31 |
-
}}
|
| 32 |
-
}}
|
| 33 |
-
```
|
| 34 |
-
|
| 35 |
-
Now translate the following {source_lang} code to {target_lang}:
|
| 36 |
|
| 37 |
{code_snippet}
|
| 38 |
-
|
| 39 |
Translated {target_lang} Code:
|
| 40 |
"""
|
| 41 |
|
| 42 |
response = requests.post(API_URL, headers=HEADERS, json={
|
| 43 |
-
"inputs": prompt
|
| 44 |
-
"parameters": {
|
| 45 |
-
"max_new_tokens": 200,
|
| 46 |
-
"temperature": 0.2,
|
| 47 |
-
"top_k": 50,
|
| 48 |
-
"stop": ["\n\n", "#", "//", "'''"]
|
| 49 |
-
}
|
| 50 |
})
|
| 51 |
|
| 52 |
if response.status_code == 200:
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
else:
|
| 62 |
-
return f"Error
|
| 63 |
|
| 64 |
# Streamlit UI
|
| 65 |
st.title("🔄 Code Translator using StarCoder")
|
|
|
|
| 4 |
|
| 5 |
# Get API token from environment variable
|
| 6 |
API_TOKEN = os.getenv("HF_API_TOKEN") # Ensure you set this in your environment
|
| 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 with improved prompt."""
|
| 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 |
+
response_json = response.json()
|
| 31 |
+
|
| 32 |
+
if isinstance(response_json, list) and len(response_json) > 0:
|
| 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"⚠️ Error {response.status_code}: {response.text}"
|
| 59 |
|
| 60 |
# Streamlit UI
|
| 61 |
st.title("🔄 Code Translator using StarCoder")
|