brackozi commited on
Commit
8255252
·
1 Parent(s): de85f5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -30
app.py CHANGED
@@ -7,41 +7,40 @@ import json
7
  openai.api_key = os.environ["OPENAI_API_KEY"]
8
 
9
  def translate_code(code, from_language, to_language):
10
- # Define a message for each language pair to ensure the output can compile
11
- if from_language == "Python" and to_language == "JavaScript":
12
- 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. You may also need to add required imports/modules."
13
- elif from_language == "JavaScript" and to_language == "Python":
14
- 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. You may also need to add required imports/modules."
15
- elif from_language == "Java" and to_language == "C++":
16
- 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. You may also need to add required includes/libraries."
17
  elif from_language == "C++" and to_language == "Java":
18
- 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. You may also need to add required imports/packages."
 
 
19
  elif from_language == "C#" and to_language == "Java":
20
- 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. You may also need to add required imports/packages."
21
  elif from_language == "Java" and to_language == "C#":
22
  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. You may also need to add required using statements."
23
- elif from_language == "Ruby" and to_language == "Python":
24
- 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. You may also need to add required imports/modules."
25
- elif from_language == "Python" and to_language == "Ruby":
26
  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. You may also need to add required gems/libraries."
27
  elif from_language == "Swift" and to_language == "Python":
28
- 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. You may also need to add required imports/modules."
29
  elif from_language == "Python" and to_language == "Swift":
30
- 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. You may also need to add required imports/frameworks."
31
- elif from_language == "Go" and to_language == "Python":
32
- 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. You may also need to add required imports/modules."
33
- elif from_language == "Python" and to_language == "Go":
34
- 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. You may also need to add required imports/packages."
35
- elif from_language == "PHP" and to_language == "Python":
36
- 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. You may also need to add required imports/modules."
37
- elif from_language == "Python" and to_language == "PHP":
38
- 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. You may also need to add required includes/libraries."
39
  else:
40
- context = f"Translate the following {from_language} code snippet to {to_language}:\n\n{code}\n\nEnsure that the translation is accurate and idiomatic, considering language-specific best practices."
41
-
42
  data = {
43
  "model": "gpt-3.5-turbo",
44
- "messages": [{"role": "system", "content": "You are a code translator assistant that translates code snippets between programming languages."}, {"role": "user", "content": context}],
45
  "max_tokens": 200,
46
  "temperature": 0.6,
47
  "top_p": 1,
@@ -55,13 +54,12 @@ def translate_code(code, from_language, to_language):
55
  }
56
 
57
  try:
58
- response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, data=json.dumps(data
59
- ))
60
  response.raise_for_status()
61
  result = response.json()
62
 
63
- return result["choices"][0]["message"]["content"].strip()
64
-
65
  except requests.exceptions.HTTPError as e:
66
  return f"Error: {str(e)}\nResponse: {response.text}"
67
 
@@ -73,4 +71,4 @@ inputs = [
73
 
74
  outputs = gr.outputs.Textbox(label="Translated Code")
75
 
76
- 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.", theme="compact").launch()
 
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. You may also need to 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. You may also need to 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. You may also need to 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. You may also need to 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. You may also need to 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. You may also need to 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. You may also need to 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. You may also need to 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. You may also need to 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. You may also need to 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. You may also need to 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. You may also need to 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. You may also need to 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. You may also need to 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": 200,
45
  "temperature": 0.6,
46
  "top_p": 1,
 
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
+ strip()
63
  except requests.exceptions.HTTPError as e:
64
  return f"Error: {str(e)}\nResponse: {response.text}"
65
 
 
71
 
72
  outputs = gr.outputs.Textbox(label="Translated Code")
73
 
74
+ 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, along with specific instructions for each language pair to ensure that the code can be compiled without issues.", theme="compact").launch()