Spaces:
Sleeping
Sleeping
File size: 37,133 Bytes
a0f57d6 3290861 a0f57d6 4eff17c 3290861 a0f57d6 6146397 4c46f34 a0f57d6 0df9635 4eff17c 5d26448 9053015 fb9266f a0f57d6 9053015 a0f57d6 5d26448 a0f57d6 4eff17c dbd6fa0 5262349 4eff17c a0f57d6 4eff17c a0f57d6 5262349 a0f57d6 dbd6fa0 a0f57d6 5262349 599725a a0f57d6 dbd6fa0 5262349 a0f57d6 dbd6fa0 5cac637 dbd6fa0 0df9635 a0f57d6 5262349 5cac637 a0f57d6 70dd0f7 a0f57d6 5262349 5cac637 a0f57d6 e8459e6 a0f57d6 e8459e6 a0f57d6 5262349 e8459e6 a0f57d6 e8459e6 a0f57d6 e8459e6 dbd6fa0 5262349 dbd6fa0 5262349 dbd6fa0 5262349 dbd6fa0 5262349 a0f57d6 95584d3 a0f57d6 e8459e6 dbd6fa0 a0f57d6 dbd6fa0 5262349 28d44a5 a0f57d6 95584d3 a0f57d6 dbd6fa0 5262349 dbd6fa0 5cac637 e8459e6 dbd6fa0 5262349 95584d3 5cac637 dbd6fa0 e8459e6 6dbcd3a dbd6fa0 28d44a5 dbd6fa0 0c740cf dbd6fa0 28d44a5 dbd6fa0 0c740cf dbd6fa0 28d44a5 dbd6fa0 5262349 dbd6fa0 5262349 dbd6fa0 5262349 0596125 dbd6fa0 5262349 0596125 dbd6fa0 5262349 0596125 dbd6fa0 5262349 0596125 5262349 dbd6fa0 5262349 95584d3 5262349 dbd6fa0 0596125 5262349 dbd6fa0 5262349 0596125 dbd6fa0 5262349 dbd6fa0 0596125 dbd6fa0 3290861 6dbcd3a dbd6fa0 6dbcd3a dbd6fa0 5262349 dbd6fa0 5262349 0596125 5262349 0596125 dbd6fa0 5262349 dbd6fa0 0596125 dbd6fa0 5262349 dbd6fa0 5262349 dbd6fa0 5262349 0596125 5262349 0596125 dbd6fa0 5262349 dbd6fa0 0596125 dbd6fa0 5262349 dbd6fa0 5cac637 dbd6fa0 5cac637 dbd6fa0 5262349 95584d3 0596125 5262349 95584d3 5262349 95584d3 dbd6fa0 28d44a5 0596125 5262349 0596125 dbd6fa0 5262349 dbd6fa0 5262349 dbd6fa0 5262349 dbd6fa0 5262349 0596125 5262349 0596125 dbd6fa0 5262349 dbd6fa0 5262349 dbd6fa0 0596125 dbd6fa0 0596125 dbd6fa0 5262349 0596125 5262349 0596125 dbd6fa0 5262349 dbd6fa0 5262349 dbd6fa0 5262349 dbd6fa0 5262349 0596125 5262349 0596125 dbd6fa0 5262349 dbd6fa0 5262349 dbd6fa0 5cac637 5262349 0596125 dbd6fa0 5262349 0596125 dbd6fa0 5262349 dbd6fa0 5262349 0596125 dbd6fa0 5262349 0596125 dbd6fa0 5262349 0596125 dbd6fa0 5262349 dbd6fa0 3290861 dbd6fa0 6dbcd3a dbd6fa0 3290861 dbd6fa0 3290861 5262349 0596125 5262349 0596125 3290861 e8459e6 dbd6fa0 5262349 dbd6fa0 5262349 dbd6fa0 5262349 dbd6fa0 28d44a5 dbd6fa0 5262349 dbd6fa0 5262349 0596125 5262349 0596125 dbd6fa0 5262349 dbd6fa0 95584d3 dbd6fa0 0596125 5262349 dbd6fa0 95584d3 0596125 a0f57d6 dbd6fa0 6dbcd3a 5cac637 5262349 95584d3 a0f57d6 dbd6fa0 9053015 5262349 9053015 a0f57d6 5262349 a0f57d6 dbd6fa0 e8459e6 5cac637 5262349 e8459e6 5262349 dbd6fa0 5262349 e8459e6 28d44a5 6dbcd3a e8459e6 5cac637 e8459e6 a0f57d6 dbd6fa0 9053015 a0f57d6 dbd6fa0 28d44a5 0c740cf a0f57d6 5cac637 5262349 9053015 a0f57d6 dbd6fa0 fa6da58 0c740cf fa6da58 0c740cf 28d44a5 0c740cf 28d44a5 dbd6fa0 28d44a5 60d69e9 0c740cf 9053015 28d44a5 60d69e9 28d44a5 e8459e6 60d69e9 28d44a5 60d69e9 dbd6fa0 28d44a5 9053015 60d69e9 dbd6fa0 5262349 a0f57d6 dbd6fa0 a0f57d6 dbd6fa0 a0f57d6 e8459e6 5262349 a0f57d6 e8459e6 5262349 a0f57d6 95584d3 dbd6fa0 a0f57d6 0596125 dbd6fa0 0596125 5262349 0596125 dbd6fa0 4eff17c 5262349 dbd6fa0 5cac637 dbd6fa0 |
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 |
import os
import re
import time
import json
import io
import requests
import gradio as gr
import google.generativeai as genai
from huggingface_hub import create_repo, list_models, upload_file, constants
from huggingface_hub.utils import build_hf_headers, get_session, hf_raise_for_status
# --- Helper functions for Hugging Face integration ---
def show_profile(profile: gr.OAuthProfile | None) -> str:
if profile is None:
return "*Not logged in.*"
return f"β
Logged in as **{profile.username}**"
def list_private_models(
profile: gr.OAuthProfile | None,
oauth_token: gr.OAuthToken | None
) -> str:
if profile is None or oauth_token is None:
return "Please log in to see your models."
try:
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)
except Exception as e:
return f"Error listing models: {e}"
def create_space_action(repo_name: str, sdk: str, profile: gr.OAuthProfile, token: gr.OAuthToken):
repo_id = f"{profile.username}/{repo_name}"
try:
create_repo(
repo_id=repo_id,
token=token.token,
exist_ok=True,
repo_type="space",
space_sdk=sdk
)
url = f"https://huggingface.co/spaces/{repo_id}"
iframe = f'<iframe src="{url}" width="100%" height="500px"></iframe>'
return repo_id, iframe
except Exception as e:
raise RuntimeError(f"Failed to create Space {repo_id}: {e}")
def upload_file_to_space_action(
file_obj,
path_in_repo: str,
repo_id: str,
profile: gr.OAuthProfile,
token: gr.OAuthToken
) -> None:
if not (profile and token and repo_id):
raise ValueError("Hugging Face profile, token, or repo_id is missing.")
try:
upload_file(
path_or_fileobj=file_obj,
path_in_repo=path_in_repo,
repo_id=repo_id,
token=token.token,
repo_type="space"
)
except Exception as e:
raise RuntimeError(f"Failed to upload `{path_in_repo}` to {repo_id}: {e}")
def _fetch_space_logs_level(repo_id: str, level: str, token: str) -> str:
if not repo_id or not token:
return f"Cannot fetch {level} logs: repo_id or token missing."
jwt_url = f"{constants.ENDPOINT}/api/spaces/{repo_id}/jwt"
try:
r = get_session().get(jwt_url, headers=build_hf_headers(token=token))
hf_raise_for_status(r)
jwt = r.json()["token"]
logs_url = f"https://api.hf.space/v1/{repo_id}/logs/{level}"
lines, count = [], 0
with get_session().get(logs_url, headers=build_hf_headers(token=jwt), stream=True, timeout=30) as resp:
hf_raise_for_status(resp)
for raw in resp.iter_lines():
if count >= 200:
lines.append("... truncated ...")
break
if not raw.startswith(b"data: "):
continue
payload = raw[len(b"data: "):]
try:
event = json.loads(payload.decode())
ts = event.get("timestamp", "")
txt = event.get("data", "").strip()
if txt:
lines.append(f"[{ts}] {txt}")
count += 1
except json.JSONDecodeError:
continue
return "\n".join(lines) if lines else f"No {level} logs found."
except Exception as e:
return f"Error fetching {level} logs: {e}"
def get_build_logs_action(repo_id, profile, token):
if not (repo_id and profile and token):
return "β οΈ Cannot fetch build logs: log in and create a Space first."
time.sleep(5)
return _fetch_space_logs_level(repo_id, "build", token.token)
def get_container_logs_action(repo_id, profile, token):
if not (repo_id and profile and token):
return "β οΈ Cannot fetch container logs: log in and create a Space first."
time.sleep(10)
return _fetch_space_logs_level(repo_id, "run", token.token)
# --- Google Gemini integration with model selection ---
def configure_gemini(api_key: str | None, model_name: str | None) -> str:
if not api_key:
return "Gemini API key is not set."
if not model_name:
return "Please select a Gemini model."
try:
genai.configure(api_key=api_key)
genai.GenerativeModel(model_name).generate_content("ping", stream=False)
return f"Gemini configured successfully with **{model_name}**."
except Exception as e:
return f"Error configuring Gemini: {e}"
def call_gemini(prompt: str, api_key: str, model_name: str) -> str:
if not api_key or not model_name:
raise ValueError("Gemini API key or model not set.")
try:
genai.configure(api_key=api_key)
model = genai.GenerativeModel(model_name)
response = model.generate_content(prompt, stream=False)
return response.text or ""
except Exception as e:
raise RuntimeError(f"Gemini API call failed: {e}")
# --- AI workflow logic (State Machine) ---
# Define States
STATE_IDLE = "idle"
STATE_AWAITING_REPO_NAME = "awaiting_repo_name"
STATE_CREATING_SPACE = "creating_space"
STATE_GENERATING_CODE = "generating_code"
STATE_UPLOADING_APP_PY = "uploading_app_py"
STATE_GENERATING_REQUIREMENTS = "generating_requirements"
STATE_UPLOADING_REQUIREMENTS = "uploading_requirements"
STATE_GENERATING_README = "generating_readme"
STATE_UPLOADING_README = "uploading_readme"
STATE_CHECKING_LOGS_BUILD = "checking_logs_build"
STATE_CHECKING_LOGS_RUN = "checking_logs_run"
STATE_DEBUGGING_CODE = "debugging_code"
STATE_UPLOADING_FIXED_APP_PY = "uploading_fixed_app_py"
STATE_COMPLETE = "complete"
MAX_DEBUG_ATTEMPTS = 3
def add_bot_message(history: list[dict], bot_message: str) -> list[dict]:
"""Helper to add a new assistant message to the history."""
history.append({"role": "assistant", "content": bot_message})
return history
def ai_workflow_chat(
message: str,
history: list[dict],
hf_profile: gr.OAuthProfile | None,
hf_token: gr.OAuthToken | None,
gemini_api_key: str | None,
gemini_model: str | None,
repo_id_state: str | None,
workflow_state: str,
space_sdk: str,
preview_html: str,
container_logs: str,
build_logs: str,
debug_attempts_state: int,
app_description_state: str | None,
repo_name_state: str | None,
generated_code_state: str | None,
*args,
**kwargs
) -> tuple[
list[dict],
str | None,
str,
str,
str,
str,
int,
str | None,
str | None,
str | None,
]:
# Unpack state variables
repo_id = repo_id_state
state = workflow_state
attempts = debug_attempts_state
app_desc = app_description_state
repo_name = repo_name_state
generated_code = generated_code_state
updated_preview = preview_html
updated_build = build_logs
updated_run = container_logs
# Add user message to history in the new format
user_message_entry = {"role": "user", "content": message}
if hf_profile and hf_profile.username:
user_message_entry["name"] = hf_profile.username
history.append(user_message_entry)
# Yield immediately to show user message
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
try:
# --- State Machine Logic ---
if state == STATE_IDLE:
# Check prerequisites within the state logic as well
if not (hf_profile and hf_token):
history = add_bot_message(history, "Workflow paused: Please log in to Hugging Face first.")
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
return
if not (gemini_api_key and gemini_model):
history = add_bot_message(history, "Workflow paused: Please enter your API key and select a Gemini model.")
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
return
# Look for commands
reset_match = "reset" in message.lower()
generate_match = re.search(r'generate (?:me )?(?:a|an) \w+ app called (\w+)', message, re.I)
create_match = re.search(r'create (?:a|an)? space called (\w+)', message, re.I)
if reset_match:
history = add_bot_message(history, "Workflow reset.")
yield history, None, STATE_IDLE, "<p>No Space created yet.</p>", "", "", 0, None, None, None
return
elif generate_match:
new_repo_name = generate_match.group(1)
new_app_desc = message
history = add_bot_message(history, f"Acknowledged: '{message}'. Starting workflow to create Space `{hf_profile.username}/{new_repo_name}`.")
state = STATE_CREATING_SPACE
repo_name = new_repo_name
app_desc = new_app_desc
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
elif create_match:
new_repo_name = create_match.group(1)
history = add_bot_message(history, f"Acknowledged: '{message}'. Starting workflow to create Space `{hf_profile.username}/{new_repo_name}`.")
state = STATE_CREATING_SPACE
repo_name = new_repo_name
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
elif "create" in message.lower() and not repo_id:
history = add_bot_message(history, "Okay, what should the Space be called? (e.g., `my-awesome-app`)")
state = STATE_AWAITING_REPO_NAME
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
else:
history = add_bot_message(history, "Command not recognized. Try 'generate me a gradio app called myapp', or 'reset'.")
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
return
if state == STATE_AWAITING_REPO_NAME:
new_repo_name = message.strip()
if not new_repo_name or re.search(r'[^a-zA-Z0-9_-]', new_repo_name):
history = add_bot_message(history, "Invalid name. Please provide a single word/slug for the Space name (letters, numbers, underscores, hyphens only).")
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
else:
history = add_bot_message(history, f"Using Space name `{new_repo_name}`. Creating Space `{hf_profile.username}/{new_repo_name}`...")
state = STATE_CREATING_SPACE
repo_name = new_repo_name
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
elif state == STATE_CREATING_SPACE:
if not repo_name:
history = add_bot_message(history, "Internal error: Repo name missing for creation. Resetting.")
yield history, None, STATE_IDLE, "<p>Error creating space.</p>", "", "", 0, None, None, None
return
try:
new_repo_id, iframe_html = create_space_action(repo_name, space_sdk, hf_profile, hf_token)
updated_preview = iframe_html
repo_id = new_repo_id
history = add_bot_message(history, f"β
Space `{repo_id}` created. Click 'Send' to generate and upload code.")
state = STATE_GENERATING_CODE
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
except Exception as e:
history = add_bot_message(history, f"β Error creating space: {e}. Click 'reset'.")
yield history, None, STATE_IDLE, "<p>Error creating space.</p>", "", "", 0, None, None, None
elif state == STATE_GENERATING_CODE:
prompt_desc = app_desc if app_desc else 'a Gradio image-blur test app with upload and slider controls'
prompt = f"""
You are an AI assistant specializing in Hugging Face Spaces using the {space_sdk} SDK.
Generate a full, single-file Python app based on:
'{prompt_desc}'
Return **only** the python code block for app.py. Do not include any extra text, explanations, or markdown outside the code block.
"""
try:
history = add_bot_message(history, "π§ Generating `app.py` code with Gemini...")
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
code = call_gemini(prompt, gemini_api_key, gemini_model)
code = code.strip()
if code.startswith("```python"):
code = code[len("```python"):].strip()
if code.endswith("```"):
code = code[:-len("```")].strip()
if not code:
raise ValueError("Gemini returned empty code.")
history = add_bot_message(history, "β
`app.py` code generated. Click 'Send' to upload.")
state = STATE_UPLOADING_APP_PY
generated_code = code
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
except Exception as e:
history = add_bot_message(history, f"β Error generating code: {e}. Click 'reset'.")
yield history, None, STATE_IDLE, updated_preview, updated_run, updated_build, 0, None, None, None
elif state == STATE_UPLOADING_APP_PY:
if not generated_code:
history = add_bot_message(history, "Internal error: No code to upload. Resetting.")
yield history, None, STATE_IDLE, updated_preview, updated_run, updated_build, 0, None, None, None
return
history = add_bot_message(history, "βοΈ Uploading `app.py`...")
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
try:
upload_file_to_space_action(io.StringIO(generated_code), "app.py", repo_id, hf_profile, hf_token)
history = add_bot_message(history, "β
Uploaded `app.py`. Click 'Send' to generate requirements.")
state = STATE_GENERATING_REQUIREMENTS
generated_code = None
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
except Exception as e:
history = add_bot_message(history, f"β Error uploading app.py: {e}. Click 'reset'.")
yield history, None, STATE_IDLE, updated_preview, updated_run, updated_build, 0, None, None, None
elif state == STATE_GENERATING_REQUIREMENTS:
history = add_bot_message(history, "π Generating `requirements.txt`...")
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
reqs_list = ["gradio"] if space_sdk == "gradio" else ["streamlit"]
if "google.generativeai" in str(app_desc).lower() or "gemini" in str(app_desc).lower() or gemini_api_key:
reqs_list.append("google-generativeai")
if "requests" in str(app_desc).lower():
reqs_list.append("requests")
reqs_list.append("huggingface_hub")
if "image" in str(app_desc).lower() or "upload" in str(app_desc).lower() or "blur" in str(app_desc).lower() or "vision" in str(app_desc).lower():
reqs_list.append("Pillow")
if "numpy" in str(app_desc).lower(): reqs_list.append("numpy")
if "pandas" in str(app_desc).lower(): reqs_list.append("pandas")
if "scikit-image" in str(app_desc).lower() or "skimage" in str(app_desc).lower() or "cv2" in str(app_desc).lower() or "opencv-python" in str(app_desc).lower():
reqs_list.append("scikit-image")
reqs_list.append("opencv-python")
reqs_list = list(dict.fromkeys(reqs_list))
reqs_content = "\n".join(reqs_list) + "\n"
history = add_bot_message(history, "β
`requirements.txt` generated. Click 'Send' to upload.")
state = STATE_UPLOADING_REQUIREMENTS
generated_code = reqs_content
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
elif state == STATE_UPLOADING_REQUIREMENTS:
reqs_content_to_upload = generated_code
if not reqs_content_to_upload:
history = add_bot_message(history, "Internal error: No requirements content to upload. Resetting.")
yield history, None, STATE_IDLE, updated_preview, updated_run, updated_build, 0, None, None, None
return
history = add_bot_message(history, "βοΈ Uploading `requirements.txt`...")
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
try:
upload_file_to_space_action(io.StringIO(reqs_content_to_upload), "requirements.txt", repo_id, hf_profile, hf_token)
history = add_bot_message(history, "β
Uploaded `requirements.txt`. Click 'Send' to generate README.")
state = STATE_GENERATING_README
generated_code = None
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
except Exception as e:
history = add_bot_message(history, f"β Error uploading requirements.txt: {e}. Click 'reset'.")
yield history, None, STATE_IDLE, updated_preview, updated_run, updated_build, 0, None, None, None
elif state == STATE_GENERATING_README:
history = add_bot_message(history, "π Generating `README.md`...")
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
readme_title = repo_name if repo_name else "My Awesome Space"
readme_description = app_desc if app_desc else f"This Hugging Face Space hosts an AI-generated {space_sdk} application."
readme_content = f"# {readme_title}\n\n{readme_description}\n\n" \
"This Space was automatically generated by an AI workflow.\n\n" \
f"Built with the {space_sdk} SDK.\n"
history = add_bot_message(history, "β
`README.md` generated. Click 'Send' to upload.")
state = STATE_UPLOADING_README
generated_code = readme_content
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
elif state == STATE_UPLOADING_README:
readme_content_to_upload = generated_code
if not readme_content_to_upload:
history = add_bot_message(history, "Internal error: No README content to upload. Resetting.")
yield history, None, STATE_IDLE, updated_preview, updated_run, updated_build, 0, None, None, None
return
history = add_bot_message(history, "βοΈ Uploading `README.md`...")
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
try:
upload_file_to_space_action(io.StringIO(readme_content_to_upload), "README.md", repo_id, hf_profile, hf_token)
history = add_bot_message(history, "β
Uploaded `README.md`. All files uploaded. Space is now building. Click 'Send' to check build logs.")
state = STATE_CHECKING_LOGS_BUILD
generated_code = None
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
except Exception as e:
history = add_bot_message(history, f"β Error uploading README.md: {e}. Click 'reset'.")
yield history, None, STATE_IDLE, updated_preview, updated_run, updated_build, 0, None, None, None
elif state == STATE_CHECKING_LOGS_BUILD:
history = add_bot_message(history, "π Fetching build logs...")
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
build_logs_text = get_build_logs_action(repo_id, hf_profile, hf_token)
updated_build = build_logs_text
if "Error" in updated_build or "Exception" in updated_build or "Build failed" in updated_build:
history = add_bot_message(history, "β οΈ Build logs indicate potential issues. Please inspect above. Click 'Send' to check container logs (app might still start).")
state = STATE_CHECKING_LOGS_RUN
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
else:
history = add_bot_message(history, "β
Build logs fetched. Click 'Send' to check container logs.")
state = STATE_CHECKING_LOGS_RUN
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
elif state == STATE_CHECKING_LOGS_RUN:
history = add_bot_message(history, "π Fetching container logs...")
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
container_logs_text = get_container_logs_action(repo_id, hf_profile, hf_token)
updated_run = container_logs_text
if ("Error" in updated_run or "Exception" in updated_run) and attempts < MAX_DEBUG_ATTEMPTS:
attempts += 1
history = add_bot_message(history, f"β Errors detected in container logs. Attempting debug fix #{attempts}/{MAX_DEBUG_ATTEMPTS}. Click 'Send' to proceed.")
state = STATE_DEBUGGING_CODE
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
elif ("Error" in updated_run or "Exception" in updated_run) and attempts >= MAX_DEBUG_ATTEMPTS:
history = add_bot_message(history, f"β Errors detected in container logs. Max debug attempts ({MAX_DEBUG_ATTEMPTS}) reached. Please inspect logs manually or click 'reset'.")
state = STATE_COMPLETE
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
else:
history = add_bot_message(history, "β
App appears to be running successfully! Check the iframe above. Click 'reset' to start a new project.")
state = STATE_COMPLETE
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
elif state == STATE_DEBUGGING_CODE:
history = add_bot_message(history, f"π§ Calling Gemini to generate fix based on logs...")
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
debug_prompt = f"""
You are debugging a {space_sdk} Space. The goal is to fix the code in `app.py` based on the container logs provided.
Here are the container logs:
{updated_run}
Generate the *complete, fixed* content for `app.py` based on these logs.
Return **only** the python code block for app.py. Do not include any extra text, explanations, or markdown outside the code block.
"""
try:
fix_code = call_gemini(debug_prompt, gemini_api_key, gemini_model)
fix_code = fix_code.strip()
if fix_code.startswith("```python"):
fix_code = fix_code[len("```python"):].strip()
if fix_code.endswith("```"):
fix_code = fix_code[:-len("```")].strip()
if not fix_code:
raise ValueError("Gemini returned empty fix code.")
history = add_bot_message(history, "β
Fix code generated. Click 'Send' to upload.")
state = STATE_UPLOADING_FIXED_APP_PY
generated_code = fix_code
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
except Exception as e:
history = add_bot_message(history, f"β Error generating debug code: {e}. Click 'reset'.")
yield history, None, STATE_IDLE, updated_preview, updated_run, updated_build, 0, None, None, None
elif state == STATE_UPLOADING_FIXED_APP_PY:
fixed_code_to_upload = generated_code
if not fixed_code_to_upload:
history = add_bot_message(history, "Internal error: No fixed code available to upload. Resetting.")
yield history, None, STATE_IDLE, updated_preview, updated_run, updated_build, 0, None, None, None
return
history = add_bot_message(history, "βοΈ Uploading fixed `app.py`...")
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
try:
upload_file_to_space_action(io.StringIO(fixed_code_to_upload), "app.py", repo_id, hf_profile, hf_token)
history = add_bot_message(history, "β
Fixed `app.py` uploaded. Space will rebuild. Click 'Send' to check logs again.")
state = STATE_CHECKING_LOGS_RUN
generated_code = None
yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
except Exception as e:
history = add_bot_message(history, f"β Error uploading fixed app.py: {e}. Click 'reset'.")
yield history, None, STATE_IDLE, updated_preview, updated_run, updated_build, 0, None, None, None
elif state == STATE_COMPLETE:
pass
except Exception as e:
error_message = f"Workflow step failed unexpectedly ({state}): {e}. Click 'Send' to re-attempt this step or 'reset'."
history = add_bot_message(history, error_message)
print(f"Critical Error in state {state}: {e}")
yield history, None, STATE_IDLE, updated_preview, updated_run, updated_build, 0, None, None, None
return
# --- Build the Gradio UI ---
with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
# State variables
hf_profile = gr.State(None)
hf_token = gr.State(None)
gemini_key = gr.State(None)
gemini_model = gr.State("gemini-1.5-flash")
repo_id = gr.State(None)
workflow = gr.State(STATE_IDLE)
sdk_state = gr.State("gradio")
debug_attempts = gr.State(0)
app_description = gr.State(None)
repo_name_state = gr.State(None)
generated_code_state = gr.State(None)
with gr.Row():
# Sidebar
with gr.Column(scale=1, min_width=300):
gr.Markdown("## Hugging Face Login")
login_status = gr.Markdown("*Not logged in.*")
login_btn = gr.LoginButton(variant="huggingface")
# Initial load to check login status
ai_builder_tab.load(show_profile, outputs=login_status)
# Update status on login click
login_btn.click(show_profile, outputs=login_status)
gr.Markdown("## Google AI Studio API Key")
gemini_input = gr.Textbox(label="API Key", type="password", interactive=True)
gemini_status = gr.Markdown("")
gr.Markdown("## Gemini Model")
model_selector = gr.Radio(
choices=[
("Gemini 1.5 Flash", "gemini-1.5-flash"),
("Gemini 1.5 Pro", "gemini-1.5-pro"),
("Gemini 1.0 Pro", "gemini-1.0-pro"),
],
value="gemini-1.5-flash",
label="Select model",
interactive=True
)
# Configure Gemini status on load
ai_builder_tab.load(
configure_gemini,
inputs=[gemini_key, gemini_model],
outputs=[gemini_status]
)
gr.Markdown("## Space SDK")
sdk_selector = gr.Radio(choices=["gradio","streamlit"], value="gradio", label="Template SDK", interactive=True)
sdk_selector.change(lambda s: s, inputs=sdk_selector, outputs=sdk_state)
gr.Markdown("## Workflow Status")
status_text = gr.Textbox(label="Current State", value=STATE_IDLE, interactive=False)
repo_id_text = gr.Textbox(label="Current Space ID", value="None", interactive=False)
# --- Debugging Indicator ---
prereq_status = gr.Markdown("Checking...")
# Main content
with gr.Column(scale=3):
chatbot = gr.Chatbot(type='messages', label="AI Workflow Chat")
user_input = gr.Textbox(placeholder="Type your messageβ¦", interactive=True)
send_btn = gr.Button("Send", interactive=False)
# Logic to enable send button only when logged in and API key is set
def update_send_button_state(profile: gr.OAuthProfile | None, token: gr.OAuthToken | None, key: str | None, model: str | None, *args, **kwargs):
"""
Gradio sometimes passes extra positional/keyword args (old_value, event, etc.),
so we absorb them with *args/**kwargs.
Returns Gradio.update for the button's interactive state and a status string.
"""
is_logged_in = profile is not None and token is not None
is_gemini_ready = key is not None and model is not None
# Print states for debugging
print(f"update_send_button_state called:")
print(f" Profile: {profile.username if profile else None}")
print(f" Token: {'Set' if token else 'None'}")
print(f" API Key: {'Set' if key else 'None'}")
print(f" Model: {model}")
print(f" Logged in: {is_logged_in}, Gemini Ready: {is_gemini_ready}")
status_str = ""
if is_logged_in and is_gemini_ready:
status_str = "β
Ready to send commands."
else: # Consolidated status checks
status_parts = []
if not is_logged_in:
status_parts.append("β οΈ Not logged in to Hugging Face.")
if not key:
status_parts.append("β οΈ Gemini API key not set.")
if not model:
status_parts.append("β οΈ Gemini model not selected.")
status_str = " ".join(status_parts)
if not status_str: # Should be ready if status_parts is empty
status_str = "β
Ready to send commands."
return gr.update(interactive=is_logged_in and is_gemini_ready), status_str
# --- Implement Chained Events ---
# 1. Login Button: Update profile/token state, THEN update send button state
login_btn.click(
lambda x: (x[0], x[1]), # Extract profile and token from LoginButton output
inputs=[login_btn],
outputs=[hf_profile, hf_token]
).then( # Chain the next action after state is updated
update_send_button_state,
inputs=[hf_profile, hf_token, gemini_key, gemini_model],
outputs=[send_btn, prereq_status]
)
# 2. Gemini Key Input: Update key state, THEN configure Gemini, THEN update send button state
gemini_input.change(
lambda k: k, # Update gemini_key state
inputs=[gemini_input],
outputs=[gemini_key]
).then( # Chain configure_gemini after key is updated
configure_gemini,
inputs=[gemini_key, gemini_model],
outputs=[gemini_status]
).then( # Chain update_send_button_state after config status is updated
update_send_button_state,
inputs=[hf_profile, hf_token, gemini_key, gemini_model],
outputs=[send_btn, prereq_status]
)
# 3. Gemini Model Selector: Update model state, THEN configure Gemini, THEN update send button state
model_selector.change(
lambda m: m, # Update gemini_model state
inputs=[model_selector],
outputs=[gemini_model]
).then( # Chain configure_gemini after model is updated
configure_gemini,
inputs=[gemini_key, gemini_model],
outputs=[gemini_status]
).then( # Chain update_send_button_state after config status is updated
update_send_button_state,
inputs=[hf_profile, hf_token, gemini_key, gemini_model],
outputs=[send_btn, prereq_status]
)
# 4. Initial Load: Update send button state based on initial (or cached) state
ai_builder_tab.load(
update_send_button_state,
inputs=[hf_profile, hf_token, gemini_key, gemini_model],
outputs=[send_btn, prereq_status]
)
iframe = gr.HTML("<p>No Space created yet.</p>")
build_txt = gr.Textbox(label="Build Logs", lines=10, interactive=False, value="")
run_txt = gr.Textbox(label="Container Logs", lines=10, interactive=False, value="")
# The main event handler for the Send button
send_btn.click(
ai_workflow_chat,
inputs=[
user_input, chatbot,
hf_profile, hf_token,
gemini_key, gemini_model,
repo_id, workflow, sdk_state,
iframe, run_txt, build_txt,
debug_attempts, app_description, repo_name_state, generated_code_state
],
outputs=[
chatbot,
repo_id, workflow,
iframe, run_txt, build_txt,
debug_attempts, app_description, repo_name_state, generated_code_state
]
).success(
lambda: gr.update(value=""),
inputs=None,
outputs=user_input
)
# Link state variables to UI status displays (reactive updates)
workflow.change(lambda s: s, inputs=workflow, outputs=status_text)
repo_id.change(lambda r: r if r else "None", inputs=repo_id, outputs=repo_id_text)
# Add an initial message to the chatbot on load
def greet():
return [{"role": "assistant", "content": "Welcome! Please log in to Hugging Face and provide your Google AI Studio API key to start building Spaces. Once ready, type 'generate me a gradio app called myapp' or 'create' to begin."}]
ai_builder_tab.load(greet, outputs=chatbot)
if __name__ == "__main__":
# Optional: Configure retries for huggingface_hub requests
# from requests.adapters import HTTPAdapter
# from urllib3.util.retry import Retry
# retry_strategy = Retry(total=5, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504])
# adapter = HTTPAdapter(max_retries=retry_strategy)
# get_session().mount("http://", adapter)
# get_session().mount("https://", adapter)
ai_builder_tab.launch() |