Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -69,7 +69,7 @@ def fetch_logs(repo_id: str, level: str) -> str:
|
|
69 |
continue
|
70 |
return "\n".join(lines)
|
71 |
|
72 |
-
# — CORE LOOP —
|
73 |
|
74 |
def handle_user_message(
|
75 |
history,
|
@@ -80,18 +80,28 @@ def handle_user_message(
|
|
80 |
profile: gr.OAuthProfile | None,
|
81 |
oauth_token: gr.OAuthToken | None
|
82 |
):
|
|
|
83 |
if profile is None or oauth_token is None:
|
84 |
-
|
85 |
history + [{"role":"assistant","content":"⚠️ Please log in first."}],
|
86 |
-
"",
|
87 |
-
"",
|
88 |
-
"<p>No Space yet.</p>"
|
89 |
)
|
|
|
90 |
|
91 |
-
username
|
92 |
-
repo_name
|
93 |
-
repo_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
|
|
95 |
client = genai.Client(api_key=gemini_api_key)
|
96 |
system_msg = {
|
97 |
"role":"system",
|
@@ -106,11 +116,9 @@ def handle_user_message(
|
|
106 |
readme_fn = "README.md"
|
107 |
reqs_fn = "requirements.txt"
|
108 |
|
109 |
-
# Try up to 5 times to generate & push working code
|
110 |
for _ in range(5):
|
111 |
tools = [Tool(google_search=GoogleSearch())] if grounding_enabled else []
|
112 |
cfg = GenerateContentConfig(tools=tools, response_modalities=["TEXT"])
|
113 |
-
|
114 |
resp = client.models.generate_content(
|
115 |
model="gemini-2.5-flash-preview-04-17",
|
116 |
contents=[m["content"] for m in chat],
|
@@ -121,11 +129,8 @@ def handle_user_message(
|
|
121 |
code = extract_code(raw_code)
|
122 |
chat.append({"role":"assistant","content":code})
|
123 |
|
124 |
-
# Write
|
125 |
-
with open(code_fn, "w") as f:
|
126 |
-
f.write(code)
|
127 |
-
|
128 |
-
# Write README with dynamic SDK version
|
129 |
sdk_version = get_sdk_version(sdk_choice)
|
130 |
readme = f"""---
|
131 |
title: Wuhp Auto Space
|
@@ -140,16 +145,12 @@ pinned: false
|
|
140 |
|
141 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
142 |
"""
|
143 |
-
with open(readme_fn, "w") as f:
|
144 |
-
f.write(readme)
|
145 |
-
|
146 |
-
# Write requirements
|
147 |
base_reqs = "pandas\n"
|
148 |
extra = "streamlit\n" if sdk_choice=="streamlit" else "gradio\n"
|
149 |
-
with open(reqs_fn, "w") as f:
|
150 |
-
f.write(base_reqs + extra)
|
151 |
|
152 |
-
# Push to
|
153 |
create_repo(
|
154 |
repo_id=repo_id,
|
155 |
token=oauth_token.token,
|
@@ -166,34 +167,30 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
|
|
166 |
repo_type="space"
|
167 |
)
|
168 |
|
169 |
-
#
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
break
|
174 |
|
|
|
175 |
chat.append({
|
176 |
"role":"user",
|
177 |
"content":(
|
178 |
-
f"Build logs:\n{
|
179 |
-
f"Run logs:\n{
|
180 |
"Please fix the code."
|
181 |
)
|
182 |
})
|
183 |
time.sleep(2)
|
184 |
|
185 |
-
|
186 |
-
messages = [{"role":m["role"],"content":m["content"]} for m in chat if m["role"]!="system"]
|
187 |
-
preview_url = f"https://huggingface.co/spaces/{repo_id}"
|
188 |
-
iframe = (
|
189 |
-
f'<iframe src="{preview_url}" '
|
190 |
-
f'width="100%" height="500px" frameborder="0"></iframe>'
|
191 |
-
)
|
192 |
-
|
193 |
-
placeholder = "✅ Space created! Click “Refresh Logs” to pull build/run logs."
|
194 |
-
return messages, placeholder, placeholder, iframe
|
195 |
-
|
196 |
-
# — REFRESH LOGS —
|
197 |
|
198 |
def refresh_logs(
|
199 |
space_suffix: str,
|
@@ -221,27 +218,29 @@ with gr.Blocks(title="HF Space Auto‑Builder") as demo:
|
|
221 |
login_btn.click(list_private_models, inputs=None, outputs=models_md)
|
222 |
|
223 |
# SETTINGS
|
224 |
-
sdk_choice
|
225 |
-
api_key
|
226 |
-
grounding
|
227 |
-
space_suffix= gr.Textbox(label="Space suffix", value="auto-space",
|
228 |
-
|
229 |
|
230 |
# CHAT + OUTPUTS
|
231 |
-
chatbot
|
232 |
-
user_in
|
233 |
-
send_btn
|
234 |
-
build_box
|
235 |
-
run_box
|
236 |
-
preview
|
237 |
|
238 |
send_btn.click(
|
239 |
fn=handle_user_message,
|
240 |
inputs=[chatbot, sdk_choice, api_key, grounding, space_suffix],
|
241 |
-
outputs=[chatbot, build_box, run_box, preview]
|
|
|
|
|
242 |
)
|
243 |
|
244 |
-
# Manual log refresh
|
245 |
refresh_btn = gr.Button("Refresh Logs")
|
246 |
refresh_btn.click(
|
247 |
fn=refresh_logs,
|
|
|
69 |
continue
|
70 |
return "\n".join(lines)
|
71 |
|
72 |
+
# — CORE LOOP (now a generator) —
|
73 |
|
74 |
def handle_user_message(
|
75 |
history,
|
|
|
80 |
profile: gr.OAuthProfile | None,
|
81 |
oauth_token: gr.OAuthToken | None
|
82 |
):
|
83 |
+
# 1) Require login
|
84 |
if profile is None or oauth_token is None:
|
85 |
+
yield (
|
86 |
history + [{"role":"assistant","content":"⚠️ Please log in first."}],
|
87 |
+
"", "", "<p>No Space yet.</p>"
|
|
|
|
|
88 |
)
|
89 |
+
return
|
90 |
|
91 |
+
username = profile.username
|
92 |
+
repo_name = f"{username}-{space_suffix}"
|
93 |
+
repo_id = f"{username}/{repo_name}"
|
94 |
+
preview_url = f"https://{username}-{space_suffix}.hf.space"
|
95 |
+
iframe_html = (
|
96 |
+
f'<iframe src="{preview_url}" '
|
97 |
+
f'width="100%" height="500px" frameborder="0"></iframe>'
|
98 |
+
)
|
99 |
+
|
100 |
+
# 2) Initial “in progress” state
|
101 |
+
msg_init = history + [{"role":"assistant","content":"🚀 Generating and deploying your Space..."}]
|
102 |
+
yield msg_init, "Fetching logs…", "Fetching logs…", iframe_html
|
103 |
|
104 |
+
# 3) LLM + push loop
|
105 |
client = genai.Client(api_key=gemini_api_key)
|
106 |
system_msg = {
|
107 |
"role":"system",
|
|
|
116 |
readme_fn = "README.md"
|
117 |
reqs_fn = "requirements.txt"
|
118 |
|
|
|
119 |
for _ in range(5):
|
120 |
tools = [Tool(google_search=GoogleSearch())] if grounding_enabled else []
|
121 |
cfg = GenerateContentConfig(tools=tools, response_modalities=["TEXT"])
|
|
|
122 |
resp = client.models.generate_content(
|
123 |
model="gemini-2.5-flash-preview-04-17",
|
124 |
contents=[m["content"] for m in chat],
|
|
|
129 |
code = extract_code(raw_code)
|
130 |
chat.append({"role":"assistant","content":code})
|
131 |
|
132 |
+
# Write files
|
133 |
+
with open(code_fn, "w") as f: f.write(code)
|
|
|
|
|
|
|
134 |
sdk_version = get_sdk_version(sdk_choice)
|
135 |
readme = f"""---
|
136 |
title: Wuhp Auto Space
|
|
|
145 |
|
146 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
147 |
"""
|
148 |
+
with open(readme_fn, "w") as f: f.write(readme)
|
|
|
|
|
|
|
149 |
base_reqs = "pandas\n"
|
150 |
extra = "streamlit\n" if sdk_choice=="streamlit" else "gradio\n"
|
151 |
+
with open(reqs_fn, "w") as f: f.write(base_reqs + extra)
|
|
|
152 |
|
153 |
+
# Push to HF
|
154 |
create_repo(
|
155 |
repo_id=repo_id,
|
156 |
token=oauth_token.token,
|
|
|
167 |
repo_type="space"
|
168 |
)
|
169 |
|
170 |
+
# Fetch logs
|
171 |
+
build_logs = fetch_logs(repo_id, "build")
|
172 |
+
run_logs = fetch_logs(repo_id, "run")
|
173 |
+
|
174 |
+
# Yield incremental update
|
175 |
+
msgs = [{"role":m["role"], "content":m["content"]} for m in chat if m["role"]!="system"]
|
176 |
+
yield msgs, build_logs, run_logs, iframe_html
|
177 |
+
|
178 |
+
# If successful, stop retrying
|
179 |
+
if "ERROR" not in build_logs.upper() and "ERROR" not in run_logs.upper():
|
180 |
break
|
181 |
|
182 |
+
# Otherwise feed logs back to LLM
|
183 |
chat.append({
|
184 |
"role":"user",
|
185 |
"content":(
|
186 |
+
f"Build logs:\n{build_logs}\n\n"
|
187 |
+
f"Run logs:\n{run_logs}\n\n"
|
188 |
"Please fix the code."
|
189 |
)
|
190 |
})
|
191 |
time.sleep(2)
|
192 |
|
193 |
+
# — REFRESH LOGS (unchanged) —
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
|
195 |
def refresh_logs(
|
196 |
space_suffix: str,
|
|
|
218 |
login_btn.click(list_private_models, inputs=None, outputs=models_md)
|
219 |
|
220 |
# SETTINGS
|
221 |
+
sdk_choice = gr.Radio(["gradio","streamlit"], value="gradio", label="SDK template")
|
222 |
+
api_key = gr.Textbox(label="Gemini API Key", type="password")
|
223 |
+
grounding = gr.Checkbox(label="Enable grounding", value=False)
|
224 |
+
space_suffix = gr.Textbox(label="Space suffix", value="auto-space",
|
225 |
+
info="E.g. 'auto-space', 'auto-space2', etc.")
|
226 |
|
227 |
# CHAT + OUTPUTS
|
228 |
+
chatbot = gr.Chatbot(type="messages")
|
229 |
+
user_in = gr.Textbox(placeholder="Your prompt…", label="Prompt")
|
230 |
+
send_btn = gr.Button("Send")
|
231 |
+
build_box = gr.Textbox(label="Build logs", lines=5, interactive=False)
|
232 |
+
run_box = gr.Textbox(label="Run logs", lines=5, interactive=False)
|
233 |
+
preview = gr.HTML("<p>No Space yet.</p>")
|
234 |
|
235 |
send_btn.click(
|
236 |
fn=handle_user_message,
|
237 |
inputs=[chatbot, sdk_choice, api_key, grounding, space_suffix],
|
238 |
+
outputs=[chatbot, build_box, run_box, preview],
|
239 |
+
queue=True,
|
240 |
+
stream=True
|
241 |
)
|
242 |
|
243 |
+
# Manual log refresh (optional)
|
244 |
refresh_btn = gr.Button("Refresh Logs")
|
245 |
refresh_btn.click(
|
246 |
fn=refresh_logs,
|