Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,164 +1,207 @@
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import streamlit as st
|
| 4 |
-
from huggingface_hub import snapshot_download,
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
st.session_state.upload_to_hf = False
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
path = os.path.abspath(directory_name)
|
| 14 |
-
return str(path)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def download_model(hf_model_name, output_dir="/tmp/models"):
|
| 25 |
-
""
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
st.success("β
Model downloaded
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
"""
|
| 37 |
-
st.write(f"π Converting `{model_dir}` to GGUF format...")
|
| 38 |
-
os.makedirs(os.path.dirname(output_file), exist_ok=True)
|
| 39 |
cmd = [
|
| 40 |
-
"python3",
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
]
|
| 43 |
-
|
| 44 |
-
if
|
| 45 |
-
st.
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def
|
| 50 |
-
"""
|
| 51 |
-
|
| 52 |
-
""
|
| 53 |
-
st.write(f"β‘ Quantizing `{model_path}` with `{quant_type}` precision...")
|
| 54 |
-
os.makedirs(os.path.dirname(quantized_output_path), exist_ok=True)
|
| 55 |
-
quantize_path = "/app/llama.cpp/build/bin/llama-quantize"
|
| 56 |
-
|
| 57 |
cmd = [
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
quant_type
|
| 62 |
]
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
if
|
| 67 |
-
st.
|
| 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 |
-
st.
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
st.
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
st.session_state.
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import streamlit as st
|
| 4 |
+
from huggingface_hub import snapshot_download, HfApi
|
| 5 |
|
| 6 |
+
# ============================================================
|
| 7 |
+
# SESSION STATE
|
| 8 |
+
# ============================================================
|
|
|
|
| 9 |
|
| 10 |
+
if "quantized_models" not in st.session_state:
|
| 11 |
+
st.session_state.quantized_models = []
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# ============================================================
|
| 14 |
+
# CONFIG
|
| 15 |
+
# ============================================================
|
| 16 |
|
| 17 |
+
MODELS_LIST = ['rahul7star/Qwen3-4B-Thinking-2509-Genius-Coder-AI-Full']
|
| 18 |
|
| 19 |
+
QUANT_TYPES = [
|
| 20 |
+
"Q2_K", "Q3_K_l", "Q3_K_M", "Q3_K_S",
|
| 21 |
+
"Q4_0", "Q4_1", "Q4_K_M", "Q4_K_S",
|
| 22 |
+
"Q5_0", "Q5_1", "Q5_K_M", "Q5_K_S",
|
| 23 |
+
"Q6_K", "Q8_0", "BF16", "F16", "F32"
|
| 24 |
+
]
|
| 25 |
|
| 26 |
+
LLAMA_CPP_PATH = "/app/llama.cpp"
|
| 27 |
+
CONVERT_SCRIPT = f"{LLAMA_CPP_PATH}/convert_hf_to_gguf.py"
|
| 28 |
+
QUANTIZE_BIN = f"{LLAMA_CPP_PATH}/build/bin/llama-quantize"
|
| 29 |
+
|
| 30 |
+
# ============================================================
|
| 31 |
+
# UTILS
|
| 32 |
+
# ============================================================
|
| 33 |
+
|
| 34 |
+
def check_dependencies():
|
| 35 |
+
if not os.path.exists(CONVERT_SCRIPT):
|
| 36 |
+
st.error("β convert_hf_to_gguf.py not found")
|
| 37 |
+
st.stop()
|
| 38 |
+
if not os.path.exists(QUANTIZE_BIN):
|
| 39 |
+
st.error("β llama-quantize binary not found")
|
| 40 |
+
st.stop()
|
| 41 |
|
| 42 |
def download_model(hf_model_name, output_dir="/tmp/models"):
|
| 43 |
+
st.write(f"π₯ Downloading `{hf_model_name}` ...")
|
| 44 |
+
model_path = snapshot_download(
|
| 45 |
+
repo_id=hf_model_name,
|
| 46 |
+
local_dir=output_dir,
|
| 47 |
+
local_dir_use_symlinks=False
|
| 48 |
+
)
|
| 49 |
+
st.success("β
Model downloaded")
|
| 50 |
+
return model_path
|
| 51 |
+
|
| 52 |
+
def convert_to_gguf(model_path, output_file):
|
| 53 |
+
st.write("π Converting to GGUF...")
|
|
|
|
|
|
|
|
|
|
| 54 |
cmd = [
|
| 55 |
+
"python3",
|
| 56 |
+
CONVERT_SCRIPT,
|
| 57 |
+
model_path,
|
| 58 |
+
"--outfile",
|
| 59 |
+
output_file
|
| 60 |
]
|
| 61 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 62 |
+
if result.returncode != 0:
|
| 63 |
+
st.error(result.stderr)
|
| 64 |
+
raise RuntimeError("Conversion failed")
|
| 65 |
+
st.success("β
GGUF created")
|
| 66 |
+
|
| 67 |
+
def quantize_model(gguf_file, quant_type):
|
| 68 |
+
output_file = gguf_file.replace(".gguf", f"-{quant_type}.gguf")
|
| 69 |
+
|
| 70 |
+
st.write(f"β‘ Quantizing β {quant_type}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
cmd = [
|
| 72 |
+
QUANTIZE_BIN,
|
| 73 |
+
gguf_file,
|
| 74 |
+
output_file,
|
| 75 |
quant_type
|
| 76 |
]
|
| 77 |
+
|
| 78 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 79 |
+
|
| 80 |
+
if result.returncode != 0:
|
| 81 |
+
st.error(result.stderr)
|
| 82 |
+
return None
|
| 83 |
+
|
| 84 |
+
st.success(f"β
{quant_type} done")
|
| 85 |
+
return output_file
|
| 86 |
+
|
| 87 |
+
def upload_to_huggingface(file_path, repo_id):
|
| 88 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 89 |
+
|
| 90 |
+
if not hf_token:
|
| 91 |
+
st.error("β HF_TOKEN not found in environment variables")
|
| 92 |
+
return
|
| 93 |
+
|
| 94 |
+
api = HfApi(token=hf_token)
|
| 95 |
+
|
| 96 |
+
api.create_repo(repo_id, exist_ok=True, repo_type="model")
|
| 97 |
+
|
| 98 |
+
api.upload_file(
|
| 99 |
+
path_or_fileobj=file_path,
|
| 100 |
+
path_in_repo=os.path.basename(file_path),
|
| 101 |
+
repo_id=repo_id,
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
st.success(f"π Uploaded to https://huggingface.co/{repo_id}")
|
| 105 |
+
|
| 106 |
+
# ============================================================
|
| 107 |
+
# UI
|
| 108 |
+
# ============================================================
|
| 109 |
+
|
| 110 |
+
st.title("π¦ LLaMA.cpp Multi-Quantization Tool")
|
| 111 |
+
|
| 112 |
+
check_dependencies()
|
| 113 |
+
|
| 114 |
+
# Model selection
|
| 115 |
+
selected_model = st.selectbox(
|
| 116 |
+
"Select Hugging Face Model",
|
| 117 |
+
MODELS_LIST,
|
| 118 |
+
index=None
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
hf_model_name = selected_model or st.text_input(
|
| 122 |
+
"Or Enter Custom HF Model ID"
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Multi-checkbox quant selection
|
| 126 |
+
st.subheader("Select Quantization Types")
|
| 127 |
+
|
| 128 |
+
selected_quants = []
|
| 129 |
+
cols = st.columns(4)
|
| 130 |
+
|
| 131 |
+
for i, quant in enumerate(QUANT_TYPES):
|
| 132 |
+
with cols[i % 4]:
|
| 133 |
+
if st.checkbox(quant):
|
| 134 |
+
selected_quants.append(quant)
|
| 135 |
+
|
| 136 |
+
# Start button
|
| 137 |
+
if st.button("π Start Quantization"):
|
| 138 |
+
|
| 139 |
+
if not hf_model_name:
|
| 140 |
+
st.warning("Please enter a model name")
|
| 141 |
+
st.stop()
|
| 142 |
+
|
| 143 |
+
if not selected_quants:
|
| 144 |
+
st.warning("Select at least one quant type")
|
| 145 |
+
st.stop()
|
| 146 |
+
|
| 147 |
+
with st.spinner("Processing..."):
|
| 148 |
+
try:
|
| 149 |
+
base_dir = "/tmp/models"
|
| 150 |
+
os.makedirs(base_dir, exist_ok=True)
|
| 151 |
+
|
| 152 |
+
model_path = download_model(hf_model_name, base_dir)
|
| 153 |
+
|
| 154 |
+
gguf_file = os.path.join(
|
| 155 |
+
base_dir,
|
| 156 |
+
hf_model_name.replace("/", "_") + ".gguf"
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
convert_to_gguf(model_path, gguf_file)
|
| 160 |
+
|
| 161 |
+
st.session_state.quantized_models = []
|
| 162 |
+
|
| 163 |
+
for quant in selected_quants:
|
| 164 |
+
quant_file = quantize_model(gguf_file, quant)
|
| 165 |
+
if quant_file:
|
| 166 |
+
st.session_state.quantized_models.append(quant_file)
|
| 167 |
+
|
| 168 |
+
st.success("π All quantizations completed")
|
| 169 |
+
|
| 170 |
+
except Exception as e:
|
| 171 |
+
st.error(f"β Error: {str(e)}")
|
| 172 |
+
|
| 173 |
+
# ============================================================
|
| 174 |
+
# DOWNLOAD + UPLOAD SECTION
|
| 175 |
+
# ============================================================
|
| 176 |
+
|
| 177 |
+
if st.session_state.quantized_models:
|
| 178 |
+
|
| 179 |
+
st.subheader("π¦ Generated Models")
|
| 180 |
+
|
| 181 |
+
for file_path in st.session_state.quantized_models:
|
| 182 |
+
|
| 183 |
+
with open(file_path, "rb") as f:
|
| 184 |
+
st.download_button(
|
| 185 |
+
label=f"β¬οΈ Download {os.path.basename(file_path)}",
|
| 186 |
+
data=f,
|
| 187 |
+
file_name=os.path.basename(file_path),
|
| 188 |
+
key=file_path
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
st.divider()
|
| 192 |
+
|
| 193 |
+
st.subheader("π Upload to Hugging Face")
|
| 194 |
+
|
| 195 |
+
repo_id = st.text_input(
|
| 196 |
+
"Target Repository (e.g. username/model-quant)"
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
if st.button("π€ Upload All to HF"):
|
| 200 |
+
if not repo_id:
|
| 201 |
+
st.warning("Enter repository ID")
|
| 202 |
+
else:
|
| 203 |
+
with st.spinner("Uploading..."):
|
| 204 |
+
for file_path in st.session_state.quantized_models:
|
| 205 |
+
upload_to_huggingface(file_path, repo_id)
|
| 206 |
+
|
| 207 |
+
st.success("β
All files uploaded successfully")
|