File size: 1,272 Bytes
54e8483
b503163
1323468
54e8483
b503163
 
 
54e8483
b503163
 
 
 
 
 
 
 
 
 
 
 
 
 
3b69718
 
 
b503163
 
 
 
 
3b69718
 
 
 
 
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
import gradio as gr
from transformers import T5Tokenizer, T5ForConditionalGeneration
from datasets import load_dataset

# Load the tokenizer and model
tokenizer = T5Tokenizer.from_pretrained('t5-small')
model = T5ForConditionalGeneration.from_pretrained('t5-small')

def generate_sql(question):
    # Format the question for the model if needed. For example:
    # input_text = f"translate English to SQL: {question}"
    input_text = f"{question}"  # Directly use the question if the model is fine-tuned for SQL generation
    
    # Tokenize the input text
    input_ids = tokenizer.encode(input_text, return_tensors="pt")
    
    # Generate the output sequence
    output_ids = model.generate(input_ids, max_length=512, num_beams=5)[0]
    
    # Decode the generated ids to get the SQL query
    sql_query = tokenizer.decode(output_ids, skip_special_tokens=True)
    return sql_query

# Define the Gradio interface
iface = gr.Interface(
    fn=generate_sql,
    inputs=gr.Textbox(lines=2, placeholder="Enter your question here..."),
    outputs=gr.Textbox(),
    title="Natural Language to SQL",
    description="This app uses a Seq2Seq model to generate SQL queries from natural language questions."
)

# Launch the app
if __name__ == "__main__":
    iface.launch()