starsnatched commited on
Commit
97678da
·
unverified ·
2 Parent(s): 3aa6b31 e71d6ee

Merge pull request #87 from starsnatched/codex/fix-login-functionality-for-huggingface-spaces

Browse files
Files changed (1) hide show
  1. frontend.py +45 -10
frontend.py CHANGED
@@ -3,6 +3,8 @@
3
  from __future__ import annotations
4
 
5
  import asyncio
 
 
6
  from typing import Iterator, List, Optional
7
  import typing
8
 
@@ -25,6 +27,19 @@ def hf_login(token: str) -> str:
25
  return info.get("name") or info.get("email")
26
 
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  # ---------------------------------------------------------------------------
29
  # File helpers
30
  # ---------------------------------------------------------------------------
@@ -117,20 +132,35 @@ def remove_session(user: str, session: str) -> None:
117
  # ---------------------------------------------------------------------------
118
 
119
  def build_ui() -> gr.Blocks:
 
 
 
 
 
 
 
 
 
 
120
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
121
- user_state = gr.State(str)
122
  session_state = gr.State("default")
123
  history_state = gr.State([])
124
 
125
- with gr.Column() as login_col:
126
  gr.Markdown("## HuggingFace Login")
127
  token_box = gr.Textbox(type="password", label="HuggingFace token")
128
  login_btn = gr.Button("Login")
129
  login_status = gr.Markdown()
130
 
131
- with gr.Row(visible=False) as main_row:
132
  with gr.Column(scale=3):
133
- session_drop = gr.Dropdown(label="Session", interactive=True)
 
 
 
 
 
134
  new_session = gr.Textbox(label="New Session Name")
135
  create_btn = gr.Button("Create Session")
136
  chatbox = gr.Chatbot(type="messages")
@@ -151,23 +181,28 @@ def build_ui() -> gr.Blocks:
151
  def do_login(token: str):
152
  user = hf_login(token)
153
  sessions = load_sessions(user)
154
- return {user_state: user, session_drop: gr.Dropdown.update(choices=sessions), login_col: gr.Column.update(visible=False), main_row: gr.Row.update(visible=True)}
 
 
 
 
 
155
 
156
  login_btn.click(do_login, inputs=token_box, outputs=[user_state, session_drop, login_col, main_row])
157
 
158
  def create_session(user: str, name: str):
159
  if not name:
160
- return gr.Dropdown.update()
161
  reset_history(user, name)
162
  sessions = load_sessions(user)
163
- return gr.Dropdown.update(value=name, choices=sessions)
164
 
165
  create_btn.click(create_session, inputs=[user_state, new_session], outputs=session_drop)
166
 
167
  def refresh_files(user: str, path: str):
168
  entries = list_directory(user, path)
169
  data = [[e["name"], e["is_dir"]] for e in entries]
170
- return {file_list: gr.Dataframe.update(value=data), dir_path: path}
171
 
172
  refresh_btn.click(refresh_files, inputs=[user_state, dir_path], outputs=[file_list, dir_path])
173
 
@@ -179,13 +214,13 @@ def build_ui() -> gr.Blocks:
179
 
180
  def save_file(user: str, path: str, content: str):
181
  write_file(user, path, content)
182
- return gr.Textbox.update()
183
 
184
  save_btn.click(save_file, inputs=[user_state, open_path, file_editor], outputs=file_editor)
185
 
186
  def delete_file(user: str, path: str):
187
  delete_path(user, path)
188
- return gr.Textbox.update(value="")
189
 
190
  delete_btn.click(delete_file, inputs=[user_state, open_path], outputs=open_path)
191
 
 
3
  from __future__ import annotations
4
 
5
  import asyncio
6
+ import os
7
+ from pathlib import Path
8
  from typing import Iterator, List, Optional
9
  import typing
10
 
 
27
  return info.get("name") or info.get("email")
28
 
29
 
30
+ def get_env_token() -> str | None:
31
+ """Return an available HuggingFace token from environment variables."""
32
+ for name in (
33
+ "HF_TOKEN",
34
+ "HF_SPACE_TOKEN",
35
+ "HF_API_TOKEN",
36
+ "HF_ACCESS_TOKEN",
37
+ ):
38
+ if token := os.getenv(name):
39
+ return token
40
+ return None
41
+
42
+
43
  # ---------------------------------------------------------------------------
44
  # File helpers
45
  # ---------------------------------------------------------------------------
 
132
  # ---------------------------------------------------------------------------
133
 
134
  def build_ui() -> gr.Blocks:
135
+ token = get_env_token()
136
+ auto_user: str | None = None
137
+ auto_sessions: list[str] = []
138
+ if token:
139
+ try:
140
+ auto_user = hf_login(token)
141
+ auto_sessions = load_sessions(auto_user)
142
+ except Exception as exc: # pragma: no cover - best effort
143
+ print(f"Automatic login failed: {exc}")
144
+
145
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
146
+ user_state = gr.State(auto_user or str)
147
  session_state = gr.State("default")
148
  history_state = gr.State([])
149
 
150
+ with gr.Column(visible=auto_user is None) as login_col:
151
  gr.Markdown("## HuggingFace Login")
152
  token_box = gr.Textbox(type="password", label="HuggingFace token")
153
  login_btn = gr.Button("Login")
154
  login_status = gr.Markdown()
155
 
156
+ with gr.Row(visible=auto_user is not None) as main_row:
157
  with gr.Column(scale=3):
158
+ session_drop = gr.Dropdown(
159
+ label="Session",
160
+ interactive=True,
161
+ choices=auto_sessions,
162
+ value=auto_sessions[0] if auto_sessions else None,
163
+ )
164
  new_session = gr.Textbox(label="New Session Name")
165
  create_btn = gr.Button("Create Session")
166
  chatbox = gr.Chatbot(type="messages")
 
181
  def do_login(token: str):
182
  user = hf_login(token)
183
  sessions = load_sessions(user)
184
+ return {
185
+ user_state: user,
186
+ session_drop: gr.update(choices=sessions),
187
+ login_col: gr.update(visible=False),
188
+ main_row: gr.update(visible=True),
189
+ }
190
 
191
  login_btn.click(do_login, inputs=token_box, outputs=[user_state, session_drop, login_col, main_row])
192
 
193
  def create_session(user: str, name: str):
194
  if not name:
195
+ return gr.update()
196
  reset_history(user, name)
197
  sessions = load_sessions(user)
198
+ return gr.update(value=name, choices=sessions)
199
 
200
  create_btn.click(create_session, inputs=[user_state, new_session], outputs=session_drop)
201
 
202
  def refresh_files(user: str, path: str):
203
  entries = list_directory(user, path)
204
  data = [[e["name"], e["is_dir"]] for e in entries]
205
+ return {file_list: gr.update(value=data), dir_path: path}
206
 
207
  refresh_btn.click(refresh_files, inputs=[user_state, dir_path], outputs=[file_list, dir_path])
208
 
 
214
 
215
  def save_file(user: str, path: str, content: str):
216
  write_file(user, path, content)
217
+ return gr.update()
218
 
219
  save_btn.click(save_file, inputs=[user_state, open_path, file_editor], outputs=file_editor)
220
 
221
  def delete_file(user: str, path: str):
222
  delete_path(user, path)
223
+ return gr.update(value="")
224
 
225
  delete_btn.click(delete_file, inputs=[user_state, open_path], outputs=open_path)
226