Spaces:
Build error
Build error
Austin Stockbridge
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModel
|
| 3 |
+
from safetensors.torch import load_file, save_file
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Function to download and modify model
|
| 8 |
+
def add_fuzz_to_model(model_name, min_val, max_val):
|
| 9 |
+
# Download model weights from Hugging Face
|
| 10 |
+
model = AutoModel.from_pretrained(model_name, use_safetensors=True)
|
| 11 |
+
model_file = f"{model_name}.safetensors"
|
| 12 |
+
model.save_pretrained(".", safe_serialization=True) # Save locally for access
|
| 13 |
+
|
| 14 |
+
# Load safetensors
|
| 15 |
+
weights = load_file(model_file)
|
| 16 |
+
|
| 17 |
+
# Apply fuzz to each weight tensor
|
| 18 |
+
def add_fuzz(tensor, min_val, max_val):
|
| 19 |
+
noise = torch.empty_like(tensor).uniform_(min_val, max_val)
|
| 20 |
+
return tensor + noise
|
| 21 |
+
|
| 22 |
+
modified_weights = {}
|
| 23 |
+
for key, tensor in weights.items():
|
| 24 |
+
modified_weights[key] = add_fuzz(tensor, min_val, max_val)
|
| 25 |
+
|
| 26 |
+
# Save modified weights
|
| 27 |
+
modified_file = f"{model_name}_fuzzed.safetensors"
|
| 28 |
+
save_file(modified_weights, modified_file)
|
| 29 |
+
|
| 30 |
+
return f"Model processed and saved as {modified_file}"
|
| 31 |
+
|
| 32 |
+
# Gradio UI
|
| 33 |
+
def fuzz_weights_ui():
|
| 34 |
+
with gr.Blocks() as interface:
|
| 35 |
+
gr.Markdown("### Add Fuzz to Hugging Face Model Weights")
|
| 36 |
+
|
| 37 |
+
with gr.Row():
|
| 38 |
+
model_name = gr.Textbox(label="Hugging Face Model Name", placeholder="e.g., gpt2")
|
| 39 |
+
|
| 40 |
+
with gr.Row():
|
| 41 |
+
min_val = gr.Slider(-0.5, 0.0, value=-0.1, step=0.01, label="Minimum Fuzz")
|
| 42 |
+
max_val = gr.Slider(0.0, 0.5, value=0.1, step=0.01, label="Maximum Fuzz")
|
| 43 |
+
|
| 44 |
+
with gr.Row():
|
| 45 |
+
process_button = gr.Button("Apply Fuzz")
|
| 46 |
+
result = gr.Textbox(label="Result")
|
| 47 |
+
|
| 48 |
+
# Define interaction
|
| 49 |
+
process_button.click(
|
| 50 |
+
fn=add_fuzz_to_model,
|
| 51 |
+
inputs=[model_name, min_val, max_val],
|
| 52 |
+
outputs=[result],
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
return interface
|
| 56 |
+
|
| 57 |
+
# Launch the app
|
| 58 |
+
interface = fuzz_weights_ui()
|
| 59 |
+
interface.launch()
|