Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +42 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# Load model and tokenizer
|
| 7 |
+
model_name = "tscholak/cxmefzzi"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 10 |
+
device = torch.device("cpu")
|
| 11 |
+
|
| 12 |
+
def generate_sql(natural_language, csv_file):
|
| 13 |
+
# Read uploaded dataset
|
| 14 |
+
if csv_file is not None:
|
| 15 |
+
df = pd.read_csv(csv_file.name)
|
| 16 |
+
table_columns = ", ".join(df.columns)
|
| 17 |
+
context = f"The table has columns: {table_columns}."
|
| 18 |
+
else:
|
| 19 |
+
context = ""
|
| 20 |
+
|
| 21 |
+
prompt = f"{context} Convert the question to SQL: {natural_language}"
|
| 22 |
+
|
| 23 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 24 |
+
outputs = model.generate(**inputs, max_length=128)
|
| 25 |
+
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 26 |
+
|
| 27 |
+
return sql_query
|
| 28 |
+
|
| 29 |
+
# Gradio UI
|
| 30 |
+
iface = gr.Interface(
|
| 31 |
+
fn=generate_sql,
|
| 32 |
+
inputs=[
|
| 33 |
+
gr.Textbox(label="Enter your question (natural language)"),
|
| 34 |
+
gr.File(label="Upload your CSV file (optional)")
|
| 35 |
+
],
|
| 36 |
+
outputs=gr.Textbox(label="Generated SQL Query"),
|
| 37 |
+
title="🧠 Natural Language to SQL Converter",
|
| 38 |
+
description="Upload a dataset and type your question in natural language to generate an SQL query automatically.",
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|
| 4 |
+
pandas
|