Upload 9 files
Browse files
app.py
CHANGED
|
@@ -5,73 +5,41 @@ import json
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from transformers import pipeline
|
| 7 |
import re
|
| 8 |
-
# llama-cpp-python
|
| 9 |
Llama = None
|
| 10 |
import sys
|
| 11 |
import importlib
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
for pkg in pkg_resources.working_set:
|
| 18 |
-
if "llama" in pkg.key.lower():
|
| 19 |
-
print(f"- {pkg.key} ({pkg.version})")
|
| 20 |
-
except Exception as e:
|
| 21 |
-
print(f"パッケージ一覧の取得に失敗: {e}")
|
| 22 |
|
| 23 |
-
#
|
| 24 |
possible_modules = [
|
| 25 |
"llama_cpp",
|
| 26 |
-
"llama_cpp_python"
|
| 27 |
-
"llama",
|
| 28 |
-
"llamacpp",
|
| 29 |
-
"cpp_llama",
|
| 30 |
-
"llama.cpp",
|
| 31 |
-
"llama_cpp.llama",
|
| 32 |
-
"llama_cpp_python.llama_cpp",
|
| 33 |
-
"llama_cpp_python.llama"
|
| 34 |
]
|
| 35 |
|
| 36 |
-
#
|
| 37 |
for module_name in possible_modules:
|
| 38 |
try:
|
| 39 |
-
|
| 40 |
module = importlib.import_module(module_name)
|
| 41 |
-
print(f"成功: {module_name}をインポートしました")
|
| 42 |
|
| 43 |
# モジュール内のLlamaクラスを探す
|
| 44 |
if hasattr(module, "Llama"):
|
| 45 |
Llama = module.Llama
|
| 46 |
-
|
| 47 |
break
|
| 48 |
-
else:
|
| 49 |
-
print(f"{module_name}にLlamaクラスが見つかりません")
|
| 50 |
-
|
| 51 |
-
# モジュールの属性を調べる
|
| 52 |
-
print(f"{module_name}の属性: {dir(module)}")
|
| 53 |
-
|
| 54 |
-
# 'llama'という名前の属性があるか確認
|
| 55 |
-
if hasattr(module, "llama"):
|
| 56 |
-
llama_submodule = getattr(module, "llama")
|
| 57 |
-
if hasattr(llama_submodule, "Llama"):
|
| 58 |
-
Llama = llama_submodule.Llama
|
| 59 |
-
print(f"Llamaクラスを{module_name}.llamaから取得しました")
|
| 60 |
-
break
|
| 61 |
except ImportError:
|
| 62 |
-
|
| 63 |
except Exception as e:
|
| 64 |
-
|
| 65 |
|
| 66 |
# Llamaクラスが見つからなかった場合
|
| 67 |
if Llama is None:
|
| 68 |
-
|
| 69 |
-
print("Pythonパス:")
|
| 70 |
-
for path in sys.path:
|
| 71 |
-
print(f"- {path}")
|
| 72 |
-
|
| 73 |
-
# フォールバックとして、transformersのパイプラインを使用
|
| 74 |
-
print("フォールバックとして、transformersのパイプラインを使用します。")
|
| 75 |
from huggingface_hub import hf_hub_download
|
| 76 |
from generate_dialogue_with_swallow import generate_dialogue_with_swallow
|
| 77 |
|
|
@@ -558,6 +526,49 @@ except FileNotFoundError:
|
|
| 558 |
padding: 15px !important;
|
| 559 |
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1) !important;
|
| 560 |
margin-bottom: 20px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 561 |
}
|
| 562 |
"""
|
| 563 |
|
|
@@ -583,7 +594,7 @@ except Exception as e:
|
|
| 583 |
print(f"テーマカスタマイズエラー: {e}")
|
| 584 |
# エラーが発生した場合はデフォルトのテーマを使用
|
| 585 |
|
| 586 |
-
with gr.Blocks(css=custom_css, theme=custom_theme) as demo:
|
| 587 |
scene_state = gr.State(DEFAULT_SCENE_PARAMS)
|
| 588 |
affection_state = gr.State(30)
|
| 589 |
history_state = gr.State([])
|
|
@@ -647,22 +658,30 @@ with gr.Blocks(css=custom_css, theme=custom_theme) as demo:
|
|
| 647 |
with gr.Column(scale=2):
|
| 648 |
# チャットコンテナ(背景と分離)
|
| 649 |
with gr.Group(elem_id="chat_container", elem_classes="chat-box"):
|
| 650 |
-
# Gradio 5.x用のChatbot
|
| 651 |
chatbot = gr.Chatbot(
|
| 652 |
label="麻理との会話",
|
| 653 |
elem_id="chat_area",
|
| 654 |
-
show_label=
|
| 655 |
-
height=
|
| 656 |
-
#
|
| 657 |
-
#
|
| 658 |
-
avatar_images=
|
| 659 |
"https://cdn.pixabay.com/photo/2016/04/01/10/04/amusing-1299756_1280.png",
|
| 660 |
"https://cdn.pixabay.com/photo/2016/03/31/21/40/bot-1296595_1280.png"
|
| 661 |
-
|
| 662 |
)
|
| 663 |
# 入力欄(背景と分離)
|
| 664 |
with gr.Group(elem_classes="input-box"):
|
| 665 |
-
msg_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 666 |
|
| 667 |
# ステータス部分(右側、背景と分離)
|
| 668 |
with gr.Column(scale=1):
|
|
@@ -678,11 +697,19 @@ with gr.Blocks(css=custom_css, theme=custom_theme) as demo:
|
|
| 678 |
アイコン: <a href="https://pixabay.com" target="_blank">Pixabay</a>
|
| 679 |
</div>
|
| 680 |
""")
|
|
|
|
| 681 |
msg_input.submit(
|
| 682 |
respond,
|
| 683 |
[msg_input, chatbot, affection_state, history_state, scene_state],
|
| 684 |
[msg_input, chatbot, affection_gauge, stage_display, affection_state, history_state, scene_state, background_display]
|
| 685 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 686 |
# 通常の関数として定義
|
| 687 |
def load_stage(affection):
|
| 688 |
return get_relationship_stage(affection)
|
|
@@ -693,10 +720,23 @@ if __name__ == "__main__":
|
|
| 693 |
# Hugging Face Spacesでの実行時の設定
|
| 694 |
if os.getenv("SPACE_ID"):
|
| 695 |
# Hugging Face Spacesでは、サーバーの設定を自動的に行う
|
| 696 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 697 |
else:
|
| 698 |
# ローカル環境での実行時の設定
|
| 699 |
demo.launch(
|
| 700 |
-
|
| 701 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 702 |
)
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from transformers import pipeline
|
| 7 |
import re
|
| 8 |
+
# llama-cpp-pythonのインポート(シンプルに)
|
| 9 |
Llama = None
|
| 10 |
import sys
|
| 11 |
import importlib
|
| 12 |
|
| 13 |
+
# ログファイルを設定
|
| 14 |
+
import logging
|
| 15 |
+
logging.basicConfig(filename='app.log', level=logging.INFO)
|
| 16 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# 可能性のあるモジュール名のリスト(最小限に)
|
| 19 |
possible_modules = [
|
| 20 |
"llama_cpp",
|
| 21 |
+
"llama_cpp_python"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
]
|
| 23 |
|
| 24 |
+
# 各モジュールを試す(最小限の出力)
|
| 25 |
for module_name in possible_modules:
|
| 26 |
try:
|
| 27 |
+
logger.info(f"{module_name}のインポートを試みます...")
|
| 28 |
module = importlib.import_module(module_name)
|
|
|
|
| 29 |
|
| 30 |
# モジュール内のLlamaクラスを探す
|
| 31 |
if hasattr(module, "Llama"):
|
| 32 |
Llama = module.Llama
|
| 33 |
+
logger.info(f"Llamaクラスを{module_name}から取得しました")
|
| 34 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except ImportError:
|
| 36 |
+
logger.info(f"{module_name}のインポートに失敗しました")
|
| 37 |
except Exception as e:
|
| 38 |
+
logger.error(f"{module_name}のインポート中にエラーが発生しました: {e}")
|
| 39 |
|
| 40 |
# Llamaクラスが見つからなかった場合
|
| 41 |
if Llama is None:
|
| 42 |
+
logger.warning("Llamaクラスが見つかりませんでした。フォールバックを使用します。")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
from huggingface_hub import hf_hub_download
|
| 44 |
from generate_dialogue_with_swallow import generate_dialogue_with_swallow
|
| 45 |
|
|
|
|
| 526 |
padding: 15px !important;
|
| 527 |
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1) !important;
|
| 528 |
margin-bottom: 20px !important;
|
| 529 |
+
min-height: 400px !important;
|
| 530 |
+
max-height: 600px !important;
|
| 531 |
+
overflow-y: auto !important;
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
+
/* 送信ボタンのスタイル */
|
| 535 |
+
button.primary {
|
| 536 |
+
background-color: #ff6b6b !important;
|
| 537 |
+
color: white !important;
|
| 538 |
+
border-radius: 8px !important;
|
| 539 |
+
padding: 10px 20px !important;
|
| 540 |
+
font-weight: bold !important;
|
| 541 |
+
margin-top: 10px !important;
|
| 542 |
+
width: 100% !important;
|
| 543 |
+
max-width: 200px !important;
|
| 544 |
+
}
|
| 545 |
+
|
| 546 |
+
/* 入力欄のスタイル */
|
| 547 |
+
.input-box textarea {
|
| 548 |
+
border: 2px solid #ff6b6b !important;
|
| 549 |
+
border-radius: 8px !important;
|
| 550 |
+
padding: 10px !important;
|
| 551 |
+
font-size: 16px !important;
|
| 552 |
+
width: 100% !important;
|
| 553 |
+
}
|
| 554 |
+
|
| 555 |
+
/* チャットメッセージのスタイル */
|
| 556 |
+
.message {
|
| 557 |
+
margin-bottom: 10px !important;
|
| 558 |
+
padding: 10px !important;
|
| 559 |
+
border-radius: 8px !important;
|
| 560 |
+
}
|
| 561 |
+
|
| 562 |
+
/* ユーザーメッセージ */
|
| 563 |
+
.user-message {
|
| 564 |
+
background-color: rgba(230, 230, 255, 0.95) !important;
|
| 565 |
+
border-left: 4px solid #6c5ce7 !important;
|
| 566 |
+
}
|
| 567 |
+
|
| 568 |
+
/* ボットメッセージ */
|
| 569 |
+
.bot-message {
|
| 570 |
+
background-color: rgba(255, 230, 230, 0.95) !important;
|
| 571 |
+
border-left: 4px solid #ff6b6b !important;
|
| 572 |
}
|
| 573 |
"""
|
| 574 |
|
|
|
|
| 594 |
print(f"テーマカスタマイズエラー: {e}")
|
| 595 |
# エラーが発生した場合はデフォルトのテーマを使用
|
| 596 |
|
| 597 |
+
with gr.Blocks(css=custom_css, theme=custom_theme, title="麻理チャット") as demo:
|
| 598 |
scene_state = gr.State(DEFAULT_SCENE_PARAMS)
|
| 599 |
affection_state = gr.State(30)
|
| 600 |
history_state = gr.State([])
|
|
|
|
| 658 |
with gr.Column(scale=2):
|
| 659 |
# チャットコンテナ(背景と分離)
|
| 660 |
with gr.Group(elem_id="chat_container", elem_classes="chat-box"):
|
| 661 |
+
# Gradio 5.x用のChatbot設定(シンプルな設定に変更)
|
| 662 |
chatbot = gr.Chatbot(
|
| 663 |
label="麻理との会話",
|
| 664 |
elem_id="chat_area",
|
| 665 |
+
show_label=True, # ラベルを表示
|
| 666 |
+
height=500, # 高さを増やす
|
| 667 |
+
bubble=True, # バブルスタイルを使用
|
| 668 |
+
# アバター画像を設定
|
| 669 |
+
avatar_images=(
|
| 670 |
"https://cdn.pixabay.com/photo/2016/04/01/10/04/amusing-1299756_1280.png",
|
| 671 |
"https://cdn.pixabay.com/photo/2016/03/31/21/40/bot-1296595_1280.png"
|
| 672 |
+
)
|
| 673 |
)
|
| 674 |
# 入力欄(背景と分離)
|
| 675 |
with gr.Group(elem_classes="input-box"):
|
| 676 |
+
msg_input = gr.Textbox(
|
| 677 |
+
label="あなたのメッセージ",
|
| 678 |
+
placeholder="「水族館はどう?」と聞いた後、「いいね、行こう!」のように返してみてください",
|
| 679 |
+
show_label=False,
|
| 680 |
+
lines=2,
|
| 681 |
+
max_lines=5
|
| 682 |
+
)
|
| 683 |
+
# 送信ボタンを明示的に追加
|
| 684 |
+
submit_btn = gr.Button("送信", variant="primary")
|
| 685 |
|
| 686 |
# ステータス部分(右側、背景と分離)
|
| 687 |
with gr.Column(scale=1):
|
|
|
|
| 697 |
アイコン: <a href="https://pixabay.com" target="_blank">Pixabay</a>
|
| 698 |
</div>
|
| 699 |
""")
|
| 700 |
+
# 入力欄のEnterキーと送信ボタンの両方にイベントハンドラを設定
|
| 701 |
msg_input.submit(
|
| 702 |
respond,
|
| 703 |
[msg_input, chatbot, affection_state, history_state, scene_state],
|
| 704 |
[msg_input, chatbot, affection_gauge, stage_display, affection_state, history_state, scene_state, background_display]
|
| 705 |
)
|
| 706 |
+
|
| 707 |
+
# 送信ボタンにも同じイベントハンドラを設定
|
| 708 |
+
submit_btn.click(
|
| 709 |
+
respond,
|
| 710 |
+
[msg_input, chatbot, affection_state, history_state, scene_state],
|
| 711 |
+
[msg_input, chatbot, affection_gauge, stage_display, affection_state, history_state, scene_state, background_display]
|
| 712 |
+
)
|
| 713 |
# 通常の関数として定義
|
| 714 |
def load_stage(affection):
|
| 715 |
return get_relationship_stage(affection)
|
|
|
|
| 720 |
# Hugging Face Spacesでの実行時の設定
|
| 721 |
if os.getenv("SPACE_ID"):
|
| 722 |
# Hugging Face Spacesでは、サーバーの設定を自動的に行う
|
| 723 |
+
demo.launch(
|
| 724 |
+
server_name="0.0.0.0",
|
| 725 |
+
share=False,
|
| 726 |
+
debug=False,
|
| 727 |
+
show_api=False,
|
| 728 |
+
show_error=False,
|
| 729 |
+
quiet=True,
|
| 730 |
+
favicon_path="https://cdn.pixabay.com/photo/2016/03/31/21/40/bot-1296595_1280.png"
|
| 731 |
+
)
|
| 732 |
else:
|
| 733 |
# ローカル環境での実行時の設定
|
| 734 |
demo.launch(
|
| 735 |
+
server_name="0.0.0.0",
|
| 736 |
+
share=False,
|
| 737 |
+
debug=True,
|
| 738 |
+
show_api=False,
|
| 739 |
+
show_error=True,
|
| 740 |
+
quiet=False,
|
| 741 |
+
favicon_path="https://cdn.pixabay.com/photo/2016/03/31/21/40/bot-1296595_1280.png"
|
| 742 |
)
|