File size: 9,856 Bytes
c399543
 
8e05fb6
d2e9ea2
8e05fb6
 
d2e9ea2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e05fb6
 
d2e9ea2
 
8e05fb6
 
d2e9ea2
 
8e05fb6
d2e9ea2
 
8e05fb6
d2e9ea2
 
8e05fb6
d2e9ea2
 
 
8e05fb6
d2e9ea2
 
8e05fb6
c399543
 
8e05fb6
16cf588
 
8e05fb6
d2e9ea2
8e05fb6
16cf588
 
 
 
 
 
c399543
d2e9ea2
 
 
8e05fb6
 
 
 
c399543
8e05fb6
d2e9ea2
 
c399543
8e05fb6
c399543
 
 
 
 
 
8e05fb6
c399543
 
 
 
d2e9ea2
 
 
c399543
87de44d
c399543
16cf588
 
8e05fb6
d2e9ea2
8e05fb6
16cf588
 
 
 
 
 
c399543
 
 
cf19a37
c399543
8e05fb6
cf19a37
8e05fb6
c399543
8e05fb6
87de44d
cf19a37
8e05fb6
 
 
 
 
cf19a37
8e05fb6
cf19a37
8e05fb6
 
 
 
 
cf19a37
 
8e05fb6
cf19a37
8e05fb6
c399543
 
d2e9ea2
c399543
 
 
16cf588
 
c399543
8e05fb6
d2e9ea2
8e05fb6
 
d2e9ea2
 
 
 
 
 
8e05fb6
 
c399543
8e05fb6
 
 
 
d2e9ea2
8e05fb6
c399543
 
8e05fb6
d2e9ea2
 
 
c399543
 
d2e9ea2
c399543
 
 
87de44d
c399543
 
 
d2e9ea2
 
cf19a37
d2e9ea2
cf19a37
d2e9ea2
c399543
 
 
 
 
 
 
 
 
 
 
 
 
cf19a37
c399543
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16cf588
c399543
 
 
 
 
 
87de44d
c399543
 
87de44d
c399543
87de44d
c399543
87de44d
c399543
 
 
87de44d
c399543
87de44d
c399543
 
 
87de44d
 
c399543
 
 
 
16cf588
c399543
 
 
 
 
 
16cf588
c399543
8e05fb6
c399543
 
 
 
 
87de44d
c399543
 
 
 
 
 
 
 
 
16cf588
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import gradio as gr
import time
import json
import traceback

# Import RAG system components
print("Starting RAG system initialization...")
try:
    from rag_system.vector_store import VectorStore
    print("βœ“ VectorStore imported successfully")
except Exception as e:
    print(f"βœ— VectorStore import failed: {e}")
    traceback.print_exc()

try:
    from rag_system.retriever import SQLRetriever
    print("βœ“ SQLRetriever imported successfully")
except Exception as e:
    print(f"βœ— SQLRetriever import failed: {e}")
    traceback.print_exc()

try:
    from rag_system.prompt_engine import PromptEngine
    print("βœ“ PromptEngine imported successfully")
except Exception as e:
    print(f"βœ— PromptEngine import failed: {e}")
    traceback.print_exc()

try:
    from rag_system.sql_generator import SQLGenerator
    print("βœ“ SQLGenerator imported successfully")
except Exception as e:
    print(f"βœ— SQLGenerator import failed: {e}")
    traceback.print_exc()

# Initialize RAG system components
print("Initializing RAG system components...")
sql_generator = None
try:
    vector_store = VectorStore()
    print("βœ“ VectorStore initialized")
    
    retriever = SQLRetriever(vector_store)
    print("βœ“ SQLRetriever initialized")
    
    prompt_engine = PromptEngine()
    print("βœ“ PromptEngine initialized")
    
    sql_generator = SQLGenerator(retriever, prompt_engine)
    print("βœ“ SQLGenerator initialized")
    
    print("πŸŽ‰ RAG system initialized successfully!")
except Exception as e:
    print(f"❌ Error initializing RAG system: {e}")
    traceback.print_exc()
    sql_generator = None

def generate_sql(question, table_headers):
    """Generate SQL using the RAG system directly."""
    print(f"generate_sql called with: {question}, {table_headers}")
    
    if sql_generator is None:
        return "❌ Error: RAG system not initialized. Check the logs for initialization errors."
    
    if not question or not question.strip():
        return "❌ Error: Please enter a question."
    
    if not table_headers or not table_headers.strip():
        return "❌ Error: Please enter table headers."
    
    try:
        print(f"Generating SQL for: {question}")
        print(f"Table headers: {table_headers}")
        
        start_time = time.time()
        
        # Generate SQL using RAG system
        result = sql_generator.generate_sql(question, table_headers)
        
        processing_time = time.time() - start_time
        print(f"SQL generation completed in {processing_time:.2f}s")
        print(f"Result: {result}")
        
        return f"""
**Generated SQL:**
```sql
{result['sql_query']}
```

**Model Used:** {result['model_used']}
**Processing Time:** {processing_time:.2f}s
**Status:** {result['status']}
**Retrieved Examples:** {len(result['retrieved_examples'])} examples used for RAG
"""
    except Exception as e:
        error_msg = f"❌ Error: {str(e)}\n\nFull traceback:\n{traceback.format_exc()}"
        print(error_msg)
        return error_msg

def batch_generate_sql(questions_text, table_headers):
    """Generate SQL for multiple questions."""
    print(f"batch_generate_sql called with: {questions_text}, {table_headers}")
    
    if sql_generator is None:
        return "❌ Error: RAG system not initialized. Check the logs for initialization errors."
    
    if not questions_text or not questions_text.strip():
        return "❌ Error: Please enter questions."
    
    if not table_headers or not table_headers.strip():
        return "❌ Error: Please enter table headers."
    
    try:
        # Parse questions
        questions = [q.strip() for q in questions_text.split("\n") if q.strip()]
        total_questions = len(questions)
        
        output = f"**Batch Results:**\n"
        output += f"Total Queries: {total_questions}\n\n"
        successful_count = 0
        
        for i, question in enumerate(questions):
            print(f"Processing query {i+1}/{total_questions}: {question}")
            
            try:
                start_time = time.time()
                result = sql_generator.generate_sql(question, table_headers)
                processing_time = time.time() - start_time
                
                output += f"**Query {i+1}:** {question}\n"
                output += f"```sql\n{result['sql_query']}\n```\n"
                output += f"Model: {result['model_used']} | Time: {processing_time:.2f}s\n\n"
                
                if result['status'] == 'success':
                    successful_count += 1
                    
            except Exception as e:
                output += f"**Query {i+1}:** {question}\n"
                output += f"❌ Error: {str(e)}\n\n"
        
        output += f"**Summary:** {successful_count}/{total_questions} queries successful"
        return output
        
    except Exception as e:
        return f"❌ Error: {str(e)}\n\nFull traceback:\n{traceback.format_exc()}"

def check_system_health():
    """Check the health of the RAG system."""
    print("check_system_health called")
    
    try:
        if sql_generator is None:
            return "❌ System Status: RAG system not initialized\n\nCheck the logs above for initialization errors."
        
        # Get model info
        try:
            model_info = sql_generator.get_model_info()
            model_status = "Available"
        except Exception as e:
            model_info = {"error": str(e)}
            model_status = f"Error: {e}"
        
        return f"""
**System Health:**
- **Status:** βœ… Healthy
- **System Loaded:** βœ… Yes
- **System Loading:** ❌ No
- **Error:** None
- **Model Status:** {model_status}
- **Timestamp:** {time.strftime('%Y-%m-%d %H:%M:%S')}

**Model Info:**
{json.dumps(model_info, indent=2) if model_info else 'Not available'}

**Initialization Logs:**
Check the console/logs above for detailed initialization information.
"""
    except Exception as e:
        return f"❌ Health check error: {str(e)}\n\nFull traceback:\n{traceback.format_exc()}"

# Create Gradio interface
with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) as demo:
    gr.Markdown("#Text-to-SQL RAG with CodeLlama")
    gr.Markdown("Generate SQL queries from natural language using **RAG (Retrieval-Augmented Generation)** and **CodeLlama** models.")
    gr.Markdown("**Features:** RAG-enhanced generation, CodeLlama integration, Vector-based retrieval, Advanced prompt engineering")
    
    # Add initialization status
    if sql_generator is None:
        gr.Markdown("⚠️ **Warning:** RAG system failed to initialize. Check the logs for errors.")
    else:
        gr.Markdown("βœ… **Status:** RAG system initialized successfully!")
    
    with gr.Tab("Single Query"):
        with gr.Row():
            with gr.Column(scale=1):
                question_input = gr.Textbox(
                    label="Question",
                    placeholder="e.g., Show me all employees with salary greater than 50000",
                    lines=3
                )
                table_headers_input = gr.Textbox(
                    label="Table Headers (comma-separated)",
                    placeholder="e.g., id, name, salary, department",
                    value="id, name, salary, department"
                )
                generate_btn = gr.Button("Generate SQL", variant="primary", size="lg")
            
            with gr.Column(scale=1):
                output = gr.Markdown(label="Result")
    
    with gr.Tab("Batch Queries"):
        with gr.Row():
            with gr.Column(scale=1):
                batch_questions = gr.Textbox(
                    label="Questions (one per line)",
                    placeholder="Show me all employees\nCount total employees\nAverage salary by department",
                    lines=5
                )
                batch_headers = gr.Textbox(
                    label="Table Headers (comma-separated)",
                    placeholder="e.g., id, name, salary, department",
                    value="id, name, salary, department"
                )
                batch_btn = gr.Button("Generate Batch SQL", variant="primary", size="lg")
            
            with gr.Column(scale=1):
                batch_output = gr.Markdown(label="Batch Results")
    
    with gr.Tab("System Health"):
        with gr.Row():
            health_btn = gr.Button("πŸ” Check System Health", variant="secondary", size="lg")
            health_output = gr.Markdown(label="Health Status")
    
    # Event handlers - Simple and working
    generate_btn.click(
        generate_sql,
        inputs=[question_input, table_headers_input],
        outputs=output
    )
    
    batch_btn.click(
        batch_generate_sql,
        inputs=[batch_questions, batch_headers],
        outputs=batch_output
    )
    
    health_btn.click(
        check_system_health,
        outputs=health_output
    )
    
    gr.Markdown("---")
    gr.Markdown("""
    ## How It Works
    
    1. **RAG System**: Retrieves relevant SQL examples from vector database
    2. **CodeLlama**: Generates SQL using retrieved examples as context
    3. **Vector Search**: Finds similar questions and their SQL solutions
    4. **Enhanced Generation**: Combines retrieval + generation for better accuracy
    
    ## Technology Stack
    
    - **Backend**: Direct RAG system integration
    - **LLM**: CodeLlama-7B-Python-GGUF (primary)
    - **Vector DB**: ChromaDB with sentence transformers
    - **Frontend**: Gradio interface
    - **Hosting**: Hugging Face Spaces
    
    ## πŸ“Š Performance
    
    - **Model**: CodeLlama-7B-Python-GGUF
    - **Response Time**: < 5 seconds
    - **Accuracy**: High (RAG-enhanced)
    - **Cost**: Free (local inference)
    """)

# Launch the interface
if __name__ == "__main__":
    demo.launch()