nuatmochoi commited on
Commit
4efa655
·
verified ·
1 Parent(s): 9e753c7

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +174 -3
README.md CHANGED
@@ -1,3 +1,174 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - ko
4
+ license: apache-2.0
5
+ tags:
6
+ - text2sql
7
+ - spider
8
+ - korean
9
+ - llama
10
+ - text-generation
11
+ - table-question-answering
12
+ datasets:
13
+ - spider
14
+ - huggingface-KREW/spider-ko
15
+ base_model: unsloth/Meta-Llama-3.1-8B-Instruct
16
+ model-index:
17
+ - name: Llama-3.1-8B-Spider-SQL-Ko
18
+ results:
19
+ - task:
20
+ type: text2sql
21
+ name: Text to SQL
22
+ dataset:
23
+ name: Spider (Korean)
24
+ type: text2sql
25
+ metrics:
26
+ - type: exact_match
27
+ value: 42.65
28
+ - type: execution_accuracy
29
+ value: 65.47
30
+ ---
31
+
32
+ # Llama-3.1-8B-Spider-SQL-Ko
33
+
34
+ 한국어 질문을 SQL 쿼리로 변환하는 Text-to-SQL 모델입니다. spider 데이터셋의 train 🤖
35
+ [Spider](https://yale-lily.github.io/spider) 데이터셋을 한국어로 번역한 [spider-ko](https://huggingface.co/datasets/huggingface-KREW/spider-ko) 데이터셋을 활용하여 미세조정하였습니다.
36
+
37
+ ## 📊 주요 성능
38
+
39
+ Spider 한국어 검증 데이터셋(1,034개) 평가 결과:
40
+ - **정확 일치율**: 42.65% (441/1034)
41
+ - **실행 정확도**: 65.47% (677/1034)
42
+
43
+ > 💡 실행 정확도가 정확 일치율보다 높은 이유는, SQL 문법이 다르더라도 동일한 결과를 반환하는 경우가 많기 때문입니다.
44
+
45
+ ## 🚀 바로 시작하기
46
+
47
+ ```python
48
+ from unsloth import FastLanguageModel
49
+
50
+ # 모델 불러오기
51
+ model, tokenizer = FastLanguageModel.from_pretrained(
52
+ model_name="huggingface-KREW/Llama-3.1-8B-Spider-SQL-Ko",
53
+ max_seq_length=2048,
54
+ dtype=None,
55
+ load_in_4bit=True,
56
+ )
57
+
58
+ # 한국어 질문 → SQL 변환
59
+ question = "가수는 몇 명이 있나요?"
60
+ schema = """테이블: singer
61
+ 컬럼: singer_id, name, country, age"""
62
+
63
+ prompt = f"""데이터베이스 스키마:
64
+ {schema}
65
+
66
+ 질문: {question}
67
+ SQL:"""
68
+
69
+ # 결과: SELECT count(*) FROM singer
70
+ ```
71
+
72
+ ## 📝 모델 소개
73
+
74
+ - **기반 모델**: Llama 3.1 8B Instruct (4bit 양자화)
75
+ - **학습 데이터**: [spider-ko](https://huggingface.co/datasets/huggingface-KREW/spider-ko) (1-epoch)
76
+ - **지원 DB**: 166개의 다양한 도메인 데이터베이스 ([spider dataset]([Spider](https://yale-lily.github.io/spider)))
77
+ - **학습 방법**: LoRA (r=16, alpha=32)
78
+
79
+ ## 💬 활용 예시
80
+
81
+ ### 기본 사용법
82
+
83
+ ```python
84
+ def generate_sql(question, schema_info):
85
+ """한국어 질문을 SQL로 변환"""
86
+ prompt = f"""다음 데이터베이스 스키마를 참고하여 질문에 대한 SQL 쿼리를 생성하세요.
87
+
88
+ ### 데이터베이스 스키마:
89
+ {schema_info}
90
+
91
+ ### 질문: {question}
92
+
93
+ ### SQL 쿼리:"""
94
+
95
+ messages = [{"role": "user", "content": prompt}]
96
+ inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
97
+
98
+ outputs = model.generate(inputs, max_new_tokens=150, temperature=0.1)
99
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
100
+
101
+ return response.split("### SQL 쿼리:")[-1].strip()
102
+ ```
103
+
104
+ ### 실제 사용 예시
105
+
106
+ ```python
107
+ # 예시 1: 집계 함수
108
+ question = "부서장들 중 56세보다 나이가 많은 사람이 몇 명입니까?"
109
+ # 결과: SELECT count(*) FROM head WHERE age > 56
110
+
111
+ # 예시 2: 조인
112
+ question = "가장 많은 대회를 개최한 도시의 상태는 무엇인가요?"
113
+ # 결과: SELECT T1.Status FROM city AS T1 JOIN farm_competition AS T2 ON T1.City_ID = T2.Host_city_ID GROUP BY T2.Host_city_ID ORDER BY COUNT(*) DESC LIMIT 1
114
+
115
+ # 예시 3: 서브쿼리
116
+ question = "기업가가 아닌 사람들의 이름은 무엇입니까?"
117
+ # 결과: SELECT Name FROM people WHERE People_ID NOT IN (SELECT People_ID FROM entrepreneur)
118
+ ```
119
+
120
+ ## ⚠️ 사용 시 주의사항
121
+
122
+ ### 제한사항
123
+ - ✅ 영어 테이블/컬럼명 사용 (한국어 질문 → 영어 SQL)
124
+ - ✅ Spider 데이터셋 도메인에 최적화
125
+ - ❌ NoSQL, 그래프 DB 미지원
126
+ - ❌ 매우 복잡한 중첩 쿼리는 정확도 하락
127
+
128
+ ## 🔧 기술 사양
129
+
130
+ ### 학습 환경
131
+ - **GPU**: NVIDIA Tesla T4 (16GB)
132
+ - **학습 시간**: 약 4시간
133
+ - **메모리 사용**: 최대 7.6GB VRAM
134
+
135
+ ### 하이퍼파라미터
136
+ ```python
137
+ training_args = {
138
+ "per_device_train_batch_size": 2,
139
+ "gradient_accumulation_steps": 4,
140
+ "learning_rate": 5e-4,
141
+ "num_train_epochs": 1,
142
+ "optimizer": "adamw_8bit",
143
+ "lr_scheduler_type": "cosine",
144
+ "warmup_ratio": 0.05
145
+ }
146
+
147
+ lora_config = {
148
+ "r": 16,
149
+ "lora_alpha": 32,
150
+ "lora_dropout": 0,
151
+ "target_modules": ["q_proj", "k_proj", "v_proj", "o_proj",
152
+ "gate_proj", "up_proj", "down_proj"]
153
+ }
154
+ ```
155
+
156
+ ## 📚 참고 자료
157
+
158
+ ### 인용
159
+ ```bibtex
160
+ @misc{llama31_spider_sql_ko_2025,
161
+ title={Llama-3.1-8B-Spider-SQL-Ko: Korean Text-to-SQL Model},
162
+ author={[Sohyun Sim, Youngjun Cho, Seongwoo Choi]},
163
+ year={2025},
164
+ publisher={Hugging Face KREW},
165
+ url={https://huggingface.co/huggingface-KREW/Llama-3.1-8B-Spider-SQL-Ko}
166
+ }
167
+ ```
168
+
169
+ ### 관련 논문
170
+ - [Spider: A Large-Scale Human-Labeled Dataset](https://arxiv.org/abs/1809.08887) (Yu et al., 2018)
171
+
172
+ ## 🤝 기여자
173
+
174
+ [@sim-so](https://huggingface.co/sim-so), [@choincnp](https://huggingface.co/choincnp), [@nuatmochoi](https://huggingface.co/nuatmochoi)