Abhisek987 commited on
Commit
baac0df
Β·
verified Β·
1 Parent(s): 7ff57f7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -0
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ print("πŸš€ Loading model...")
6
+
7
+ # Load your merged model
8
+ model_name = "Abhisek987/llama-3.2-sql-merged"
9
+
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
+ model = AutoModelForCausalLM.from_pretrained(
12
+ model_name,
13
+ device_map="auto",
14
+ torch_dtype=torch.float16,
15
+ low_cpu_mem_usage=True
16
+ )
17
+
18
+ print("βœ… Model loaded successfully!")
19
+
20
+ def generate_sql(database, question):
21
+ """Generate SQL query from natural language question"""
22
+
23
+ prompt = f"""### Instruction:
24
+ You are a SQL expert. Generate a SQL query to answer the given question for the specified database.
25
+
26
+ ### Input:
27
+ Database: {database}
28
+ Question: {question}
29
+
30
+ ### Response:
31
+ """
32
+
33
+ # Tokenize and generate
34
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
35
+
36
+ with torch.no_grad():
37
+ outputs = model.generate(
38
+ **inputs,
39
+ max_new_tokens=256,
40
+ temperature=0.1,
41
+ do_sample=True,
42
+ pad_token_id=tokenizer.eos_token_id,
43
+ eos_token_id=tokenizer.eos_token_id
44
+ )
45
+
46
+ # Decode and extract SQL
47
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True)
48
+ sql_query = result.split("### Response:")[-1].strip()
49
+
50
+ return sql_query
51
+
52
+ # Create Gradio interface
53
+ demo = gr.Interface(
54
+ fn=generate_sql,
55
+ inputs=[
56
+ gr.Textbox(
57
+ label="Database Name",
58
+ placeholder="e.g., employees, sales, customers",
59
+ value="employees"
60
+ ),
61
+ gr.Textbox(
62
+ label="Question",
63
+ placeholder="e.g., Show all employees with salary above 60000",
64
+ lines=3
65
+ )
66
+ ],
67
+ outputs=gr.Textbox(
68
+ label="Generated SQL Query",
69
+ lines=5
70
+ ),
71
+ title="πŸ€– Text-to-SQL Generator",
72
+ description="""
73
+ Fine-tuned Llama 3.2 3B model for SQL query generation using LoRA.
74
+
75
+ Enter a database name and your question in natural language, and the model will generate the corresponding SQL query.
76
+ """,
77
+ examples=[
78
+ ["employees", "Show all employees with salary above 60000"],
79
+ ["sales", "Show me the top 5 products by total sales"],
80
+ ["customers", "How many customers are from each country?"],
81
+ ["orders", "Find all orders placed in the last 30 days"],
82
+ ["concert_singer", "What are the names of all singers ordered by net worth?"]
83
+ ],
84
+ theme="soft",
85
+ article="""
86
+ ### About This Model
87
+
88
+ - **Base Model:** Llama 3.2 3B
89
+ - **Fine-tuning Method:** LoRA (Parameter-Efficient Fine-Tuning)
90
+ - **Dataset:** Spider (7,000 training examples)
91
+ - **Training Loss:** 0.37 (85% reduction)
92
+
93
+ [View Model on HuggingFace β†’](https://huggingface.co/Abhisek987/llama-3.2-sql-lora)
94
+ """
95
+ )
96
+
97
+ if __name__ == "__main__":
98
+ demo.launch()
99
+ ```
100
+
101
+ 5. Click **"Commit new file to main"**
102
+
103
+ ---
104
+
105
+ ### **Step 3: Create `requirements.txt` File**
106
+
107
+ 1. Click **"Add file"** β†’ **"Create a new file"**
108
+ 2. Filename: `requirements.txt`
109
+ 3. Paste this:
110
+ ```
111
+ transformers>=4.44.0
112
+ torch>=2.0.0
113
+ gradio>=4.0.0
114
+ accelerate>=0.30.0
115
+ ```
116
+
117
+ 4. Click **"Commit new file to main"**
118
+
119
+ ---
120
+
121
+ ### **Step 4: Wait for Build**
122
+
123
+ 1. Go to **"App"** tab
124
+ 2. You'll see: "Building..." (takes 5-10 minutes)
125
+ 3. Once done, your app will be live!
126
+
127
+ ---
128
+
129
+ ### **Step 5: Your Public URL**
130
+
131
+ Once built, your app will be available at:
132
+ ```
133
+ https://huggingface.co/spaces/Abhisek987/sql-generator
134
+ ```
135
+
136
+ ---
137
+
138
+ ## **What You'll Get:**
139
+
140
+ βœ… **Free forever** (no charges!)
141
+ βœ… **Public demo URL** for resume
142
+ βœ… **Works on any device** (runs on HF servers)
143
+ βœ… **Professional interface**
144
+ βœ… **Example queries** for visitors to try
145
+ βœ… **No maintenance needed**
146
+
147
+ ---
148
+
149
+ ## **For Your Resume:**
150
+ ```
151
+ πŸ”— Live Demo: https://huggingface.co/spaces/Abhisek987/sql-generator
152
+ πŸ”— Model: https://huggingface.co/Abhisek987/llama-3.2-sql-lora