File size: 10,782 Bytes
a5e67cb b5ebf9e a5e67cb b5ebf9e a5e67cb b5ebf9e a5e67cb b5ebf9e a5e67cb b5ebf9e a5e67cb b5ebf9e a5e67cb b5ebf9e a5e67cb b5ebf9e a5e67cb b5ebf9e a5e67cb b5ebf9e a5e67cb b5ebf9e a5e67cb b5ebf9e a5e67cb b5ebf9e a5e67cb b5ebf9e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
import gradio as gr
import torch
try:
from unsloth import FastLanguageModel
except ImportError:
print("Unsloth๊ฐ ์ค์น๋์ด ์์ง ์์ต๋๋ค. ์ค์น ์ค...")
import subprocess
subprocess.check_call(["pip", "install", "unsloth"])
from unsloth import FastLanguageModel
# Hugging Face์ ์
๋ก๋๋ ๋ชจ๋ธ ์ฌ์ฉ
MODEL_NAME = "huggingface-KREW/Llama-3.1-8B-Spider-SQL-Ko"
print(f"Loading model from Hugging Face: {MODEL_NAME}")
# Unsloth๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ก๋
try:
model, tokenizer = FastLanguageModel.from_pretrained(
model_name=MODEL_NAME,
max_seq_length=2048,
dtype=None, # ์๋ ๊ฐ์ง
load_in_4bit=True, # 4๋นํธ ์์ํ ์ฌ์ฉ
)
# ์ถ๋ก ๋ชจ๋๋ก ์ค์
FastLanguageModel.for_inference(model)
print("Model loaded successfully with Unsloth!")
except Exception as e:
print(f"Error loading model with Unsloth: {e}")
print("\n๋ชจ๋ธ์ด Hugging Face์ ์ ๋๋ก ์
๋ก๋๋์ง ์์์ ์ ์์ต๋๋ค.")
print("๋ก์ปฌ ๋ชจ๋ธ์ ์ฌ์ฉํ๊ฑฐ๋ ๋ชจ๋ธ์ ๋ค์ ์
๋ก๋ํด์ฃผ์ธ์.")
raise
# Example databases and questions
examples = [
{
"db_id": "department_management",
"question": "๊ฐ ๋ถ์๋ณ ์ง์ ์๋ฅผ ๋ณด์ฌ์ฃผ์ธ์.",
"schema": """๋ฐ์ดํฐ๋ฒ ์ด์ค ์คํค๋ง:
ํ
์ด๋ธ: department
์ปฌ๋ผ:
- Department_ID (number) (๊ธฐ๋ณธ ํค)
- Name (text)
- Creation (text)
- Ranking (number)
- Budget_in_Billions (number)
- Num_Employees (number)
ํ
์ด๋ธ: head
์ปฌ๋ผ:
- head_ID (number) (๊ธฐ๋ณธ ํค)
- name (text)
- born_state (text)
- age (number)
ํ
์ด๋ธ: management
์ปฌ๋ผ:
- department_ID (number) (๊ธฐ๋ณธ ํค)
- head_ID (number)
- temporary_acting (text)
์ธ๋ ํค ๊ด๊ณ:
- management.head_ID โ head.head_ID
- management.department_ID โ department.Department_ID"""
},
{
"db_id": "concert_singer",
"question": "๊ฐ์ฅ ๋ง์ ์ฝ์ํธ๋ฅผ ์ฐ ๊ฐ์๋ ๋๊ตฌ์ธ๊ฐ์?",
"schema": """๋ฐ์ดํฐ๋ฒ ์ด์ค ์คํค๋ง:
ํ
์ด๋ธ: singer
์ปฌ๋ผ:
- Singer_ID (number) (๊ธฐ๋ณธ ํค)
- Name (text)
- Country (text)
- Song_Name (text)
- Song_release_year (text)
- Age (number)
- Is_male (text)
ํ
์ด๋ธ: concert
์ปฌ๋ผ:
- concert_ID (number) (๊ธฐ๋ณธ ํค)
- concert_Name (text)
- Theme (text)
- Stadium_ID (number)
- Year (text)
ํ
์ด๋ธ: singer_in_concert
์ปฌ๋ผ:
- concert_ID (number)
- Singer_ID (number)
์ธ๋ ํค ๊ด๊ณ:
- singer_in_concert.Singer_ID โ singer.Singer_ID
- singer_in_concert.concert_ID โ concert.concert_ID"""
},
{
"db_id": "pets_1",
"question": "๊ฐ์ฅ ๋์ด๊ฐ ๋ง์ ๊ฐ์ ์ด๋ฆ์ ๋ฌด์์ธ๊ฐ์?",
"schema": """๋ฐ์ดํฐ๋ฒ ์ด์ค ์คํค๋ง:
ํ
์ด๋ธ: Student
์ปฌ๋ผ:
- StuID (number) (๊ธฐ๋ณธ ํค)
- LName (text)
- Fname (text)
- Age (number)
- Sex (text)
- Major (number)
- Advisor (number)
- city_code (text)
ํ
์ด๋ธ: Has_Pet
์ปฌ๋ผ:
- StuID (number)
- PetID (number)
ํ
์ด๋ธ: Pets
์ปฌ๋ผ:
- PetID (number) (๊ธฐ๋ณธ ํค)
- PetType (text)
- pet_age (number)
- weight (number)"""
},
{
"db_id": "car_1",
"question": "๋ฏธ๊ตญ์ฐ ์๋์ฐจ ์ค ๊ฐ์ฅ ๋น ๋ฅธ ์๋์ฐจ๋ ๋ฌด์์ธ๊ฐ์?",
"schema": """๋ฐ์ดํฐ๋ฒ ์ด์ค ์คํค๋ง:
ํ
์ด๋ธ: continents
์ปฌ๋ผ:
- ContId (number) (๊ธฐ๋ณธ ํค)
- Continent (text)
ํ
์ด๋ธ: countries
์ปฌ๋ผ:
- CountryId (number) (๊ธฐ๋ณธ ํค)
- CountryName (text)
- Continent (number)
ํ
์ด๋ธ: car_makers
์ปฌ๋ผ:
- Id (number) (๊ธฐ๋ณธ ํค)
- Maker (text)
- FullName (text)
- Country (number)
ํ
์ด๋ธ: model_list
์ปฌ๋ผ:
- ModelId (number) (๊ธฐ๋ณธ ํค)
- Maker (number)
- Model (text)
ํ
์ด๋ธ: car_names
์ปฌ๋ผ:
- MakeId (number) (๊ธฐ๋ณธ ํค)
- Model (text)
- Make (text)
ํ
์ด๋ธ: cars_data
์ปฌ๋ผ:
- Id (number) (๊ธฐ๋ณธ ํค)
- MPG (text)
- Cylinders (number)
- Edispl (text)
- Horsepower (text)
- Weight (number)
- Accelerate (number)
- Year (number)"""
},
{
"db_id": "tvshow",
"question": "๊ฐ์ฅ ๋์ ํ์ ์ ๋ฐ์ TV ์ผ๋ ๋ฌด์์ธ๊ฐ์?",
"schema": """๋ฐ์ดํฐ๋ฒ ์ด์ค ์คํค๋ง:
ํ
์ด๋ธ: TV_Channel
์ปฌ๋ผ:
- id (number) (๊ธฐ๋ณธ ํค)
- series_name (text)
- Country (text)
- Language (text)
- Content (text)
- Pixel_aspect_ratio_PAR (text)
- Hight_definition_TV (text)
- Pay_per_view_PPV (text)
- Package_Option (text)
ํ
์ด๋ธ: TV_series
์ปฌ๋ผ:
- id (number)
- Episode (text)
- Air_Date (text)
- Rating (text)
- Share (text)
- 18_49_Rating_Share (text)
- Viewers_m (text)
- Weekly_Rank (number)
- Channel (number)
ํ
์ด๋ธ: Cartoon
์ปฌ๋ผ:
- id (number) (๊ธฐ๋ณธ ํค)
- Title (text)
- Directed_by (text)
- Written_by (text)
- Original_air_date (text)
- Production_code (number)
- Channel (number)"""
}
]
def generate_sql(question, db_id, schema_info):
"""Generate SQL query using the model."""
# Create prompt with schema
prompt = f"""๋น์ ์ ์์ฐ์ด ์ง๋ฌธ์ SQL ์ฟผ๋ฆฌ๋ก ๋ณํํ๋ ์ ๋ฌธ AI ์ด์์คํดํธ์
๋๋ค. ์ฌ์ฉ์๊ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ ๋ณด๋ฅผ ์ป๊ธฐ ์ํด ์ผ์ ์ธ์ด๋ก ์ง๋ฌธํ๋ฉด, ๋น์ ์ ํด๋น ์ง๋ฌธ์ ์ ํํ SQL ์ฟผ๋ฆฌ๋ก ๋ณํํด์ผ ํฉ๋๋ค.
{schema_info}
์ง๋ฌธ: {question}
SQL:"""
# ์ฑํ
๋ฉ์์ง๋ก ํฌ๋งทํ
messages = [{"role": "user", "content": prompt}]
# ์ฑํ
ํ
ํ๋ฆฟ ์ ์ฉ
inputs = tokenizer.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
# Generate
with torch.no_grad():
outputs = model.generate(
inputs,
max_new_tokens=256,
temperature=0.1,
top_p=0.95,
do_sample=True,
use_cache=True
)
# Decode
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract SQL after the prompt
if prompt in response:
sql_part = response.split(prompt)[-1].strip()
else:
sql_part = response
# Clean up the response
if sql_part.startswith("assistant"):
sql_part = sql_part[len("assistant"):].strip()
# Extract SQL query
lines = sql_part.split('\n')
sql_query = ""
for line in lines:
line = line.strip()
if line.lower().startswith(('select', 'with', '(select')):
sql_query = line
# Continue collecting lines until we hit a semicolon or empty line
for next_line in lines[lines.index(line)+1:]:
next_line = next_line.strip()
if not next_line or next_line.startswith(('์ง๋ฌธ', '๋ฐ์ดํฐ๋ฒ ์ด์ค')):
break
sql_query += " " + next_line
if next_line.endswith(';'):
break
break
# Clean up SQL
sql_query = sql_query.strip()
if sql_query.endswith(';'):
sql_query = sql_query[:-1]
return sql_query if sql_query else "SQL ์์ฑ์ ์คํจํ์ต๋๋ค."
def process_question(question, db_id, custom_schema=None):
"""Process user question and generate SQL query."""
if not question or not db_id:
return "์ง๋ฌธ๊ณผ ๋ฐ์ดํฐ๋ฒ ์ด์ค ID๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
# Use custom schema if provided, otherwise find from examples
if custom_schema and custom_schema.strip():
schema_info = custom_schema
else:
# Find schema from examples
schema_info = None
for example in examples:
if example["db_id"] == db_id:
schema_info = example["schema"]
break
if not schema_info:
return "์คํค๋ง ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. ์ปค์คํ
์คํค๋ง๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
# Generate SQL
try:
sql_query = generate_sql(question, db_id, schema_info)
return sql_query
except Exception as e:
return f"์ค๋ฅ ๋ฐ์: {str(e)}"
# Create Gradio interface
with gr.Blocks(title="Spider SQL Generator - Korean", theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# ๐ท๏ธ Spider SQL Generator - Korean
ํ๊ตญ์ด ์ง๋ฌธ์ SQL ์ฟผ๋ฆฌ๋ก ๋ณํํ๋ Llama 3.1 8B ๋ชจ๋ธ์
๋๋ค.
## ๐ ์ฑ๋ฅ
- **Exact Match**: 42.65%
- **Execution Accuracy**: 65.47%
- **Training**: Spider ๋ฐ์ดํฐ์
(ํ๊ตญ์ด ๋ฒ์ญ)
""")
with gr.Row():
with gr.Column():
db_id_input = gr.Textbox(
label="๋ฐ์ดํฐ๋ฒ ์ด์ค ID",
placeholder="์: department_management",
value="department_management"
)
question_input = gr.Textbox(
label="์ง๋ฌธ (ํ๊ตญ์ด)",
placeholder="์: ๊ฐ ๋ถ์๋ณ ์ง์ ์๋ฅผ ๋ณด์ฌ์ฃผ์ธ์.",
lines=2
)
with gr.Accordion("์คํค๋ง ์ ๋ณด (์ ํ์ฌํญ)", open=False):
schema_input = gr.Textbox(
label="์ปค์คํ
์คํค๋ง",
placeholder="์ปค์คํ
์คํค๋ง๋ฅผ ์
๋ ฅํ์ธ์. ๋น์๋๋ฉด ์์ ์คํค๋ง๋ฅผ ์ฌ์ฉํฉ๋๋ค.",
lines=10
)
submit_btn = gr.Button("SQL ์์ฑ", variant="primary", size="lg")
with gr.Column():
output = gr.Textbox(
label="์์ฑ๋ SQL ์ฟผ๋ฆฌ",
lines=4,
elem_classes=["code"]
)
gr.Markdown("""
### ๐ก ์ฌ์ฉ ํ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ID๋ ์์ ์์ ์ ํํ๊ฑฐ๋ ์ง์ ์
๋ ฅํ์ธ์
- ์ง๋ฌธ์ ํ๊ตญ์ด๋ก ์์ฐ์ค๋ฝ๊ฒ ์์ฑํ์ธ์
- ์คํค๋ง ์ ๋ณด๋ ์ ํ์ฌํญ์
๋๋ค
""")
# Examples
gr.Markdown("### ๐ ์์ (ํด๋ฆญํ์ฌ ์ฌ์ฉ)")
gr.Examples(
examples=[
[ex["db_id"], ex["question"], ex["schema"]] for ex in examples
],
inputs=[db_id_input, question_input, schema_input],
outputs=output,
fn=process_question,
cache_examples=False
)
# Submit action
submit_btn.click(
fn=process_question,
inputs=[question_input, db_id_input, schema_input],
outputs=output
)
# Model info
gr.Markdown(f"""
---
### ๐ค ๋ชจ๋ธ ์ ๋ณด
- **Hugging Face**: [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME})
- **Base Model**: Llama 3.1 8B
- **Fine-tuning**: LoRA with Unsloth
- **Dataset**: Spider (Korean translated)
""")
# Launch the app
if __name__ == "__main__":
demo.launch(share=True) |