Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- README.md +25 -6
- app.py +148 -0
- requirements.txt +7 -0
README.md
CHANGED
|
@@ -1,12 +1,31 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: LoRA Model Demo
|
| 3 |
+
emoji: ๐ค
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.0.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# LoRA ํ์ธํ๋ ๋ชจ๋ธ ๋ฐ๋ชจ
|
| 13 |
+
|
| 14 |
+
Day 1์์ ํ์ตํ LoRA ๋ชจ๋ธ ๋ฐ๋ชจ์
๋๋ค.
|
| 15 |
+
|
| 16 |
+
## ํฌํจ๋ ๋ชจ๋ธ
|
| 17 |
+
|
| 18 |
+
- ํ๊ตญ์ด ์์ฝ (03๋ฒ) - EXAONE-3.5
|
| 19 |
+
- ๊ฐ์ ๋ถ๋ฅ (06๋ฒ) - IMDB
|
| 20 |
+
- ์์ด QA (06๋ฒ) - SQuAD
|
| 21 |
+
|
| 22 |
+
## ์ ํ ๊ฐ๋ฅ (์ฃผ์ ํด์ )
|
| 23 |
+
|
| 24 |
+
- Granite ์์ฝ (05๋ฒ)
|
| 25 |
+
- Qwen3 ์์ฝ (05๋ฒ)
|
| 26 |
+
|
| 27 |
+
## ์ฌ์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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
|
| 6 |
+
# ๋ชจ๋ธ ์ค์ (์ฌ๊ธฐ๋ฅผ ์์ ํ์ธ์!)
|
| 7 |
+
MODELS = {
|
| 8 |
+
# ========================================
|
| 9 |
+
# 03๋ฒ: ํ๊ตญ์ด ์์ฝ (EXAONE-3.5)
|
| 10 |
+
# ========================================
|
| 11 |
+
"ํ๊ตญ์ด ์์ฝ (03๋ฒ)": {
|
| 12 |
+
"base_model": "LGAI-EXAONE/EXAONE-3.5-2.4B-Instruct",
|
| 13 |
+
"lora_path": "your-username/exaone-summary-lora", # TODO: ๋ณธ์ธ ๊ฒฝ๋ก๋ก!
|
| 14 |
+
"prompt_template": "{input}\n\n์์ฝ:",
|
| 15 |
+
"max_new_tokens": 60,
|
| 16 |
+
"placeholder": "๋ด์ค ๊ธฐ์ฌ๋ฅผ ์
๋ ฅํ์ธ์...",
|
| 17 |
+
"example": "์์ธ์๊ฐ ๋ด๋
๋ถํฐ ์ ๊ธฐ์ฐจ ์ถฉ์ ์๋ฅผ ๋ํญ ํ๋ํ๋ค.",
|
| 18 |
+
},
|
| 19 |
+
|
| 20 |
+
# ========================================
|
| 21 |
+
# 05๋ฒ: ๋ค์ค ๋ชจ๋ธ ๋น๊ต (์ ํ์ฌํญ)
|
| 22 |
+
# ========================================
|
| 23 |
+
# "Granite ์์ฝ (05๋ฒ)": {
|
| 24 |
+
# "base_model": "ibm-granite/granite-4.0-micro",
|
| 25 |
+
# "lora_path": "your-username/granite-summary-lora",
|
| 26 |
+
# "prompt_template": "<|user|>\n{input}\n\n์ ๊ธฐ์ฌ๋ฅผ ์์ฝํด์ฃผ์ธ์.<|assistant|>\n",
|
| 27 |
+
# "max_new_tokens": 60,
|
| 28 |
+
# "placeholder": "๋ด์ค ๊ธฐ์ฌ๋ฅผ ์
๋ ฅํ์ธ์...",
|
| 29 |
+
# "example": "์์ธ์๊ฐ ๋ด๋
๋ถํฐ ์ ๊ธฐ์ฐจ ์ถฉ์ ์๋ฅผ ๋ํญ ํ๋ํ๋ค.",
|
| 30 |
+
# },
|
| 31 |
+
# "Qwen3 ์์ฝ (05๋ฒ)": {
|
| 32 |
+
# "base_model": "Qwen/Qwen3-4B-Instruct-2507",
|
| 33 |
+
# "lora_path": "your-username/qwen3-summary-lora",
|
| 34 |
+
# "prompt_template": "<|im_start|>user\n{input}\n\n์ ๊ธฐ์ฌ๋ฅผ ์์ฝํด์ฃผ์ธ์.<|im_end|>\n<|im_start|>assistant\n",
|
| 35 |
+
# "max_new_tokens": 60,
|
| 36 |
+
# "placeholder": "๋ด์ค ๊ธฐ์ฌ๋ฅผ ์
๋ ฅํ์ธ์...",
|
| 37 |
+
# "example": "์์ธ์๊ฐ ๋ด๋
๋ถํฐ ์ ๊ธฐ์ฐจ ์ถฉ์ ์๋ฅผ ๋ํญ ํ๋ํ๋ค.",
|
| 38 |
+
# },
|
| 39 |
+
|
| 40 |
+
# ========================================
|
| 41 |
+
# 06๋ฒ: ๊ฐ์ ๋ถ๋ฅ + ์์ด QA
|
| 42 |
+
# ========================================
|
| 43 |
+
"๊ฐ์ ๋ถ๋ฅ (06๋ฒ)": {
|
| 44 |
+
"base_model": "LGAI-EXAONE/EXAONE-3.5-2.4B-Instruct",
|
| 45 |
+
"lora_path": "your-username/lora-sentiment",
|
| 46 |
+
"prompt_template": "๋ค์ ์ํ ๋ฆฌ๋ทฐ์ ๊ฐ์ ์ ๋ถ๋ฅํ์ธ์.\n\n๋ฆฌ๋ทฐ: {input}\n\n๊ฐ์ :",
|
| 47 |
+
"max_new_tokens": 10,
|
| 48 |
+
"placeholder": "์ํ ๋ฆฌ๋ทฐ๋ฅผ ์
๋ ฅํ์ธ์...",
|
| 49 |
+
"example": "This movie was amazing! Great story and excellent acting.",
|
| 50 |
+
},
|
| 51 |
+
"์์ด QA (06๋ฒ)": {
|
| 52 |
+
"base_model": "LGAI-EXAONE/EXAONE-3.5-2.4B-Instruct",
|
| 53 |
+
"lora_path": "your-username/lora-qa",
|
| 54 |
+
"prompt_template": "Context: The Eiffel Tower is in Paris, France.\n\nQuestion: {input}\n\nAnswer:",
|
| 55 |
+
"max_new_tokens": 30,
|
| 56 |
+
"placeholder": "์ง๋ฌธ์ ์
๋ ฅํ์ธ์...",
|
| 57 |
+
"example": "Where is the Eiffel Tower located?",
|
| 58 |
+
},
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
loaded_models = {}
|
| 62 |
+
|
| 63 |
+
def load_model(model_name):
|
| 64 |
+
if model_name in loaded_models:
|
| 65 |
+
return loaded_models[model_name]
|
| 66 |
+
|
| 67 |
+
config = MODELS[model_name]
|
| 68 |
+
|
| 69 |
+
tokenizer = AutoTokenizer.from_pretrained(config["base_model"], use_fast=False)
|
| 70 |
+
if tokenizer.pad_token is None:
|
| 71 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 72 |
+
|
| 73 |
+
quant_config = BitsAndBytesConfig(
|
| 74 |
+
load_in_4bit=True,
|
| 75 |
+
bnb_4bit_use_double_quant=True,
|
| 76 |
+
bnb_4bit_quant_type="nf4",
|
| 77 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 81 |
+
config["base_model"],
|
| 82 |
+
device_map="auto",
|
| 83 |
+
trust_remote_code=True,
|
| 84 |
+
quantization_config=quant_config,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
model = PeftModel.from_pretrained(base_model, config["lora_path"])
|
| 88 |
+
|
| 89 |
+
loaded_models[model_name] = (model, tokenizer, config)
|
| 90 |
+
return model, tokenizer, config
|
| 91 |
+
|
| 92 |
+
def generate_response(model_name, user_input):
|
| 93 |
+
try:
|
| 94 |
+
model, tokenizer, config = load_model(model_name)
|
| 95 |
+
prompt = config["prompt_template"].format(input=user_input)
|
| 96 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 97 |
+
|
| 98 |
+
with torch.no_grad():
|
| 99 |
+
outputs = model.generate(
|
| 100 |
+
**inputs,
|
| 101 |
+
max_new_tokens=config["max_new_tokens"],
|
| 102 |
+
temperature=0.7,
|
| 103 |
+
do_sample=True,
|
| 104 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 108 |
+
|
| 109 |
+
# ํ๋กฌํํธ ์ ๊ฑฐ
|
| 110 |
+
if "์์ฝ:" in result:
|
| 111 |
+
return result.split("์์ฝ:")[-1].strip()
|
| 112 |
+
elif "๊ฐ์ :" in result:
|
| 113 |
+
return result.split("๊ฐ์ :")[-1].strip()
|
| 114 |
+
elif "Answer:" in result:
|
| 115 |
+
return result.split("Answer:")[-1].strip()
|
| 116 |
+
elif "<|assistant|>" in result:
|
| 117 |
+
return result.split("<|assistant|>")[-1].strip()
|
| 118 |
+
elif "<|im_start|>assistant" in result:
|
| 119 |
+
return result.split("<|im_start|>assistant")[-1].replace("<|im_end|>", "").strip()
|
| 120 |
+
else:
|
| 121 |
+
return result[len(prompt):].strip()
|
| 122 |
+
except Exception as e:
|
| 123 |
+
return f"โ ์ค๋ฅ: {str(e)}"
|
| 124 |
+
|
| 125 |
+
with gr.Blocks(title="LoRA ๋ชจ๋ธ ๋ฐ๋ชจ") as demo:
|
| 126 |
+
gr.Markdown("# ๐ค LoRA ํ์ธํ๋ ๋ชจ๋ธ ๋ฐ๋ชจ")
|
| 127 |
+
gr.Markdown("Day 1์์ ํ์ตํ ์ฌ๋ฌ LoRA ๋ชจ๋ธ์ ํ
์คํธํด๋ณด์ธ์!")
|
| 128 |
+
|
| 129 |
+
with gr.Row():
|
| 130 |
+
with gr.Column():
|
| 131 |
+
model_dropdown = gr.Dropdown(
|
| 132 |
+
choices=list(MODELS.keys()),
|
| 133 |
+
value=list(MODELS.keys())[0],
|
| 134 |
+
label="๐ ๋ชจ๋ธ ์ ํ"
|
| 135 |
+
)
|
| 136 |
+
input_text = gr.Textbox(label="๐ฌ ์
๋ ฅ", lines=5)
|
| 137 |
+
submit_btn = gr.Button("๐ ์คํ", variant="primary")
|
| 138 |
+
|
| 139 |
+
with gr.Column():
|
| 140 |
+
output_text = gr.Textbox(label="โจ ๊ฒฐ๊ณผ", lines=10)
|
| 141 |
+
|
| 142 |
+
submit_btn.click(
|
| 143 |
+
fn=generate_response,
|
| 144 |
+
inputs=[model_dropdown, input_text],
|
| 145 |
+
outputs=[output_text]
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
peft
|
| 5 |
+
bitsandbytes
|
| 6 |
+
accelerate
|
| 7 |
+
sentencepiece
|