Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from transformers import AutoModelForSeq2SeqLM, AutoTokenizer | |
| import torch | |
| # GPU kullanılabilirliğini kontrol edelim | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| # Arayüz ayarları | |
| st.set_page_config(layout="centered") | |
| st.title("English-to-SQL Deep Learning Model") | |
| # Model ve Tokenizer tanımlayalım: Sadece bir kez yüklenmesi için önbelleğe de aldık. | |
| def load_model(): | |
| model_name = "mrm8488/t5-base-finetuned-wikiSQL" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name, force_download=True) | |
| model = AutoModelForSeq2SeqLM.from_pretrained(model_name, force_download=True).to(device) | |
| return tokenizer, model | |
| tokenizer, model = load_model() | |
| def prompt_to_sql(prompt): | |
| # Modelin eğitim verisine uygun girdi formatı: | |
| input_text = f"translate English to SQL: {prompt}" | |
| # Girilen metni tensörlere çevirelim ve GPU(cuda)'ya aktaralım. | |
| input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device) | |
| # Çıktıyı (tensör) üretelim: | |
| outputs = model.generate(input_ids,max_length=128) | |
| # Çıktı tensörünü okunabilir metne çevirelim | |
| sql = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return sql | |
| # Kullanıcı veri girişi | |
| user_input = st.text_input("Prompt:",placeholder="What is the average age of users who live in Istanbul?") | |
| if st.button("Generate SQL"): | |
| if user_input.strip(): | |
| with st.spinner("Generating..."): | |
| sql = prompt_to_sql(user_input) | |
| st.success("Generation succeeded!") | |
| st.code(sql, language="sql") | |
| else: | |
| st.error("Prompt invalid!") |