Spaces:
Sleeping
Sleeping
Duplicate from brackozi/CodeTranslator
Browse filesCo-authored-by: Ziga Bracko <brackozi@users.noreply.huggingface.co>
- .gitattributes +34 -0
- README.md +13 -0
- app.py +75 -0
- requirements.txt +2 -0
.gitattributes
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: CodeTranslator
|
| 3 |
+
emoji: 🐨
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.24.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
duplicated_from: brackozi/CodeTranslator
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import requests
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
openai.api_key = os.environ["OPENAI_API_KEY"]
|
| 8 |
+
|
| 9 |
+
def translate_code(code, from_language, to_language):
|
| 10 |
+
if from_language == "JavaScript" and to_language == "Java":
|
| 11 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation follows Java syntax and idioms, considering language-specific best practices. add required import statements."
|
| 12 |
+
elif from_language == "Java" and to_language == "JavaScript":
|
| 13 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation follows JavaScript syntax and idioms, considering language-specific best practices. add required modules."
|
| 14 |
+
elif from_language == "C++" and to_language == "Java":
|
| 15 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation follows Java syntax and idioms, considering language-specific best practices. add required import statements."
|
| 16 |
+
elif from_language == "Java" and to_language == "C++":
|
| 17 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation follows C++ syntax and idioms, considering language-specific best practices. Include required headers."
|
| 18 |
+
elif from_language == "C#" and to_language == "Java":
|
| 19 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation follows Java syntax and idioms, considering language-specific best practices. add required import statements."
|
| 20 |
+
elif from_language == "Java" and to_language == "C#":
|
| 21 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation follows C# syntax and idioms, considering language-specific best practices. add required using statements."
|
| 22 |
+
elif from_language == "Ruby" and to_language == "Java":
|
| 23 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation follows Java syntax and idioms, considering language-specific best practices. add required import statements."
|
| 24 |
+
elif from_language == "Java" and to_language == "Ruby":
|
| 25 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation follows Ruby syntax and idioms, considering language-specific best practices. add required gems/libraries."
|
| 26 |
+
elif from_language == "Swift" and to_language == "Python":
|
| 27 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation follows Python syntax and idioms, considering language-specific best practices. add required modules."
|
| 28 |
+
elif from_language == "Python" and to_language == "Swift":
|
| 29 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation follows Swift syntax and idioms, considering language-specific best practices. add required frameworks."
|
| 30 |
+
elif from_language == "Go" and to_language == "Java":
|
| 31 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation follows Java syntax and idioms, considering language-specific best practices. add required import statements."
|
| 32 |
+
elif from_language == "Java" and to_language == "Go":
|
| 33 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation follows Go syntax and idioms, considering language-specific best practices. add required packages."
|
| 34 |
+
elif from_language == "PHP" and to_language == "Java":
|
| 35 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation follows Java syntax and idioms, considering language-specific best practices. add required import statements."
|
| 36 |
+
elif from_language == "Java" and to_language == "PHP":
|
| 37 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation follows PHP syntax and idioms, considering language-specific best practices. add required packages/libraries."
|
| 38 |
+
else:
|
| 39 |
+
context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nKeep the translation accurate and idiomatic, considering language-specific best practices."
|
| 40 |
+
|
| 41 |
+
data = {
|
| 42 |
+
"model": "gpt-3.5-turbo",
|
| 43 |
+
"messages": [{"role": "system", "content": "You are a code translator assistant that translates code snippets between programming languages."}, {"role": "user", "content": context}],
|
| 44 |
+
"max_tokens": 3000,
|
| 45 |
+
"temperature": 0.6,
|
| 46 |
+
"top_p": 1,
|
| 47 |
+
"frequency_penalty": 0,
|
| 48 |
+
"presence_penalty": 0,
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
headers = {
|
| 52 |
+
"Content-Type": "application/json",
|
| 53 |
+
"Authorization": f"Bearer {openai.api_key}",
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, data=json.dumps(data))
|
| 58 |
+
response.raise_for_status()
|
| 59 |
+
result = response.json()
|
| 60 |
+
|
| 61 |
+
return result["choices"][0]["message"]["content"]
|
| 62 |
+
except requests.exceptions.HTTPError as e:
|
| 63 |
+
return f"Error: {str(e)}\nResponse: {response.text}"
|
| 64 |
+
|
| 65 |
+
inputs = [
|
| 66 |
+
gr.inputs.Textbox(lines=10, placeholder="Enter your code here...", label="Code"),
|
| 67 |
+
gr.inputs.Dropdown(choices=["Python", "JavaScript", "Java", "C++", "C#", "Ruby", "Swift", "Go", "PHP"], label="From Language"),
|
| 68 |
+
gr.inputs.Dropdown(choices=["Python", "JavaScript", "Java", "C++", "C#", "Ruby", "Swift", "Go", "PHP"], label="To Language"),
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
+
outputs = gr.outputs.Textbox(label="Translated Code")
|
| 72 |
+
|
| 73 |
+
gr.Interface(fn=translate_code, inputs=inputs, outputs=outputs, title="Code Translator", description="Enter your code, select the source language, and the language you want to translate it to. The AI will try to provide the translated code, Please note that due to the high value of the 'max tokens' parameter, the generation of results may take approximately 30 to 90 seconds.", theme="compact").launch()
|
| 74 |
+
|
| 75 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
openai
|