Spaces:
Running on Zero
Running on Zero
Upload 130 files
Browse filesFiles changed (3)
- app.py+28-11
- obliteratus/abliterate.py+33-2
- obliteratus/tourney.py+1-1
1510 unmodified lines151115121513151415151516151726 unmodified lines154415451546154715481549155043 unmodified lines1594159515961597159815991600152 unmodified lines17531754175517561757175817591738 unmodified lines349834993500350135023503350435053506350735083509645 unmodified lines415541564157415841594160416144 unmodified lines4206420742084209421042114212369 unmodified lines45824583458445854586458745881510 unmodified lines@spaces.GPU(duration=300)def obliterate(model_choice: str, method_choice: str, hub_repo: str,prompt_volume_choice: str, dataset_source_choice: str,custom_harmful: str, custom_harmless: str,# Advanced params (sliders)26 unmodified linesmodel_id = MODELS.get(model_choice, model_choice)is_preset = model_choice in MODELSmethod = METHODS.get(method_choice, "advanced")push_to_hub = hub_repo.strip() if hub_repo and hub_repo.strip() else Noneprompt_volume = PROMPT_VOLUMES.get(prompt_volume_choice, 33)# Resolve "adaptive" β telemetry-recommended method for this model43 unmodified lines# Early validation: Hub repo format + HF_TOKENif push_to_hub:if not re.match(r'^[a-zA-Z0-9_-]+/[a-zA-Z0-9_.-]+$', push_to_hub):yield ("**Error:** Invalid Hub repo format. Use `username/model-name`.","", gr.update(), gr.update(), gr.update(), gr.update(),152 unmodified linesvol_label = "all" if prompt_volume == -1 else str(prompt_volume)log_lines.append(f"Prompt volume: {vol_label} pairs")if push_to_hub:log_lines.append(f"Push to Hub: {push_to_hub}")if quantization:log_lines.append(f"Quantization: {quantization} (auto-detected for GPU fit)")log_lines.append("")1738 unmodified lines)with gr.Row():hub_repo = gr.Textbox(label="Push to Hub (optional)",placeholder="your-username/model-name-abliterated",info="HF Hub repo ID β saves locally then uploads. ""Requires HF_TOKEN env var with write access.",)hub_warning_md = gr.Markdown("")# ββ Advanced Settings (auto-populated from method preset) ββββ645 unmodified linestourney_hub_org = gr.Textbox(label="HF Hub Org (optional)",placeholder="my-org",info="Push winner to hub-org/model-name-obliterated",)with gr.Column(scale=1):tourney_hub_repo = gr.Textbox(44 unmodified lines# Resolve display label β HuggingFace model IDmodel_id = model_choice.strip()if model_id in MODELS:model_id = MODELS[model_id]["id"]hub_org_val = hub_org.strip() if hub_org and hub_org.strip() else Nonehub_repo_val = hub_repo.strip() if hub_repo and hub_repo.strip() else None369 unmodified lines# may not fire after generator teardown.obliterate_btn.click(fn=obliterate,inputs=[model_dd, method_dd, hub_repo, prompt_vol_dd, dataset_dd,custom_harmful_tb, custom_harmless_tb] + _adv_controls,outputs=[status_md, log_box, chat_status, session_model_dd, metrics_md, ab_session_model_dd],).then(1510 unmodified lines1511151215131514151515161517151826 unmodified lines1545154615471548154915501551155215531554155515561557155843 unmodified lines1602160316041605160616071608152 unmodified lines17611762176317641765176617671768176917701738 unmodified lines350935103511351235133514351535163517351835193520352135223523352435253526645 unmodified lines417241734174417541764177417844 unmodified lines4223422442254226422742284229369 unmodified lines45994600460146024603460446051510 unmodified lines@spaces.GPU(duration=300)def obliterate(model_choice: str, method_choice: str,hub_auto_push: bool, hub_repo: str,prompt_volume_choice: str, dataset_source_choice: str,custom_harmful: str, custom_harmless: str,# Advanced params (sliders)26 unmodified linesmodel_id = MODELS.get(model_choice, model_choice)is_preset = model_choice in MODELSmethod = METHODS.get(method_choice, "advanced")# Resolve push-to-hub: explicit repo overrides auto-naming_hub_override = hub_repo.strip() if hub_repo and hub_repo.strip() else Noneif _hub_override:push_to_hub = _hub_overrideelif hub_auto_push:push_to_hub = "auto" # resolved to {user}/{model}-OBLITERATED at push timeelse:push_to_hub = Noneprompt_volume = PROMPT_VOLUMES.get(prompt_volume_choice, 33)# Resolve "adaptive" β telemetry-recommended method for this model43 unmodified lines# Early validation: Hub repo format + HF_TOKENif push_to_hub:if push_to_hub != "auto" and not re.match(r'^[a-zA-Z0-9_-]+/[a-zA-Z0-9_.-]+$', push_to_hub):yield ("**Error:** Invalid Hub repo format. Use `username/model-name`.","", gr.update(), gr.update(), gr.update(), gr.update(),152 unmodified linesvol_label = "all" if prompt_volume == -1 else str(prompt_volume)log_lines.append(f"Prompt volume: {vol_label} pairs")if push_to_hub:if push_to_hub == "auto":log_lines.append("Push to Hub: auto ({you}/{model}-OBLITERATED)")else:log_lines.append(f"Push to Hub: {push_to_hub}")if quantization:log_lines.append(f"Quantization: {quantization} (auto-detected for GPU fit)")log_lines.append("")1738 unmodified lines)with gr.Row():hub_auto_push = gr.Checkbox(label="Auto-push to Hub",value=False,info="Auto-names your model {you}/{model}-OBLITERATED and pushes to HF Hub. ""Requires HF_TOKEN env var with write access.",)hub_repo = gr.Textbox(label="Push to Hub (optional override)",placeholder="auto-filled when checkbox is ticked, or type your own",info="Leave blank with checkbox ticked for auto-naming, ""or enter a custom repo ID (e.g. your-username/my-model).",)hub_warning_md = gr.Markdown("")# ββ Advanced Settings (auto-populated from method preset) ββββ645 unmodified linestourney_hub_org = gr.Textbox(label="HF Hub Org (optional)",placeholder="my-org",info="Push winner to hub-org/model-name-OBLITERATED",)with gr.Column(scale=1):tourney_hub_repo = gr.Textbox(44 unmodified lines# Resolve display label β HuggingFace model IDmodel_id = model_choice.strip()if model_id in MODELS:model_id = MODELS[model_id]hub_org_val = hub_org.strip() if hub_org and hub_org.strip() else Nonehub_repo_val = hub_repo.strip() if hub_repo and hub_repo.strip() else None369 unmodified lines# may not fire after generator teardown.obliterate_btn.click(fn=obliterate,inputs=[model_dd, method_dd, hub_auto_push, hub_repo, prompt_vol_dd, dataset_dd,custom_harmful_tb, custom_harmless_tb] + _adv_controls,outputs=[status_md, log_box, chat_status, session_model_dd, metrics_md, ab_session_model_dd],).then(
503 unmodified lines5045055065075085095411 unmodified lines59215922592359245925592659275928592959305931503 unmodified linesdetails: dict[str, Any] = field(default_factory=dict)# ββ Main pipeline βββββββββββββββββββββββββββββββββββββββββββββββββββββββclass AbliterationPipeline:5411 unmodified lines# 5. Optionally push the saved directory to the Hub.if self.push_to_hub:repo_id = self.push_to_hubself.log(f"Uploading to Hub: {repo_id}")from huggingface_hub import HfApiapi = HfApi()api.create_repo(repo_id, exist_ok=True)api.upload_folder(folder_path=str(self.output_dir),503 unmodified lines5045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345411 unmodified lines59465947594859495950595159525953595459555956595759585959596059615962503 unmodified linesdetails: dict[str, Any] = field(default_factory=dict)def auto_hub_repo_id(model_name: str, *, api=None) -> str:"""Generate a Hub repo ID like ``{hf_user}/{short_model}-OBLITERATED``.Resolves the authenticated HF username via the API, and extracts ashort model name (e.g. ``meta-llama/Llama-3-8B`` β ``Llama-3-8B``)."""import reif api is None:from huggingface_hub import HfApiapi = HfApi()# Resolve HF username from tokenuser_info = api.whoami()username = user_info.get("name") or user_info.get("user", "unknown")# Extract short model name (part after '/')short = model_name.split("/")[-1] if "/" in model_name else model_name# Sanitize: keep alphanumeric, hyphens, dotsshort = re.sub(r"[^a-zA-Z0-9\-.]", "-", short)short = re.sub(r"-+", "-", short).strip("-")return f"{username}/{short}-OBLITERATED"# ββ Main pipeline βββββββββββββββββββββββββββββββββββββββββββββββββββββββclass AbliterationPipeline:5411 unmodified lines# 5. Optionally push the saved directory to the Hub.if self.push_to_hub:from huggingface_hub import HfApiapi = HfApi()# Resolve "auto" β {hf_user}/{short_model}-OBLITERATEDif self.push_to_hub == "auto":repo_id = auto_hub_repo_id(self.model_name, api=api)self.log(f"Auto-named Hub repo: {repo_id}")else:repo_id = self.push_to_hubself.log(f"Uploading to Hub: {repo_id}")api.create_repo(repo_id, exist_ok=True)api.upload_folder(folder_path=str(self.output_dir),
556 unmodified lines557558559560561562563556 unmodified linesif self.hub_repo:repo_id = self.hub_repoelse:repo_id = f"{self.hub_org}/{short_model}-obliterated"result.hub_repo = repo_idself.log(f"\nPushing winner to Hub: {repo_id}")556 unmodified lines557558559560561562563556 unmodified linesif self.hub_repo:repo_id = self.hub_repoelse:repo_id = f"{self.hub_org}/{short_model}-OBLITERATED"result.hub_repo = repo_idself.log(f"\nPushing winner to Hub: {repo_id}")
app.py+28-11
obliteratus/abliterate.py+33-2
obliteratus/tourney.py+1-1