Spaces:
Sleeping
Sleeping
update
Browse files- app.py +10 -9
- bak/app4.py +532 -0
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
import time
|
| 4 |
import os
|
|
@@ -24,7 +23,7 @@ IMG_MSG = "(画像ファイルを追加しました。送信ボタンの下に
|
|
| 24 |
ANT_MSG = "(下部の[出力ファイル]にファイルを追加しました。)"
|
| 25 |
|
| 26 |
# 各種設定値
|
| 27 |
-
MAX_TRIAL =
|
| 28 |
INTER_SEC = 3 # 試行間隔(秒)
|
| 29 |
|
| 30 |
# サンプル用情報
|
|
@@ -221,7 +220,7 @@ def bot(state, history, file_id):
|
|
| 221 |
client = state["client"]
|
| 222 |
assistant_id = state["assistant_id"]
|
| 223 |
thread_id = state["thread_id"]
|
| 224 |
-
|
| 225 |
code_mode = state["code_mode"]
|
| 226 |
|
| 227 |
print("system_prompt")
|
|
@@ -268,16 +267,16 @@ def bot(state, history, file_id):
|
|
| 268 |
# 前回のメッセージより後を昇順で取り出す
|
| 269 |
messages = client.beta.threads.messages.list(
|
| 270 |
thread_id=thread_id,
|
| 271 |
-
after=
|
| 272 |
order="asc"
|
| 273 |
)
|
| 274 |
|
| 275 |
-
print(
|
| 276 |
# print(messages.data)
|
| 277 |
|
| 278 |
msg_log = client.beta.threads.messages.list(
|
| 279 |
thread_id=thread_id,
|
| 280 |
-
# after=
|
| 281 |
order="asc"
|
| 282 |
)
|
| 283 |
|
|
@@ -286,7 +285,7 @@ def bot(state, history, file_id):
|
|
| 286 |
# messageを取り出す
|
| 287 |
for msg in messages:
|
| 288 |
|
| 289 |
-
msg_id = msg.id
|
| 290 |
|
| 291 |
if msg.role == "assistant":
|
| 292 |
|
|
@@ -321,7 +320,7 @@ def bot(state, history, file_id):
|
|
| 321 |
history = history + [[None, res_text]]
|
| 322 |
|
| 323 |
# 最終メッセージID更新
|
| 324 |
-
|
| 325 |
|
| 326 |
else:
|
| 327 |
|
|
@@ -369,11 +368,12 @@ def bot(state, history, file_id):
|
|
| 369 |
history[-1][1] = res_text
|
| 370 |
|
| 371 |
# 最終メッセージID更新
|
| 372 |
-
|
| 373 |
|
| 374 |
# yield gr.Chatbot(label=run.status ,value=history), image_file, ant_file, err_msg
|
| 375 |
|
| 376 |
print(run.status)
|
|
|
|
| 377 |
|
| 378 |
# 完了なら終了
|
| 379 |
if run.status == "completed":
|
|
@@ -530,3 +530,4 @@ with gr.Blocks() as demo:
|
|
| 530 |
demo.queue()
|
| 531 |
|
| 532 |
demo.launch(debug=True)
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
| 3 |
import os
|
|
|
|
| 23 |
ANT_MSG = "(下部の[出力ファイル]にファイルを追加しました。)"
|
| 24 |
|
| 25 |
# 各種設定値
|
| 26 |
+
MAX_TRIAL = 50 # メッセージ取得最大試行数
|
| 27 |
INTER_SEC = 3 # 試行間隔(秒)
|
| 28 |
|
| 29 |
# サンプル用情報
|
|
|
|
| 220 |
client = state["client"]
|
| 221 |
assistant_id = state["assistant_id"]
|
| 222 |
thread_id = state["thread_id"]
|
| 223 |
+
last_msg_id = state["last_msg_id"]
|
| 224 |
code_mode = state["code_mode"]
|
| 225 |
|
| 226 |
print("system_prompt")
|
|
|
|
| 267 |
# 前回のメッセージより後を昇順で取り出す
|
| 268 |
messages = client.beta.threads.messages.list(
|
| 269 |
thread_id=thread_id,
|
| 270 |
+
after=last_msg_id,
|
| 271 |
order="asc"
|
| 272 |
)
|
| 273 |
|
| 274 |
+
print(last_msg_id)
|
| 275 |
# print(messages.data)
|
| 276 |
|
| 277 |
msg_log = client.beta.threads.messages.list(
|
| 278 |
thread_id=thread_id,
|
| 279 |
+
# after=last_msg_id,
|
| 280 |
order="asc"
|
| 281 |
)
|
| 282 |
|
|
|
|
| 285 |
# messageを取り出す
|
| 286 |
for msg in messages:
|
| 287 |
|
| 288 |
+
# msg_id = msg.id
|
| 289 |
|
| 290 |
if msg.role == "assistant":
|
| 291 |
|
|
|
|
| 320 |
history = history + [[None, res_text]]
|
| 321 |
|
| 322 |
# 最終メッセージID更新
|
| 323 |
+
last_msg_id = msg.id
|
| 324 |
|
| 325 |
else:
|
| 326 |
|
|
|
|
| 368 |
history[-1][1] = res_text
|
| 369 |
|
| 370 |
# 最終メッセージID更新
|
| 371 |
+
last_msg_id = msg.id
|
| 372 |
|
| 373 |
# yield gr.Chatbot(label=run.status ,value=history), image_file, ant_file, err_msg
|
| 374 |
|
| 375 |
print(run.status)
|
| 376 |
+
state["last_msg_id"] = last_msg_id
|
| 377 |
|
| 378 |
# 完了なら終了
|
| 379 |
if run.status == "completed":
|
|
|
|
| 530 |
demo.queue()
|
| 531 |
|
| 532 |
demo.launch(debug=True)
|
| 533 |
+
|
bak/app4.py
ADDED
|
@@ -0,0 +1,532 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import time
|
| 4 |
+
import os
|
| 5 |
+
import datetime
|
| 6 |
+
# from zoneinfo import ZoneInfo
|
| 7 |
+
from openai import OpenAI
|
| 8 |
+
|
| 9 |
+
from openai.types.beta.threads.runs import (
|
| 10 |
+
ToolCallsStepDetails,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# GPT用設定
|
| 14 |
+
SYS_PROMPT_DEFAULT = "あなたは優秀なアシスタントです。質問をされた場合は、質問に答えるコードを作成して実行します。回答は日本語でお願いします。"
|
| 15 |
+
DUMMY = "********************"
|
| 16 |
+
file_format = {".txt", ".csv", ".pdf"}
|
| 17 |
+
|
| 18 |
+
# 各種出力フォルダ
|
| 19 |
+
IMG_FOLDER = "sample_data"
|
| 20 |
+
ANT_FOLDER = "sample_data"
|
| 21 |
+
|
| 22 |
+
# 各種メッセージ
|
| 23 |
+
IMG_MSG = "(画像ファイルを追加しました。送信ボタンの下に表示されています。)"
|
| 24 |
+
ANT_MSG = "(下部の[出力ファイル]にファイルを追加しました。)"
|
| 25 |
+
|
| 26 |
+
# 各種設定値
|
| 27 |
+
MAX_TRIAL = 30 # メッセージ取得最大試行数
|
| 28 |
+
INTER_SEC = 3 # 試行間隔(秒)
|
| 29 |
+
|
| 30 |
+
# サンプル用情報
|
| 31 |
+
examples = ["sample_data/東京都年別人口.csv", "sample_data/練馬区年齢別人口.csv", "sample_data/桃太郎あらすじ.txt"]
|
| 32 |
+
example_toid = {"東京都年別人口.csv" : "file-GOEk4X4WpU5gBJAuHCMtiJrn"
|
| 33 |
+
, "練馬区年齢別人口.csv" : "file-YAFPMMqG3Zl5DRx5hTLjCfFa"
|
| 34 |
+
, "桃太郎あらすじ.txt" : "file-oDjAzL3G4ktwAUEkcGDCQMuQ"}
|
| 35 |
+
|
| 36 |
+
# file_id = "file-0Ly64DA2jzE9mOFYayOKJJK0"
|
| 37 |
+
# file_id = "file-aVnVcpEVpsy77xQ8SlTp1WoX" # ライ麦
|
| 38 |
+
# file_id = "file-HFCaJbf3k7j0fhBqh1Rwf2VV" # 練馬区
|
| 39 |
+
|
| 40 |
+
# コード出力用
|
| 41 |
+
code_mode = {'ON': True, 'OFF': False}
|
| 42 |
+
|
| 43 |
+
def set_state(openai_key, sys_prompt, code_output, state):
|
| 44 |
+
""" 設定タブの情報をセッションに保存する関数 """
|
| 45 |
+
|
| 46 |
+
state["openai_key"] = openai_key
|
| 47 |
+
state["system_prompt"] = sys_prompt
|
| 48 |
+
state["code_mode"] = code_mode[code_output]
|
| 49 |
+
|
| 50 |
+
return state
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def init(state, text, file):
|
| 54 |
+
""" 入力チェックを行う関数
|
| 55 |
+
※ここで例外を起こすと入力できなくなるので次の関数でエラーにする """
|
| 56 |
+
|
| 57 |
+
err_msg = ""
|
| 58 |
+
file_id = None
|
| 59 |
+
|
| 60 |
+
# if state["openai_key"] == "" or state["openai_key"] is None:
|
| 61 |
+
|
| 62 |
+
# # OpenAI API Key未入力
|
| 63 |
+
# err_msg = "OpenAI API Keyを入力してください。(設定タブ)"
|
| 64 |
+
|
| 65 |
+
if not text:
|
| 66 |
+
|
| 67 |
+
# テキスト未入力
|
| 68 |
+
err_msg = "テキストを入力して下さい。"
|
| 69 |
+
|
| 70 |
+
return state, text, file, file_id, err_msg
|
| 71 |
+
|
| 72 |
+
elif file:
|
| 73 |
+
|
| 74 |
+
# 入力画像のファイル形式チェック
|
| 75 |
+
root, ext = os.path.splitext(file)
|
| 76 |
+
|
| 77 |
+
if ext not in file_format:
|
| 78 |
+
|
| 79 |
+
# ファイル形式チェック
|
| 80 |
+
err_msg = "指定した形式のファイルをアップしてください。(注意事項タブに記載)"
|
| 81 |
+
|
| 82 |
+
return state, text, gr.Image(value=None,type="filepath", interactive=False), file_id, err_msg
|
| 83 |
+
|
| 84 |
+
if state["client"] is None:
|
| 85 |
+
|
| 86 |
+
# 初回起動時は初期処理をする
|
| 87 |
+
os.environ["OPENAI_API_KEY"] = os.environ["TEST_OPENAI_KEY"] # テスト時
|
| 88 |
+
|
| 89 |
+
# os.environ["OPENAI_API_KEY"] = state["openai_key"]
|
| 90 |
+
|
| 91 |
+
client = OpenAI()
|
| 92 |
+
|
| 93 |
+
# client作成後は消す
|
| 94 |
+
os.environ["OPENAI_API_KEY"] = ""
|
| 95 |
+
|
| 96 |
+
# アシスタント作成
|
| 97 |
+
# assistant = client.beta.assistants.create(
|
| 98 |
+
# name="codeinter_test",
|
| 99 |
+
# instructions=state["system_prompt"],
|
| 100 |
+
# # model="gpt-4-1106-preview",
|
| 101 |
+
# model="gpt-3.5-turbo-1106",
|
| 102 |
+
# tools=[{"type": "code_interpreter"}]
|
| 103 |
+
# )
|
| 104 |
+
|
| 105 |
+
# print(assistant.id)
|
| 106 |
+
|
| 107 |
+
# スレッド作成
|
| 108 |
+
thread = client.beta.threads.create()
|
| 109 |
+
|
| 110 |
+
# セッションにセット
|
| 111 |
+
state["client"] = client
|
| 112 |
+
# state["assistant_id"] = assistant.id
|
| 113 |
+
state["assistant_id"] = os.environ["ASSIST_ID"] # テスト中アシスタントは固定
|
| 114 |
+
state["thread_id"] = thread.id
|
| 115 |
+
|
| 116 |
+
if file:
|
| 117 |
+
|
| 118 |
+
# ファイル名取得
|
| 119 |
+
basename = os.path.basename(file)
|
| 120 |
+
|
| 121 |
+
if example_toid.get(basename):
|
| 122 |
+
|
| 123 |
+
# サンプルの場合は用意したIDをセット
|
| 124 |
+
file_id = example_toid.get(basename)
|
| 125 |
+
|
| 126 |
+
else:
|
| 127 |
+
|
| 128 |
+
# ファイルのアップ
|
| 129 |
+
# file_response = client.files.create(
|
| 130 |
+
# purpose="assistants",
|
| 131 |
+
# file=open(file,"rb"),
|
| 132 |
+
# )
|
| 133 |
+
|
| 134 |
+
# if file_response.status != 'processed':
|
| 135 |
+
|
| 136 |
+
# # 失敗時
|
| 137 |
+
# err_msg = "ファイルのアップロードに失敗しました"
|
| 138 |
+
|
| 139 |
+
# else
|
| 140 |
+
# # ファイルのIDをセット
|
| 141 |
+
# file_id = file_response.id
|
| 142 |
+
|
| 143 |
+
# file_id = "file-0Ly64DA2jzE9mOFYayOKJJK0"
|
| 144 |
+
# file_id = "file-aVnVcpEVpsy77xQ8SlTp1WoX" # ライ麦
|
| 145 |
+
|
| 146 |
+
# file_id = "file-HFCaJbf3k7j0fhBqh1Rwf2VV" # 練馬区
|
| 147 |
+
file_id = ""
|
| 148 |
+
|
| 149 |
+
# print(file_id)
|
| 150 |
+
|
| 151 |
+
return state, text, file, file_id, err_msg
|
| 152 |
+
|
| 153 |
+
def raise_exception(err_msg):
|
| 154 |
+
""" エラーの場合例外を起こす関数 """
|
| 155 |
+
|
| 156 |
+
if err_msg != "":
|
| 157 |
+
raise Exception("これは入力チェ���クでの例外です。")
|
| 158 |
+
|
| 159 |
+
return
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def add_history(history, text, file_id):
|
| 163 |
+
""" Chat履歴"history"に追加を行う関数 """
|
| 164 |
+
|
| 165 |
+
# print("前:")
|
| 166 |
+
# print(history)
|
| 167 |
+
|
| 168 |
+
err_msg = ""
|
| 169 |
+
new_row_flg = False
|
| 170 |
+
|
| 171 |
+
# 新しい行を追加するか判定
|
| 172 |
+
# if len(history) == 0:
|
| 173 |
+
|
| 174 |
+
# new_row_flg = True
|
| 175 |
+
|
| 176 |
+
# elif history[-1][0] is not None:
|
| 177 |
+
|
| 178 |
+
# # 前回がアシスタントでない場合も追加
|
| 179 |
+
# new_row_flg = True
|
| 180 |
+
|
| 181 |
+
new_row_flg = True
|
| 182 |
+
|
| 183 |
+
if file_id is None or file_id == "":
|
| 184 |
+
|
| 185 |
+
if new_row_flg:
|
| 186 |
+
|
| 187 |
+
# テキストだけの場合そのまま追加
|
| 188 |
+
history = history + [(text, None)]
|
| 189 |
+
else:
|
| 190 |
+
history[-1][0] = text
|
| 191 |
+
|
| 192 |
+
elif file_id is not None:
|
| 193 |
+
|
| 194 |
+
if new_row_flg:
|
| 195 |
+
|
| 196 |
+
# ファイルがあればファイルIDとテキストを追加
|
| 197 |
+
history = history + [("file:" + file_id, DUMMY)]
|
| 198 |
+
history = history + [(text, None)]
|
| 199 |
+
|
| 200 |
+
else:
|
| 201 |
+
history[-1][0] = "file:" + file_id
|
| 202 |
+
history = history + [(text, None)]
|
| 203 |
+
|
| 204 |
+
print(history)
|
| 205 |
+
|
| 206 |
+
# テキストだけ初期化
|
| 207 |
+
new_text = gr.Textbox(value="", interactive=True)
|
| 208 |
+
|
| 209 |
+
return history, new_text, err_msg
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
def bot(state, history, file_id):
|
| 213 |
+
|
| 214 |
+
err_msg = ""
|
| 215 |
+
image_file = None
|
| 216 |
+
ant_file = None
|
| 217 |
+
# new_row_flg = False
|
| 218 |
+
|
| 219 |
+
# セッション情報取得
|
| 220 |
+
system_prompt = state["system_prompt"]
|
| 221 |
+
client = state["client"]
|
| 222 |
+
assistant_id = state["assistant_id"]
|
| 223 |
+
thread_id = state["thread_id"]
|
| 224 |
+
msg_id = state["last_msg_id"]
|
| 225 |
+
code_mode = state["code_mode"]
|
| 226 |
+
|
| 227 |
+
print("system_prompt")
|
| 228 |
+
|
| 229 |
+
if file_id is None or file_id == "":
|
| 230 |
+
|
| 231 |
+
# ファイルがない場合
|
| 232 |
+
message = client.beta.threads.messages.create(
|
| 233 |
+
thread_id=thread_id,
|
| 234 |
+
role="user",
|
| 235 |
+
content=history[-1][0],
|
| 236 |
+
)
|
| 237 |
+
else:
|
| 238 |
+
|
| 239 |
+
# ファイルがあるときはIDをセット
|
| 240 |
+
message = client.beta.threads.messages.create(
|
| 241 |
+
thread_id=thread_id,
|
| 242 |
+
role="user",
|
| 243 |
+
content=history[-1][0],
|
| 244 |
+
file_ids=[file_id]
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
print(message)
|
| 248 |
+
|
| 249 |
+
# RUNスタート
|
| 250 |
+
run = client.beta.threads.runs.create(
|
| 251 |
+
thread_id=thread_id,
|
| 252 |
+
assistant_id=assistant_id,
|
| 253 |
+
instructions=system_prompt
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
# "completed"となるまで繰り返す(指定秒おき)
|
| 257 |
+
for i in range(0, MAX_TRIAL, 1):
|
| 258 |
+
|
| 259 |
+
if i > 0:
|
| 260 |
+
time.sleep(INTER_SEC)
|
| 261 |
+
|
| 262 |
+
# メッセージ受け取り
|
| 263 |
+
run = client.beta.threads.runs.retrieve(
|
| 264 |
+
thread_id=thread_id,
|
| 265 |
+
run_id=run.id
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
# 前回のメッセージより後を昇順で取り出す
|
| 269 |
+
messages = client.beta.threads.messages.list(
|
| 270 |
+
thread_id=thread_id,
|
| 271 |
+
after=msg_id,
|
| 272 |
+
order="asc"
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
print(msg_id)
|
| 276 |
+
# print(messages.data)
|
| 277 |
+
|
| 278 |
+
msg_log = client.beta.threads.messages.list(
|
| 279 |
+
thread_id=thread_id,
|
| 280 |
+
# after=msg_id,
|
| 281 |
+
order="asc"
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
print(msg_log)
|
| 285 |
+
|
| 286 |
+
# messageを取り出す
|
| 287 |
+
for msg in messages:
|
| 288 |
+
|
| 289 |
+
msg_id = msg.id
|
| 290 |
+
|
| 291 |
+
if msg.role == "assistant":
|
| 292 |
+
|
| 293 |
+
for content in msg.content:
|
| 294 |
+
|
| 295 |
+
res_text = ""
|
| 296 |
+
file_id = ""
|
| 297 |
+
ant_file = None
|
| 298 |
+
|
| 299 |
+
cont_dict = content.model_dump() # 辞書型に変換
|
| 300 |
+
|
| 301 |
+
ct_image_file = cont_dict.get("image_file")
|
| 302 |
+
|
| 303 |
+
if ct_image_file:
|
| 304 |
+
|
| 305 |
+
# imageファイルがあるならIDセット
|
| 306 |
+
res_file_id = ct_image_file.get("file_id")
|
| 307 |
+
|
| 308 |
+
# ファイルをダウンロード
|
| 309 |
+
image_file = file_download(client, res_file_id, IMG_FOLDER , ".png")
|
| 310 |
+
|
| 311 |
+
if image_file is None:
|
| 312 |
+
|
| 313 |
+
err_msg = "ファイルのダウンロードに失敗しました。"
|
| 314 |
+
|
| 315 |
+
else:
|
| 316 |
+
|
| 317 |
+
print("画像ファイル追加")
|
| 318 |
+
|
| 319 |
+
res_text = IMG_MSG
|
| 320 |
+
|
| 321 |
+
history = history + [[None, res_text]]
|
| 322 |
+
|
| 323 |
+
# 最終メッセージID更新
|
| 324 |
+
state["last_msg_id"] = msg_id
|
| 325 |
+
|
| 326 |
+
else:
|
| 327 |
+
|
| 328 |
+
# 返答テキスト取得
|
| 329 |
+
res_text = cont_dict["text"].get("value")
|
| 330 |
+
|
| 331 |
+
# 注釈(参照ファイル)ががある場合取得
|
| 332 |
+
if len(cont_dict.get("text").get("annotations")) > 0:
|
| 333 |
+
|
| 334 |
+
ct_ant = cont_dict.get("text").get("annotations")
|
| 335 |
+
|
| 336 |
+
if ct_ant[0].get("file_path") is not None:
|
| 337 |
+
|
| 338 |
+
# 参照ファイルのID取得
|
| 339 |
+
ant_file_id = ct_ant[0].get("file_path").get("file_id")
|
| 340 |
+
|
| 341 |
+
if ct_ant[0].get("text") is not None:
|
| 342 |
+
|
| 343 |
+
# ファイル形式(拡張子)取得
|
| 344 |
+
ext = "." + ct_ant[0].get("text")[ct_ant[0].get("text").rfind('.') + 1:]
|
| 345 |
+
|
| 346 |
+
# ファイルダウンロード
|
| 347 |
+
ant_file = file_download(client, ant_file_id, ANT_FOLDER, ext)
|
| 348 |
+
|
| 349 |
+
if ant_file is None:
|
| 350 |
+
|
| 351 |
+
err_msg = "参照ファイルのダウンロードに失敗しました。"
|
| 352 |
+
|
| 353 |
+
else:
|
| 354 |
+
|
| 355 |
+
# 参照ファイルがある旨のメッセージを追加
|
| 356 |
+
res_text = res_text + "\n\n" + ANT_MSG
|
| 357 |
+
|
| 358 |
+
print(res_text)
|
| 359 |
+
|
| 360 |
+
if res_text != "":
|
| 361 |
+
|
| 362 |
+
# Chat画面更新
|
| 363 |
+
if history[-1][1] is not None:
|
| 364 |
+
|
| 365 |
+
# 新しい行を追加
|
| 366 |
+
history = history + [[None, res_text]]
|
| 367 |
+
else:
|
| 368 |
+
|
| 369 |
+
history[-1][1] = res_text
|
| 370 |
+
|
| 371 |
+
# 最終メッセージID更新
|
| 372 |
+
state["last_msg_id"] = msg_id
|
| 373 |
+
|
| 374 |
+
# yield gr.Chatbot(label=run.status ,value=history), image_file, ant_file, err_msg
|
| 375 |
+
|
| 376 |
+
print(run.status)
|
| 377 |
+
|
| 378 |
+
# 完了なら終了
|
| 379 |
+
if run.status == "completed":
|
| 380 |
+
|
| 381 |
+
if not code_mode:
|
| 382 |
+
|
| 383 |
+
yield gr.Chatbot(label=run.status ,value=history), image_file, ant_file, err_msg
|
| 384 |
+
|
| 385 |
+
break
|
| 386 |
+
else:
|
| 387 |
+
|
| 388 |
+
# コードモードがONの場合
|
| 389 |
+
run_steps = client.beta.threads.runs.steps.list(
|
| 390 |
+
thread_id=thread_id, run_id=run.id
|
| 391 |
+
)
|
| 392 |
+
|
| 393 |
+
# コードを取得
|
| 394 |
+
input_code = get_code(run_steps)
|
| 395 |
+
|
| 396 |
+
if len(input_code) > 0:
|
| 397 |
+
|
| 398 |
+
for code in input_code:
|
| 399 |
+
|
| 400 |
+
code = "[input_code]\n\n" + code
|
| 401 |
+
|
| 402 |
+
# コードを追加
|
| 403 |
+
history = history + [[None, code]]
|
| 404 |
+
|
| 405 |
+
yield gr.Chatbot(label=run.status ,value=history), image_file, ant_file, err_msg
|
| 406 |
+
|
| 407 |
+
break
|
| 408 |
+
|
| 409 |
+
elif run.status == "failed":
|
| 410 |
+
|
| 411 |
+
# エラーとして終了
|
| 412 |
+
err_msg = "※メッセージ取得に失敗しました。"
|
| 413 |
+
return gr.Chatbot(label=run.status ,value=history), image_file, ant_file, err_msg
|
| 414 |
+
|
| 415 |
+
elif i == MAX_TRIAL:
|
| 416 |
+
|
| 417 |
+
# エラーとして終了
|
| 418 |
+
err_msg = "※メッセージ取得の際にタイムアウトしました。"
|
| 419 |
+
return gr.Chatbot(label=run.status ,value=history), image_file, ant_file, err_msg
|
| 420 |
+
|
| 421 |
+
else:
|
| 422 |
+
if i > 3:
|
| 423 |
+
|
| 424 |
+
# 作業中とわかるようにする
|
| 425 |
+
# history = history + [[None, "………"]]
|
| 426 |
+
yield gr.Chatbot(label=run.status + " (Request:" + str(i) + ")" ,value=history), image_file, ant_file, err_msg
|
| 427 |
+
|
| 428 |
+
|
| 429 |
+
def get_code(run_steps):
|
| 430 |
+
|
| 431 |
+
input_code = []
|
| 432 |
+
|
| 433 |
+
for data in run_steps.data:
|
| 434 |
+
|
| 435 |
+
if isinstance(data.step_details, ToolCallsStepDetails):
|
| 436 |
+
|
| 437 |
+
for tool_call in data.step_details.tool_calls:
|
| 438 |
+
|
| 439 |
+
input_code.append(tool_call.code_interpreter.input)
|
| 440 |
+
|
| 441 |
+
return input_code
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
def file_download(client, file_id, folder, ext):
|
| 445 |
+
""" OpenAIからファイルをダウンロードしてパスを返す """
|
| 446 |
+
api_response = client.files.with_raw_response.retrieve_content(file_id)
|
| 447 |
+
|
| 448 |
+
if api_response.status_code == 200:
|
| 449 |
+
|
| 450 |
+
content = api_response.content
|
| 451 |
+
|
| 452 |
+
file_path = folder + "/" + file_id + ext
|
| 453 |
+
|
| 454 |
+
with open(file_path, 'wb') as f:
|
| 455 |
+
f.write(content)
|
| 456 |
+
|
| 457 |
+
return file_path
|
| 458 |
+
|
| 459 |
+
else:
|
| 460 |
+
return None
|
| 461 |
+
|
| 462 |
+
|
| 463 |
+
def finally_proc():
|
| 464 |
+
""" 最終処理用関数 """
|
| 465 |
+
|
| 466 |
+
new_up_file = gr.File(value=None, interactive = True)
|
| 467 |
+
new_file_id = gr.Textbox(value="")
|
| 468 |
+
|
| 469 |
+
return new_up_file, new_file_id
|
| 470 |
+
|
| 471 |
+
|
| 472 |
+
with gr.Blocks() as demo:
|
| 473 |
+
|
| 474 |
+
gr.Markdown("<h2>GPT Code Interpreter対応チャット</h2>")
|
| 475 |
+
|
| 476 |
+
# セッションの宣言
|
| 477 |
+
state = gr.State({
|
| 478 |
+
"system_prompt": SYS_PROMPT_DEFAULT,
|
| 479 |
+
"openai_key" : None,
|
| 480 |
+
"code_mode" : False,
|
| 481 |
+
"client" : None,
|
| 482 |
+
"assistant_id" : None,
|
| 483 |
+
"thread_id" : None,
|
| 484 |
+
"last_msg_id" : ""
|
| 485 |
+
})
|
| 486 |
+
|
| 487 |
+
with gr.Tab("Chat画面") as chat:
|
| 488 |
+
|
| 489 |
+
# 各コンポーネント定義
|
| 490 |
+
chatbot = gr.Chatbot(label="チャット画面")
|
| 491 |
+
text_msg = gr.Textbox(label="テキスト")
|
| 492 |
+
with gr.Row():
|
| 493 |
+
up_file = gr.File(label="ファイルアップロード", type="filepath",interactive = True)
|
| 494 |
+
result_image = gr.Image(label="出力画像", type="filepath", interactive = False)
|
| 495 |
+
gr.Examples(label="サンプルデータ", examples=examples, inputs=[up_file])
|
| 496 |
+
with gr.Row():
|
| 497 |
+
btn = gr.Button(value="送信")
|
| 498 |
+
# btn_download = gr.Button(value="画像のダウンロード") # 保留中
|
| 499 |
+
btn_clear = gr.ClearButton(value="リセット", components=[chatbot, text_msg, up_file, state])
|
| 500 |
+
sys_msg = gr.Textbox(label="システムメッセージ", interactive = False)
|
| 501 |
+
result_file = gr.File(label="出力ファイル", type="filepath",interactive = False)
|
| 502 |
+
|
| 503 |
+
# ���ァイルID保存用
|
| 504 |
+
file_id = gr.Textbox(visible=False)
|
| 505 |
+
|
| 506 |
+
# 送信ボタンクリック時の処理
|
| 507 |
+
bc = btn.click(init, [state, text_msg, up_file], [state, text_msg, up_file, file_id, sys_msg], queue=False).success(
|
| 508 |
+
raise_exception, sys_msg, None).success(
|
| 509 |
+
add_history, [chatbot, text_msg, file_id], [chatbot, text_msg, sys_msg], queue=False).success(
|
| 510 |
+
bot, [state, chatbot, file_id],[chatbot, result_image, result_file, sys_msg]).then(
|
| 511 |
+
finally_proc, None, [up_file, file_id], queue=False
|
| 512 |
+
)
|
| 513 |
+
|
| 514 |
+
# クリア時でもOpenAIKeyなどは再セット
|
| 515 |
+
# btn_clear.click(set_state, [openai_key, system_prompt, code_output, state], state)
|
| 516 |
+
|
| 517 |
+
# テキスト入力Enter時の処理
|
| 518 |
+
# txt_msg = text_msg.submit(respond, inputs=[text_msg, image, chatbot], outputs=[text_msg, image, chatbot])
|
| 519 |
+
|
| 520 |
+
with gr.Tab("設定") as set:
|
| 521 |
+
openai_key = gr.Textbox(label="OpenAI API Key")
|
| 522 |
+
# language = gr.Dropdown(choices=["Japanese", "English"], value = "Japanese", label="Language", interactive = True)
|
| 523 |
+
system_prompt = gr.Textbox(value = SYS_PROMPT_DEFAULT,lines = 5, label="Custom instructions", interactive = True)
|
| 524 |
+
# Enter不使用
|
| 525 |
+
code_output = gr.Dropdown(label="コード出力", choices=["OFF", "ON"], value = "OFF", interactive = True)
|
| 526 |
+
|
| 527 |
+
# 設定タブからChatタブに戻った時の処理
|
| 528 |
+
chat.select(set_state, [openai_key, system_prompt, code_output, state], state)
|
| 529 |
+
|
| 530 |
+
demo.queue()
|
| 531 |
+
|
| 532 |
+
demo.launch(debug=True)
|