Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import numpy as np
|
|
| 6 |
import base64
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
-
#
|
| 10 |
def get_image_data(plt):
|
| 11 |
buf = io.BytesIO()
|
| 12 |
plt.savefig(buf, format='PNG')
|
|
@@ -14,16 +14,15 @@ def get_image_data(plt):
|
|
| 14 |
img = Image.open(buf)
|
| 15 |
return img
|
| 16 |
|
| 17 |
-
#
|
| 18 |
def execute_code(code):
|
| 19 |
exec(code)
|
| 20 |
return get_image_data(plt)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def gpt3_inference(prompt):
|
| 25 |
response = openai.Completion.create(
|
| 26 |
-
engine=
|
| 27 |
prompt=prompt,
|
| 28 |
max_tokens=100
|
| 29 |
)
|
|
@@ -31,5 +30,10 @@ def gpt3_inference(prompt):
|
|
| 31 |
img = execute_code(code)
|
| 32 |
return img
|
| 33 |
|
| 34 |
-
iface = gr.Interface(
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import base64
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
+
# Convert image to base64 for display in gradio
|
| 10 |
def get_image_data(plt):
|
| 11 |
buf = io.BytesIO()
|
| 12 |
plt.savefig(buf, format='PNG')
|
|
|
|
| 14 |
img = Image.open(buf)
|
| 15 |
return img
|
| 16 |
|
| 17 |
+
# Execute Python code and generate images
|
| 18 |
def execute_code(code):
|
| 19 |
exec(code)
|
| 20 |
return get_image_data(plt)
|
| 21 |
|
| 22 |
+
def gpt_inference(prompt, model, openai_key):
|
| 23 |
+
openai.api_key = openai_key
|
|
|
|
| 24 |
response = openai.Completion.create(
|
| 25 |
+
engine=model,
|
| 26 |
prompt=prompt,
|
| 27 |
max_tokens=100
|
| 28 |
)
|
|
|
|
| 30 |
img = execute_code(code)
|
| 31 |
return img
|
| 32 |
|
| 33 |
+
iface = gr.Interface(
|
| 34 |
+
fn=gpt_inference,
|
| 35 |
+
inputs=["text", gr.inputs.Dropdown(choices=["gpt3.5-turbo", "gpt4"], label="Model"), "text"],
|
| 36 |
+
outputs=gr.outputs.Image(type="pil"),
|
| 37 |
+
input_labels=["Prompt", "Model", "OpenAI Key"]
|
| 38 |
+
)
|
| 39 |
+
iface.launch()
|