CoolFace
Apppublic

camillelacan1/R2Python

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py39 linesDownload Raw Back to root
1import gradio as gr2import openai3import os4 5# Set the API key for the OpenAI API6 7# Set the model to use (e.g. "text-davinci-002")8model = "text-davinci-002"9 10def convert_code(code, language_in, language_out):11    """12    Convert the given code to the specified language using GPT-3.13    """14    # Set the prompt to use as input to the model15    prompt = f""""Convert {language_in} code to {language_out} code:\n{code}"""16 17    # Call the GPT-3 API to generate text18    response = openai.Completion.create(19        engine=model,20        prompt=prompt,21        max_tokens=1024,22        temperature=0.5,23        top_p=1,24        frequency_penalty=0,25        presence_penalty=026    )27 28    # Return the generated text29    return response["choices"][0]["text"]30 31# Create the Gradio app32app = gr.Interface(33    fn=convert_code,34    inputs=[gr.inputs.Textbox(lines=10, label="Code to convert"), gr.Radio(["Python", "R"], label="Source language"), gr.Radio(["Python", "R"], label="Output language")],35    outputs=[gr.outputs.Textbox(label="Converted code")]36)37 38app.launch()39