R2Python / app.py
camillelacan1's picture
Update app.py
043af13
raw
history blame
1.06 kB
import gradio as gr
import openai
import os
# Set the API key for the OpenAI API
openai.api_key = "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"
# Set the model to use (e.g. "text-davinci-002")
model = "text-davinci-002"
def convert_code(code, language):
"""
Convert the given code to the specified language using GPT-3.
"""
# Set the prompt to use as input to the model
prompt = f"Convert {language} code to R 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"),
controls=gr.controls.Radio(["Python", "R"], label="Source language"),
outputs=gr.outputs.Textbox(label="Converted code"))
app.launch()