wuhp commited on
Commit
b2c5c54
Β·
verified Β·
1 Parent(s): c9b9839

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -76
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import os, json
2
  import gradio as gr
3
  from huggingface_hub import (
4
  create_repo, list_models, upload_file, list_repo_files, constants
@@ -21,7 +21,13 @@ def list_private_models(profile, oauth_token):
21
  ]
22
  return "No models found." if not models else "Models:\n\n" + "\n - ".join(models)
23
 
24
- # β€” HF SPACE HELPERS β€” #
 
 
 
 
 
 
25
 
26
  def create_space(repo_name, sdk, profile, oauth_token):
27
  if not (profile and oauth_token):
@@ -45,9 +51,9 @@ def fetch_logs(repo_id, profile, oauth_token, level):
45
  if not (profile and oauth_token and repo_id):
46
  return "⚠️ Log in & create a Space first."
47
  # get JWT
48
- r = get_session().get(f"{constants.ENDPOINT}/api/spaces/{repo_id}/jwt",
49
- headers=build_hf_headers())
50
- hf_raise_for_status(r); jwt = r.json()["token"]
51
  url = f"https://api.hf.space/v1/{repo_id}/logs/{level}"
52
  lines = []
53
  with get_session().get(url, headers=build_hf_headers(token=jwt), stream=True) as resp:
@@ -63,7 +69,7 @@ def list_files(repo_id, profile, oauth_token):
63
  return "⚠️ Log in & create a Space first."
64
  return "\n".join(list_repo_files(repo_id, token=oauth_token.token, repo_type="space"))
65
 
66
- # β€” GEMINI FUNCTION DECLS β€” #
67
 
68
  func_decls = [
69
  {
@@ -106,7 +112,7 @@ func_decls = [
106
  # β€” CHAT HANDLER β€” #
107
 
108
  def process_message(profile, oauth_token, user_msg, gemini_key, repo_name, sdk, chat_history, session):
109
- # init on first call
110
  if session.get("chat") is None:
111
  client = genai.Client(api_key=gemini_key)
112
  cfg = types.GenerateContentConfig(
@@ -142,7 +148,7 @@ def process_message(profile, oauth_token, user_msg, gemini_key, repo_name, sdk,
142
  else:
143
  result = {"log": f"⚠️ Unknown function {name}"}
144
 
145
- # feed back into chat
146
  chat.send_message(
147
  types.Content(
148
  role="function",
@@ -162,8 +168,9 @@ def process_message(profile, oauth_token, user_msg, gemini_key, repo_name, sdk,
162
  chat_history[-1] = (user_msg, final)
163
  return chat_history, session.get("iframe",""), session.get("log",""), session.get("files",""), session
164
 
 
 
165
  def sync_manual(profile, oauth_token, session):
166
- # pull in any manual file changes
167
  if not (profile and oauth_token and session.get("repo_id")):
168
  return session.get("iframe",""), "⚠️ Cannot sync.", session.get("files",""), session
169
  files = list_files(session["repo_id"], profile, oauth_token)
@@ -171,112 +178,83 @@ def sync_manual(profile, oauth_token, session):
171
  session["log"] = "πŸ”„ Manual changes synced."
172
  return session.get("iframe",""), session["log"], session["files"], session
173
 
174
- # β€” LAYOUT β€” #
175
 
176
  with gr.Blocks(css="""
177
- #sidebar {background:#f7f7f7;padding:1em;border-right:1px solid #ddd;}
178
- #main {padding:1em;}
179
  """) as demo:
180
-
181
  with gr.Row():
182
  # Sidebar
183
  with gr.Column(elem_id="sidebar", scale=1):
184
  gr.Markdown("### πŸ”‘ Login & Config")
185
- login_btn = gr.LoginButton(variant="huggingface", size="sm")
186
- profile_state = gr.State(None)
187
- token_state = gr.State(None)
188
- # capture login outputs
189
- login_btn.click(None, outputs=[profile_state, token_state])
190
-
191
- status_md = gr.Markdown("*Not logged in.*")
192
- profile_state.change(show_profile, inputs=[profile_state], outputs=[status_md])
193
- login_btn.click(show_profile, inputs=[profile_state], outputs=[status_md])
194
-
195
- models_md = gr.Markdown()
196
- profile_state.change(list_private_models,
197
- inputs=[profile_state, token_state],
198
- outputs=[models_md])
199
- login_btn.click(list_private_models,
200
- inputs=[profile_state, token_state],
201
- outputs=[models_md])
202
 
203
  gemini_key = gr.Textbox(label="Gemini API Key", type="password")
204
- repo_name = gr.Textbox(label="Space name", placeholder="e.g. my-space")
205
  sdk_choice = gr.Radio(["gradio","streamlit"], value="gradio", label="SDK")
206
 
207
  gr.Markdown("---")
208
  sync_btn = gr.Button("πŸ”„ Confirm Manual Changes")
209
- sync_out = gr.Markdown()
210
- sync_btn.click(sync_manual,
211
- inputs=[profile_state, token_state, gr.State({})],
212
- outputs=[gr.HTML(), sync_out, gr.Textbox(), gr.State({})])
213
 
214
  # Main area
215
  with gr.Column(elem_id="main", scale=3):
216
  tabs = gr.Tabs()
 
217
  with tabs:
218
  with gr.TabItem("πŸ’¬ Chat"):
219
- chatbox = gr.Chatbot(type="messages")
220
  user_in = gr.Textbox(show_label=False, placeholder="Ask the LLM…")
221
  send = gr.Button("Send")
222
  with gr.TabItem("πŸ› οΈ Manual"):
223
  gr.Markdown("#### Create a Space")
224
- repo_m = gr.Textbox(label="Name")
225
- sdk_m = gr.Radio(["gradio","streamlit"], label="SDK")
226
- create = gr.Button("Create", interactive=False)
227
  sess = gr.Textbox(visible=False)
228
  log_c = gr.Textbox(label="Log", interactive=False, lines=2)
229
  preview = gr.HTML("<p>No Space yet</p>")
230
 
231
- profile_state.change(enable_create,
232
- inputs=[profile_state, token_state],
233
- outputs=[create])
234
- login_btn.click(enable_create,
235
- inputs=[profile_state, token_state],
236
- outputs=[create])
237
-
238
- create.click(create_space,
239
- inputs=[repo_m, sdk_m, profile_state, token_state],
240
- outputs=[sess, log_c, preview])
241
 
242
  gr.Markdown("#### Upload File")
243
- path = gr.Textbox(label="Path in Repo", value="app.py")
244
- file_u = gr.File()
245
- up_btn = gr.Button("Upload", interactive=False)
246
- log_u = gr.Textbox(label="Log", interactive=False, lines=2)
247
-
248
- profile_state.change(enable_repo_actions,
249
- inputs=[sess, profile_state, token_state],
250
- outputs=[up_btn])
251
- login_btn.click(enable_repo_actions,
252
- inputs=[sess, profile_state, token_state],
253
- outputs=[up_btn])
254
 
 
 
 
255
  up_btn.click(upload_file_to_space,
256
- inputs=[file_u, path, sess, profile_state, token_state],
257
  outputs=[log_u])
258
 
259
  gr.Markdown("#### Fetch Logs")
260
- b_btn = gr.Button("Build Logs", interactive=False)
261
- r_btn = gr.Button("Run Logs", interactive=False)
262
- log_b = gr.Textbox(label="Build", interactive=False, lines=5)
263
- log_r = gr.Textbox(label="Run", interactive=False, lines=5)
264
-
265
- profile_state.change(enable_repo_actions,
266
- inputs=[sess, profile_state, token_state],
267
- outputs=[b_btn, r_btn])
268
- login_btn.click(enable_repo_actions,
269
- inputs=[sess, profile_state, token_state],
270
- outputs=[b_btn, r_btn])
271
 
 
 
 
272
  b_btn.click(fetch_logs,
273
- inputs=[sess, profile_state, token_state, gr.State("build")],
274
  outputs=[log_b])
275
  r_btn.click(fetch_logs,
276
- inputs=[sess, profile_state, token_state, gr.State("run")],
277
  outputs=[log_r])
278
 
279
- # always‑visible panels
280
  gr.Markdown("---")
281
  iframe_out = gr.HTML(label="πŸ–ΌοΈ Preview")
282
  log_out = gr.Textbox(label="πŸ“‹ Latest Log", lines=4)
@@ -284,8 +262,12 @@ with gr.Blocks(css="""
284
 
285
  state = gr.State({})
286
  send.click(process_message,
287
- inputs=[profile_state, token_state, user_in, gemini_key, repo_name, sdk_choice, chatbox, state],
288
  outputs=[chatbox, iframe_out, log_out, files_out, state])
289
 
 
 
 
 
290
  if __name__ == "__main__":
291
  demo.launch()
 
1
+ import json, os
2
  import gradio as gr
3
  from huggingface_hub import (
4
  create_repo, list_models, upload_file, list_repo_files, constants
 
21
  ]
22
  return "No models found." if not models else "Models:\n\n" + "\n - ".join(models)
23
 
24
+ # β€” MANUAL HELPERS β€” #
25
+
26
+ def enable_create(profile, oauth_token):
27
+ return gr.update(interactive=bool(profile and oauth_token))
28
+
29
+ def enable_repo_actions(repo_id, profile, oauth_token):
30
+ return gr.update(interactive=bool(repo_id and profile and oauth_token))
31
 
32
  def create_space(repo_name, sdk, profile, oauth_token):
33
  if not (profile and oauth_token):
 
51
  if not (profile and oauth_token and repo_id):
52
  return "⚠️ Log in & create a Space first."
53
  # get JWT
54
+ r = get_session().get(f"{constants.ENDPOINT}/api/spaces/{repo_id}/jwt", headers=build_hf_headers())
55
+ hf_raise_for_status(r)
56
+ jwt = r.json()["token"]
57
  url = f"https://api.hf.space/v1/{repo_id}/logs/{level}"
58
  lines = []
59
  with get_session().get(url, headers=build_hf_headers(token=jwt), stream=True) as resp:
 
69
  return "⚠️ Log in & create a Space first."
70
  return "\n".join(list_repo_files(repo_id, token=oauth_token.token, repo_type="space"))
71
 
72
+ # β€” GEMINI FUNCTION DECLARATIONS β€” #
73
 
74
  func_decls = [
75
  {
 
112
  # β€” CHAT HANDLER β€” #
113
 
114
  def process_message(profile, oauth_token, user_msg, gemini_key, repo_name, sdk, chat_history, session):
115
+ # initialize on first call
116
  if session.get("chat") is None:
117
  client = genai.Client(api_key=gemini_key)
118
  cfg = types.GenerateContentConfig(
 
148
  else:
149
  result = {"log": f"⚠️ Unknown function {name}"}
150
 
151
+ # feed back
152
  chat.send_message(
153
  types.Content(
154
  role="function",
 
168
  chat_history[-1] = (user_msg, final)
169
  return chat_history, session.get("iframe",""), session.get("log",""), session.get("files",""), session
170
 
171
+ # β€” SYNC MANUAL CHANGES β€” #
172
+
173
  def sync_manual(profile, oauth_token, session):
 
174
  if not (profile and oauth_token and session.get("repo_id")):
175
  return session.get("iframe",""), "⚠️ Cannot sync.", session.get("files",""), session
176
  files = list_files(session["repo_id"], profile, oauth_token)
 
178
  session["log"] = "πŸ”„ Manual changes synced."
179
  return session.get("iframe",""), session["log"], session["files"], session
180
 
181
+ # β€” BUILD THE UI β€” #
182
 
183
  with gr.Blocks(css="""
184
+ #sidebar {background:#f7f7f7;padding:1rem;border-right:1px solid #ddd;}
185
+ #main {padding:1rem;}
186
  """) as demo:
 
187
  with gr.Row():
188
  # Sidebar
189
  with gr.Column(elem_id="sidebar", scale=1):
190
  gr.Markdown("### πŸ”‘ Login & Config")
191
+ hf_login = gr.LoginButton(variant="huggingface", size="sm")
192
+ status = gr.Markdown("*Not logged in.*")
193
+ models = gr.Markdown()
194
+
195
+ hf_login.click(show_profile, inputs=[hf_login], outputs=[status])
196
+ hf_login.click(list_private_models, inputs=[hf_login], outputs=[models])
 
 
 
 
 
 
 
 
 
 
 
197
 
198
  gemini_key = gr.Textbox(label="Gemini API Key", type="password")
199
+ repo_name = gr.Textbox(label="Space name", placeholder="my-space")
200
  sdk_choice = gr.Radio(["gradio","streamlit"], value="gradio", label="SDK")
201
 
202
  gr.Markdown("---")
203
  sync_btn = gr.Button("πŸ”„ Confirm Manual Changes")
 
 
 
 
204
 
205
  # Main area
206
  with gr.Column(elem_id="main", scale=3):
207
  tabs = gr.Tabs()
208
+
209
  with tabs:
210
  with gr.TabItem("πŸ’¬ Chat"):
211
+ chatbox = gr.Chatbot(type="messages", label="Chat")
212
  user_in = gr.Textbox(show_label=False, placeholder="Ask the LLM…")
213
  send = gr.Button("Send")
214
  with gr.TabItem("πŸ› οΈ Manual"):
215
  gr.Markdown("#### Create a Space")
216
+ repo_m = gr.Textbox(label="Name")
217
+ sdk_m = gr.Radio(["gradio","streamlit"], value="gradio", label="SDK")
218
+ create_btn = gr.Button("Create Space", interactive=False)
219
  sess = gr.Textbox(visible=False)
220
  log_c = gr.Textbox(label="Log", interactive=False, lines=2)
221
  preview = gr.HTML("<p>No Space yet</p>")
222
 
223
+ hf_login.change(enable_create, inputs=[hf_login], outputs=[create_btn])
224
+ create_btn.click(create_space,
225
+ inputs=[repo_m, sdk_m, hf_login, hf_login],
226
+ outputs=[sess, log_c, preview])
 
 
 
 
 
 
227
 
228
  gr.Markdown("#### Upload File")
229
+ path = gr.Textbox(label="Path in Repo", value="app.py")
230
+ file_u = gr.File()
231
+ up_btn = gr.Button("Upload File", interactive=False)
232
+ log_u = gr.Textbox(label="Log", interactive=False, lines=2)
 
 
 
 
 
 
 
233
 
234
+ hf_login.change(enable_repo_actions,
235
+ inputs=[sess, hf_login, hf_login],
236
+ outputs=[up_btn])
237
  up_btn.click(upload_file_to_space,
238
+ inputs=[file_u, path, sess, hf_login, hf_login],
239
  outputs=[log_u])
240
 
241
  gr.Markdown("#### Fetch Logs")
242
+ b_btn = gr.Button("Build Logs", interactive=False)
243
+ r_btn = gr.Button("Run Logs", interactive=False)
244
+ log_b = gr.Textbox(label="Build", interactive=False, lines=5)
245
+ log_r = gr.Textbox(label="Run", interactive=False, lines=5)
 
 
 
 
 
 
 
246
 
247
+ hf_login.change(enable_repo_actions,
248
+ inputs=[sess, hf_login, hf_login],
249
+ outputs=[b_btn, r_btn])
250
  b_btn.click(fetch_logs,
251
+ inputs=[sess, hf_login, hf_login, gr.State("build")],
252
  outputs=[log_b])
253
  r_btn.click(fetch_logs,
254
+ inputs=[sess, hf_login, hf_login, gr.State("run")],
255
  outputs=[log_r])
256
 
257
+ # Persistent panels
258
  gr.Markdown("---")
259
  iframe_out = gr.HTML(label="πŸ–ΌοΈ Preview")
260
  log_out = gr.Textbox(label="πŸ“‹ Latest Log", lines=4)
 
262
 
263
  state = gr.State({})
264
  send.click(process_message,
265
+ inputs=[hf_login, hf_login, user_in, gemini_key, repo_name, sdk_choice, chatbox, state],
266
  outputs=[chatbox, iframe_out, log_out, files_out, state])
267
 
268
+ sync_btn.click(sync_manual,
269
+ inputs=[hf_login, hf_login, state],
270
+ outputs=[iframe_out, log_out, files_out, state])
271
+
272
  if __name__ == "__main__":
273
  demo.launch()