Spaces:
Sleeping
Sleeping
File size: 12,401 Bytes
0df9635 c44aed0 fb9266f 91af2dc fb9266f 6146397 f2231db c9b9839 0df9635 c9b9839 91af2dc fb9266f 91af2dc fb9266f c9b9839 fb9266f 91af2dc c9b9839 91af2dc c9b9839 91af2dc c9b9839 91af2dc 0df9635 91af2dc 6146397 91af2dc 0df9635 91af2dc fb9266f 91af2dc c9b9839 0df9635 c9b9839 0df9635 91af2dc fb9266f 91af2dc fb9266f 0df9635 91af2dc fb9266f 0df9635 91af2dc fb9266f 0df9635 91af2dc fb9266f 0df9635 c9b9839 0df9635 91af2dc c9b9839 0df9635 91af2dc 0df9635 91af2dc 0df9635 c44aed0 0df9635 c9b9839 f2231db 0df9635 c9b9839 fb9266f c9b9839 0df9635 c9b9839 0df9635 c9b9839 0df9635 c9b9839 0df9635 f2231db 0df9635 91af2dc c9b9839 0df9635 c9b9839 0df9635 91af2dc c9b9839 91af2dc c9b9839 91af2dc c9b9839 91af2dc c9b9839 91af2dc c9b9839 91af2dc c9b9839 f2231db c9b9839 91af2dc c9b9839 91af2dc c9b9839 91af2dc c9b9839 91af2dc c9b9839 91af2dc c9b9839 91af2dc c9b9839 91af2dc c9b9839 91af2dc c9b9839 91af2dc c9b9839 91af2dc c9b9839 91af2dc c9b9839 91af2dc fb9266f 91af2dc 0df9635 91af2dc c9b9839 91af2dc 9194337 8e77934 45e9fba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
import os, json
import gradio as gr
from huggingface_hub import (
create_repo, list_models, upload_file, list_repo_files, constants
)
from huggingface_hub.utils import build_hf_headers, get_session, hf_raise_for_status
from google import genai
from google.genai import types
# โ HF LOGIN CALLBACKS โ #
def show_profile(profile):
return f"โ
Logged in as **{profile.username}**" if profile else "*Not logged in.*"
def list_private_models(profile, oauth_token):
if not (profile and oauth_token):
return "Please log in to see your models."
models = [
f"{m.id} ({'private' if m.private else 'public'})"
for m in list_models(author=profile.username, token=oauth_token.token)
]
return "No models found." if not models else "Models:\n\n" + "\n - ".join(models)
# โ HF SPACE HELPERS โ #
def create_space(repo_name, sdk, profile, oauth_token):
if not (profile and oauth_token):
return "", "โ ๏ธ Please log in first.", "<p>No Space.</p>"
rid = f"{profile.username}/{repo_name}"
create_repo(rid, token=oauth_token.token, exist_ok=True, repo_type="space", space_sdk=sdk)
url = f"https://huggingface.co/spaces/{rid}"
return rid, f"โ
Space ready: {url}", f'<iframe src="{url}" width="100%" height="400px"></iframe>'
def upload_file_to_space(file, path, repo_id, profile, oauth_token):
if not (profile and oauth_token):
return "โ ๏ธ Please log in first."
if not repo_id:
return "โ ๏ธ Create a Space first."
if not file:
return "โ ๏ธ No file selected."
upload_file(file.name, path, repo_id, token=oauth_token.token, repo_type="space")
return f"โ
Uploaded `{path}`"
def fetch_logs(repo_id, profile, oauth_token, level):
if not (profile and oauth_token and repo_id):
return "โ ๏ธ Log in & create a Space first."
# get JWT
r = get_session().get(f"{constants.ENDPOINT}/api/spaces/{repo_id}/jwt",
headers=build_hf_headers())
hf_raise_for_status(r); jwt = r.json()["token"]
url = f"https://api.hf.space/v1/{repo_id}/logs/{level}"
lines = []
with get_session().get(url, headers=build_hf_headers(token=jwt), stream=True) as resp:
hf_raise_for_status(resp)
for raw in resp.iter_lines():
if raw.startswith(b"data: "):
ev = json.loads(raw[6:])
lines.append(f"[{ev.get('timestamp','')}] {ev.get('data','')}")
return "\n".join(lines)
def list_files(repo_id, profile, oauth_token):
if not (profile and oauth_token and repo_id):
return "โ ๏ธ Log in & create a Space first."
return "\n".join(list_repo_files(repo_id, token=oauth_token.token, repo_type="space"))
# โ GEMINI FUNCTION DECLS โ #
func_decls = [
{
"name":"create_space","description":"Create/get a HF Space",
"parameters":{
"type":"object",
"properties":{
"repo_name":{"type":"string"},
"sdk":{"type":"string","enum":["gradio","streamlit"]}
},
"required":["repo_name","sdk"]
}
},
{
"name":"list_files","description":"List files in Space",
"parameters":{
"type":"object",
"properties":{"repo_id":{"type":"string"}},
"required":["repo_id"]
}
},
{
"name":"get_build_logs","description":"Fetch build logs",
"parameters":{
"type":"object",
"properties":{"repo_id":{"type":"string"}},
"required":["repo_id"]
}
},
{
"name":"get_run_logs","description":"Fetch run logs",
"parameters":{
"type":"object",
"properties":{"repo_id":{"type":"string"}},
"required":["repo_id"]
}
},
]
# โ CHAT HANDLER โ #
def process_message(profile, oauth_token, user_msg, gemini_key, repo_name, sdk, chat_history, session):
# init on first call
if session.get("chat") is None:
client = genai.Client(api_key=gemini_key)
cfg = types.GenerateContentConfig(
system_instruction="You are an admin for HF Spaces: create spaces, list files, and fetch logs.",
temperature=0,
tools=[ types.Tool(function_declarations=func_decls) ]
)
session["chat"] = client.chats.create(model="gemini-2.0-flash", config=cfg)
session["repo_id"] = None
chat = session["chat"]
chat_history.append((user_msg, None))
resp = chat.send_message(user_msg)
part = resp.candidates[0].content.parts[0]
result = {}
if part.function_call:
name = part.function_call.name
args = json.loads(part.function_call.args)
if name == "create_space":
rid, log, iframe = create_space(args["repo_name"], args["sdk"], profile, oauth_token)
session["repo_id"] = rid
result = {"log": log, "iframe": iframe}
elif name == "list_files":
files = list_files(session["repo_id"], profile, oauth_token)
result = {"files": files}
elif name == "get_build_logs":
log = fetch_logs(session["repo_id"], profile, oauth_token, "build")
result = {"log": log}
elif name == "get_run_logs":
log = fetch_logs(session["repo_id"], profile, oauth_token, "run")
result = {"log": log}
else:
result = {"log": f"โ ๏ธ Unknown function {name}"}
# feed back into chat
chat.send_message(
types.Content(
role="function",
parts=[ types.Part(function_call=part.function_call,
function_response=json.dumps(result)) ]
)
)
final = chat.get_history()[-1].parts[0].text
else:
final = part.text
# update panels
if "iframe" in result: session["iframe"] = result["iframe"]
if "log" in result: session["log"] = result["log"]
if "files" in result: session["files"] = result["files"]
chat_history[-1] = (user_msg, final)
return chat_history, session.get("iframe",""), session.get("log",""), session.get("files",""), session
def sync_manual(profile, oauth_token, session):
# pull in any manual file changes
if not (profile and oauth_token and session.get("repo_id")):
return session.get("iframe",""), "โ ๏ธ Cannot sync.", session.get("files",""), session
files = list_files(session["repo_id"], profile, oauth_token)
session["files"] = files
session["log"] = "๐ Manual changes synced."
return session.get("iframe",""), session["log"], session["files"], session
# โ LAYOUT โ #
with gr.Blocks(css="""
#sidebar {background:#f7f7f7;padding:1em;border-right:1px solid #ddd;}
#main {padding:1em;}
""") as demo:
with gr.Row():
# Sidebar
with gr.Column(elem_id="sidebar", scale=1):
gr.Markdown("### ๐ Login & Config")
login_btn = gr.LoginButton(variant="huggingface", size="sm")
profile_state = gr.State(None)
token_state = gr.State(None)
# capture login outputs
login_btn.click(None, outputs=[profile_state, token_state])
status_md = gr.Markdown("*Not logged in.*")
profile_state.change(show_profile, inputs=[profile_state], outputs=[status_md])
login_btn.click(show_profile, inputs=[profile_state], outputs=[status_md])
models_md = gr.Markdown()
profile_state.change(list_private_models,
inputs=[profile_state, token_state],
outputs=[models_md])
login_btn.click(list_private_models,
inputs=[profile_state, token_state],
outputs=[models_md])
gemini_key = gr.Textbox(label="Gemini API Key", type="password")
repo_name = gr.Textbox(label="Space name", placeholder="e.g. my-space")
sdk_choice = gr.Radio(["gradio","streamlit"], value="gradio", label="SDK")
gr.Markdown("---")
sync_btn = gr.Button("๐ Confirm Manual Changes")
sync_out = gr.Markdown()
sync_btn.click(sync_manual,
inputs=[profile_state, token_state, gr.State({})],
outputs=[gr.HTML(), sync_out, gr.Textbox(), gr.State({})])
# Main area
with gr.Column(elem_id="main", scale=3):
tabs = gr.Tabs()
with tabs:
with gr.TabItem("๐ฌ Chat"):
chatbox = gr.Chatbot(type="messages")
user_in = gr.Textbox(show_label=False, placeholder="Ask the LLMโฆ")
send = gr.Button("Send")
with gr.TabItem("๐ ๏ธ Manual"):
gr.Markdown("#### Create a Space")
repo_m = gr.Textbox(label="Name")
sdk_m = gr.Radio(["gradio","streamlit"], label="SDK")
create = gr.Button("Create", interactive=False)
sess = gr.Textbox(visible=False)
log_c = gr.Textbox(label="Log", interactive=False, lines=2)
preview = gr.HTML("<p>No Space yet</p>")
profile_state.change(enable_create,
inputs=[profile_state, token_state],
outputs=[create])
login_btn.click(enable_create,
inputs=[profile_state, token_state],
outputs=[create])
create.click(create_space,
inputs=[repo_m, sdk_m, profile_state, token_state],
outputs=[sess, log_c, preview])
gr.Markdown("#### Upload File")
path = gr.Textbox(label="Path in Repo", value="app.py")
file_u = gr.File()
up_btn = gr.Button("Upload", interactive=False)
log_u = gr.Textbox(label="Log", interactive=False, lines=2)
profile_state.change(enable_repo_actions,
inputs=[sess, profile_state, token_state],
outputs=[up_btn])
login_btn.click(enable_repo_actions,
inputs=[sess, profile_state, token_state],
outputs=[up_btn])
up_btn.click(upload_file_to_space,
inputs=[file_u, path, sess, profile_state, token_state],
outputs=[log_u])
gr.Markdown("#### Fetch Logs")
b_btn = gr.Button("Build Logs", interactive=False)
r_btn = gr.Button("Run Logs", interactive=False)
log_b = gr.Textbox(label="Build", interactive=False, lines=5)
log_r = gr.Textbox(label="Run", interactive=False, lines=5)
profile_state.change(enable_repo_actions,
inputs=[sess, profile_state, token_state],
outputs=[b_btn, r_btn])
login_btn.click(enable_repo_actions,
inputs=[sess, profile_state, token_state],
outputs=[b_btn, r_btn])
b_btn.click(fetch_logs,
inputs=[sess, profile_state, token_state, gr.State("build")],
outputs=[log_b])
r_btn.click(fetch_logs,
inputs=[sess, profile_state, token_state, gr.State("run")],
outputs=[log_r])
# alwaysโvisible panels
gr.Markdown("---")
iframe_out = gr.HTML(label="๐ผ๏ธ Preview")
log_out = gr.Textbox(label="๐ Latest Log", lines=4)
files_out = gr.Textbox(label="๐ Files", lines=4)
state = gr.State({})
send.click(process_message,
inputs=[profile_state, token_state, user_in, gemini_key, repo_name, sdk_choice, chatbox, state],
outputs=[chatbox, iframe_out, log_out, files_out, state])
if __name__ == "__main__":
demo.launch()
|