brackozi commited on
Commit
94a60fe
·
1 Parent(s): 7e03a89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -11
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 = os.environ["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---\nTranslated code:\n"
11
-
12
  data = {
13
  "model": "gpt-3.5-turbo",
14
- "messages": [{"role": "system", "content": "You are a helpful assistant that translates programming languages."}, {"role": "user", "content": context}],
15
- "max_tokens": 200,
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.Textbox(lines=100, 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()
 
 
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()