File size: 6,065 Bytes
d548975
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55cbdc2
 
 
 
 
d548975
55cbdc2
 
 
 
 
 
 
 
 
d548975
55cbdc2
d548975
55cbdc2
 
 
 
 
 
d548975
 
 
 
 
 
 
 
 
 
 
 
 
70766d2
d548975
70766d2
 
 
14015fd
 
55cbdc2
 
14015fd
 
 
70766d2
 
 
 
55cbdc2
 
 
70766d2
 
55cbdc2
70766d2
 
 
 
 
 
 
 
 
 
 
 
 
 
d548975
 
70766d2
d548975
70766d2
d548975
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import shutil
import gradio as gr
#from mysite.libs.utilities import chat_with_interpreter, completion, process_file
#from interpreter import interpreter
#import mysite.interpreter.interpreter_config  # インポートするだけで設定が適用されます
import importlib
import os
import pkgutil
#from babyagi.babyagi import gradio_babyagi
#from routers.gra_02_openInterpreter.OpenInterpreter import gradio_interface
#from llamafactory.webui.interface import create_ui
import importlib
import os
import pkgutil

import importlib
import os
import pkgutil
import traceback

def include_gradio_interfaces():
    gradio_interfaces = {}  # 辞書型: { interface_name: gradio_interface }
    
    # 検索対象ディレクトリを指定(ContBKは統合ダッシュボードで表示するため除外)
    search_dirs = [
        ("controllers", "controllers"),  # メインのcontrollersディレクトリのみ
    ]
    
    package_paths = []
    
    # 各検索ディレクトリをスキャン
    for package_dir, module_prefix in search_dirs:
        if os.path.exists(package_dir):
            print(f"📂 Scanning directory: {package_dir}")
            for root, dirs, files in os.walk(package_dir):
                if "__pycache__" in root:  # `__pycache__` を除外
                    continue
                package_paths.append((root, module_prefix))

    for package_path, module_prefix in package_paths:
        # パッケージの Python モジュールを取得
        rel_path = os.path.relpath(package_path, module_prefix.split('.')[0] if '.' in module_prefix else module_prefix)
        
        if rel_path == ".":
            package_name = module_prefix
        else:
            package_name = module_prefix + "." + rel_path.replace(os.sep, ".")

        for module_info in pkgutil.iter_modules([package_path]):
            sub_module_name = f"{package_name}.{module_info.name}"
            print(f"Trying to import {sub_module_name}")

            try:
                module = importlib.import_module(sub_module_name)
                print(f"Successfully imported {sub_module_name}")

                # `gradio_interface` を持つモジュールのみ追加
                if hasattr(module, "gradio_interface"):
                    print(f"Found gradio_interface in {sub_module_name}")

                    # 美しいタイトルを生成(絵文字付き)
                    base_name = module_info.name
                    
                    # 特定のモジュールに対する美しいタイトルマッピング
                    title_mapping = {
                        'conversation_history': '💬 会話履歴管理',
                        'conversation_logger': '📝 会話ログ',
                        'conversation_demo': '🎯 会話履歴統合デモ',
                        'contbk_unified_dashboard': '🎯 ContBK統合ダッシュボード',
                        # 'contbk_example': '🎯 ContBK ダッシュボード',  # 無効化済み
                        # 'contbk_dashboard': '📊 ContBK 統合',  # 無効化済み
                        # 'example_gradio_interface': '🔧 サンプル',  # 無効化済み
                        'hasura': '🗄️ Hasura API',
                        'Chat': '💬 チャット',
                        'OpenInterpreter': '🤖 AI インタープリター',
                        'programfromdoc': '📄 ドキュメント生成',
                        'gradio_interface': '🚀 AI開発プラットフォーム',
                        'lavelo': '💾 プロンプト管理システム',
                        'rides': '🚗 データベース管理',
                        'files': '📁 ファイル管理',
                        'gradio': '🌐 HTML表示',
                        'rpa_automation': '🤖 RPA自動化システム',
                    }
                    
                    # モジュールにtitle属性があるかチェック
                    if hasattr(module, 'interface_title'):
                        display_name = module.interface_title
                    elif base_name in title_mapping:
                        display_name = title_mapping[base_name]
                    else:
                        # デフォルトの美しいタイトル生成
                        formatted_name = base_name.replace('_', ' ').title()
                        display_name = f"✨ {formatted_name}"

                    # 名前の一意性を保証する処理
                    unique_name = display_name
                    count = 1

                    # 重複がある場合は番号を付与
                    while unique_name in gradio_interfaces:
                        unique_name = f"{display_name} ({count})"
                        count += 1

                    gradio_interfaces[unique_name] = module.gradio_interface
            except ModuleNotFoundError as e:
                print(f"ModuleNotFoundError: {sub_module_name} - {e}")
            except AttributeError as e:
                print(f"AttributeError in {sub_module_name}: {e}")
            except Exception as e:
                print(f"Failed to import {sub_module_name}: {e}")
                print(traceback.format_exc())

    # 名前とインターフェースのリストを返す
    print(f"Collected Gradio Interfaces: {list(gradio_interfaces.keys())}")
    return list(gradio_interfaces.values()), list(gradio_interfaces.keys())


def setup_gradio_interfaces():

    ##
    #from routers.gra_06_video.video import gradio_interface as video
    default_interfaces = []#,demo]
    default_names = ["CreateTASK","Chat","OpenInterpreter","DataBase","CreateFromDOC","HTML","FILES"]#"demo"]

    gradio_interfaces, gradio_names = include_gradio_interfaces()

    all_interfaces = gradio_interfaces
    all_names = gradio_names

    tabs = gr.TabbedInterface(all_interfaces, all_names)
    tabs.queue()
    return tabs
if __name__ == "__main__":
    interfaces, names = include_gradio_interfaces()