Ghib_Maker / app.py
nekoniii3's picture
new
c4a4533
raw
history blame
2.2 kB
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()