|
import gradio as gr |
|
import openai |
|
import matplotlib.pyplot as plt |
|
import io |
|
import numpy as np |
|
import base64 |
|
from PIL import Image |
|
|
|
|
|
def get_image_data(plt): |
|
buf = io.BytesIO() |
|
plt.savefig(buf, format='PNG') |
|
buf.seek(0) |
|
img = Image.open(buf) |
|
return img |
|
|
|
|
|
def execute_code(code): |
|
exec(code) |
|
return get_image_data(plt) |
|
|
|
openai.api_key = 'your-openai-key' |
|
|
|
def gpt3_inference(prompt): |
|
response = openai.Completion.create( |
|
engine="text-davinci-002", |
|
prompt=prompt, |
|
max_tokens=100 |
|
) |
|
code = response.choices[0].text.strip() |
|
img = execute_code(code) |
|
return img |
|
|
|
iface = gr.Interface(fn=gpt3_inference, inputs="text", outputs="image") |
|
iface.launch() |
|
|