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