File size: 963 Bytes
e27f3f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
import gradio as gr
import openai
import os

# Set the API key for the OpenAI API
openai.api_key = os.environ["OPENAI_SECRET_KEY"]

# Set the model to use (e.g. "text-davinci-002")
model = "text-davinci-002"

def convert_code(code, from_lang, to_lang):
    """
    Convert the given code from the specified source language to the
    specified target language using GPT-3.
    """
    # Set the prompt to use as input to the model
    prompt = f"Convert {from_lang} code to {to_lang} code:\n{code}"

    # Call the GPT-3 API to generate text
    response = openai.Completion.create(
        engine=model,
        prompt=prompt,
        max_tokens=1024,
        temperature=0.5,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
    )

    # Return the generated text
    return response["choices"][0]["text"]

# Create the Gradio app
app = gr.Interface(
    fn=convert_code,
    inputs=gr.inputs.Textbox(lines=10, label="Code to convert"),