dlimeng commited on
Commit
551db13
·
1 Parent(s): 9012852

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -8
app.py CHANGED
@@ -6,7 +6,7 @@ import numpy as np
6
  import base64
7
  from PIL import Image
8
 
9
- # 将图像转换为 base64,以便在 gradio 中显示
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
- # 执行 Python 代码并生成图像
18
  def execute_code(code):
19
  exec(code)
20
  return get_image_data(plt)
21
 
22
- openai.api_key = 'sk-tDHORTwNMfpIJGeeHNRpFx2CaiEKeqsoJH2cfncNyl67DWoU'
23
-
24
- def gpt3_inference(prompt):
25
  response = openai.Completion.create(
26
- engine="gpt-3.5-turbo",
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(fn=gpt3_inference, inputs="text", outputs="image")
35
- iface.launch()
 
 
 
 
 
 
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()