Spaces:
Sleeping
Sleeping
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() |