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,22 +80,17 @@ def handle_user_message(
|
|
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 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
preview_url = f"https://{username}-{space_suffix}.hf.space"
|
92 |
-
iframe_html = (
|
93 |
-
f'<iframe src="{preview_url}" '
|
94 |
-
f'width="100%" height="500px" frameborder="0"></iframe>'
|
95 |
-
)
|
96 |
|
97 |
-
|
98 |
-
|
|
|
99 |
|
100 |
client = genai.Client(api_key=gemini_api_key)
|
101 |
system_msg = {
|
@@ -107,26 +102,30 @@ def handle_user_message(
|
|
107 |
}
|
108 |
chat = [system_msg] + history
|
109 |
|
110 |
-
code_fn = "app.py" if sdk_choice
|
111 |
readme_fn = "README.md"
|
112 |
reqs_fn = "requirements.txt"
|
113 |
|
114 |
-
#
|
115 |
for _ in range(5):
|
116 |
tools = [Tool(google_search=GoogleSearch())] if grounding_enabled else []
|
117 |
cfg = GenerateContentConfig(tools=tools, response_modalities=["TEXT"])
|
118 |
-
|
|
|
119 |
model="gemini-2.5-flash-preview-04-17",
|
120 |
contents=[m["content"] for m in chat],
|
121 |
config=cfg
|
122 |
)
|
123 |
|
124 |
-
|
|
|
125 |
chat.append({"role":"assistant","content":code})
|
126 |
|
127 |
-
# Write
|
128 |
with open(code_fn, "w") as f:
|
129 |
f.write(code)
|
|
|
|
|
130 |
sdk_version = get_sdk_version(sdk_choice)
|
131 |
readme = f"""---
|
132 |
title: Wuhp Auto Space
|
@@ -143,8 +142,10 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
|
|
143 |
"""
|
144 |
with open(readme_fn, "w") as f:
|
145 |
f.write(readme)
|
|
|
|
|
146 |
base_reqs = "pandas\n"
|
147 |
-
extra = "streamlit\n" if sdk_choice
|
148 |
with open(reqs_fn, "w") as f:
|
149 |
f.write(base_reqs + extra)
|
150 |
|
@@ -165,29 +166,33 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
|
|
165 |
repo_type="space"
|
166 |
)
|
167 |
|
168 |
-
#
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
# Stream update
|
173 |
-
msgs = [{"role":m["role"], "content":m["content"]} for m in chat if m["role"] != "system"]
|
174 |
-
yield msgs, build_logs, run_logs, iframe_html
|
175 |
-
|
176 |
-
# Stop if successful
|
177 |
-
if "ERROR" not in build_logs.upper() and "ERROR" not in run_logs.upper():
|
178 |
break
|
179 |
|
180 |
-
# Otherwise feed logs back for retry
|
181 |
chat.append({
|
182 |
-
"role":
|
183 |
-
"content":
|
184 |
-
f"Build logs:\n{
|
185 |
-
f"Run logs:\n{
|
186 |
"Please fix the code."
|
187 |
)
|
188 |
})
|
189 |
time.sleep(2)
|
190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
# — REFRESH LOGS —
|
192 |
|
193 |
def refresh_logs(
|
@@ -203,48 +208,44 @@ def refresh_logs(
|
|
203 |
# — BUILD THE UI —
|
204 |
|
205 |
with gr.Blocks(title="HF Space Auto‑Builder") as demo:
|
206 |
-
gr.Markdown(
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
demo.load(show_profile, inputs=None, outputs=status_md)
|
216 |
-
demo.load(list_private_models, inputs=None, outputs=models_md)
|
217 |
login_btn.click(show_profile, inputs=None, outputs=status_md)
|
218 |
login_btn.click(list_private_models, inputs=None, outputs=models_md)
|
219 |
|
220 |
# SETTINGS
|
221 |
-
sdk_choice
|
222 |
-
api_key
|
223 |
-
grounding
|
224 |
-
space_suffix
|
225 |
-
|
226 |
|
227 |
# CHAT + OUTPUTS
|
228 |
-
chatbot
|
229 |
-
user_in
|
230 |
-
send_btn
|
231 |
-
build_box
|
232 |
-
run_box
|
233 |
-
preview
|
234 |
-
|
235 |
-
# Hook up generator (profile & token pulled from login_btn)
|
236 |
send_btn.click(
|
237 |
fn=handle_user_message,
|
238 |
-
inputs=[chatbot, sdk_choice, api_key, grounding, space_suffix
|
239 |
-
outputs=[chatbot, build_box, run_box, preview]
|
240 |
-
queue=True
|
241 |
)
|
242 |
|
243 |
-
#
|
244 |
refresh_btn = gr.Button("Refresh Logs")
|
245 |
refresh_btn.click(
|
246 |
fn=refresh_logs,
|
247 |
-
inputs=[space_suffix
|
248 |
outputs=[build_box, run_box]
|
249 |
)
|
250 |
|
|
|
69 |
continue
|
70 |
return "\n".join(lines)
|
71 |
|
72 |
+
# — CORE LOOP —
|
73 |
|
74 |
def handle_user_message(
|
75 |
history,
|
|
|
80 |
profile: gr.OAuthProfile | None,
|
81 |
oauth_token: gr.OAuthToken | None
|
82 |
):
|
|
|
83 |
if profile is None or oauth_token is None:
|
84 |
+
return (
|
85 |
+
history + [{"role":"assistant","content":"⚠️ Please log in first."}],
|
86 |
+
"",
|
87 |
+
"",
|
88 |
+
"<p>No Space yet.</p>"
|
89 |
+
)
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
+
username = profile.username
|
92 |
+
repo_name = f"{username}-{space_suffix}"
|
93 |
+
repo_id = f"{username}/{repo_name}"
|
94 |
|
95 |
client = genai.Client(api_key=gemini_api_key)
|
96 |
system_msg = {
|
|
|
102 |
}
|
103 |
chat = [system_msg] + history
|
104 |
|
105 |
+
code_fn = "app.py" if sdk_choice=="gradio" else "streamlit_app.py"
|
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],
|
117 |
config=cfg
|
118 |
)
|
119 |
|
120 |
+
raw_code = resp.text
|
121 |
+
code = extract_code(raw_code)
|
122 |
chat.append({"role":"assistant","content":code})
|
123 |
|
124 |
+
# Write code file
|
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
|
|
|
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 |
|
|
|
166 |
repo_type="space"
|
167 |
)
|
168 |
|
169 |
+
# If build succeeds without errors, break; otherwise feed logs back to LLM
|
170 |
+
build = fetch_logs(repo_id, "build")
|
171 |
+
run = fetch_logs(repo_id, "run")
|
172 |
+
if "ERROR" not in build.upper() and "ERROR" not in run.upper():
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
break
|
174 |
|
|
|
175 |
chat.append({
|
176 |
+
"role":"user",
|
177 |
+
"content":(
|
178 |
+
f"Build logs:\n{build}\n\n"
|
179 |
+
f"Run logs:\n{run}\n\n"
|
180 |
"Please fix the code."
|
181 |
)
|
182 |
})
|
183 |
time.sleep(2)
|
184 |
|
185 |
+
# Prepare messages + immediate iframe preview
|
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(
|
|
|
208 |
# — BUILD THE UI —
|
209 |
|
210 |
with gr.Blocks(title="HF Space Auto‑Builder") as demo:
|
211 |
+
gr.Markdown("## Sign in + Auto‑Build Spaces\n\n"
|
212 |
+
"1. Sign in 2. Enter your prompt 3. Watch code, README, requirements, logs, and preview\n\n---")
|
213 |
+
|
214 |
+
# LOGIN controls
|
215 |
+
login_btn = gr.LoginButton(variant="huggingface", size="lg")
|
216 |
+
status_md = gr.Markdown("*Not logged in.*")
|
217 |
+
models_md = gr.Markdown()
|
218 |
+
demo.load(show_profile, inputs=None, outputs=status_md)
|
219 |
+
demo.load(list_private_models, inputs=None, outputs=models_md)
|
|
|
|
|
220 |
login_btn.click(show_profile, inputs=None, outputs=status_md)
|
221 |
login_btn.click(list_private_models, inputs=None, outputs=models_md)
|
222 |
|
223 |
# SETTINGS
|
224 |
+
sdk_choice = gr.Radio(["gradio","streamlit"], value="gradio", label="SDK template")
|
225 |
+
api_key = gr.Textbox(label="Gemini API Key", type="password")
|
226 |
+
grounding = gr.Checkbox(label="Enable grounding", value=False)
|
227 |
+
space_suffix= gr.Textbox(label="Space suffix", value="auto-space",
|
228 |
+
info="E.g. 'auto-space', 'auto-space2', etc.")
|
229 |
|
230 |
# CHAT + OUTPUTS
|
231 |
+
chatbot = gr.Chatbot(type="messages")
|
232 |
+
user_in = gr.Textbox(placeholder="Your prompt…", label="Prompt")
|
233 |
+
send_btn = gr.Button("Send")
|
234 |
+
build_box = gr.Textbox(label="Build logs", lines=5, interactive=False)
|
235 |
+
run_box = gr.Textbox(label="Run logs", lines=5, interactive=False)
|
236 |
+
preview = gr.HTML("<p>No Space yet.</p>")
|
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,
|
248 |
+
inputs=[space_suffix],
|
249 |
outputs=[build_box, run_box]
|
250 |
)
|
251 |
|