Spaces:
Sleeping
Sleeping
File size: 2,197 Bytes
c4a4533 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import os
import gradio as gr
import base64
from openai import OpenAI, BadRequestError
OUT_FOL = "output"
def create_image(image_path, request: gr.Request):
err_msg = ""
save_image_path = None
prompt = """
ใใฎ็ปๅใใธใใช้ขจใซใใฆไธใใใ
"""
file_name = os.path.basename(image_path)
save_file_name = "Ghi_" + file_name
os.makedirs(OUT_FOL, exist_ok=True)
save_image_path = OUT_FOL + "/" + save_file_name
client = OpenAI()
try:
result = client.images.edit(
model="gpt-image-1",
image=open(image_path, "rb"),
prompt=prompt,
# quality="low",
size="1024x1024"
)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
with open(save_image_path, "wb") as f:
f.write(image_bytes)
except BadRequestError as e:
err_msg = "็ปๅใๅใไปใใใใพใใใงใใใๅฅใฎ็ปๅใง่ฉฆใใฆใใ ใใใ"
print(e)
except Exception as e:
err_msg = "็ปๅไฝๆใงใจใฉใผใ็บ็ใใพใใใๅฅใฎ็ปๅใง่ฉฆใใฆใใ ใใใ"
print(e)
return save_image_path, err_msg
with gr.Blocks(theme=gr.themes.Ocean()) as demo:
title = "<center><h2>ใธใใช้ขจใคใฉในใใกใผใซใผ</h2></center>"
message = "<center><h3><b>ไฝฟใๆนใฏ็ปๅใใขใใใใฆ็ปๅไฝๆใใฟใณใๆผใใ ใใงใใ<br>"
message += "โป1ๆฅใใจใซๅฉ็จๅถ้ใใใใพใใๅฉ็จใงใใชใๅ ดๅใฏ็ฟๆฅใ่ฉฆใใใ ใใใ"
message += "</b></h3></center>"
gr.Markdown(title)
gr.Markdown(message)
with gr.Row():
with gr.Column():
# ๅ
ฅๅ
input_image = gr.Image(label="ๅ
ฅๅ็ปๅ", type="filepath")
btn = gr.Button("็ปๅไฝๆ")
with gr.Column():
# ๅบๅ
out_image = gr.Image(label="็ๆ็ปๅ", type="filepath", interactive=False)
sys_msg = gr.Text(label="ใทในใใ ใกใใปใผใธ")
btn.click(create_image, input_image, [out_image, sys_msg])
if __name__ == '__main__':
demo.queue()
demo.launch() |