Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from editor import setup_editor | |
| from chat import setup_chat | |
| from config import setup_config, load_api_key | |
| from utils import create_default_prompt_files | |
| import os | |
| # デフォルトのプロンプトファイルを作成 | |
| create_default_prompt_files() | |
| # アプリ設定 | |
| st.set_page_config( | |
| page_title="コーディングアシスタント", | |
| page_icon="💻", | |
| layout="centered" # モバイルフレンドリーな中央寄せレイアウト | |
| ) | |
| # APIキーの取得 | |
| api_key = load_api_key() | |
| # セッションの初期化 | |
| if "files" not in st.session_state: | |
| st.session_state.files = {} # ファイル名: コンテンツ | |
| if "current_file" not in st.session_state: | |
| st.session_state.current_file = None | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| if "thinking_process" not in st.session_state: | |
| st.session_state.thinking_process = [] | |
| if "current_mode" not in st.session_state: | |
| st.session_state.current_mode = "通常モード" | |
| if "is_first_message" not in st.session_state: | |
| st.session_state.is_first_message = True | |
| if "mobile_mode" not in st.session_state: | |
| # デバイスの画面幅に基づいて自動判定(JavaScript使用) | |
| st.session_state.mobile_mode = True | |
| if "active_tab" not in st.session_state: | |
| st.session_state.active_tab = "エディタ" | |
| if "last_generated_code" not in st.session_state: | |
| st.session_state.last_generated_code = None | |
| if "ai_edit_mode" not in st.session_state: | |
| st.session_state.ai_edit_mode = False | |
| if "edit_instructions" not in st.session_state: | |
| st.session_state.edit_instructions = "" | |
| # 会話状態の初期化 | |
| if "conversation_state" not in st.session_state: | |
| st.session_state.conversation_state = { | |
| "topic": None, | |
| "last_question_type": None, | |
| "mentioned_entities": set(), | |
| "follow_up_count": 0 | |
| } | |
| # モバイルモードトグル | |
| mobile_mode = st.checkbox("モバイルモード", value=st.session_state.mobile_mode, key="mobile_toggle") | |
| st.session_state.mobile_mode = mobile_mode | |
| # APIキーがない場合の警告表示 | |
| if not api_key: | |
| st.warning(""" | |
| ### ⚠️ GROQ_API_KEYが設定されていません | |
| このアプリを使用するには、GROQ_API_KEYを設定する必要があります: | |
| 1. Hugging Face Spacesの場合: Spacesのダッシュボード -> Settings -> Repository secrets | |
| 2. ローカル環境の場合: .streamlit/secrets.toml または環境変数 | |
| """) | |
| st.stop() | |
| # モバイルモードの場合はタブで切り替え、デスクトップモードの場合は並べて表示 | |
| if st.session_state.mobile_mode: | |
| # サイドバーには最小限の設定のみを表示 | |
| with st.sidebar: | |
| setup_config(compact=True) | |
| # モード選択用のラジオボタン(横並び) | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| editor_selected = st.button("📝 エディタ", | |
| use_container_width=True, | |
| type="primary" if st.session_state.active_tab == "エディタ" else "secondary") | |
| if editor_selected: | |
| st.session_state.active_tab = "エディタ" | |
| st.rerun() | |
| with col2: | |
| chat_selected = st.button("💬 チャット", | |
| use_container_width=True, | |
| type="primary" if st.session_state.active_tab == "チャット" else "secondary") | |
| if chat_selected: | |
| st.session_state.active_tab = "チャット" | |
| st.rerun() | |
| # アクティブなタブに応じてコンテンツを表示 | |
| if st.session_state.active_tab == "エディタ": | |
| setup_editor(api_key=api_key, mobile=True) | |
| else: | |
| setup_chat(api_key=api_key, mobile=True) | |
| else: | |
| # サイドバー設定 | |
| with st.sidebar: | |
| setup_config(compact=False) | |
| # コードエディタとチャットを並べて表示 | |
| col1, col2 = st.columns([1, 1]) | |
| with col1: | |
| setup_editor(api_key=api_key, mobile=False) | |
| with col2: | |
| setup_chat(api_key=api_key, mobile=False) |