Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import os, json
|
2 |
import gradio as gr
|
3 |
-
import requests
|
4 |
from huggingface_hub import (
|
5 |
create_repo,
|
6 |
list_models,
|
@@ -196,16 +195,13 @@ def process_message(
|
|
196 |
session["repo_id"] = None
|
197 |
|
198 |
chat = session["chat"]
|
199 |
-
# user turn
|
200 |
chat_history = chat_history + [(user_msg, None)]
|
201 |
resp = chat.send_message(user_msg)
|
202 |
part = resp.candidates[0].content.parts[0]
|
203 |
|
204 |
-
# function call?
|
205 |
if part.function_call:
|
206 |
name = part.function_call.name
|
207 |
args = json.loads(part.function_call.args)
|
208 |
-
# dispatch
|
209 |
if name=="create_space":
|
210 |
rid, log, iframe = create_space(args["repo_name"], args["sdk"], profile, oauth_token)
|
211 |
session["repo_id"] = rid
|
@@ -220,7 +216,6 @@ def process_message(
|
|
220 |
else:
|
221 |
result = {"log":f"β οΈ Unknown function {name}"}
|
222 |
|
223 |
-
# send back
|
224 |
chat.send_message(
|
225 |
types.Content(
|
226 |
role="function",
|
@@ -231,7 +226,7 @@ def process_message(
|
|
231 |
else:
|
232 |
final = part.text
|
233 |
|
234 |
-
#
|
235 |
iframe = session.get("iframe", "")
|
236 |
log = session.get("log", "")
|
237 |
files = session.get("files", "")
|
@@ -248,7 +243,7 @@ def process_message(
|
|
248 |
|
249 |
with gr.Blocks(title="HF Spaces + Gemini Chat") as demo:
|
250 |
with gr.Row():
|
251 |
-
# β Sidebar: login
|
252 |
with gr.Column(scale=1):
|
253 |
gr.Markdown("## π Sign in & Chat Config")
|
254 |
login_btn = gr.LoginButton(variant="huggingface", size="sm")
|
@@ -262,10 +257,10 @@ with gr.Blocks(title="HF Spaces + Gemini Chat") as demo:
|
|
262 |
gemini_key = gr.Textbox(label="Gemini API Key", type="password")
|
263 |
repo_name = gr.Textbox(label="Space name", placeholder="e.g. my-space")
|
264 |
sdk_choice = gr.Radio(["gradio","streamlit"], value="gradio", label="SDK")
|
265 |
-
# βΆ Main: chat + panels + manual
|
266 |
with gr.Column(scale=3):
|
267 |
gr.Markdown("## π¬ Chat to manage your Space")
|
268 |
-
chatbox = gr.Chatbot()
|
269 |
user_in = gr.Textbox(show_label=False, placeholder="e.g. Create Space")
|
270 |
send_btn = gr.Button("Send")
|
271 |
|
@@ -273,52 +268,54 @@ with gr.Blocks(title="HF Spaces + Gemini Chat") as demo:
|
|
273 |
log_out = gr.Textbox(label="π Latest Log", lines=8)
|
274 |
files_out = gr.Textbox(label="π Files in Space", lines=4)
|
275 |
|
276 |
-
gr.Markdown("---\n## π οΈ Manual Controls
|
277 |
-
#
|
278 |
-
repo_name_m
|
279 |
-
sdk_selector
|
280 |
-
create_btn
|
281 |
-
session_id
|
282 |
-
create_logs
|
283 |
-
preview_m
|
284 |
-
|
285 |
-
demo.load(enable_create,
|
286 |
-
login_btn.click(enable_create,
|
287 |
create_btn.click(
|
288 |
fn=create_space,
|
289 |
-
inputs=[repo_name_m, sdk_selector
|
290 |
outputs=[session_id, create_logs, preview_m]
|
291 |
)
|
292 |
|
293 |
-
#
|
294 |
-
path_in_repo
|
295 |
-
file_uploader
|
296 |
-
upload_btn
|
297 |
-
upload_logs
|
298 |
|
299 |
-
demo.load(enable_repo_actions, inputs=[session_id
|
|
|
300 |
upload_btn.click(
|
301 |
fn=upload_file_to_space,
|
302 |
-
inputs=[file_uploader, path_in_repo, session_id
|
303 |
outputs=[upload_logs]
|
304 |
)
|
305 |
|
306 |
-
#
|
307 |
-
build_btn
|
308 |
-
run_btn
|
309 |
-
build_logs_md
|
310 |
-
run_logs_md
|
311 |
|
312 |
for b in (build_btn, run_btn):
|
313 |
-
demo.load(enable_repo_actions, inputs=[session_id
|
|
|
314 |
|
315 |
-
build_btn.click(fn=get_build_logs,
|
316 |
-
run_btn.click( fn=get_container_logs,
|
317 |
|
318 |
state = gr.State({})
|
319 |
send_btn.click(
|
320 |
fn=process_message,
|
321 |
-
inputs=[user_in, status_md, login_btn
|
322 |
outputs=[chatbox, iframe_out, log_out, files_out, state]
|
323 |
)
|
324 |
|
|
|
1 |
import os, json
|
2 |
import gradio as gr
|
|
|
3 |
from huggingface_hub import (
|
4 |
create_repo,
|
5 |
list_models,
|
|
|
195 |
session["repo_id"] = None
|
196 |
|
197 |
chat = session["chat"]
|
|
|
198 |
chat_history = chat_history + [(user_msg, None)]
|
199 |
resp = chat.send_message(user_msg)
|
200 |
part = resp.candidates[0].content.parts[0]
|
201 |
|
|
|
202 |
if part.function_call:
|
203 |
name = part.function_call.name
|
204 |
args = json.loads(part.function_call.args)
|
|
|
205 |
if name=="create_space":
|
206 |
rid, log, iframe = create_space(args["repo_name"], args["sdk"], profile, oauth_token)
|
207 |
session["repo_id"] = rid
|
|
|
216 |
else:
|
217 |
result = {"log":f"β οΈ Unknown function {name}"}
|
218 |
|
|
|
219 |
chat.send_message(
|
220 |
types.Content(
|
221 |
role="function",
|
|
|
226 |
else:
|
227 |
final = part.text
|
228 |
|
229 |
+
# panels
|
230 |
iframe = session.get("iframe", "")
|
231 |
log = session.get("log", "")
|
232 |
files = session.get("files", "")
|
|
|
243 |
|
244 |
with gr.Blocks(title="HF Spaces + Gemini Chat") as demo:
|
245 |
with gr.Row():
|
246 |
+
# β Sidebar: login + chat config
|
247 |
with gr.Column(scale=1):
|
248 |
gr.Markdown("## π Sign in & Chat Config")
|
249 |
login_btn = gr.LoginButton(variant="huggingface", size="sm")
|
|
|
257 |
gemini_key = gr.Textbox(label="Gemini API Key", type="password")
|
258 |
repo_name = gr.Textbox(label="Space name", placeholder="e.g. my-space")
|
259 |
sdk_choice = gr.Radio(["gradio","streamlit"], value="gradio", label="SDK")
|
260 |
+
# βΆ Main: chat + panels + manual controls
|
261 |
with gr.Column(scale=3):
|
262 |
gr.Markdown("## π¬ Chat to manage your Space")
|
263 |
+
chatbox = gr.Chatbot(type="messages")
|
264 |
user_in = gr.Textbox(show_label=False, placeholder="e.g. Create Space")
|
265 |
send_btn = gr.Button("Send")
|
266 |
|
|
|
268 |
log_out = gr.Textbox(label="π Latest Log", lines=8)
|
269 |
files_out = gr.Textbox(label="π Files in Space", lines=4)
|
270 |
|
271 |
+
gr.Markdown("---\n## π οΈ Manual Controls")
|
272 |
+
# CREATE SPACE
|
273 |
+
repo_name_m = gr.Textbox(label="New Space name", placeholder="my-space")
|
274 |
+
sdk_selector = gr.Radio(["gradio","streamlit"], value="gradio", label="SDK")
|
275 |
+
create_btn = gr.Button("Create Space", interactive=False)
|
276 |
+
session_id = gr.Textbox(visible=False)
|
277 |
+
create_logs = gr.Textbox(label="Create Logs", interactive=False, lines=3)
|
278 |
+
preview_m = gr.HTML("<p>No Space created yet.</p>")
|
279 |
+
|
280 |
+
demo.load(enable_create, inputs=None, outputs=[create_btn])
|
281 |
+
login_btn.click(enable_create, inputs=None, outputs=[create_btn])
|
282 |
create_btn.click(
|
283 |
fn=create_space,
|
284 |
+
inputs=[repo_name_m, sdk_selector],
|
285 |
outputs=[session_id, create_logs, preview_m]
|
286 |
)
|
287 |
|
288 |
+
# UPLOAD FILE
|
289 |
+
path_in_repo = gr.Textbox(label="Path in Space", value="app.py")
|
290 |
+
file_uploader = gr.File(label="Select file")
|
291 |
+
upload_btn = gr.Button("Upload File", interactive=False)
|
292 |
+
upload_logs = gr.Textbox(label="Upload Logs", interactive=False, lines=2)
|
293 |
|
294 |
+
demo.load(enable_repo_actions, inputs=[session_id], outputs=[upload_btn])
|
295 |
+
login_btn.click(enable_repo_actions, inputs=[session_id], outputs=[upload_btn])
|
296 |
upload_btn.click(
|
297 |
fn=upload_file_to_space,
|
298 |
+
inputs=[file_uploader, path_in_repo, session_id],
|
299 |
outputs=[upload_logs]
|
300 |
)
|
301 |
|
302 |
+
# FETCH LOGS
|
303 |
+
build_btn = gr.Button("Get Build Logs", interactive=False)
|
304 |
+
run_btn = gr.Button("Get Container Logs", interactive=False)
|
305 |
+
build_logs_md = gr.Textbox(label="Build Logs", interactive=False, lines=10)
|
306 |
+
run_logs_md = gr.Textbox(label="Container Logs", interactive=False, lines=10)
|
307 |
|
308 |
for b in (build_btn, run_btn):
|
309 |
+
demo.load(enable_repo_actions, inputs=[session_id], outputs=[b])
|
310 |
+
login_btn.click(enable_repo_actions, inputs=[session_id], outputs=[b])
|
311 |
|
312 |
+
build_btn.click(fn=get_build_logs, inputs=[session_id], outputs=[build_logs_md])
|
313 |
+
run_btn.click( fn=get_container_logs, inputs=[session_id], outputs=[run_logs_md])
|
314 |
|
315 |
state = gr.State({})
|
316 |
send_btn.click(
|
317 |
fn=process_message,
|
318 |
+
inputs=[user_in, status_md, login_btn, gemini_key, repo_name, sdk_choice, chatbox, state],
|
319 |
outputs=[chatbox, iframe_out, log_out, files_out, state]
|
320 |
)
|
321 |
|