import gradio as gr import torch from transformers import AutoModelForSeq2SeqLM, AutoTokenizer # Set up device (GPU if available) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Load the fine-tuned model and tokenizer model_name = "aarohanverma/text2sql-flan-t5-base-qlora-finetuned" # Replace with your model repo name model = AutoModelForSeq2SeqLM.from_pretrained(model_name, torch_dtype=torch.bfloat16).to(device) tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-base") def generate_sql(context: str, query: str) -> str: """ Constructs a prompt using the user-provided context and query, then generates a SQL query. """ prompt = f"""Context: {context} Query: {query} Response: """ inputs = tokenizer(prompt, return_tensors="pt").to(device) generated_ids = model.generate( input_ids=inputs["input_ids"], max_new_tokens=250, temperature=0.0, # Deterministic output num_beams=3, # Beam search for quality output early_stopping=True, ) return tokenizer.decode(generated_ids[0], skip_special_tokens=True) # Create a Gradio interface with two input boxes: one for context, one for query. iface = gr.Interface( fn=generate_sql, inputs=[ gr.Textbox(lines=8, label="Context", placeholder="Enter table schema, sample data, etc."), gr.Textbox(lines=2, label="Query", placeholder="Enter your natural language query here...") ], outputs="text", title="Text-to-SQL Generator", description="Enter your own context (e.g., database schema and sample data) and a natural language query. The model will generate the corresponding SQL statement." ) iface.launch()