Spaces:
Sleeping
Sleeping
File size: 6,285 Bytes
94a60fe 20260e5 ccfb540 7e03a89 ccfb540 20260e5 ccfb540 8255252 f808525 8255252 f808525 7654fbe f808525 8255252 f808525 7654fbe f808525 7654fbe f808525 8255252 f808525 8255252 f808525 de85f5b f808525 7654fbe f808525 8255252 f808525 8255252 f808525 8255252 f808525 8255252 f808525 7654fbe 8255252 7e03a89 8255252 961f5ea 7e03a89 3817cb8 8255252 7e03a89 fa74e6e 7e03a89 20260e5 ccfb540 20260e5 7e03a89 4499b20 43cfd12 9054811 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import os
import openai
import gradio as gr
import requests
import json
openai.api_key = os.environ["OPENAI_API_KEY"]
def translate_code(code, from_language, to_language):
if from_language == "JavaScript" and to_language == "Java":
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."
elif from_language == "Java" and to_language == "JavaScript":
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."
elif from_language == "C++" and to_language == "Java":
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."
elif from_language == "Java" and to_language == "C++":
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."
elif from_language == "C#" and to_language == "Java":
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."
elif from_language == "Java" and to_language == "C#":
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."
elif from_language == "Ruby" and to_language == "Java":
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."
elif from_language == "Java" and to_language == "Ruby":
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."
elif from_language == "Swift" and to_language == "Python":
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."
elif from_language == "Python" and to_language == "Swift":
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."
elif from_language == "Go" and to_language == "Java":
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."
elif from_language == "Java" and to_language == "Go":
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."
elif from_language == "PHP" and to_language == "Java":
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."
elif from_language == "Java" and to_language == "PHP":
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."
else:
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."
data = {
"model": "gpt-3.5-turbo",
"messages": [{"role": "system", "content": "You are a code translator assistant that translates code snippets between programming languages."}, {"role": "user", "content": context}],
"max_tokens": 3000,
"temperature": 0.6,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {openai.api_key}",
}
try:
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, data=json.dumps(data))
response.raise_for_status()
result = response.json()
return result["choices"][0]["message"]["content"]
except requests.exceptions.HTTPError as e:
return f"Error: {str(e)}\nResponse: {response.text}"
inputs = [
gr.inputs.Textbox(lines=10, placeholder="Enter your code here...", label="Code"),
gr.inputs.Dropdown(choices=["Python", "JavaScript", "Java", "C++", "C#", "Ruby", "Swift", "Go", "PHP"], label="From Language"),
gr.inputs.Dropdown(choices=["Python", "JavaScript", "Java", "C++", "C#", "Ruby", "Swift", "Go", "PHP"], label="To Language"),
]
outputs = gr.outputs.Textbox(label="Translated Code")
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()
|