wuhp commited on
Commit
03483b4
·
verified ·
1 Parent(s): d4f7838

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -16
app.py CHANGED
@@ -1,13 +1,33 @@
1
  import re
 
2
  import time
3
  import importlib.metadata
 
4
  from huggingface_hub import create_repo, upload_file, list_models, constants
5
  from huggingface_hub.utils import build_hf_headers, get_session, hf_raise_for_status
6
  from google import genai
7
  from google.genai.types import Tool, GenerateContentConfig, GoogleSearch
8
- import gradio as gr
9
 
10
- # — UTILITIES
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  def get_sdk_version(sdk_choice: str) -> str:
13
  pkg = "gradio" if sdk_choice == "gradio" else "streamlit"
@@ -17,13 +37,10 @@ def get_sdk_version(sdk_choice: str) -> str:
17
  return "UNKNOWN"
18
 
19
  def extract_code(text: str) -> str:
20
- """
21
- Pull out the last ```…``` block, or fall back to the whole text.
22
- """
23
  blocks = re.findall(r"```(?:\w*\n)?([\s\S]*?)```", text)
24
  return blocks[-1].strip() if blocks else text.strip()
25
 
26
- # — HF SPACE LOGGING —
27
 
28
  def _get_space_jwt(repo_id: str):
29
  url = f"{constants.ENDPOINT}/api/spaces/{repo_id}/jwt"
@@ -48,7 +65,7 @@ def fetch_logs(repo_id: str, level: str) -> str:
48
  continue
49
  return "\n".join(lines)
50
 
51
- # — CORE LOOP —
52
 
53
  def handle_user_message(
54
  history,
@@ -58,7 +75,7 @@ def handle_user_message(
58
  profile: gr.OAuthProfile | None,
59
  oauth_token: gr.OAuthToken | None
60
  ):
61
- if not profile or not oauth_token:
62
  return history + [{"role":"assistant","content":"⚠️ Please log in first."}], "", "", "<p>No Space yet.</p>"
63
 
64
  client = genai.Client(api_key=gemini_api_key)
@@ -146,23 +163,26 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
146
  iframe = f'<iframe src="https://huggingface.co/spaces/{repo_id}" width="100%" height="500px"></iframe>'
147
  return messages, build_logs, run_logs, iframe
148
 
149
- # — BUILD THE UI —
150
 
151
  with gr.Blocks(title="HF Space Auto‑Builder") as demo:
152
- gr.Markdown("## Sign in + Auto‑Build Spaces\n\n..."
153
- )
 
154
  login_btn = gr.LoginButton(variant="huggingface", size="lg")
155
  status_md = gr.Markdown("*Not logged in.*")
156
  models_md = gr.Markdown()
157
- demo.load(show_profile, inputs=None, outputs=status_md)
158
  demo.load(list_private_models, inputs=None, outputs=models_md)
159
  login_btn.click(show_profile, inputs=None, outputs=status_md)
160
  login_btn.click(list_private_models, inputs=None, outputs=models_md)
161
 
 
162
  sdk_choice = gr.Radio(["gradio","streamlit"], value="gradio", label="SDK template")
163
  api_key = gr.Textbox(label="Gemini API Key", type="password")
164
  grounding = gr.Checkbox(label="Enable grounding", value=False)
165
 
 
166
  chatbot = gr.Chatbot(type="messages")
167
  user_in = gr.Textbox(placeholder="Your prompt…", label="Prompt")
168
  send_btn = gr.Button("Send")
@@ -177,11 +197,15 @@ with gr.Blocks(title="HF Space Auto‑Builder") as demo:
177
  outputs=[chatbot, build_box, run_box, preview]
178
  )
179
 
180
- # — New “Refresh Logs control for manual edits
181
- refresh_btn = gr.Button("Refresh Logs")
182
- def _refresh(profile, token):
 
183
  repo = f"{profile.username}/{profile.username}-auto-space"
184
  return fetch_logs(repo, "build"), fetch_logs(repo, "run")
185
- refresh_btn.click(_refresh, inputs=[status_md, models_md], outputs=[build_box, run_box])
 
 
 
186
 
187
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import re
2
+ import json
3
  import time
4
  import importlib.metadata
5
+ import gradio as gr
6
  from huggingface_hub import create_repo, upload_file, list_models, constants
7
  from huggingface_hub.utils import build_hf_headers, get_session, hf_raise_for_status
8
  from google import genai
9
  from google.genai.types import Tool, GenerateContentConfig, GoogleSearch
 
10
 
11
+ # — USER INFO & MODEL LISTING
12
+
13
+ def show_profile(profile: gr.OAuthProfile | None) -> str:
14
+ if profile is None:
15
+ return "*Not logged in.*"
16
+ return f"✅ Logged in as **{profile.username}**"
17
+
18
+ def list_private_models(
19
+ profile: gr.OAuthProfile | None,
20
+ oauth_token: gr.OAuthToken | None
21
+ ) -> str:
22
+ if profile is None or oauth_token is None:
23
+ return "Please log in to see your models."
24
+ models = [
25
+ f"{m.id} ({'private' if m.private else 'public'})"
26
+ for m in list_models(author=profile.username, token=oauth_token.token)
27
+ ]
28
+ return "No models found." if not models else "Models:\n\n" + "\n - ".join(models)
29
+
30
+ # — UTILITIES —
31
 
32
  def get_sdk_version(sdk_choice: str) -> str:
33
  pkg = "gradio" if sdk_choice == "gradio" else "streamlit"
 
37
  return "UNKNOWN"
38
 
39
  def extract_code(text: str) -> str:
 
 
 
40
  blocks = re.findall(r"```(?:\w*\n)?([\s\S]*?)```", text)
41
  return blocks[-1].strip() if blocks else text.strip()
42
 
43
+ # — HF SPACE LOGGING —
44
 
45
  def _get_space_jwt(repo_id: str):
46
  url = f"{constants.ENDPOINT}/api/spaces/{repo_id}/jwt"
 
65
  continue
66
  return "\n".join(lines)
67
 
68
+ # — CORE LOOP —
69
 
70
  def handle_user_message(
71
  history,
 
75
  profile: gr.OAuthProfile | None,
76
  oauth_token: gr.OAuthToken | None
77
  ):
78
+ if profile is None or oauth_token is None:
79
  return history + [{"role":"assistant","content":"⚠️ Please log in first."}], "", "", "<p>No Space yet.</p>"
80
 
81
  client = genai.Client(api_key=gemini_api_key)
 
163
  iframe = f'<iframe src="https://huggingface.co/spaces/{repo_id}" width="100%" height="500px"></iframe>'
164
  return messages, build_logs, run_logs, iframe
165
 
166
+ # — BUILD THE UI —
167
 
168
  with gr.Blocks(title="HF Space Auto‑Builder") as demo:
169
+ gr.Markdown("## Sign in + Auto‑Build Spaces\n\n1. Sign in 2. Enter your prompt 3. Watch code, README, requirements, logs, and preview\n\n---")
170
+
171
+ # LOGIN & MODEL LISTING
172
  login_btn = gr.LoginButton(variant="huggingface", size="lg")
173
  status_md = gr.Markdown("*Not logged in.*")
174
  models_md = gr.Markdown()
175
+ demo.load(show_profile, inputs=None, outputs=status_md)
176
  demo.load(list_private_models, inputs=None, outputs=models_md)
177
  login_btn.click(show_profile, inputs=None, outputs=status_md)
178
  login_btn.click(list_private_models, inputs=None, outputs=models_md)
179
 
180
+ # CONTROLS
181
  sdk_choice = gr.Radio(["gradio","streamlit"], value="gradio", label="SDK template")
182
  api_key = gr.Textbox(label="Gemini API Key", type="password")
183
  grounding = gr.Checkbox(label="Enable grounding", value=False)
184
 
185
+ # CHAT + OUTPUTS
186
  chatbot = gr.Chatbot(type="messages")
187
  user_in = gr.Textbox(placeholder="Your prompt…", label="Prompt")
188
  send_btn = gr.Button("Send")
 
197
  outputs=[chatbot, build_box, run_box, preview]
198
  )
199
 
200
+ # — Refresh Logs button
201
+ def _refresh(profile: gr.OAuthProfile | None, oauth_token: gr.OAuthToken | None):
202
+ if not profile or not oauth_token:
203
+ return "", ""
204
  repo = f"{profile.username}/{profile.username}-auto-space"
205
  return fetch_logs(repo, "build"), fetch_logs(repo, "run")
206
+
207
+ refresh_btn = gr.Button("Refresh Logs")
208
+ # Gradio will auto‑inject `profile` and `oauth_token` here.
209
+ refresh_btn.click(_refresh, inputs=None, outputs=[build_box, run_box])
210
 
211
  demo.launch(server_name="0.0.0.0", server_port=7860)