Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig | |
| # --- 1. Model ve Tokenizer'ı Yükle --- | |
| # Bu kısım sadece uygulama ilk başladığında bir kez çalışır. | |
| print("Model ve Tokenizer yükleniyor... Lütfen bekleyin.") | |
| model_id = "Qwen/Qwen1.5-7B-Chat" | |
| bnb_config = BitsAndBytesConfig( | |
| load_in_4bit=True, | |
| bnb_4bit_quant_type="nf4", | |
| bnb_4bit_compute_dtype=torch.bfloat16 | |
| ) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_id, | |
| quantization_config=bnb_config, | |
| device_map="auto", | |
| trust_remote_code=True | |
| ) | |
| tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True) | |
| if tokenizer.pad_token is None: | |
| tokenizer.pad_token = tokenizer.eos_token | |
| print("Model başarıyla yüklendi!") | |
| # --- 2. SQL Üretme Fonksiyonu --- | |
| # Bu fonksiyon, kullanıcı butona her bastığında çalışır. | |
| def generate_sql(user_question): | |
| print(f"Yeni soru alındı: {user_question}") | |
| # Veritabanı şemasını doğrudan koda ekliyoruz. | |
| schema = { | |
| "Musteriler": ["musteri_id", "ad", "soyad", "sehir", "kayit_tarihi"], | |
| "Kategoriler": ["kategori_id", "kategori_adi"], | |
| "Urunler": ["urun_id", "urun_adi", "kategori_id", "fiyat"], | |
| "Siparisler": ["siparis_id", "musteri_id", "siparis_tarihi"], | |
| "SiparisDetaylari": ["detay_id", "siparis_id", "urun_id", "miktar"] | |
| } | |
| schema_str = "" | |
| for table, columns in schema.items(): | |
| schema_str += f"Tablo `{table}`: {', '.join(columns)}\n" | |
| 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." | |
| messages = [ | |
| {"role": "system", "content": system_prompt}, | |
| {"role": "user", "content": f"Veritabanı Şeması:\n{schema_str}\nSoru: {user_question}"} | |
| ] | |
| prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) | |
| inputs = tokenizer(prompt, return_tensors="pt").to("cuda") | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=600, | |
| eos_token_id=tokenizer.eos_token_id, | |
| pad_token_id=tokenizer.pad_token_id, | |
| temperature=0.1, | |
| top_p=0.9 | |
| ) | |
| response_ids = outputs[0][inputs.input_ids.shape[1]:] | |
| generated_text = tokenizer.decode(response_ids, skip_special_tokens=True) | |
| if '```sql' in generated_text: | |
| generated_text = generated_text.split('```sql')[1] | |
| if '```' in generated_text: | |
| generated_text = generated_text.split('```')[0] | |
| print(f"Üretilen SQL:\n{generated_text.strip()}") | |
| return generated_text.strip() | |
| # --- 3. Gradio Arayüzünü Oluştur --- | |
| # Web sayfasının görünümünü ve işlevselliğini tanımlar. | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# 🤖 Türkçe Soru-SQL Botu") | |
| gr.Markdown("Veritabanı hakkında sormak istediğiniz soruyu Türkçe olarak yazın, sizin için SQL sorgusunu üretelim.") | |
| with gr.Row(): | |
| question_input = gr.Textbox(lines=5, label="Soru", placeholder="Örnek: Her müşterinin toplam harcamasını ve son sipariş tarihini listele.") | |
| sql_output = gr.Textbox(lines=15, label="Üretilen SQL Sorgusu", interactive=False) | |
| submit_button = gr.Button("SQL Üret") | |
| submit_button.click( | |
| fn=generate_sql, | |
| inputs=question_input, | |
| outputs=sql_output | |
| ) | |
| gr.Markdown("---") | |
| gr.Markdown("Bu demo, Alibaba Qwen1.5-7B modeli kullanılarak oluşturulmuştur.") | |
| demo.launch() |