Spaces:
Sleeping
Sleeping
| 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() | |