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()