Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,18 @@
|
|
1 |
-
import openai
|
2 |
import os
|
|
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
import json
|
6 |
|
7 |
-
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":
|
15 |
-
"max_tokens":
|
16 |
"temperature": 0.6,
|
17 |
"top_p": 1,
|
18 |
"frequency_penalty": 0,
|
@@ -35,11 +35,11 @@ def translate_code(code, from_language, to_language):
|
|
35 |
return f"Error: {str(e)}\nResponse: {response.text}"
|
36 |
|
37 |
inputs = [
|
38 |
-
gr.inputs.
|
39 |
-
gr.inputs.Dropdown(choices=["Python", "JavaScript", "Java", "C++", "C#", "Ruby", "Go", "PHP"], label="From Language"),
|
40 |
-
gr.inputs.Dropdown(choices=["Python", "JavaScript", "Java", "C++", "C#", "Ruby", "Go", "PHP"], label="To Language"),
|
41 |
]
|
42 |
|
43 |
-
outputs = gr.outputs.
|
44 |
|
45 |
-
gr.Interface(fn=translate_code, inputs=inputs, outputs=outputs, title="Code Translator", description="
|
|
|
|
|
1 |
import os
|
2 |
+
import openai
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
import json
|
6 |
|
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,
|
18 |
"frequency_penalty": 0,
|
|
|
35 |
return f"Error: {str(e)}\nResponse: {response.text}"
|
36 |
|
37 |
inputs = [
|
38 |
+
gr.inputs.Textbox(lines=10, placeholder="Enter your code here...", label="Code"),
|
39 |
+
gr.inputs.Dropdown(choices=["Python", "JavaScript", "Java", "C++", "C#", "Ruby", "Swift", "Go", "PHP"], label="From Language"),
|
40 |
+
gr.inputs.Dropdown(choices=["Python", "JavaScript", "Java", "C++", "C#", "Ruby", "Swift", "Go", "PHP"], label="To Language"),
|
41 |
]
|
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()
|