Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 4 |
+
|
| 5 |
+
# --- 1. Model ve Tokenizer'ı Yükle ---
|
| 6 |
+
# Bu kısım sadece uygulama ilk başladığında bir kez çalışır.
|
| 7 |
+
print("Model ve Tokenizer yükleniyor... Lütfen bekleyin.")
|
| 8 |
+
|
| 9 |
+
model_id = "Qwen/Qwen1.5-7B-Chat"
|
| 10 |
+
|
| 11 |
+
bnb_config = BitsAndBytesConfig(
|
| 12 |
+
load_in_4bit=True,
|
| 13 |
+
bnb_4bit_quant_type="nf4",
|
| 14 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
+
model_id,
|
| 19 |
+
quantization_config=bnb_config,
|
| 20 |
+
device_map="auto",
|
| 21 |
+
trust_remote_code=True
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 25 |
+
if tokenizer.pad_token is None:
|
| 26 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 27 |
+
|
| 28 |
+
print("Model başarıyla yüklendi!")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# --- 2. SQL Üretme Fonksiyonu ---
|
| 32 |
+
# Bu fonksiyon, kullanıcı butona her bastığında çalışır.
|
| 33 |
+
def generate_sql(user_question):
|
| 34 |
+
print(f"Yeni soru alındı: {user_question}")
|
| 35 |
+
|
| 36 |
+
# Veritabanı şemasını doğrudan koda ekliyoruz.
|
| 37 |
+
schema = {
|
| 38 |
+
"Musteriler": ["musteri_id", "ad", "soyad", "sehir", "kayit_tarihi"],
|
| 39 |
+
"Kategoriler": ["kategori_id", "kategori_adi"],
|
| 40 |
+
"Urunler": ["urun_id", "urun_adi", "kategori_id", "fiyat"],
|
| 41 |
+
"Siparisler": ["siparis_id", "musteri_id", "siparis_tarihi"],
|
| 42 |
+
"SiparisDetaylari": ["detay_id", "siparis_id", "urun_id", "miktar"]
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
schema_str = ""
|
| 46 |
+
for table, columns in schema.items():
|
| 47 |
+
schema_str += f"Tablo `{table}`: {', '.join(columns)}\n"
|
| 48 |
+
|
| 49 |
+
system_prompt = "Sen, dünya standartlarında bir SQL sorgu üretme uzmanısın. Sağlanan şemaya harfiyen uymalı ve tabloları sadece doğru anahtarları kullanarak birleştirmelisin. Karmaşık problemleri çözmek için adım adım düşün ve birden fazla CTE kullan."
|
| 50 |
+
|
| 51 |
+
messages = [
|
| 52 |
+
{"role": "system", "content": system_prompt},
|
| 53 |
+
{"role": "user", "content": f"Veritabanı Şeması:\n{schema_str}\nSoru: {user_question}"}
|
| 54 |
+
]
|
| 55 |
+
|
| 56 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 57 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 58 |
+
|
| 59 |
+
outputs = model.generate(
|
| 60 |
+
**inputs,
|
| 61 |
+
max_new_tokens=600,
|
| 62 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 63 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 64 |
+
temperature=0.1,
|
| 65 |
+
top_p=0.9
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
response_ids = outputs[0][inputs.input_ids.shape[1]:]
|
| 69 |
+
generated_text = tokenizer.decode(response_ids, skip_special_tokens=True)
|
| 70 |
+
|
| 71 |
+
if '```sql' in generated_text:
|
| 72 |
+
generated_text = generated_text.split('```sql')[1]
|
| 73 |
+
if '```' in generated_text:
|
| 74 |
+
generated_text = generated_text.split('```')[0]
|
| 75 |
+
|
| 76 |
+
print(f"Üretilen SQL:\n{generated_text.strip()}")
|
| 77 |
+
return generated_text.strip()
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# --- 3. Gradio Arayüzünü Oluştur ---
|
| 81 |
+
# Web sayfasının görünümünü ve işlevselliğini tanımlar.
|
| 82 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 83 |
+
gr.Markdown("# 🤖 Türkçe Soru-SQL Botu")
|
| 84 |
+
gr.Markdown("Veritabanı hakkında sormak istediğiniz soruyu Türkçe olarak yazın, sizin için SQL sorgusunu üretelim.")
|
| 85 |
+
|
| 86 |
+
with gr.Row():
|
| 87 |
+
question_input = gr.Textbox(lines=5, label="Soru", placeholder="Örnek: Her müşterinin toplam harcamasını ve son sipariş tarihini listele.")
|
| 88 |
+
sql_output = gr.Textbox(lines=15, label="Üretilen SQL Sorgusu", interactive=False)
|
| 89 |
+
|
| 90 |
+
submit_button = gr.Button("SQL Üret")
|
| 91 |
+
|
| 92 |
+
submit_button.click(
|
| 93 |
+
fn=generate_sql,
|
| 94 |
+
inputs=question_input,
|
| 95 |
+
outputs=sql_output
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
gr.Markdown("---")
|
| 99 |
+
gr.Markdown("Bu demo, Alibaba Qwen1.5-7B modeli kullanılarak oluşturulmuştur.")
|
| 100 |
+
|
| 101 |
+
demo.launch()
|