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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -43
app.py CHANGED
@@ -1,53 +1,45 @@
1
  import os
2
  import openai
3
  import gradio as gr
 
 
4
 
5
- # Read the OpenAI API key from the environment variable
6
  openai.api_key = os.environ["OPENAI_API_KEY"]
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.ChatCompletion.create(**data)
23
-
24
- translated_code = response.choices[0].text.strip()
25
- return translated_code
26
- except Exception as e:
27
- return f"Error: {str(e)}"
28
-
29
- languages = [
30
- "Python",
31
- "Javascript",
32
- "PHP",
33
- "Java",
34
- "Ruby",
35
- "Golang",
36
- "C#",
37
- "SQL",
38
- "Perl",
39
  ]
40
 
41
- iface = gr.Interface(
42
- fn=translate_code,
43
- inputs=[
44
- gr.inputs.Textbox(lines=20, label="Original Code"),
45
- gr.inputs.Dropdown(choices=languages, label="From Language"),
46
- gr.inputs.Dropdown(choices=languages, label="To Language"),
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()
 
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,
19
+ "presence_penalty": 0,
20
+ }
21
+
22
+ headers = {
23
+ "Content-Type": "application/json",
24
+ "Authorization": f"Bearer {openai.api_key}",
25
+ }
26
+
27
  try:
28
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, data=json.dumps(data))
29
+ response.raise_for_status()
30
+ result = response.json()
31
+
32
+ return result["choices"][0]["message"]["content"].strip()
33
+
34
+ except requests.exceptions.HTTPError as e:
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()