Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,3 @@
|
|
1 |
-
# -*- coding: utf-8 -*-
|
2 |
-
"""gradio_chat_image.ipynb
|
3 |
-
|
4 |
-
Automatically generated by Colaboratory.
|
5 |
-
|
6 |
-
Original file is located at
|
7 |
-
https://colab.research.google.com/drive/1UOcwXwg1bHrPbhkM9tB5ivx6OnI9lB7q
|
8 |
-
"""
|
9 |
-
|
10 |
-
!pip install gradio
|
11 |
-
!pip install openai
|
12 |
-
|
13 |
-
import os
|
14 |
-
|
15 |
-
os.environ["TEST_OPENAI_KEY"] = "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"
|
16 |
-
os.environ["ASSIST_ID"] = "asst_ePkuRLY0DUmChyCv8tXtf0gC"
|
17 |
-
os.environ["MAX_TRIAL"] = "50"
|
18 |
-
os.environ["INTER_SEC"] = "1" # 画像は1
|
19 |
-
os.environ["MAX_TOKENS"] = "300"
|
20 |
-
|
21 |
-
from logging import DEBUG
|
22 |
import os
|
23 |
import json
|
24 |
import base64
|
@@ -53,6 +32,7 @@ ANT_MSG = "(下部の[出力ファイル]にファイルを追加しました
|
|
53 |
MAX_TRIAL = int(os.environ["MAX_TRIAL"]) # メッセージ取得最大試行数
|
54 |
INTER_SEC = int(os.environ["INTER_SEC"]) # 試行間隔(秒)
|
55 |
MAX_TOKENS = int(os.environ["MAX_TOKENS"]) # Vison最大トークン
|
|
|
56 |
|
57 |
# 正規表現用パターン
|
58 |
pt = r".*\[(.*)\]\((.*)\)"
|
@@ -223,6 +203,7 @@ def bot(state, history, image_path):
|
|
223 |
thread_id = state["thread_id"]
|
224 |
last_msg_id = state["last_msg_id"]
|
225 |
user_id = state["user_id"]
|
|
|
226 |
|
227 |
# メッセージ設定
|
228 |
message = client.beta.threads.messages.create(
|
@@ -277,7 +258,8 @@ def bot(state, history, image_path):
|
|
277 |
image_name = dt.strftime("%Y%m%d%H%M%S") + ".png"
|
278 |
|
279 |
# ファイルパスは手動設定(誤りがないように)
|
280 |
-
out_image_path = user_id + "/" + image_name
|
|
|
281 |
|
282 |
# dall-e3のとき"image_path"は出力ファイルパス
|
283 |
func_args["image_path"] = out_image_path
|
@@ -300,7 +282,14 @@ def bot(state, history, image_path):
|
|
300 |
continue
|
301 |
|
302 |
# 関数を実行
|
303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
|
305 |
print(func_output)
|
306 |
|
@@ -395,6 +384,8 @@ def bot(state, history, image_path):
|
|
395 |
# Functionで画像を取得していた場合表示
|
396 |
history = history + [(None, (out_image_path,))]
|
397 |
|
|
|
|
|
398 |
image_preview = False
|
399 |
|
400 |
# 最終メッセージID更新
|
@@ -405,6 +396,7 @@ def bot(state, history, image_path):
|
|
405 |
|
406 |
# セッションのメッセージID更新
|
407 |
state["last_msg_id"] = last_msg_id
|
|
|
408 |
|
409 |
# 完了なら終了
|
410 |
if run.status == "completed":
|
@@ -546,7 +538,7 @@ with gr.Blocks() as demo:
|
|
546 |
"thread_id" : "",
|
547 |
"last_msg_id" : "",
|
548 |
"user_id" : "",
|
549 |
-
"image_count" :
|
550 |
})
|
551 |
|
552 |
with gr.Tab("Chat画面") as chat:
|
@@ -717,29 +709,3 @@ def request_Vision(client, prompt, image_path, detail, max_tokens):
|
|
717 |
"error_message" : err_msg
|
718 |
}
|
719 |
return json.dumps(vision_result)
|
720 |
-
|
721 |
-
def request_DallE3(client, prompt, size, quality, out_image_path):
|
722 |
-
""" DallE3を呼び出す """
|
723 |
-
|
724 |
-
err_msg = ""
|
725 |
-
|
726 |
-
imgage_path = "/content/sample_datacat1.png"
|
727 |
-
|
728 |
-
dalle3_result = {
|
729 |
-
"imgage_path" : imgage_path,
|
730 |
-
"error_message" : err_msg
|
731 |
-
}
|
732 |
-
|
733 |
-
return json.dumps(dalle3_result)
|
734 |
-
|
735 |
-
|
736 |
-
def request_Vision(prompt, detail, image_path):
|
737 |
-
""" GPT4 Visionを呼び出す """
|
738 |
-
|
739 |
-
response_text = "この画像は驚いた表情をしている人物を写した写真です。"
|
740 |
-
|
741 |
-
vision_result = {
|
742 |
-
"answer" : response_text
|
743 |
-
}
|
744 |
-
|
745 |
-
return json.dumps(vision_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import json
|
3 |
import base64
|
|
|
32 |
MAX_TRIAL = int(os.environ["MAX_TRIAL"]) # メッセージ取得最大試行数
|
33 |
INTER_SEC = int(os.environ["INTER_SEC"]) # 試行間隔(秒)
|
34 |
MAX_TOKENS = int(os.environ["MAX_TOKENS"]) # Vison最大トークン
|
35 |
+
IMAGE_LIMIT = int(os.environ["IMAGE_LIMIT"])
|
36 |
|
37 |
# 正規表現用パターン
|
38 |
pt = r".*\[(.*)\]\((.*)\)"
|
|
|
203 |
thread_id = state["thread_id"]
|
204 |
last_msg_id = state["last_msg_id"]
|
205 |
user_id = state["user_id"]
|
206 |
+
image_count = state["image_count"]
|
207 |
|
208 |
# メッセージ設定
|
209 |
message = client.beta.threads.messages.create(
|
|
|
258 |
image_name = dt.strftime("%Y%m%d%H%M%S") + ".png"
|
259 |
|
260 |
# ファイルパスは手動設定(誤りがないように)
|
261 |
+
# out_image_path = user_id + "/" + image_name
|
262 |
+
out_image_path = "cat.jpg"
|
263 |
|
264 |
# dall-e3のとき"image_path"は出力ファイルパス
|
265 |
func_args["image_path"] = out_image_path
|
|
|
282 |
continue
|
283 |
|
284 |
# 関数を実行
|
285 |
+
|
286 |
+
if image_count < IMAGE_LIMIT:
|
287 |
+
func_output = func_action(state, func_name, func_args)
|
288 |
+
|
289 |
+
else:
|
290 |
+
|
291 |
+
# 10枚以上生成した場合終了とする
|
292 |
+
func_output = '{"answer" : "", "error_message" : "画像の生成上限を超えました。"}'
|
293 |
|
294 |
print(func_output)
|
295 |
|
|
|
384 |
# Functionで画像を取得していた場合表示
|
385 |
history = history + [(None, (out_image_path,))]
|
386 |
|
387 |
+
image_count += 1
|
388 |
+
|
389 |
image_preview = False
|
390 |
|
391 |
# 最終メッセージID更新
|
|
|
396 |
|
397 |
# セッションのメッセージID更新
|
398 |
state["last_msg_id"] = last_msg_id
|
399 |
+
state["image_count"] = image_count
|
400 |
|
401 |
# 完了なら終了
|
402 |
if run.status == "completed":
|
|
|
538 |
"thread_id" : "",
|
539 |
"last_msg_id" : "",
|
540 |
"user_id" : "",
|
541 |
+
"image_count" : 0
|
542 |
})
|
543 |
|
544 |
with gr.Tab("Chat画面") as chat:
|
|
|
709 |
"error_message" : err_msg
|
710 |
}
|
711 |
return json.dumps(vision_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|