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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 4 |
+
import zipfile
|
| 5 |
+
import os
|
| 6 |
+
import tempfile
|
| 7 |
+
import json
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
+
|
| 10 |
+
# νκ²½ λ³μ λ° μΊμ λλ ν 리 μ€μ (μλ΅, κΈ°μ‘΄ μ½λμ λμΌ)
|
| 11 |
+
|
| 12 |
+
# ... (νκ²½ λ³μ λ° μΊμ λλ ν 리 μμ± λΆλΆμ κΈ°μ‘΄ μ½λμ λμΌ)
|
| 13 |
+
|
| 14 |
+
HF_REPO_ID = "sdfsdh/koalpaca-cpu-model"
|
| 15 |
+
HF_FILENAME = "koalpaca_cpu_deployment.zip"
|
| 16 |
+
|
| 17 |
+
def download_model_from_hf_hub():
|
| 18 |
+
try:
|
| 19 |
+
st.info("π₯ Hugging Face Model Repositoryμμ λ€μ΄λ‘λ μ€...")
|
| 20 |
+
zip_path = hf_hub_download(
|
| 21 |
+
repo_id=HF_REPO_ID,
|
| 22 |
+
filename=HF_FILENAME,
|
| 23 |
+
cache_dir="/tmp/hf_hub_cache",
|
| 24 |
+
repo_type="model"
|
| 25 |
+
)
|
| 26 |
+
st.success(f"β
λ€μ΄λ‘λ μλ£: {os.path.getsize(zip_path) / 1024**3:.2f}GB")
|
| 27 |
+
st.info(f"π λ€μ΄λ‘λ κ²½λ‘: {zip_path}")
|
| 28 |
+
return zip_path
|
| 29 |
+
except Exception as e:
|
| 30 |
+
st.error(f"β Hugging Face Hub λ€μ΄λ‘λ μ€ν¨: {e}")
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
def verify_zip_file(zip_path):
|
| 34 |
+
try:
|
| 35 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 36 |
+
zip_ref.testzip()
|
| 37 |
+
file_list = zip_ref.namelist()
|
| 38 |
+
st.info(f"π¦ ZIP νμΌ κ²μ¦ μ±κ³΅: {len(file_list)}κ° νμΌ")
|
| 39 |
+
return True
|
| 40 |
+
except zipfile.BadZipFile:
|
| 41 |
+
st.error("β μ ν¨νμ§ μμ ZIP νμΌ")
|
| 42 |
+
return False
|
| 43 |
+
except Exception as e:
|
| 44 |
+
st.error(f"β ZIP νμΌ κ²μ¦ μ€ν¨: {e}")
|
| 45 |
+
return False
|
| 46 |
+
|
| 47 |
+
def extract_zip_streaming(zip_path, extract_path):
|
| 48 |
+
"""RAM μ¬μ©λ μ΅μνλ₯Ό μν μ€νΈλ¦¬λ° λ°©μ μμΆ ν΄μ """
|
| 49 |
+
st.info("π¦ μ€νΈλ¦¬λ° λ°©μμΌλ‘ μμΆ ν΄μ μ€...")
|
| 50 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 51 |
+
for member in zip_ref.infolist():
|
| 52 |
+
# λλ ν 리면 μμ±λ§
|
| 53 |
+
extracted_path = os.path.join(extract_path, member.filename)
|
| 54 |
+
if member.is_dir():
|
| 55 |
+
os.makedirs(extracted_path, exist_ok=True)
|
| 56 |
+
continue
|
| 57 |
+
# μμ λλ ν 리 μμ±
|
| 58 |
+
os.makedirs(os.path.dirname(extracted_path), exist_ok=True)
|
| 59 |
+
# νμΌ μ€νΈλ¦¬λ° 볡μ¬
|
| 60 |
+
with zip_ref.open(member) as src, open(extracted_path, "wb") as dst:
|
| 61 |
+
while True:
|
| 62 |
+
chunk = src.read(1024 * 1024) # 1MB λ²νΌ
|
| 63 |
+
if not chunk:
|
| 64 |
+
break
|
| 65 |
+
dst.write(chunk)
|
| 66 |
+
st.success("β
μμΆ ν΄μ μλ£!")
|
| 67 |
+
|
| 68 |
+
@st.cache_resource
|
| 69 |
+
def download_and_load_model():
|
| 70 |
+
try:
|
| 71 |
+
temp_dir = "/tmp/koalpaca_model"
|
| 72 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 73 |
+
extract_path = os.path.join(temp_dir, "koalpaca_cpu_deployment")
|
| 74 |
+
|
| 75 |
+
zip_path = download_model_from_hf_hub()
|
| 76 |
+
if zip_path is None:
|
| 77 |
+
raise Exception("Hugging Face Hub λ€μ΄λ‘λ μ€ν¨")
|
| 78 |
+
|
| 79 |
+
if not verify_zip_file(zip_path):
|
| 80 |
+
raise Exception("ZIP νμΌ κ²μ¦ μ€ν¨")
|
| 81 |
+
|
| 82 |
+
# κΈ°μ‘΄ extractall() β μ€νΈλ¦¬λ° λ°©μμΌλ‘ λ³κ²½
|
| 83 |
+
extract_zip_streaming(zip_path, extract_path)
|
| 84 |
+
|
| 85 |
+
# λͺ¨λΈ κ²½λ‘ νμ (κΈ°μ‘΄ μ½λμ λμΌ)
|
| 86 |
+
model_path = None
|
| 87 |
+
for root, dirs, files in os.walk(extract_path):
|
| 88 |
+
if "cpu_quantized_model.pt" in files or "tokenizer.json" in files:
|
| 89 |
+
model_path = root
|
| 90 |
+
break
|
| 91 |
+
if model_path is None:
|
| 92 |
+
subdirs = [d for d in os.listdir(extract_path) if os.path.isdir(os.path.join(extract_path, d))]
|
| 93 |
+
if subdirs:
|
| 94 |
+
model_path = os.path.join(extract_path, subdirs[0])
|
| 95 |
+
else:
|
| 96 |
+
model_path = extract_path
|
| 97 |
+
|
| 98 |
+
st.info(f"π λͺ¨λΈ κ²½λ‘: {model_path}")
|
| 99 |
+
st.info(f"π νμΌ λͺ©λ‘: {os.listdir(model_path)}")
|
| 100 |
+
|
| 101 |
+
st.info("π ν ν¬λμ΄μ λ‘λ© μ€...")
|
| 102 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 103 |
+
model_path,
|
| 104 |
+
trust_remote_code=True,
|
| 105 |
+
use_fast=False,
|
| 106 |
+
cache_dir="/tmp/transformers_cache"
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
quantized_model_path = os.path.join(model_path, "cpu_quantized_model.pt")
|
| 110 |
+
if os.path.exists(quantized_model_path):
|
| 111 |
+
st.info("β‘ μμνλ CPU λͺ¨λΈ λ‘λ μ€...")
|
| 112 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 113 |
+
"beomi/KoAlpaca-Polyglot-5.8B",
|
| 114 |
+
torch_dtype=torch.float32,
|
| 115 |
+
device_map="cpu",
|
| 116 |
+
trust_remote_code=True,
|
| 117 |
+
low_cpu_mem_usage=True,
|
| 118 |
+
cache_dir="/tmp/transformers_cache"
|
| 119 |
+
)
|
| 120 |
+
checkpoint = torch.load(quantized_model_path, map_location="cpu")
|
| 121 |
+
if 'model_state_dict' in checkpoint:
|
| 122 |
+
model.load_state_dict(checkpoint['model_state_dict'])
|
| 123 |
+
st.success("β
μμνλ λͺ¨λΈ λ‘λ μλ£!")
|
| 124 |
+
else:
|
| 125 |
+
raise KeyError("model_state_dict not found")
|
| 126 |
+
else:
|
| 127 |
+
st.info("π§ νμ€ λͺ¨λΈ λ‘λ μ€...")
|
| 128 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 129 |
+
"beomi/KoAlpaca-Polyglot-5.8B",
|
| 130 |
+
torch_dtype=torch.float16,
|
| 131 |
+
device_map="auto",
|
| 132 |
+
trust_remote_code=True,
|
| 133 |
+
low_cpu_mem_usage=True,
|
| 134 |
+
cache_dir="/tmp/transformers_cache"
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
model.eval()
|
| 138 |
+
st.info("π§ Pipeline μμ± μ€...")
|
| 139 |
+
pipe = pipeline(
|
| 140 |
+
'text-generation',
|
| 141 |
+
model=model,
|
| 142 |
+
tokenizer=tokenizer,
|
| 143 |
+
device_map="auto"
|
| 144 |
+
)
|
| 145 |
+
return pipe, tokenizer
|
| 146 |
+
except Exception as e:
|
| 147 |
+
st.error(f"β λͺ¨λΈ λ‘λ μ€ν¨: {str(e)}")
|
| 148 |
+
import traceback
|
| 149 |
+
st.error(f"μμΈ μ€λ₯: {traceback.format_exc()}")
|
| 150 |
+
return None, None
|
| 151 |
+
|
| 152 |
+
def generate_response(pipe, prompt, max_new_tokens=200):
|
| 153 |
+
try:
|
| 154 |
+
formatted_prompt = f"### μ§λ¬Έ: {prompt}\n\n### λ΅λ³:"
|
| 155 |
+
result = pipe(
|
| 156 |
+
formatted_prompt,
|
| 157 |
+
do_sample=True,
|
| 158 |
+
max_new_tokens=max_new_tokens,
|
| 159 |
+
temperature=0.7,
|
| 160 |
+
top_p=0.9,
|
| 161 |
+
return_full_text=False,
|
| 162 |
+
eos_token_id=2,
|
| 163 |
+
)
|
| 164 |
+
return result[0]['generated_text'].strip()
|
| 165 |
+
except Exception as e:
|
| 166 |
+
return f"μλ΅ μμ± μ€ μ€λ₯: {str(e)}"
|
| 167 |
+
|
| 168 |
+
def main():
|
| 169 |
+
st.title("π€ KoAlpaca CPU μλ²")
|
| 170 |
+
st.markdown("Hugging Face Model Repositoryμμ λ‘λλ KoAlpaca λͺ¨λΈ μλΉ")
|
| 171 |
+
st.info("π§ μΊμ λλ ν 리 κΆν λ¬Έμ ν΄κ²°λ¨")
|
| 172 |
+
st.info(f"π TRANSFORMERS_CACHE: {os.environ.get('TRANSFORMERS_CACHE')}")
|
| 173 |
+
st.info(f"π HF_HOME: {os.environ.get('HF_HOME')}")
|
| 174 |
+
st.info(f"π Model Repository: {HF_REPO_ID}")
|
| 175 |
+
st.info(f"π νμΌλͺ
: {HF_FILENAME}")
|
| 176 |
+
|
| 177 |
+
with st.spinner("λͺ¨λΈ μ΄κΈ°ν μ€... (μΊμ κΆν λ¬Έμ ν΄κ²°λ¨, 5-10λΆ μμ)"):
|
| 178 |
+
pipe, tokenizer = download_and_load_model()
|
| 179 |
+
if pipe is None:
|
| 180 |
+
st.error("β λͺ¨λΈ λ‘λμ μ€ν¨νμ΅λλ€.")
|
| 181 |
+
st.info("π‘ ν΄κ²° λ°©λ²:")
|
| 182 |
+
st.markdown(f"""
|
| 183 |
+
1. μΊμ λλ ν 리 κΆν λ¬Έμ λ ν΄κ²°λμμ΅λλ€
|
| 184 |
+
2. Model Repository μ κ·Ό νμΈ: https://huggingface.co/{HF_REPO_ID}
|
| 185 |
+
3. λ€νΈμν¬ μ°κ²° μν νμΈ
|
| 186 |
+
""")
|
| 187 |
+
return
|
| 188 |
+
|
| 189 |
+
st.success("β
KoAlpaca λͺ¨λΈ μλΉ μ€λΉ μλ£!")
|
| 190 |
+
st.header("π API μΈν°νμ΄μ€")
|
| 191 |
+
prompt = st.text_area(
|
| 192 |
+
"μ§λ¬Έ μ
λ ₯:",
|
| 193 |
+
height=100,
|
| 194 |
+
placeholder="μ: μΌμ±μ μμ μ¬λ¬΄μνλ μ΄λ»μ΅λκΉ?"
|
| 195 |
+
)
|
| 196 |
+
col1, col2 = st.columns(2)
|
| 197 |
+
with col1:
|
| 198 |
+
max_tokens = st.slider("μ΅λ ν ν° μ", 50, 500, 200)
|
| 199 |
+
with col2:
|
| 200 |
+
temperature = st.slider("Temperature", 0.1, 1.0, 0.7)
|
| 201 |
+
if st.button("π μλ΅ μμ±", type="primary"):
|
| 202 |
+
if prompt.strip():
|
| 203 |
+
with st.spinner("μλ΅ μμ± μ€..."):
|
| 204 |
+
response = generate_response(pipe, prompt, max_tokens)
|
| 205 |
+
st.markdown("### π μλ΅:")
|
| 206 |
+
st.write(response)
|
| 207 |
+
with st.expander("π JSON μλ΅"):
|
| 208 |
+
api_response = {
|
| 209 |
+
"prompt": prompt,
|
| 210 |
+
"response": response,
|
| 211 |
+
"model": "KoAlpaca-Polyglot-5.8B",
|
| 212 |
+
"max_tokens": max_tokens,
|
| 213 |
+
"temperature": temperature,
|
| 214 |
+
"source": f"HF Model Repository: {HF_REPO_ID}"
|
| 215 |
+
}
|
| 216 |
+
st.json(api_response)
|
| 217 |
+
else:
|
| 218 |
+
st.warning("μ§λ¬Έμ μ
λ ₯ν΄μ£ΌμΈμ.")
|
| 219 |
+
with st.expander("π§ μμ€ν
μ 보"):
|
| 220 |
+
system_info = {
|
| 221 |
+
"νλ«νΌ": "Hugging Face Spaces",
|
| 222 |
+
"λͺ¨λΈ": "KoAlpaca-Polyglot-5.8B",
|
| 223 |
+
"μ΅μ ν": "CPU μμν",
|
| 224 |
+
"λ©λͺ¨λ¦¬": "16GB RAM",
|
| 225 |
+
"μμ€": f"HF Model Repository: {HF_REPO_ID}",
|
| 226 |
+
"μΊμ λλ ν 리": "/tmp (κΆν λ¬Έμ ν΄κ²°λ¨)",
|
| 227 |
+
"TRANSFORMERS_CACHE": os.environ.get('TRANSFORMERS_CACHE'),
|
| 228 |
+
"HF_HOME": os.environ.get('HF_HOME')
|
| 229 |
+
}
|
| 230 |
+
st.json(system_info)
|
| 231 |
+
|
| 232 |
+
if __name__ == "__main__":
|
| 233 |
+
main()
|