Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,20 +6,21 @@ import gradio as gr
|
|
| 6 |
openai.api_key = os.environ["OPENAI_API_KEY"]
|
| 7 |
|
| 8 |
def translate_code(code, from_language, to_language):
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
return f"Error: {str(e)}"
|
| 24 |
|
| 25 |
languages = [
|
|
@@ -41,7 +42,7 @@ iface = gr.Interface(
|
|
| 41 |
gr.inputs.Dropdown(choices=languages, label="From Language"),
|
| 42 |
gr.inputs.Dropdown(choices=languages, label="To Language"),
|
| 43 |
],
|
| 44 |
-
outputs=gr.outputs.
|
| 45 |
title="Code Translator",
|
| 46 |
description="Translate code between different programming languages using OpenAI.",
|
| 47 |
)
|
|
|
|
| 6 |
openai.api_key = os.environ["OPENAI_API_KEY"]
|
| 7 |
|
| 8 |
def translate_code(code, from_language, to_language):
|
| 9 |
+
try:
|
| 10 |
+
prompt = f"Translate the following {from_language} code to {to_language}:\n{code}\n---\nTranslated code:\n"
|
| 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
|
| 23 |
+
except Exception as e:
|
| 24 |
return f"Error: {str(e)}"
|
| 25 |
|
| 26 |
languages = [
|
|
|
|
| 42 |
gr.inputs.Dropdown(choices=languages, label="From Language"),
|
| 43 |
gr.inputs.Dropdown(choices=languages, label="To Language"),
|
| 44 |
],
|
| 45 |
+
outputs=gr.outputs.Textarea(label="Translated Code"),
|
| 46 |
title="Code Translator",
|
| 47 |
description="Translate code between different programming languages using OpenAI.",
|
| 48 |
)
|