Anujgr8 commited on
Commit
ca106a2
1 Parent(s): 575a5c5
Files changed (1) hide show
  1. app.py +11 -28
app.py CHANGED
@@ -1,39 +1,23 @@
 
1
  import gradio as gr
2
- import requests
3
- import os
4
 
5
- API_URL = "https://api-inference.huggingface.co/models/openai-gpt"
6
- API_TOKEN = os.environ.get("API_TOKEN")
7
 
8
- headers = {"Authorization": f"Bearer {API_TOKEN}"}
9
-
10
- # Function to translate code using the Hugging Face model API
11
- # Function to translate code using the Hugging Face model API
12
- # Function to translate code using the Hugging Face model API
13
- def translate_code(input_text, source_lang, target_lang):
14
- payload = {
15
- "inputs": f"convert the below {source_lang} code to {target_lang} code: {input_text}"
16
- }
17
-
18
- response = requests.post(API_URL, headers=headers, json=payload)
19
- response_data = response.json() # Store the entire response for inspection
20
- print("API Response:", response_data) # Print the response for inspection
21
-
22
- # Extract the translated code from the response
23
- translated_code = "No translation available" # Default value
24
-
25
- if response_data:
26
- if isinstance(response_data, list) and len(response_data) > 0:
27
- translated_code = response_data[0].get("generated_text", "").strip()
28
 
 
29
  return translated_code
30
 
31
-
32
-
33
- # Interface for the Gradio app
34
  iface = gr.Interface(
35
  fn=translate_code,
36
  inputs=[
 
37
  gr.inputs.Textbox(label="Enter code to translate"),
38
  gr.inputs.Textbox(label="Source Language (e.g., C++,python,java...)"),
39
  gr.inputs.Textbox(label="Target Language (e.g., C++,python,java...)")
@@ -43,5 +27,4 @@ iface = gr.Interface(
43
  description="Translate code snippets between programming languages"
44
  )
45
 
46
- # Launch the Gradio app
47
  iface.launch()
 
1
+ import openai
2
  import gradio as gr
 
 
3
 
 
 
4
 
5
+ def translate_code(api_key, input_text, source_lang, target_lang):
6
+ prompt = f"Convert the following {source_lang} code to {target_lang} code:\n{input_text}"
7
+ response = openai.Completion.create(
8
+ engine="text-davinci-003",
9
+ prompt=prompt,
10
+ max_tokens=100,
11
+ api_key=api_key
12
+ )
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ translated_code = response.choices[0].text.strip()
15
  return translated_code
16
 
 
 
 
17
  iface = gr.Interface(
18
  fn=translate_code,
19
  inputs=[
20
+ gr.inputs.Textbox(label="Enter Your OPENAI API KEY"),
21
  gr.inputs.Textbox(label="Enter code to translate"),
22
  gr.inputs.Textbox(label="Source Language (e.g., C++,python,java...)"),
23
  gr.inputs.Textbox(label="Target Language (e.g., C++,python,java...)")
 
27
  description="Translate code snippets between programming languages"
28
  )
29
 
 
30
  iface.launch()