Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,16 +7,19 @@ openai.api_key = os.environ["OPENAI_API_KEY"]
|
|
7 |
|
8 |
def translate_code(code, from_language, to_language):
|
9 |
try:
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
response = openai.Completion.create(
|
13 |
-
engine="davinci-codex",
|
14 |
-
prompt=prompt,
|
15 |
-
max_tokens=200,
|
16 |
-
n=1,
|
17 |
-
stop=None,
|
18 |
-
temperature=0.5,
|
19 |
-
)
|
20 |
|
21 |
translated_code = response.choices[0].text.strip()
|
22 |
return translated_code
|
@@ -44,7 +47,7 @@ iface = gr.Interface(
|
|
44 |
],
|
45 |
outputs=gr.outputs.Textbox(label="Translated Code"),
|
46 |
title="Code Translator",
|
47 |
-
description="Translate code between different programming languages using OpenAI.",
|
48 |
)
|
49 |
|
50 |
iface.launch()
|
|
|
7 |
|
8 |
def translate_code(code, from_language, to_language):
|
9 |
try:
|
10 |
+
context = f"Translate the following {from_language} code to {to_language}:\n{code}\n---\nTranslated code:\n"
|
11 |
+
|
12 |
+
data = {
|
13 |
+
"model": "gpt-3.5-turbo",
|
14 |
+
"messages": [{"role": "system", "content": "You are a helpful assistant that translates code between different programming languages."}, {"role": "user", "content": context}],
|
15 |
+
"max_tokens": 200,
|
16 |
+
"temperature": 0.6,
|
17 |
+
"top_p": 1,
|
18 |
+
"frequency_penalty": 0,
|
19 |
+
"presence_penalty": 0,
|
20 |
+
}
|
21 |
|
22 |
+
response = openai.Completion.create(**data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
translated_code = response.choices[0].text.strip()
|
25 |
return translated_code
|
|
|
47 |
],
|
48 |
outputs=gr.outputs.Textbox(label="Translated Code"),
|
49 |
title="Code Translator",
|
50 |
+
description="Translate code between different programming languages using OpenAI GPT-3.5 Turbo.",
|
51 |
)
|
52 |
|
53 |
iface.launch()
|