sirochild commited on
Commit
32a2e31
·
verified ·
1 Parent(s): 733f37f

Upload 2 files

Browse files
Files changed (2) hide show
  1. main_app.py +19 -21
  2. session_api_server.py +12 -0
main_app.py CHANGED
@@ -141,37 +141,35 @@ if 'server_started' not in st.session_state:
141
  logger.warning("⚠️ セッション管理サーバー起動失敗 - フォールバックモードで動作")
142
 
143
 
144
- SPACE_URL = "https://huggingface.co/spaces/sirochild/mari-chat-3"
145
- LOGIN_URL = f"{SPACE_URL}/oauth/huggingface/login"
146
- ME_URL = f"{SPACE_URL}/me"
147
  def get_current_user_id():
148
  # --- セッションステートにユーザー情報を保存 ---
149
  if "user" not in st.session_state:
150
  st.session_state.user = None
151
 
152
- # --- ログインしてなければボタンを表示 ---
153
- if st.session_state.user is None:
154
- st.markdown(f"[👉 Hugging Faceでログイン]({LOGIN_URL})")
155
-
156
- else:
157
- st.success(f"ログイン中: {st.session_state.user['preferred_username']}")
158
- st.json(st.session_state.user)
159
-
160
- # --- クエリパラメータに code があれば認証処理 ---
161
- query_params = st.query_params
162
- if "code" in query_params:
163
- code = query_params["code"]
164
- # /me にアクセスしてユーザー情報を取得
165
  try:
166
- resp = requests.get(ME_URL, cookies={"hf_oauth_code": code})
167
- if resp.status_code == 200:
168
- st.session_state.user = resp.json()
169
- st.experimental_rerun()
 
170
  else:
171
- st.error("認証失敗しました")
172
  except Exception as e:
173
  st.error(f"エラー: {e}")
174
 
 
 
 
 
 
 
 
 
175
  # --- 必要なモジュールのインポート ---
176
 
177
  # << 麻理チャット用モジュール >>
 
141
  logger.warning("⚠️ セッション管理サーバー起動失敗 - フォールバックモードで動作")
142
 
143
 
144
+ API_URL = "http://localhost:8000"
145
+ LOGIN_URL = f"{API_URL}/oauth/huggingface/login"
146
+ ME_URL = f"{API_URL}/me"
147
  def get_current_user_id():
148
  # --- セッションステートにユーザー情報を保存 ---
149
  if "user" not in st.session_state:
150
  st.session_state.user = None
151
 
152
+ # セッション開始時に /me を自動でチェック
153
+ if "user_info" not in st.session_state:
 
 
 
 
 
 
 
 
 
 
 
154
  try:
155
+ res = requests.get(ME_URL, cookies=dict(st.session_state.get("cookies", {})))
156
+ data = res.json()
157
+ if "username" in data:
158
+ st.session_state["user_info"] = data
159
+ st.success(f"ログインしました: {data['username']}")
160
  else:
161
+ st.warning("まだログインしていません。")
162
  except Exception as e:
163
  st.error(f"エラー: {e}")
164
 
165
+ # ログイン済みなら表示
166
+ if "user_info" in st.session_state:
167
+ st.write(st.session_state["user_info"])
168
+
169
+ # --- ログインしてなければボタンを表示 ---
170
+ if st.session_state.user is None:
171
+ st.markdown(f"[👉 Hugging Faceでログイン]({LOGIN_URL})")
172
+
173
  # --- 必要なモジュールのインポート ---
174
 
175
  # << 麻理チャット用モジュール >>
session_api_server.py CHANGED
@@ -50,6 +50,18 @@ def me(request: Request):
50
  "email": oauth_info.user_info.email,
51
  }
52
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  class SessionManager:
55
  """セッション管理クラス"""
 
50
  "email": oauth_info.user_info.email,
51
  }
52
 
53
+ # ログイン後に /me へ自動遷移させる
54
+ @app.get("/oauth/callback")
55
+ async def callback(request: Request):
56
+ oauth_info = parse_huggingface_oauth(request)
57
+ if oauth_info is None:
58
+ return RedirectResponse(url="/?error=login_failed")
59
+
60
+ # セッション cookie を発行して Streamlit から /me で取れるようにする
61
+ response = RedirectResponse(url="http://localhost:8501/?logged_in=1")
62
+ response.set_cookie(key="session", value=oauth_info.json(), httponly=True)
63
+ return response
64
+
65
 
66
  class SessionManager:
67
  """セッション管理クラス"""