File size: 9,132 Bytes
70766d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14015fd
70766d2
 
 
 
14015fd
70766d2
 
 
 
 
 
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import gradio as gr
import importlib
import os
import sys
import traceback
from typing import List, Tuple, Dict, Any

def create_simple_interfaces() -> Tuple[List[Any], List[str]]:
    """
    シンプルなテスト用インターフェースを作成
    """
    interfaces = []
    names = []
    
    # 1. テキスト処理インターフェース
    def text_processor(text):
        return f"処理結果: {text.upper()}"
    
    text_interface = gr.Interface(
        fn=text_processor,
        inputs=gr.Textbox(label="テキスト入力", placeholder="何か入力してください"),
        outputs=gr.Textbox(label="処理結果"),
        title="テキスト処理",
        description="入力されたテキストを大文字に変換します"
    )
    
    # 2. 計算機インターフェース
    def calculator(num1, operation, num2):
        try:
            if operation == "足し算":
                result = num1 + num2
            elif operation == "引き算":
                result = num1 - num2
            elif operation == "掛け算":
                result = num1 * num2
            elif operation == "割り算":
                result = num1 / num2 if num2 != 0 else "エラー: ゼロ除算"
            else:
                result = "不明な演算"
            return f"{num1} {operation} {num2} = {result}"
        except Exception as e:
            return f"エラー: {str(e)}"
    
    calc_interface = gr.Interface(
        fn=calculator,
        inputs=[
            gr.Number(label="数値1", value=0),
            gr.Dropdown(["足し算", "引き算", "掛け算", "割り算"], label="演算"),
            gr.Number(label="数値2", value=0)
        ],
        outputs=gr.Textbox(label="計算結果"),
        title="簡単計算機",
        description="2つの数値で四則演算を行います"
    )
    
    # 3. ファイル情報表示インターフェース
    def file_info(file):
        if file is None:
            return "ファイルが選択されていません"
        
        file_path = file.name
        file_size = os.path.getsize(file_path)
        file_name = os.path.basename(file_path)
        
        return f"""
        ファイル名: {file_name}
        ファイルサイズ: {file_size} bytes
        ファイルパス: {file_path}
        """
    
    file_interface = gr.Interface(
        fn=file_info,
        inputs=gr.File(label="ファイルを選択"),
        outputs=gr.Textbox(label="ファイル情報"),
        title="ファイル情報表示",
        description="アップロードされたファイルの情報を表示します"
    )
    
    interfaces = [text_interface, calc_interface, file_interface]
    names = ["📝 テキスト処理", "🧮 計算機", "📁 ファイル情報"]
    
    return interfaces, names

def load_working_contbk_interfaces() -> Tuple[List[Any], List[str]]:
    """
    動作確認済みのcontbkインターフェースのみを読み込み
    """
    interfaces = []
    names = []
    
    # 動作確認済みのインターフェースリスト
    working_modules = [
        ("gra_09_weather.weather", "🌤️ 天気予報"),
        ("gra_11_multimodal.image_to_ui", "🖼️ マルチモーダル"),
        ("gra_10_frontend.frontend_generator", "🎨 フロントエンド生成"),
    ]
    
    # パスを追加
    contbk_path = "/workspaces/fastapi_django_main_live/contbk"
    main_path = "/workspaces/fastapi_django_main_live"
    
    if contbk_path not in sys.path:
        sys.path.insert(0, contbk_path)
    if main_path not in sys.path:
        sys.path.insert(0, main_path)
    
    for module_name, display_name in working_modules:
        try:
            print(f"🔍 Loading {module_name}...")
            module = importlib.import_module(module_name)
            
            if hasattr(module, 'gradio_interface'):
                interfaces.append(module.gradio_interface)
                names.append(display_name)
                print(f"✅ Successfully loaded: {display_name}")
            else:
                print(f"⚠️ No gradio_interface found in {module_name}")
                
        except Exception as e:
            print(f"❌ Failed to load {module_name}: {str(e)}")
            continue
    
    return interfaces, names

def create_welcome_tab() -> gr.Blocks:
    """ウェルカムタブを作成"""
    with gr.Blocks() as welcome:
        gr.Markdown("""
        # 🎯 ContBK インターフェース ダッシュボード
        
        このダッシュボードでは、`contbk`フォルダーにある全ての Gradio インターフェースにアクセスできます。
        
        ## 📋 利用可能な機能:
        
        各タブには以下のような機能が含まれています:
        
        ### 🔧 基本機能:
        - **📝 テキスト処理**: テキストの変換・処理
        - **🧮 計算機**: 基本的な四則演算
        - **📁 ファイル情報**: ファイル情報の表示
        
        ### 🚀 高度な機能 (contbkから):
        - **🌤️ 天気予報**: 天気情報の取得・表示
        - **🖼️ マルチモーダル**: 画像とテキストの処理
        - **🎥 ビデオ処理**: 動画ファイルの処理
        - **🎨 フロントエンド生成**: UIコードの自動生成
        
        ## 🚀 使い方:
        1. 上部のタブから使いたい機能を選択
        2. 各タブの指示に従って操作
        3. 必要に応じて設定やパラメータを調整
        
        ## 📞 サポート:
        - 各機能の詳細は対応するタブで確認できます
        - 問題が発生した場合は、エラーメッセージを確認してください
        """)
        
        # システム情報を表示
        with gr.Accordion("🔧 システム情報", open=False):
            gr.Markdown(f"""
            **Python バージョン**: {sys.version}
            **ContBK パス**: /workspaces/fastapi_django_main_live/contbk
            **現在時刻**: {__import__('datetime').datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
            """)
    
    return welcome

def create_error_tab(error_message: str) -> gr.Blocks:
    """エラータブを作成"""
    with gr.Blocks() as error_tab:
        gr.Markdown(f"""
        # ❌ エラーが発生しました
        
        ```
        {error_message}
        ```
        
        ## 🔧 トラブルシューティング:
        1. contbkフォルダーが存在することを確認
        2. 各モジュールが正しくインストールされていることを確認
        3. Pythonパスが正しく設定されていることを確認
        """)
    return error_tab

def create_tabbed_interface() -> gr.TabbedInterface:
    """
    シンプル機能とcontbkフォルダーのインターフェースを統合したタブ表示を作成
    """
    try:
        # ウェルカムタブ
        welcome_tab = create_welcome_tab()
        
        # シンプルなインターフェース
        simple_interfaces, simple_names = create_simple_interfaces()
        
        # 動作するcontbkインターフェース
        contbk_interfaces, contbk_names = load_working_contbk_interfaces()
        
        # 全て統合
        all_interfaces = [welcome_tab] + simple_interfaces + contbk_interfaces
        all_names = ["🏠 ホーム"] + simple_names + contbk_names
        
        if len(all_interfaces) == 1:  # ウェルカムタブのみの場合
            error_tab = create_error_tab("インターフェースの読み込みに失敗しました。")
            all_interfaces.append(error_tab)
            all_names.append("❌ エラー")
        
        # タブ付きインターフェースを作成
        tabs = gr.TabbedInterface(
            all_interfaces,
            all_names,
            title="🎯 ContBK ダッシュボード"
        )
        
        print(f"📊 Total tabs created: {len(all_interfaces)}")
        return tabs
        
    except Exception as e:
        print(f"❌ Failed to create tabbed interface: {str(e)}")
        traceback.print_exc()
        
        # エラーの場合、基本的なインターフェースを返す
        error_tab = create_error_tab(str(e))
        welcome_tab = create_welcome_tab()
        
        return gr.TabbedInterface(
            [welcome_tab, error_tab],
            ["🏠 ホーム", "❌ エラー"],
            title="🎯 ContBK ダッシュボード (エラー)"
        )

# メインのgradio_interfaceを作成
# gradio_interface = create_tabbed_interface()  # 無効化:重複を防ぐため

# スタンドアロン実行用(テスト用)
if __name__ == "__main__":
    print("🚀 ContBK ダッシュボードを起動中...")
    gradio_interface = create_tabbed_interface()  # テスト実行時のみ
    gradio_interface.launch(
        server_name="0.0.0.0",
        server_port=7863,  # 別のポートを使用
        share=False,
        debug=True
    )