allaboutswing commited on
Commit
2ccd6df
ยท
verified ยท
1 Parent(s): e3bcb2b

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +25 -6
  2. app.py +148 -0
  3. requirements.txt +7 -0
README.md CHANGED
@@ -1,12 +1,31 @@
1
  ---
2
- title: Lora Demo2
3
- emoji: ๐Ÿ“‰
4
- colorFrom: yellow
5
- colorTo: gray
6
  sdk: gradio
7
- sdk_version: 5.49.1
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ ## ์‚ฌ์šฉ ๋ฐฉ๋ฒ•
28
+
29
+ 1. ๋ชจ๋ธ ์„ ํƒ
30
+ 2. ์ž…๋ ฅ ํ…์ŠคํŠธ ์ž…๋ ฅ
31
+ 3. ์‹คํ–‰ ๋ฒ„ํŠผ ํด๋ฆญ
app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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