File size: 1,072 Bytes
ca106a2
1e498fc
4080d21
1e498fc
ca106a2
 
 
 
 
 
 
 
1e498fc
ca106a2
1e498fc
4080d21
1e498fc
 
 
ca106a2
1e498fc
575a5c5
 
1e498fc
 
 
5ea921d
4080d21
 
1e498fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import openai
import gradio as gr


def translate_code(api_key, input_text, source_lang, target_lang):
    prompt = f"Convert the following {source_lang} code to {target_lang} code:\n{input_text}"
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=100,
        api_key=api_key
    )
    
    translated_code = response.choices[0].text.strip()
    return translated_code

iface = gr.Interface(
    fn=translate_code,
    inputs=[
        gr.inputs.Textbox(label="Enter Your OPENAI API KEY"),
        gr.inputs.Textbox(label="Enter code to translate"),
        gr.inputs.Textbox(label="Source Language (e.g., C++,python,java...)"),
        gr.inputs.Textbox(label="Target Language (e.g., C++,python,java...)")
    ],
    outputs=gr.outputs.Textbox(label="Translated Code"),
    title="Code Translator",
    description="Translate code snippets between programming languages. Right now we are working on the saperate model for it, till then kindly use the openai api key for the refrence."
)

iface.launch()