Spaces:
Runtime error
Runtime error
update
Browse files- app.py +64 -50
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -15,7 +15,8 @@ from openai import (
|
|
| 15 |
|
| 16 |
|
| 17 |
# GPT用設定
|
| 18 |
-
|
|
|
|
| 19 |
DUMMY = "********************"
|
| 20 |
file_format = {".png", ".jpeg", ".jpg", ".webp", ".gif", ".PNG", ".JPEG", ".JPG", ".WEBP", ".GIF"}
|
| 21 |
|
|
@@ -40,6 +41,7 @@ pt = r".*\[(.*)\]\((.*)\)"
|
|
| 40 |
# サンプル用情報
|
| 41 |
examples = ["1980s anime girl with straight bob-cut in school uniform, roughly drawn drawing"
|
| 42 |
, "a minimalisit logo for a sporting goods company"]
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
# 各関数定義
|
|
@@ -60,17 +62,17 @@ def init(state, text, image):
|
|
| 60 |
|
| 61 |
err_msg = ""
|
| 62 |
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
# err_msg = "OpenAI API Keyを入力してください。(設定タブ)"
|
| 69 |
|
| 70 |
if not text:
|
| 71 |
|
| 72 |
# テキスト未入力
|
| 73 |
-
err_msg = "
|
| 74 |
|
| 75 |
return state, err_msg
|
| 76 |
|
|
@@ -79,8 +81,6 @@ def init(state, text, image):
|
|
| 79 |
# 入力画像のファイル形式チェック
|
| 80 |
root, ext = os.path.splitext(image)
|
| 81 |
|
| 82 |
-
print(ext, file_format)
|
| 83 |
-
|
| 84 |
if ext not in file_format:
|
| 85 |
|
| 86 |
# ファイル形式チェック
|
|
@@ -93,8 +93,8 @@ def init(state, text, image):
|
|
| 93 |
if state["client"] is None:
|
| 94 |
|
| 95 |
# 初回起動時は初期処理をする
|
| 96 |
-
os.environ["OPENAI_API_KEY"] = os.environ["TEST_OPENAI_KEY"] # テスト時
|
| 97 |
-
|
| 98 |
|
| 99 |
# クライアント新規作成
|
| 100 |
client = OpenAI()
|
|
@@ -126,27 +126,27 @@ def init(state, text, image):
|
|
| 126 |
if state["assistant_id"] == "":
|
| 127 |
|
| 128 |
# アシスタント作成
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
# )
|
| 136 |
-
# state["assistant_id"] = assistant.id
|
| 137 |
|
| 138 |
-
|
|
|
|
|
|
|
| 139 |
|
| 140 |
-
else:
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
| 144 |
|
| 145 |
# ユーザIDでフォルダ作成
|
| 146 |
os.makedirs(state["user_id"], exist_ok=True)
|
| 147 |
|
| 148 |
-
except NotFoundError as e:
|
| 149 |
-
|
| 150 |
except AuthenticationError as e:
|
| 151 |
err_msg = "認証エラーとなりました。OpenAPIKeyが正しいか、支払い方法などが設定されているか確認して下さい。"
|
| 152 |
except Exception as e:
|
|
@@ -234,18 +234,11 @@ def bot(state, history, image_path):
|
|
| 234 |
run_id=run.id
|
| 235 |
)
|
| 236 |
|
| 237 |
-
print(run.status)
|
| 238 |
-
|
| 239 |
if run.status == "requires_action": # 関数の結果の待ちの場合
|
| 240 |
|
| 241 |
-
print(run.required_action)
|
| 242 |
-
|
| 243 |
# tool_callsの各項目取得
|
| 244 |
tool_calls = run.required_action.submit_tool_outputs.tool_calls
|
| 245 |
|
| 246 |
-
print(len(tool_calls))
|
| 247 |
-
print(tool_calls)
|
| 248 |
-
|
| 249 |
# 一つ目だけ取得
|
| 250 |
tool_id = tool_calls[0].id
|
| 251 |
func_name = tool_calls[0].function.name
|
|
@@ -290,8 +283,6 @@ def bot(state, history, image_path):
|
|
| 290 |
# 10枚以上生成した場合終了とする
|
| 291 |
func_output = '{"answer" : "", "error_message" : "画像の生成上限を超えました。"}'
|
| 292 |
|
| 293 |
-
print(func_output)
|
| 294 |
-
|
| 295 |
# tool_outputリストに追加
|
| 296 |
tool_outputs.append({"tool_call_id": tool_id, "output": func_output})
|
| 297 |
|
|
@@ -306,8 +297,6 @@ def bot(state, history, image_path):
|
|
| 306 |
# ダミー をセットする
|
| 307 |
tool_outputs.append({"tool_call_id": tool_call.id, "output": '{"answer" : ""}'})
|
| 308 |
|
| 309 |
-
print(tool_outputs)
|
| 310 |
-
|
| 311 |
# 関数の出力を提出
|
| 312 |
run = client.beta.threads.runs.submit_tool_outputs(
|
| 313 |
thread_id=thread_id,
|
|
@@ -375,8 +364,6 @@ def bot(state, history, image_path):
|
|
| 375 |
|
| 376 |
if image_preview:
|
| 377 |
|
| 378 |
-
print(out_image_path)
|
| 379 |
-
|
| 380 |
# Functionで画像を取得していた場合表示
|
| 381 |
history = history + [(None, (out_image_path,))]
|
| 382 |
|
|
@@ -430,9 +417,6 @@ def func_action(state, func_name, func_args):
|
|
| 430 |
quality = state["quality"]
|
| 431 |
detail = state["detail"]
|
| 432 |
|
| 433 |
-
print("name:", func_name)
|
| 434 |
-
print("arguments:", func_args)
|
| 435 |
-
|
| 436 |
if func_name == "request_DallE3":
|
| 437 |
|
| 438 |
func_output = request_DallE3(
|
|
@@ -515,7 +499,7 @@ with gr.Blocks() as demo:
|
|
| 515 |
title = "<h2>GPT画像入出力対応チャット</h2>"
|
| 516 |
message = "<h3>・DallE3の画像生成とGPT-4 with Visionの画像解析が利用できます。<br>"
|
| 517 |
message += "・DallE3を利用する場合はプロンプト、GPT-4 Visionを利用する場合は画像とプロンプトを入力して下さい。<br>"
|
| 518 |
-
message += "・テスト中でAPIKEY無しで動きます。(画像は10
|
| 519 |
message += "・動画での紹介はこちら→https://www.youtube.com/watch?v=CIxVPNBMFQw<br>"
|
| 520 |
message += "</h3>"
|
| 521 |
|
|
@@ -549,11 +533,12 @@ with gr.Blocks() as demo:
|
|
| 549 |
btn_dl = gr.Button(value="画像の一括ダウンロード") # 保留中
|
| 550 |
btn_clear = gr.ClearButton(value="リセット", components=[chatbot, text_msg])
|
| 551 |
|
|
|
|
|
|
|
| 552 |
with gr.Row():
|
| 553 |
image = gr.Image(label="ファイルアップロード", type="filepath",interactive = True)
|
| 554 |
out_image = gr.Image(label="出力画像", type="filepath", interactive = False)
|
| 555 |
|
| 556 |
-
sys_msg = gr.Textbox(label="システムメッセージ", interactive = False)
|
| 557 |
# out_text = gr.Textbox(label="出力テキスト", lines = 5, interactive = False)
|
| 558 |
out_file = gr.File(label="出力ファイル", type="filepath",interactive = False)
|
| 559 |
|
|
@@ -573,9 +558,9 @@ with gr.Blocks() as demo:
|
|
| 573 |
|
| 574 |
with gr.Tab("設定") as set:
|
| 575 |
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
# system_prompt = gr.Textbox(value = SYS_PROMPT_DEFAULT,lines = 5, label="Custom instructions", interactive = True)
|
| 580 |
gr.Markdown("<h4>DaLL-E3用設定</h4>")
|
| 581 |
with gr.Row():
|
|
@@ -598,6 +583,37 @@ with gr.Blocks() as demo:
|
|
| 598 |
gr.Markdown("<h3>" + caution + "</h3>")
|
| 599 |
|
| 600 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 601 |
def request_DallE3(client, prompt, size, quality, out_image_path):
|
| 602 |
|
| 603 |
err_msg = ""
|
|
@@ -613,8 +629,6 @@ def request_DallE3(client, prompt, size, quality, out_image_path):
|
|
| 613 |
response_format="b64_json"
|
| 614 |
)
|
| 615 |
|
| 616 |
-
print(response.data[0])
|
| 617 |
-
|
| 618 |
# データを受け取りデコード
|
| 619 |
image_data_json = response.data[0].b64_json
|
| 620 |
image_data = base64.b64decode(image_data_json)
|
|
@@ -676,6 +690,8 @@ def request_Vision(client, prompt, image_path, detail, max_tokens):
|
|
| 676 |
}
|
| 677 |
]
|
| 678 |
|
|
|
|
|
|
|
| 679 |
# gpt-4-visionに問い合わせて回答を表示
|
| 680 |
response = client.chat.completions.create(
|
| 681 |
model="gpt-4-vision-preview", # Visionはこのモデル指定
|
|
@@ -685,8 +701,6 @@ def request_Vision(client, prompt, image_path, detail, max_tokens):
|
|
| 685 |
|
| 686 |
response_text = response.choices[0].message.content
|
| 687 |
|
| 688 |
-
print(response_text)
|
| 689 |
-
|
| 690 |
except BadRequestError as e:
|
| 691 |
print(e)
|
| 692 |
err_msg = "リクエストエラーです。画像がポリシー違反でないか確認して下さい。"
|
|
@@ -703,7 +717,7 @@ def request_Vision(client, prompt, image_path, detail, max_tokens):
|
|
| 703 |
}
|
| 704 |
return json.dumps(vision_result)
|
| 705 |
|
| 706 |
-
|
| 707 |
if __name__ == '__main__':
|
| 708 |
|
| 709 |
demo.queue()
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
# GPT用設定
|
| 18 |
+
DF_INSTRUCTIONS = "あなたはイラストレーターです。提供されている関数を使用して画像を作ったり、画像を解析したりします。"
|
| 19 |
+
DF_MODEL = "gpt-3.5-turbo-1106"
|
| 20 |
DUMMY = "********************"
|
| 21 |
file_format = {".png", ".jpeg", ".jpg", ".webp", ".gif", ".PNG", ".JPEG", ".JPG", ".WEBP", ".GIF"}
|
| 22 |
|
|
|
|
| 41 |
# サンプル用情報
|
| 42 |
examples = ["1980s anime girl with straight bob-cut in school uniform, roughly drawn drawing"
|
| 43 |
, "a minimalisit logo for a sporting goods company"]
|
| 44 |
+
# , "この画像について説明して下さい。"]
|
| 45 |
|
| 46 |
|
| 47 |
# 各関数定義
|
|
|
|
| 62 |
|
| 63 |
err_msg = ""
|
| 64 |
|
| 65 |
+
if state["openai_key"] == "" or state["openai_key"] is None:
|
| 66 |
|
| 67 |
+
# OpenAI API Key未入力
|
| 68 |
+
err_msg = "OpenAI API Keyを入力してください。(設定タブ)"
|
| 69 |
|
| 70 |
+
return state, err_msg
|
|
|
|
| 71 |
|
| 72 |
if not text:
|
| 73 |
|
| 74 |
# テキスト未入力
|
| 75 |
+
err_msg = "プロンプトを入力して下さい。"
|
| 76 |
|
| 77 |
return state, err_msg
|
| 78 |
|
|
|
|
| 81 |
# 入力画像のファイル形式チェック
|
| 82 |
root, ext = os.path.splitext(image)
|
| 83 |
|
|
|
|
|
|
|
| 84 |
if ext not in file_format:
|
| 85 |
|
| 86 |
# ファイル形式チェック
|
|
|
|
| 93 |
if state["client"] is None:
|
| 94 |
|
| 95 |
# 初回起動時は初期処理をする
|
| 96 |
+
# os.environ["OPENAI_API_KEY"] = os.environ["TEST_OPENAI_KEY"] # テスト時
|
| 97 |
+
os.environ["OPENAI_API_KEY"] = state["openai_key"]
|
| 98 |
|
| 99 |
# クライアント新規作成
|
| 100 |
client = OpenAI()
|
|
|
|
| 126 |
if state["assistant_id"] == "":
|
| 127 |
|
| 128 |
# アシスタント作成
|
| 129 |
+
assistant = client.beta.assistants.create(
|
| 130 |
+
name="GPT_Illustrator",
|
| 131 |
+
instructions=DF_INSTRUCTIONS,
|
| 132 |
+
model=DF_MODEL,
|
| 133 |
+
tools=[func_Vision, func_Dall_E3]
|
| 134 |
+
)
|
|
|
|
|
|
|
| 135 |
|
| 136 |
+
# アシスタントIDセット
|
| 137 |
+
state["assistant_id"] = assistant.id
|
| 138 |
+
# state["assistant_id"] = os.environ["ASSIST_ID"] # テスト中アシスタントは固定
|
| 139 |
|
| 140 |
+
# else:
|
| 141 |
|
| 142 |
+
# # アシスタント確認(IDが存在しないならエラーとなる)
|
| 143 |
+
# assistant = client.beta.assistants.retrieve(state["assistant_id"])
|
| 144 |
|
| 145 |
# ユーザIDでフォルダ作成
|
| 146 |
os.makedirs(state["user_id"], exist_ok=True)
|
| 147 |
|
| 148 |
+
# except NotFoundError as e:
|
| 149 |
+
# err_msg = "アシスタントIDが間違っています。新しく作成する場合はアシスタントIDを空欄にして下さい。"
|
| 150 |
except AuthenticationError as e:
|
| 151 |
err_msg = "認証エラーとなりました。OpenAPIKeyが正しいか、支払い方法などが設定されているか確認して下さい。"
|
| 152 |
except Exception as e:
|
|
|
|
| 234 |
run_id=run.id
|
| 235 |
)
|
| 236 |
|
|
|
|
|
|
|
| 237 |
if run.status == "requires_action": # 関数の結果の待ちの場合
|
| 238 |
|
|
|
|
|
|
|
| 239 |
# tool_callsの各項目取得
|
| 240 |
tool_calls = run.required_action.submit_tool_outputs.tool_calls
|
| 241 |
|
|
|
|
|
|
|
|
|
|
| 242 |
# 一つ目だけ取得
|
| 243 |
tool_id = tool_calls[0].id
|
| 244 |
func_name = tool_calls[0].function.name
|
|
|
|
| 283 |
# 10枚以上生成した場合終了とする
|
| 284 |
func_output = '{"answer" : "", "error_message" : "画像の生成上限を超えました。"}'
|
| 285 |
|
|
|
|
|
|
|
| 286 |
# tool_outputリストに追加
|
| 287 |
tool_outputs.append({"tool_call_id": tool_id, "output": func_output})
|
| 288 |
|
|
|
|
| 297 |
# ダミー をセットする
|
| 298 |
tool_outputs.append({"tool_call_id": tool_call.id, "output": '{"answer" : ""}'})
|
| 299 |
|
|
|
|
|
|
|
| 300 |
# 関数の出力を提出
|
| 301 |
run = client.beta.threads.runs.submit_tool_outputs(
|
| 302 |
thread_id=thread_id,
|
|
|
|
| 364 |
|
| 365 |
if image_preview:
|
| 366 |
|
|
|
|
|
|
|
| 367 |
# Functionで画像を取得していた場合表示
|
| 368 |
history = history + [(None, (out_image_path,))]
|
| 369 |
|
|
|
|
| 417 |
quality = state["quality"]
|
| 418 |
detail = state["detail"]
|
| 419 |
|
|
|
|
|
|
|
|
|
|
| 420 |
if func_name == "request_DallE3":
|
| 421 |
|
| 422 |
func_output = request_DallE3(
|
|
|
|
| 499 |
title = "<h2>GPT画像入出力対応チャット</h2>"
|
| 500 |
message = "<h3>・DallE3の画像生成とGPT-4 with Visionの画像解析が利用できます。<br>"
|
| 501 |
message += "・DallE3を利用する場合はプロンプト、GPT-4 Visionを利用する場合は画像とプロンプトを入力して下さい。<br>"
|
| 502 |
+
message += "・テスト中でAPIKEY無しで動きます。(画像は10枚まで生成可能)<br>"
|
| 503 |
message += "・動画での紹介はこちら→https://www.youtube.com/watch?v=CIxVPNBMFQw<br>"
|
| 504 |
message += "</h3>"
|
| 505 |
|
|
|
|
| 533 |
btn_dl = gr.Button(value="画像の一括ダウンロード") # 保留中
|
| 534 |
btn_clear = gr.ClearButton(value="リセット", components=[chatbot, text_msg])
|
| 535 |
|
| 536 |
+
sys_msg = gr.Textbox(label="システムメッセージ", interactive = False)
|
| 537 |
+
|
| 538 |
with gr.Row():
|
| 539 |
image = gr.Image(label="ファイルアップロード", type="filepath",interactive = True)
|
| 540 |
out_image = gr.Image(label="出力画像", type="filepath", interactive = False)
|
| 541 |
|
|
|
|
| 542 |
# out_text = gr.Textbox(label="出力テキスト", lines = 5, interactive = False)
|
| 543 |
out_file = gr.File(label="出力ファイル", type="filepath",interactive = False)
|
| 544 |
|
|
|
|
| 558 |
|
| 559 |
with gr.Tab("設定") as set:
|
| 560 |
|
| 561 |
+
gr.Markdown("<h4>OpenAI設定</h4>")
|
| 562 |
+
with gr.Row():
|
| 563 |
+
openai_key = gr.Textbox(label="OpenAI API Key") # テスト中は表示せず
|
| 564 |
# system_prompt = gr.Textbox(value = SYS_PROMPT_DEFAULT,lines = 5, label="Custom instructions", interactive = True)
|
| 565 |
gr.Markdown("<h4>DaLL-E3用設定</h4>")
|
| 566 |
with gr.Row():
|
|
|
|
| 583 |
gr.Markdown("<h3>" + caution + "</h3>")
|
| 584 |
|
| 585 |
|
| 586 |
+
# 関数情報
|
| 587 |
+
func_Dall_E3 = {
|
| 588 |
+
"type": "function",
|
| 589 |
+
"function": {
|
| 590 |
+
"name": "request_DallE3",
|
| 591 |
+
"description": "画像生成AI「dall-e-3」で指定のPromptから画像を作る。",
|
| 592 |
+
"parameters": {
|
| 593 |
+
"type": "object",
|
| 594 |
+
"properties": {
|
| 595 |
+
"prompt": {"type": "string", "description": "画像を作るためのPrompt"},
|
| 596 |
+
},
|
| 597 |
+
"required": ["prompt"]
|
| 598 |
+
}
|
| 599 |
+
}
|
| 600 |
+
}
|
| 601 |
+
|
| 602 |
+
func_Vision = {
|
| 603 |
+
"type": "function",
|
| 604 |
+
"function": {
|
| 605 |
+
"name": "request_Vision",
|
| 606 |
+
"description": "画像解析技術「Vision」により、指定の画像に関する質問に回答する。",
|
| 607 |
+
"parameters": {
|
| 608 |
+
"type": "object",
|
| 609 |
+
"properties": {
|
| 610 |
+
"prompt": {"type": "string", "description": "画像に対する質問内容(Prompt)"},
|
| 611 |
+
},
|
| 612 |
+
"required": ["prompt"]
|
| 613 |
+
}
|
| 614 |
+
}
|
| 615 |
+
}
|
| 616 |
+
|
| 617 |
def request_DallE3(client, prompt, size, quality, out_image_path):
|
| 618 |
|
| 619 |
err_msg = ""
|
|
|
|
| 629 |
response_format="b64_json"
|
| 630 |
)
|
| 631 |
|
|
|
|
|
|
|
| 632 |
# データを受け取りデコード
|
| 633 |
image_data_json = response.data[0].b64_json
|
| 634 |
image_data = base64.b64decode(image_data_json)
|
|
|
|
| 690 |
}
|
| 691 |
]
|
| 692 |
|
| 693 |
+
print(messages)
|
| 694 |
+
|
| 695 |
# gpt-4-visionに問い合わせて回答を表示
|
| 696 |
response = client.chat.completions.create(
|
| 697 |
model="gpt-4-vision-preview", # Visionはこのモデル指定
|
|
|
|
| 701 |
|
| 702 |
response_text = response.choices[0].message.content
|
| 703 |
|
|
|
|
|
|
|
| 704 |
except BadRequestError as e:
|
| 705 |
print(e)
|
| 706 |
err_msg = "リクエストエラーです。画像がポリシー違反でないか確認して下さい。"
|
|
|
|
| 717 |
}
|
| 718 |
return json.dumps(vision_result)
|
| 719 |
|
| 720 |
+
|
| 721 |
if __name__ == '__main__':
|
| 722 |
|
| 723 |
demo.queue()
|
requirements.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
# gradio==4.2.0
|
| 2 |
-
openai==1.
|
|
|
|
| 1 |
# gradio==4.2.0
|
| 2 |
+
openai==1.6.0
|