brackozi commited on
Commit
ccfb540
·
1 Parent(s): af6e053

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+ import gradio as gr
4
+ from gradio.inputs import Dropdown, Snippet
5
+ from gradio.outputs import Snippet
6
+
7
+ # Read the OpenAI API key from the environment variable
8
+ openai.api_key = os.environ["OPENAI_API_KEY"]
9
+
10
+ def translate_code(code, from_language, to_language):
11
+ prompt = f"Translate the following {from_language} code to {to_language}:\n{code}\n---\nTranslated code:\n"
12
+
13
+ response = openai.Completion.create(
14
+ engine="davinci-codex",
15
+ prompt=prompt,
16
+ max_tokens=2000,
17
+ n=1,
18
+ stop=None,
19
+ temperature=0.5,
20
+ )
21
+
22
+ translated_code = response.choices[0].text.strip()
23
+ return translated_code
24
+
25
+ languages = [
26
+ "Python",
27
+ "Javascript",
28
+ "PHP",
29
+ "Java",
30
+ "Ruby",
31
+ "Golang",
32
+ "C#",
33
+ "SQL",
34
+ "Perl",
35
+ ]
36
+
37
+ iface = gr.Interface(
38
+ fn=translate_code,
39
+ inputs=[
40
+ Snippet(lines=20, label="Original Code"),
41
+ Dropdown(choices=languages, label="From Language"),
42
+ Dropdown(choices=languages, label="To Language"),
43
+ ],
44
+ outputs=Snippet(lines=20, label="Translated Code"),
45
+ title="Code Translator",
46
+ description="Translate code between different programming languages using OpenAI.",
47
+ )
48
+
49
+ iface.launch()