brackozi commited on
Commit
7654fbe
·
1 Parent(s): 20260e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -5
app.py CHANGED
@@ -7,11 +7,41 @@ import json
7
  openai.api_key = os.environ["OPENAI_API_KEY"]
8
 
9
  def translate_code(code, from_language, to_language):
10
- 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."
11
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  data = {
13
  "model": "gpt-3.5-turbo",
14
- "messages": [{"role": "system", "content": "You are a code translator assistant that translates code snippets between programming languages."}, {"role": "user", "content": context}],
15
  "max_tokens": 200,
16
  "temperature": 0.6,
17
  "top_p": 1,
@@ -25,7 +55,8 @@ def translate_code(code, from_language, to_language):
25
  }
26
 
27
  try:
28
- response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, data=json.dumps(data))
 
29
  response.raise_for_status()
30
  result = response.json()
31
 
@@ -42,4 +73,4 @@ inputs = [
42
 
43
  outputs = gr.outputs.Textbox(label="Translated Code")
44
 
45
- 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
+ # 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
  }
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
 
 
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()