Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,18 @@
|
|
1 |
-
import os
|
2 |
import openai
|
|
|
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 to {to_language}:\n{code}\n
|
11 |
-
|
12 |
data = {
|
13 |
"model": "gpt-3.5-turbo",
|
14 |
-
"messages": [{"role": "system", "content": "You are a
|
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", "
|
40 |
-
gr.inputs.Dropdown(choices=["Python", "JavaScript", "Java", "C++", "C#", "Ruby", "
|
41 |
]
|
42 |
|
43 |
-
outputs = gr.outputs.
|
44 |
|
45 |
-
gr.Interface(fn=translate_code, inputs=inputs, outputs=outputs, title="Code Translator", description="
|
|
|
|
|
1 |
import openai
|
2 |
+
import os
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
import json
|
6 |
|
7 |
+
openai.api_key = "your_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": 300,
|
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.Textarea(placeholder="Enter your code here...", label="Code"),
|
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.Textarea(label="Translated Code")
|
44 |
|
45 |
+
gr.Interface(fn=translate_code, inputs=inputs, outputs=outputs, title="Code Translator", description="Translate code between different programming languages. Note that the translations might not always be perfect and may require some manual adjustments.", theme="compact").launch()
|