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